Fix issue where custom separator could be the same as a character in the prefix
This commit is contained in:
@@ -41,11 +41,11 @@ export function askName (): boolean {
|
||||
/**
|
||||
* The string that ends each visitor field in a stored filename, from
|
||||
* `ipp.upload.fieldSeparator`. The default when the configured value could be
|
||||
* part of a field or a filename, see `isUsableSeparator`.
|
||||
* part of a field or a filename, or is in the prefix, see `isUsableSeparator`.
|
||||
*/
|
||||
export function fieldSeparator (): string {
|
||||
const configured = getConfigOption('ipp.upload.fieldSeparator', DEFAULT_FIELD_SEPARATOR)
|
||||
return isUsableSeparator(configured) ? configured : DEFAULT_FIELD_SEPARATOR
|
||||
return isUsableSeparator(configured, filenamePrefix()) ? configured : DEFAULT_FIELD_SEPARATOR
|
||||
}
|
||||
|
||||
/** Text shown under the placeholder on the upload page, from `ipp.upload.notice`. Empty hides it. */
|
||||
|
||||
@@ -25,14 +25,13 @@ const MAX_UPLOADER_LENGTH = 32
|
||||
export const DEFAULT_FIELD_SEPARATOR = ','
|
||||
|
||||
/**
|
||||
* Whether `separator` can mark the end of a field: a string that `sanitize`
|
||||
* leaves alone, with no slug character, dot or whitespace in it. A letter,
|
||||
* digit or hyphen could be part of the name, a dot could be read as the
|
||||
* extension, and whitespace would be trimmed from the ends of a stem.
|
||||
* Whether `separator` can end a field: one character, kept by `sanitize`, not
|
||||
* in `prefix`, and never part of a slug, an extension or a trimmed stem.
|
||||
*/
|
||||
export function isUsableSeparator (separator: unknown): separator is string {
|
||||
if (typeof separator !== 'string' || separator === '') return false
|
||||
if (sanitize(separator) !== separator) return false
|
||||
export function isUsableSeparator (separator: unknown, prefix: string): separator is string {
|
||||
// Counted by code point, so an emoji is one character
|
||||
if (typeof separator !== 'string' || [...separator].length !== 1) return false
|
||||
if (sanitize(separator) !== separator || storedPrefix(prefix).includes(separator)) return false
|
||||
return !/[\p{L}\p{N}\-.\s]/u.test(separator)
|
||||
}
|
||||
|
||||
@@ -79,7 +78,7 @@ export function uploadFilename (raw: unknown, prefix: string, uploader = '', sep
|
||||
if (!parts) return undefined
|
||||
const extension = parts[2]
|
||||
// The separator is removed from the visitor's name so it only ever marks a field
|
||||
const stem = sanitize(parts[1]).split(separator).join('').trim()
|
||||
const stem = sanitize(parts[1]).replaceAll(separator, '').trim()
|
||||
// An empty stem is a dotfile; a leading dot is a traversal remnant.
|
||||
if (!stem || stem.startsWith('.')) return undefined
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@ const PERMISSIONS = 'asset.read, user.read, tag.create and tag.asset'
|
||||
|
||||
const USAGE = `Tag visitor uploads in Immich by the name in their filename.
|
||||
|
||||
Usage: node dist/tag-uploads.js [--apply] [--parent <tag>] [--prefix <text>] [--separator <chars>]
|
||||
Usage: node dist/tag-uploads.js [--apply] [--parent <tag>] [--prefix <text>] [--separator <char>]
|
||||
|
||||
--apply Add the tags. Without it, only report what would be tagged.
|
||||
--parent <tag> Parent tag for the visitor tags. Default "${DEFAULT_PARENT}".
|
||||
--prefix <text> Filename prefix to look for. Default ipp.upload.filenamePrefix.
|
||||
--separator <chars> Field separator to read. Default ipp.upload.fieldSeparator.
|
||||
--separator <char> Field separator to read. Default ipp.upload.fieldSeparator.
|
||||
|
||||
IMMICH_API_KEY must hold an API key of the share owner, with the permissions
|
||||
${PERMISSIONS}.`
|
||||
@@ -235,7 +235,7 @@ export async function main (argv: string[]): Promise<number> {
|
||||
'Pass --prefix with the prefix the files were stored with.')
|
||||
}
|
||||
const separator = values.separator ?? fieldSeparator()
|
||||
if (!isUsableSeparator(separator)) return usage('"' + separator + '" cannot be a field separator.')
|
||||
if (!isUsableSeparator(separator, prefix)) return usage('"' + separator + '" cannot be a field separator with the prefix "' + prefix + '".')
|
||||
const parent = (values.parent ?? DEFAULT_PARENT).split('/').map(part => part.trim()).filter(Boolean).join('/')
|
||||
if (!parent) return usage('The parent tag is empty.')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user