From 4e6aeeda4dbbea98f7e48c5ba5b9b86f0509f659 Mon Sep 17 00:00:00 2001 From: Sushain Cherivirala Date: Mon, 13 May 2024 17:44:21 -0400 Subject: [PATCH] fix(server): support special characters in library paths (#9385) Support special characters in library paths --- .../repositories/storage.repository.spec.ts | 20 +++++++++---------- server/src/repositories/storage.repository.ts | 5 +++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/server/src/repositories/storage.repository.spec.ts b/server/src/repositories/storage.repository.spec.ts index 44c81d76a6..e770ce6c1b 100644 --- a/server/src/repositories/storage.repository.spec.ts +++ b/server/src/repositories/storage.repository.spec.ts @@ -76,17 +76,6 @@ const tests: Test[] = [ '/albums/image3.jpg': true, }, }, - { - test: 'should support globbing paths', - options: { - pathsToCrawl: ['/photos*'], - }, - files: { - '/photos1/image1.jpg': true, - '/photos2/image2.jpg': true, - '/images/image3.jpg': false, - }, - }, { test: 'should crawl a single path without trailing slash', options: { @@ -179,6 +168,15 @@ const tests: Test[] = [ [`/photos/3.jpg`]: false, }, }, + { + test: 'should support special characters in paths', + options: { + pathsToCrawl: ['/photos (new)'], + }, + files: { + ['/photos (new)/1.jpg']: true, + }, + }, ]; describe(StorageRepository.name, () => { diff --git a/server/src/repositories/storage.repository.ts b/server/src/repositories/storage.repository.ts index ae374ac812..0d0be5c062 100644 --- a/server/src/repositories/storage.repository.ts +++ b/server/src/repositories/storage.repository.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import archiver from 'archiver'; import chokidar, { WatchOptions } from 'chokidar'; -import { glob, globStream } from 'fast-glob'; +import { escapePath, glob, globStream } from 'fast-glob'; import { constants, createReadStream, existsSync, mkdirSync } from 'node:fs'; import fs from 'node:fs/promises'; import path from 'node:path'; @@ -186,7 +186,8 @@ export class StorageRepository implements IStorageRepository { } private asGlob(pathsToCrawl: string[]): string { - const base = pathsToCrawl.length === 1 ? pathsToCrawl[0] : `{${pathsToCrawl.join(',')}}`; + const escapedPaths = pathsToCrawl.map((path) => escapePath(path)); + const base = escapedPaths.length === 1 ? escapedPaths[0] : `{${escapedPaths.join(',')}}`; const extensions = `*{${mimeTypes.getSupportedFileExtensions().join(',')}}`; return `${base}/**/${extensions}`; }