A CLI tool for downloading from pixiv.net
Révision | 8504b8c2880ec27af36acfe1b67d07f2ec9a3f53 (tree) |
---|---|
l'heure | 2023-11-28 13:51:23 |
Auteur | mio <stigma@disr...> |
Commiter | mio |
have makeSafe treat more characters as invalid
Turns out this is really dependent on the filesystem's format
(e.g. ext4, exfat, ntfs, etc.). This will assume unicode is valid, but
will restrict the possible characters to 'work characters', periods, and
hyphens.
@@ -237,7 +237,9 @@ unittest | ||
237 | 237 | /** |
238 | 238 | * Attempt to make *path* safe for different operating systems. |
239 | 239 | * |
240 | - * This will replace "\\:*." with the provided *replacement*. | |
240 | + * This will replace anything that is not: a hyphen ('-'), a period ('.'), | |
241 | + * an underscore ('_'), or a "word character" (includes numbers). Any | |
242 | + * characters found will be replaced by a hypen. | |
241 | 243 | * |
242 | 244 | * Only tested on Linux and Android. |
243 | 245 | * |
@@ -247,12 +249,23 @@ unittest | ||
247 | 249 | * |
248 | 250 | * Returns: A new string with the replacements made. |
249 | 251 | */ |
250 | -string makeSafe(string path, string replacement = "_") | |
252 | +string makeSafe(string path) | |
251 | 253 | { |
252 | - import std.regex : regex, replaceAll; | |
253 | - import std.string : strip; | |
254 | + import std.regex : regex, replaceAll; | |
255 | + import std.string : replace, strip, stripRight; | |
254 | 256 | |
255 | - return replaceAll(path, regex("[\\:*.]"), replacement).strip; | |
257 | + enum kReplacement = "-"; | |
258 | + | |
259 | + auto reg = regex(`[^-\w._]`); | |
260 | + string noSpace = strip(path).replace(" ", kReplacement); | |
261 | + string almostSafe = replaceAll(noSpace, reg, kReplacement); | |
262 | + | |
263 | +static if (__VERSION__ < 2079L) { | |
264 | + import std.string : chomp; | |
265 | + return chomp(almostSafe, "._-"); | |
266 | +} else { | |
267 | + return strip(almostSafe, "._-"); | |
268 | +} | |
256 | 269 | } |
257 | 270 | |
258 | 271 | void downloadIllust(ref Illustration illust, ref Config conf, |
@@ -265,12 +278,13 @@ void downloadIllust(ref Illustration illust, ref Config conf, | ||
265 | 278 | import std.path : buildPath; |
266 | 279 | import std.stdio : stderr; |
267 | 280 | |
268 | - const rawPath = buildPath(conf.outputDirectory, | |
269 | - illust.userId ~ "_" ~ illust.userName); | |
270 | - const subDirectory = makeSafe(rawPath); | |
281 | + const accountDirectory = makeSafe(illust.userId ~ "_" ~ illust.userName); | |
282 | + const subDirectory = buildPath(conf.outputDirectory, accountDirectory); | |
271 | 283 | |
272 | - if (false == exists(subDirectory)) | |
284 | + if (false == exists(subDirectory)) { | |
285 | + warningf("subDirectory doesn't exist. Will create: %s", subDirectory); | |
273 | 286 | mkdirRecurse(subDirectory); |
287 | + } | |
274 | 288 | |
275 | 289 | stderr.writefln("[%s] Downloading %s - %s", illust.type, illust.userName, |
276 | 290 | illust.title); |