2024-02-14 15:38:57 +01:00
|
|
|
import type { AssetResponseDto } from '@immich/sdk';
|
2023-07-12 05:56:30 +02:00
|
|
|
import { getAssetFilename, getFilenameExtension } from './asset-utils';
|
2023-05-28 03:53:29 +02:00
|
|
|
|
|
|
|
describe('get file extension from filename', () => {
|
2023-07-01 06:50:47 +02:00
|
|
|
it('returns the extension without including the dot', () => {
|
|
|
|
expect(getFilenameExtension('filename.txt')).toEqual('txt');
|
|
|
|
});
|
2023-05-28 03:53:29 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
it('takes the last file extension and ignores the rest', () => {
|
|
|
|
expect(getFilenameExtension('filename.txt.pdf')).toEqual('pdf');
|
|
|
|
expect(getFilenameExtension('filename.txt.pdf.jpg')).toEqual('jpg');
|
|
|
|
});
|
2023-05-28 03:53:29 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
it('returns an empty string when no file extension is found', () => {
|
|
|
|
expect(getFilenameExtension('filename')).toEqual('');
|
|
|
|
expect(getFilenameExtension('filename.')).toEqual('');
|
|
|
|
expect(getFilenameExtension('filename..')).toEqual('');
|
|
|
|
expect(getFilenameExtension('.filename')).toEqual('');
|
|
|
|
});
|
2023-05-28 03:53:29 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
it('returns the extension from a filepath', () => {
|
|
|
|
expect(getFilenameExtension('/folder/file.txt')).toEqual('txt');
|
|
|
|
expect(getFilenameExtension('./folder/file.txt')).toEqual('txt');
|
|
|
|
expect(getFilenameExtension('~/folder/file.txt')).toEqual('txt');
|
|
|
|
expect(getFilenameExtension('./folder/.file.txt')).toEqual('txt');
|
|
|
|
expect(getFilenameExtension('/folder.with.dots/file.txt')).toEqual('txt');
|
|
|
|
});
|
2023-05-28 03:53:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('get asset filename', () => {
|
2023-07-01 06:50:47 +02:00
|
|
|
it('returns the filename including file extension', () => {
|
2024-02-02 04:18:00 +01:00
|
|
|
for (const { asset, result } of [
|
2023-07-01 06:50:47 +02:00
|
|
|
{
|
|
|
|
asset: {
|
|
|
|
originalFileName: 'filename',
|
|
|
|
originalPath: 'upload/library/test/2016/2016-08-30/filename.jpg',
|
|
|
|
},
|
|
|
|
result: 'filename.jpg',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
asset: {
|
|
|
|
originalFileName: 'new-filename',
|
|
|
|
originalPath: 'upload/library/89d14e47-a40d-4cae-a347-a914cdef1f22/2016/2016-08-30/filename.jpg',
|
|
|
|
},
|
|
|
|
result: 'new-filename.jpg',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
asset: {
|
|
|
|
originalFileName: 'new-filename.txt',
|
|
|
|
originalPath: 'upload/library/test/2016/2016-08-30/filename.txt.jpg',
|
|
|
|
},
|
|
|
|
result: 'new-filename.txt.jpg',
|
|
|
|
},
|
2024-02-02 04:18:00 +01:00
|
|
|
]) {
|
2023-07-01 06:50:47 +02:00
|
|
|
expect(getAssetFilename(asset as AssetResponseDto)).toEqual(result);
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
});
|
2023-05-28 03:53:29 +02:00
|
|
|
});
|