• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A CLI tool for downloading from pixiv.net


Commit MetaInfo

Révision8504b8c2880ec27af36acfe1b67d07f2ec9a3f53 (tree)
l'heure2023-11-28 13:51:23
Auteurmio <stigma@disr...>
Commitermio

Message de Log

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.

Change Summary

Modification

--- a/source/app.d
+++ b/source/app.d
@@ -237,7 +237,9 @@ unittest
237237 /**
238238 * Attempt to make *path* safe for different operating systems.
239239 *
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.
241243 *
242244 * Only tested on Linux and Android.
243245 *
@@ -247,12 +249,23 @@ unittest
247249 *
248250 * Returns: A new string with the replacements made.
249251 */
250-string makeSafe(string path, string replacement = "_")
252+string makeSafe(string path)
251253 {
252- import std.regex : regex, replaceAll;
253- import std.string : strip;
254+ import std.regex : regex, replaceAll;
255+ import std.string : replace, strip, stripRight;
254256
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+}
256269 }
257270
258271 void downloadIllust(ref Illustration illust, ref Config conf,
@@ -265,12 +278,13 @@ void downloadIllust(ref Illustration illust, ref Config conf,
265278 import std.path : buildPath;
266279 import std.stdio : stderr;
267280
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);
271283
272- if (false == exists(subDirectory))
284+ if (false == exists(subDirectory)) {
285+ warningf("subDirectory doesn't exist. Will create: %s", subDirectory);
273286 mkdirRecurse(subDirectory);
287+ }
274288
275289 stderr.writefln("[%s] Downloading %s - %s", illust.type, illust.userName,
276290 illust.title);