Skip to content

Commit

Permalink
fix commas and braces in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
etnoy committed Oct 7, 2024
1 parent 1b62c99 commit 55ba1b9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
50 changes: 50 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,56 @@ 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();
});

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();
});

it('should reimport a modified file', async () => {
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
Expand Down
20 changes: 13 additions & 7 deletions server/src/repositories/storage.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable } from '@nestjs/common';
import archiver from 'archiver';
import chokidar, { WatchOptions } from 'chokidar';
import { escapePath, glob, globStream } from 'fast-glob';
import { escapePath, glob, globStream, globSync } from 'fast-glob';
import { constants, createReadStream, existsSync, mkdirSync } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
Expand Down 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,10 +174,15 @@ export class StorageRepository implements IStorageRepository {
return emptyGenerator();
}

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

this.logger.debug(globbedPaths);

const stream = globStream(globbedPaths, {
absolute: true,
caseSensitiveMatch: false,
onlyFiles: true,
unique: true,
dot: includeHidden,
ignore: exclusionPatterns,
});
Expand Down Expand Up @@ -206,10 +213,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}`;
}
}

0 comments on commit 55ba1b9

Please sign in to comment.