diff --git a/server/src/cores/storage.core.spec.ts b/server/src/cores/storage.core.spec.ts new file mode 100644 index 000000000..16258f095 --- /dev/null +++ b/server/src/cores/storage.core.spec.ts @@ -0,0 +1,29 @@ +import { StorageCore } from 'src/cores/storage.core'; + +jest.mock('src/constants', () => ({ + APP_MEDIA_LOCATION: '/photos', +})); + +describe('StorageCore', () => { + describe('isImmichPath', () => { + it('should return true for APP_MEDIA_LOCATION path', () => { + const immichPath = '/photos'; + expect(StorageCore.isImmichPath(immichPath)).toBe(true); + }); + + it('should return true for paths within the APP_MEDIA_LOCATION', () => { + const immichPath = '/photos/new/'; + expect(StorageCore.isImmichPath(immichPath)).toBe(true); + }); + + it('should return false for paths outside the APP_MEDIA_LOCATION and same starts', () => { + const nonImmichPath = '/photos_new'; + expect(StorageCore.isImmichPath(nonImmichPath)).toBe(false); + }); + + it('should return false for paths outside the APP_MEDIA_LOCATION', () => { + const nonImmichPath = '/some/other/path'; + expect(StorageCore.isImmichPath(nonImmichPath)).toBe(false); + }); + }); +}); diff --git a/server/src/cores/storage.core.ts b/server/src/cores/storage.core.ts index b9dad8642..ee9f12e51 100644 --- a/server/src/cores/storage.core.ts +++ b/server/src/cores/storage.core.ts @@ -115,7 +115,13 @@ export class StorageCore { } static isImmichPath(path: string) { - return resolve(path).startsWith(resolve(APP_MEDIA_LOCATION)); + const resolvedPath = resolve(path); + const resolvedAppMediaLocation = resolve(APP_MEDIA_LOCATION); + const normalizedPath = resolvedPath.endsWith('/') ? resolvedPath : resolvedPath + '/'; + const normalizedAppMediaLocation = resolvedAppMediaLocation.endsWith('/') + ? resolvedAppMediaLocation + : resolvedAppMediaLocation + '/'; + return normalizedPath.startsWith(normalizedAppMediaLocation); } static isGeneratedAsset(path: string) {