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): support import paths with special chars #14856

Merged
merged 1 commit into from
Dec 22, 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
61 changes: 61 additions & 0 deletions e2e/src/api/specs/library.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,67 @@ describe('/libraries', () => {
utils.removeImageFile(`${testAssetDir}/temp/folder} b/assetB.png`);
});

const annoyingChars = [
"'",
'"',
'`',
'*',
'{',
'}',
',',
'(',
')',
'[',
']',
'?',
'!',
'@',
'#',
'$',
'%',
'^',
'&',
'=',
'+',
'~',
'|',
'<',
'>',
';',
':',
'/', // We never got backslashes to work
];

it.each(annoyingChars)('should scan multiple import paths with %s', async (char) => {
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
importPaths: [`${testAssetDirInternal}/temp/folder${char}1`, `${testAssetDirInternal}/temp/folder${char}2`],
});

utils.createImageFile(`${testAssetDir}/temp/folder${char}1/asset1.png`);
utils.createImageFile(`${testAssetDir}/temp/folder${char}2/asset2.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.searchAssets(admin.accessToken, { libraryId: library.id });

expect(assets.items).toEqual(
expect.arrayContaining([
expect.objectContaining({ originalPath: expect.stringContaining(`folder${char}1/asset1.png`) }),
expect.objectContaining({ originalPath: expect.stringContaining(`folder${char}2/asset2.png`) }),
]),
);

utils.removeImageFile(`${testAssetDir}/temp/folder${char}1/asset1.png`);
utils.removeImageFile(`${testAssetDir}/temp/folder${char}2/asset2.png`);
});

it('should reimport a modified file', async () => {
const library = await utils.createLibrary(admin.accessToken, {
ownerId: admin.userId,
Expand Down
2 changes: 1 addition & 1 deletion server/src/repositories/storage.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class StorageRepository implements IStorageRepository {
}

private asGlob(pathToCrawl: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there a square bracket?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Around the special characters? Turns out, that's how you escape double quotes, single quotes and asterisks when using a dynamic glob pattern. A literal " is written as ["]

const escapedPath = escapePath(pathToCrawl);
const escapedPath = escapePath(pathToCrawl).replaceAll('"', '["]').replaceAll("'", "[']").replaceAll('`', '[`]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so freaking annoying that there isn't just a library escapePath function that does it all...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We uncover all kinds of bugs in libraries, I should probably file this as an issue with fast-glob. Likely we are the first ones to even notice

const extensions = `*{${mimeTypes.getSupportedFileExtensions().join(',')}}`;
return `${escapedPath}/**/${extensions}`;
}
Expand Down
Loading