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

feat(server): specify names for thumbnail files #14425

Merged
merged 1 commit into from
Dec 1, 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
3 changes: 3 additions & 0 deletions server/src/services/asset-media.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ describe(AssetMediaService.name, () => {
path: '/path/to/preview.jpg',
cacheControl: CacheControl.PRIVATE_WITH_CACHE,
contentType: 'image/jpeg',
fileName: 'asset-id_thumbnail.jpg',
}),
);
});
Expand All @@ -585,6 +586,7 @@ describe(AssetMediaService.name, () => {
path: assetStub.image.files[0].path,
cacheControl: CacheControl.PRIVATE_WITH_CACHE,
contentType: 'image/jpeg',
fileName: 'asset-id_preview.jpg',
}),
);
});
Expand All @@ -599,6 +601,7 @@ describe(AssetMediaService.name, () => {
path: assetStub.image.files[1].path,
cacheControl: CacheControl.PRIVATE_WITH_CACHE,
contentType: 'application/octet-stream',
fileName: 'asset-id_thumbnail.ext',
}),
);
});
Expand Down
6 changes: 5 additions & 1 deletion server/src/services/asset-media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AuthRequest } from 'src/middleware/auth.guard';
import { BaseService } from 'src/services/base.service';
import { requireUploadAccess } from 'src/utils/access';
import { asRequest, getAssetFiles, onBeforeLink } from 'src/utils/asset.util';
import { ImmichFileResponse } from 'src/utils/file';
import { getFilenameExtension, getFileNameWithoutExtension, ImmichFileResponse } from 'src/utils/file';
import { mimeTypes } from 'src/utils/mime-types';
import { fromChecksum } from 'src/utils/request';
import { QueryFailedError } from 'typeorm';
Expand Down Expand Up @@ -217,8 +217,12 @@ export class AssetMediaService extends BaseService {
if (!filepath) {
throw new NotFoundException('Asset media not found');
}
let fileName = getFileNameWithoutExtension(asset.originalFileName);
fileName += `_${size}`;
fileName += getFilenameExtension(filepath);

return new ImmichFileResponse({
fileName,
path: filepath,
contentType: mimeTypes.lookup(filepath),
cacheControl: CacheControl.PRIVATE_WITH_CACHE,
Expand Down
4 changes: 4 additions & 0 deletions server/src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ImmichFileResponse {
public readonly path!: string;
public readonly contentType!: string;
public readonly cacheControl!: CacheControl;
public readonly fileName?: string;

constructor(response: ImmichFileResponse) {
Object.assign(this, response);
Expand Down Expand Up @@ -56,6 +57,9 @@ export const sendFile = async (
}

res.header('Content-Type', file.contentType);
if (file.fileName) {
res.header('Content-Disposition', `inline; filename="${file.fileName}"`);
}

const options: SendFileOptions = { dotfiles: 'allow' };
if (!isAbsolute(file.path)) {
Expand Down
Loading