preserve file extension

This commit is contained in:
mertalev 2025-04-01 16:31:03 -04:00
parent 0da1c3b279
commit b803a35bdc
No known key found for this signature in database
GPG Key ID: DF6ABC77AAD98C95
3 changed files with 17 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { randomUUID } from 'node:crypto';
import { dirname, join, resolve } from 'node:path';
import path, { dirname, join, resolve } from 'node:path';
import { APP_MEDIA_LOCATION } from 'src/constants';
import { AssetEntity } from 'src/entities/asset.entity';
import { PersonEntity } from 'src/entities/person.entity';
@ -115,18 +115,21 @@ export class StorageCore {
return normalizedPath.startsWith(normalizedAppMediaLocation);
}
async moveAssetImage(asset: AssetEntity, pathType: GeneratedImageType, format: ImageFormat) {
const { id: entityId, files } = asset;
const oldFile = getAssetFile(files, pathType);
moveAssetImage(asset: AssetEntity, pathType: GeneratedImageType) {
const oldFile = getAssetFile(asset.files, pathType);
if (!oldFile?.path) {
return;
}
return this.moveFile({
entityId,
entityId: asset.id,
pathType,
oldPath: oldFile?.path || null,
newPath: StorageCore.getImagePath(asset, pathType, format),
oldPath: oldFile.path,
newPath: StorageCore.getImagePath(asset, pathType, path.extname(oldFile.path).slice(1) as ImageFormat),
});
}
async moveAssetVideo(asset: AssetEntity) {
moveAssetVideo(asset: AssetEntity) {
return this.moveFile({
entityId: asset.id,
pathType: AssetPathType.ENCODED_VIDEO,

View File

@ -238,19 +238,19 @@ describe(MediaService.name, () => {
entityId: assetStub.image.id,
pathType: AssetPathType.FULLSIZE,
oldPath: '/uploads/user-id/fullsize/path.webp',
newPath: 'upload/thumbs/user-id/as/se/asset-id-fullsize.jpeg',
newPath: 'upload/thumbs/user-id/as/se/asset-id-fullsize.webp',
});
expect(mocks.move.create).toHaveBeenCalledWith({
entityId: assetStub.image.id,
pathType: AssetPathType.PREVIEW,
oldPath: '/uploads/user-id/thumbs/path.jpg',
newPath: 'upload/thumbs/user-id/as/se/asset-id-preview.jpeg',
newPath: 'upload/thumbs/user-id/as/se/asset-id-preview.jpg',
});
expect(mocks.move.create).toHaveBeenCalledWith({
entityId: assetStub.image.id,
pathType: AssetPathType.THUMBNAIL,
oldPath: '/uploads/user-id/webp/path.ext',
newPath: 'upload/thumbs/user-id/as/se/asset-id-thumbnail.webp',
newPath: 'upload/thumbs/user-id/as/se/asset-id-thumbnail.ext',
});
expect(mocks.move.create).toHaveBeenCalledTimes(3);
});

View File

@ -134,15 +134,14 @@ export class MediaService extends BaseService {
@OnJob({ name: JobName.MIGRATE_ASSET, queue: QueueName.MIGRATION })
async handleAssetMigration({ id }: JobOf<JobName.MIGRATE_ASSET>): Promise<JobStatus> {
const { image } = await this.getConfig({ withCache: true });
const [asset] = await this.assetRepository.getByIds([id], { files: true });
if (!asset) {
return JobStatus.FAILED;
}
await this.storageCore.moveAssetImage(asset, AssetPathType.FULLSIZE, image.fullsize.format);
await this.storageCore.moveAssetImage(asset, AssetPathType.PREVIEW, image.preview.format);
await this.storageCore.moveAssetImage(asset, AssetPathType.THUMBNAIL, image.thumbnail.format);
await this.storageCore.moveAssetImage(asset, AssetPathType.FULLSIZE);
await this.storageCore.moveAssetImage(asset, AssetPathType.PREVIEW);
await this.storageCore.moveAssetImage(asset, AssetPathType.THUMBNAIL);
await this.storageCore.moveAssetVideo(asset);
return JobStatus.SUCCESS;