Skip to content

Commit

Permalink
fix(zod/checkvalidpath): move path formatting logic after fs validati…
Browse files Browse the repository at this point in the history
…on fails (#674)

had inverted logic of `path === '.'` instead of `!==`

inverted the logic to allow for `.` as a path.

---------

Co-authored-by: Michael Goodnow <mmgoodnow@gmail.com>
  • Loading branch information
zakkarry and mmgoodnow committed May 9, 2024
1 parent c9e811f commit 0a1685b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
35 changes: 4 additions & 31 deletions src/configSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ms from "ms";
import { sep } from "path";
import { ErrorMapCtx, RefinementCtx, z, ZodIssueOptionalMessage } from "zod";
import { Action, LinkType, MatchMode } from "./constants.js";
import { logger } from "./logger.js";
Expand All @@ -15,7 +14,6 @@ const ZodErrorMessages = {
fuzzySizeThreshold: "fuzzySizeThreshold must be between 0 and 1.",
injectUrl:
"You need to specify rtorrentRpcUrl, transmissionRpcUrl, qbittorrentUrl, or delugeRpcUrl when using 'inject'",
windowsPath: `\t\t\tYour path is not formatted properly for Windows. \n\t\t\t\tPlease use "\\\\" or "/" for directory separators.`,
qBitAutoTMM:
"Using Automatic Torrent Management in qBittorrent without flatLinking enabled can result in unintended behavior.",
needsLinkDir:
Expand Down Expand Up @@ -78,20 +76,6 @@ function transformDurationString(durationStr: string, ctx: RefinementCtx) {
return duration;
}

/**
* helper function for directory validation
* @return path if valid formatting
*/
function checkValidPathFormat(path: string, ctx: RefinementCtx) {
if (
(sep === "\\" && !path.includes(`\\`) && !path.includes("/")) ||
path === "."
) {
addZodIssue(path, ZodErrorMessages.windowsPath, ctx);
}
return path;
}

/**
* an object of the zod schema
* each are named after what they are intended to validate
Expand All @@ -103,29 +87,18 @@ export const VALIDATION_SCHEMA = z
message: ZodErrorMessages.delay,
}),
torznab: z.array(z.string().url()),
dataDirs: z
.array(
z
.string()
.transform((value, ctx) =>
value && value.length > 0
? checkValidPathFormat(value, ctx)
: null,
),
)

.nullish(),
dataDirs: z.array(z.string()).nullish(),
matchMode: z.nativeEnum(MatchMode),
linkCategory: z.string().nullish(),
linkDir: z.string().transform(checkValidPathFormat).nullish(),
linkDir: z.string().nullish(),
linkType: z.nativeEnum(LinkType),
flatLinking: z
.boolean()
.nullish()
.transform((value) => (typeof value === "boolean" ? value : false)),
maxDataDepth: z.number().gte(1),
torrentDir: z.string().transform(checkValidPathFormat).nullable(),
outputDir: z.string().transform(checkValidPathFormat),
torrentDir: z.string().nullable(),
outputDir: z.string(),
includeEpisodes: z.boolean(),
includeSingleEpisodes: z.boolean(),
includeNonVideos: z.boolean(),
Expand Down
8 changes: 8 additions & 0 deletions src/startup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sep } from "path";
import { CrossSeedError } from "./errors.js";
import { Label, logger } from "./logger.js";
import { Action } from "./constants.js";
Expand All @@ -11,6 +12,7 @@ import { stat, access, constants } from "fs/promises";
* validates existence, permission, and that a path is a directory
* @param path string of path to validate
* @param optionName name of the configuration key
* @param permissions number (see constants in calling function) of permission
* @returns true if path exists and has required permission
*/
async function verifyPath(
Expand All @@ -28,6 +30,12 @@ async function verifyPath(
logger.error(
`\tYour ${optionName} "${path}" is not a valid directory on the filesystem.`,
);
if (sep === "\\" && !path.includes("\\") && !path.includes("/")) {
logger.error(
"\tIt may not be formatted properly for Windows.\n" +
'\t\t\t\tMake sure to use "\\\\" or "/" for directory separators.',
);
}
} else {
logger.error(
`\tYour ${optionName} "${path}" has invalid permissions.`,
Expand Down

0 comments on commit 0a1685b

Please sign in to comment.