Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Allow commas and braces in import paths #13259

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions e2e/src/api/specs/library.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,62 @@ describe('/libraries', () => {
expect(assets.items.find((asset) => asset.originalPath.includes('directoryB'))).toBeDefined();
});

it('should scan multiple import paths with commas', async () => {
// https://github.com/immich-app/immich/issues/10699
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
importPaths: [`${testAssetDirInternal}/temp/folder, a`, `${testAssetDirInternal}/temp/folder, b`],
});

utils.createImageFile(`${testAssetDir}/temp/folder, a/assetA.png`);
utils.createImageFile(`${testAssetDir}/temp/folder, b/assetB.png`);

const { status } = await request(app)
.post(`/libraries/${library.id}/scan`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send();
expect(status).toBe(204);

await utils.waitForQueueFinish(admin.accessToken, 'library');

const { assets } = await utils.metadataSearch(admin.accessToken, { libraryId: library.id });

expect(assets.count).toBe(2);
expect(assets.items.find((asset) => asset.originalPath.includes('folder, a'))).toBeDefined();
expect(assets.items.find((asset) => asset.originalPath.includes('folder, b'))).toBeDefined();

utils.removeImageFile(`${testAssetDir}/temp/folder, a/assetA.png`);
utils.removeImageFile(`${testAssetDir}/temp/folder, b/assetB.png`);
});

it('should scan multiple import paths with braces', async () => {
// https://github.com/immich-app/immich/issues/10699
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
importPaths: [`${testAssetDirInternal}/temp/folder{ a`, `${testAssetDirInternal}/temp/folder} b`],
});

utils.createImageFile(`${testAssetDir}/temp/folder{ a/assetA.png`);
utils.createImageFile(`${testAssetDir}/temp/folder} b/assetB.png`);

const { status } = await request(app)
.post(`/libraries/${library.id}/scan`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send();
expect(status).toBe(204);

await utils.waitForQueueFinish(admin.accessToken, 'library');

const { assets } = await utils.metadataSearch(admin.accessToken, { libraryId: library.id });

expect(assets.count).toBe(2);
expect(assets.items.find((asset) => asset.originalPath.includes('folder{ a'))).toBeDefined();
expect(assets.items.find((asset) => asset.originalPath.includes('folder} b'))).toBeDefined();

utils.removeImageFile(`${testAssetDir}/temp/folder{ a/assetA.png`);
utils.removeImageFile(`${testAssetDir}/temp/folder} b/assetB.png`);
});

it('should reimport a modified file', async () => {
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
Expand Down
15 changes: 9 additions & 6 deletions server/src/repositories/storage.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ export class StorageRepository implements IStorageRepository {
return Promise.resolve([]);
}

return glob(this.asGlob(pathsToCrawl), {
const globbedPaths = pathsToCrawl.map((path) => this.asGlob(path));

return glob(globbedPaths, {
absolute: true,
caseSensitiveMatch: false,
onlyFiles: true,
Expand All @@ -172,7 +174,9 @@ export class StorageRepository implements IStorageRepository {
return emptyGenerator();
}

const stream = globStream(this.asGlob(pathsToCrawl), {
const globbedPaths = pathsToCrawl.map((path) => this.asGlob(path));

const stream = globStream(globbedPaths, {
absolute: true,
caseSensitiveMatch: false,
onlyFiles: true,
Expand Down Expand Up @@ -206,10 +210,9 @@ export class StorageRepository implements IStorageRepository {
return () => watcher.close();
}

private asGlob(pathsToCrawl: string[]): string {
const escapedPaths = pathsToCrawl.map((path) => escapePath(path));
const base = escapedPaths.length === 1 ? escapedPaths[0] : `{${escapedPaths.join(',')}}`;
private asGlob(pathToCrawl: string): string {
const escapedPath = escapePath(pathToCrawl);
const extensions = `*{${mimeTypes.getSupportedFileExtensions().join(',')}}`;
return `${base}/**/${extensions}`;
return `${escapedPath}/**/${extensions}`;
}
}
Loading