|
17 | 17 |
|
18 | 18 | package com.lambda.util |
19 | 19 |
|
20 | | -import com.github.kittinunf.fuel.core.FuelError |
21 | | -import com.github.kittinunf.fuel.httpDownload |
22 | | -import com.github.kittinunf.fuel.httpGet |
23 | | -import com.github.kittinunf.result.getOrNull |
24 | 20 | import com.lambda.Lambda.mc |
| 21 | +import com.lambda.network.LambdaHttp |
| 22 | +import com.lambda.network.download |
25 | 23 | import com.lambda.util.StringUtils.sanitizeForFilename |
| 24 | +import io.ktor.client.request.* |
26 | 25 | import java.io.File |
27 | 26 | import java.net.InetSocketAddress |
| 27 | +import kotlin.math.sign |
| 28 | +import kotlin.time.Duration |
28 | 29 |
|
29 | 30 | object FileUtils { |
30 | 31 | /** |
31 | 32 | * Returns a sequence of all the files in a tree that matches the [predicate] |
32 | 33 | */ |
33 | 34 | fun File.listRecursive(predicate: (File) -> Boolean): Sequence<File> = walk().filter(predicate) |
34 | 35 |
|
35 | | - /** |
36 | | - * Ensures the current file exists by creating it if it does not. |
37 | | - * |
38 | | - * If the file already exists, it will not be recreated. The necessary |
39 | | - * parent directories will be created if they do not exist. |
40 | | - */ |
41 | | - fun File.createIfNotExists() = also { parentFile.mkdirs(); createNewFile() } |
42 | | - |
43 | 36 | /** |
44 | 37 | * Retrieves or creates a directory based on the current network connection and world dimension. |
45 | 38 | * |
@@ -69,56 +62,143 @@ object FileUtils { |
69 | 62 | } |
70 | 63 |
|
71 | 64 | /** |
72 | | - * Executes the [block] if the receiver file exists |
| 65 | + * Executes the [block] if the file is older than the given [duration] |
| 66 | + */ |
| 67 | + fun File.isOlderThan(duration: Duration, block: (File) -> Unit) = |
| 68 | + ifExists { if (duration.inWholeMilliseconds < System.currentTimeMillis() - lastModified()) block(this) } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns whether the receiver file is older than [duration] |
| 72 | + */ |
| 73 | + fun File.isOlderThan(duration: Duration) = |
| 74 | + duration.inWholeMilliseconds < System.currentTimeMillis() - lastModified() |
| 75 | + |
| 76 | + /** |
| 77 | + * Executes the [block] if the receiver file exists and is not empty |
73 | 78 | */ |
74 | 79 | inline fun File.ifExists(block: (File) -> Unit): File { |
75 | | - if (exists()) block(this) |
| 80 | + if (length() > 0) block(this) |
| 81 | + return this |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Ensures the current file exists by creating it if it does not. |
| 86 | + * |
| 87 | + * If the file already exists, it will not be recreated. The necessary |
| 88 | + * parent directories will be created if they do not exist. |
| 89 | + * |
| 90 | + * @param block Lambda executed if the file doesn't exist or the file is empty |
| 91 | + */ |
| 92 | + inline fun File.createIfNotExists(block: (File) -> Unit): File { |
| 93 | + if (length() == 0L) block(this) |
| 94 | + |
| 95 | + parentFile.mkdirs() |
| 96 | + createNewFile() |
| 97 | + |
76 | 98 | return this |
77 | 99 | } |
78 | 100 |
|
79 | 101 | /** |
80 | | - * Executes the [block] if the receiver file does not exist. |
| 102 | + * Executes the [block] if the receiver file does not exist or is empty. |
81 | 103 | */ |
82 | 104 | inline fun File.ifNotExists(block: (File) -> Unit): File { |
83 | | - if (!exists()) block(this) |
| 105 | + if (length() == 0L) block(this) |
84 | 106 | return this |
85 | 107 | } |
86 | 108 |
|
| 109 | + /** |
| 110 | + * Modifies the receiver file if the downloaded file compare check succeeds |
| 111 | + * |
| 112 | + * @receiver The destination file to write the bytes to |
| 113 | + * |
| 114 | + * @param url The url to download the file from |
| 115 | + * @param compare Compare method. -1 if remote is larger. 0 if both file have the same size. 1 if local is larger |
| 116 | + * @param block Configuration block for the request |
| 117 | + * |
| 118 | + * @return An exception or the file |
| 119 | + */ |
| 120 | + suspend fun File.downloadCompare( |
| 121 | + url: String, |
| 122 | + compare: Int, |
| 123 | + block: HttpRequestBuilder.() -> Unit = {}, |
| 124 | + ) = runCatching { |
| 125 | + createIfNotExists { |
| 126 | + val bytes = readBytes() |
| 127 | + val remote = LambdaHttp.download(url, block) |
| 128 | + val sign = (bytes.size - remote.size).sign |
| 129 | + |
| 130 | + if (sign == compare) writeBytes(remote) |
| 131 | + } |
| 132 | + } |
| 133 | + |
87 | 134 | /** |
88 | 135 | * Downloads the given file url if the file is not present |
89 | 136 | * |
90 | | - * This function does not guarantee that the given file will be created |
| 137 | + * @receiver The destination file to write the bytes to |
| 138 | + * |
| 139 | + * @param url The url to download the file from |
| 140 | + * @param block Configuration block for the request |
| 141 | + * |
| 142 | + * @return An exception or the file |
91 | 143 | */ |
92 | | - fun File.downloadIfNotPresent( |
| 144 | + suspend fun File.downloadIfNotPresent( |
93 | 145 | url: String, |
94 | | - success: (ByteArray) -> Unit = {}, |
95 | | - failure: (FuelError) -> Unit = {} |
96 | | - ) = ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } } |
| 146 | + block: HttpRequestBuilder.() -> Unit = {}, |
| 147 | + ) = runCatching { createIfNotExists { LambdaHttp.download(url, this, block) } } |
97 | 148 |
|
98 | 149 | /** |
99 | 150 | * Downloads the given file url if the file is not present |
100 | 151 | * |
101 | | - * This function does not guarantee that the given file will be created |
| 152 | + * @receiver The url to download the file from |
| 153 | + * |
| 154 | + * @param file The destination file to write the bytes to |
| 155 | + * @param block Configuration block for the request |
| 156 | + * |
| 157 | + * @return An exception or the file |
102 | 158 | */ |
103 | | - fun String.downloadIfNotPresent( |
| 159 | + suspend fun String.downloadIfNotPresent( |
104 | 160 | file: File, |
105 | | - success: (ByteArray) -> Unit = {}, |
106 | | - failure: (FuelError) -> Unit = {} |
107 | | - ) = file.ifNotExists { httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } } |
| 161 | + block: HttpRequestBuilder.() -> Unit = {}, |
| 162 | + ) = runCatching { file.createIfNotExists { LambdaHttp.download(this, file, block) } } |
108 | 163 |
|
109 | 164 | /** |
110 | | - * Downloads the given file url if the file is not present |
| 165 | + * Lambda that downloads the given file url if the file is not present |
| 166 | + * |
| 167 | + * @receiver The destination file to write the bytes to |
| 168 | + * @param block Configuration block for the request |
| 169 | + * |
| 170 | + * @return A lambda that returns an exception or the file |
| 171 | + */ |
| 172 | + fun File.downloadIfNotPresent(block: HttpRequestBuilder.() -> Unit = {}): suspend (String) -> Result<Unit> = |
| 173 | + { url -> runCatching { createIfNotExists { LambdaHttp.download(url, this, block) } } } |
| 174 | + |
| 175 | + /** |
| 176 | + * Downloads the given file url if the file is present |
111 | 177 | * |
112 | | - * This function does not guarantee that the given file will be created |
| 178 | + * @receiver The destination file to write the bytes to |
| 179 | + * |
| 180 | + * @param url The url to download the file from |
| 181 | + * @param block Configuration block for the request |
| 182 | + * |
| 183 | + * @return An exception or the file |
113 | 184 | */ |
114 | | - fun File.downloadIfNotPresent(): (String) -> Unit = |
115 | | - { url -> ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, _ -> } } } |
| 185 | + suspend fun File.downloadIfPresent( |
| 186 | + url: String, |
| 187 | + block: HttpRequestBuilder.() -> Unit = {}, |
| 188 | + ) = runCatching { ifExists { LambdaHttp.download(url, this, block) } } |
116 | 189 |
|
117 | 190 | /** |
118 | | - * Gets the given url if the file is not present |
| 191 | + * Downloads the given file url if the file is present |
119 | 192 | * |
120 | | - * This function does not guarantee that the given file will be created |
| 193 | + * @receiver The url to download the file from |
| 194 | + * |
| 195 | + * @param file The destination file to write the bytes to |
| 196 | + * @param block Configuration block for the request |
| 197 | + * |
| 198 | + * @return An exception or the file |
121 | 199 | */ |
122 | | - fun File.getIfNotPresent(): (String) -> Unit = |
123 | | - { url -> ifNotExists { url.httpGet().responseString().third.getOrNull()?.let { writeText(it) } } } |
| 200 | + suspend fun String.downloadIfPresent( |
| 201 | + file: File, |
| 202 | + block: HttpRequestBuilder.() -> Unit = {}, |
| 203 | + ) = runCatching { file.ifExists { LambdaHttp.download(this, file, block) } } |
124 | 204 | } |
0 commit comments