mirror of
https://github.com/immich-app/immich.git
synced 2025-12-26 09:14:58 +03:00
chore(server): save original file name with extension (#7679)
* chore(server): save original file name with extension * extract extension * update e2e test * update e2e test * download archive * fix download archive appending name * pr feedback * remove unused code * test * unit test * remove unused code * migration * noops * pr feedback * Update server/src/domain/download/download.service.ts Co-authored-by: Mert <101130780+mertalev@users.noreply.github.com> --------- Co-authored-by: Mert <101130780+mertalev@users.noreply.github.com>
This commit is contained in:
@@ -609,6 +609,42 @@ describe(`${AssetController.name} (e2e)`, () => {
|
||||
expect(asset).toMatchObject({ id: body.id, isFavorite: true });
|
||||
});
|
||||
|
||||
it('should have correct original file name and extension (simple)', async () => {
|
||||
const { body, status } = await request(server)
|
||||
.post('/asset/upload')
|
||||
.set('Authorization', `Bearer ${user1.accessToken}`)
|
||||
.field('deviceAssetId', 'example-image')
|
||||
.field('deviceId', 'TEST')
|
||||
.field('fileCreatedAt', new Date().toISOString())
|
||||
.field('fileModifiedAt', new Date().toISOString())
|
||||
.field('isFavorite', 'true')
|
||||
.field('duration', '0:00:00.000000')
|
||||
.attach('assetData', randomBytes(32), 'example.jpg');
|
||||
expect(status).toBe(201);
|
||||
expect(body).toEqual({ id: expect.any(String), duplicate: false });
|
||||
|
||||
const asset = await api.assetApi.get(server, user1.accessToken, body.id);
|
||||
expect(asset).toMatchObject({ id: body.id, originalFileName: 'example.jpg' });
|
||||
});
|
||||
|
||||
it('should have correct original file name and extension (complex)', async () => {
|
||||
const { body, status } = await request(server)
|
||||
.post('/asset/upload')
|
||||
.set('Authorization', `Bearer ${user1.accessToken}`)
|
||||
.field('deviceAssetId', 'example-image')
|
||||
.field('deviceId', 'TEST')
|
||||
.field('fileCreatedAt', new Date().toISOString())
|
||||
.field('fileModifiedAt', new Date().toISOString())
|
||||
.field('isFavorite', 'true')
|
||||
.field('duration', '0:00:00.000000')
|
||||
.attach('assetData', randomBytes(32), 'example.complex.ext.jpg');
|
||||
expect(status).toBe(201);
|
||||
expect(body).toEqual({ id: expect.any(String), duplicate: false });
|
||||
|
||||
const asset = await api.assetApi.get(server, user1.accessToken, body.id);
|
||||
expect(asset).toMatchObject({ id: body.id, originalFileName: 'example.complex.ext.jpg' });
|
||||
});
|
||||
|
||||
it('should not upload the same asset twice', async () => {
|
||||
const content = randomBytes(32);
|
||||
await api.assetApi.upload(server, user1.accessToken, 'example-image', { content });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { AssetEntity } from '@app/infra/entities';
|
||||
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
||||
import { extname } from 'node:path';
|
||||
import { parse } from 'node:path';
|
||||
import { AccessCore, Permission } from '../access';
|
||||
import { AssetIdsDto } from '../asset';
|
||||
import { AuthDto } from '../auth';
|
||||
@@ -91,12 +91,13 @@ export class DownloadService {
|
||||
}
|
||||
|
||||
const { originalPath, originalFileName } = asset;
|
||||
const extension = extname(originalPath);
|
||||
let filename = `${originalFileName}${extension}`;
|
||||
|
||||
let filename = originalFileName;
|
||||
const count = paths[filename] || 0;
|
||||
paths[filename] = count + 1;
|
||||
if (count !== 0) {
|
||||
filename = `${originalFileName}+${count}${extension}`;
|
||||
const parsedFilename = parse(originalFileName);
|
||||
filename = `${parsedFilename.name}+${count}${parsedFilename.ext}`;
|
||||
}
|
||||
|
||||
zip.addFile(originalPath, filename);
|
||||
|
||||
@@ -26,7 +26,6 @@ import {
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { parse } from 'node:path';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
import { IAssetRepositoryV1 } from './asset-repository';
|
||||
import { AssetBulkUploadCheckDto } from './dto/asset-check.dto';
|
||||
@@ -356,7 +355,7 @@ export class AssetService {
|
||||
duration: dto.duration || null,
|
||||
isVisible: dto.isVisible ?? true,
|
||||
livePhotoVideo: livePhotoAssetId === null ? null : ({ id: livePhotoAssetId } as AssetEntity),
|
||||
originalFileName: parse(file.originalName).name,
|
||||
originalFileName: file.originalName,
|
||||
sidecarPath: sidecarPath || null,
|
||||
isReadOnly: dto.isReadOnly ?? false,
|
||||
isOffline: dto.isOffline ?? false,
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddExtensionToOriginalFileName1709763765506 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
WITH extension AS (WITH cte AS (SELECT a.id, STRING_TO_ARRAY(a."originalPath", '.')::TEXT[] AS arr
|
||||
FROM assets a)
|
||||
SELECT cte.id, cte.arr[ARRAY_UPPER(cte.arr, 1)] AS "ext"
|
||||
FROM cte)
|
||||
UPDATE assets
|
||||
SET "originalFileName" = assets."originalFileName" || '.' || extension."ext"
|
||||
FROM extension
|
||||
INNER JOIN assets a ON a.id = extension.id;
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(): Promise<void> {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
4
server/test/fixtures/asset.stub.ts
vendored
4
server/test/fixtures/asset.stub.ts
vendored
@@ -16,7 +16,7 @@ export const assetStackStub = (stackId: string, assets: AssetEntity[]): AssetSta
|
||||
export const assetStub = {
|
||||
noResizePath: Object.freeze<AssetEntity>({
|
||||
id: 'asset-id',
|
||||
originalFileName: 'IMG_123',
|
||||
originalFileName: 'IMG_123.jpg',
|
||||
deviceAssetId: 'device-asset-id',
|
||||
fileModifiedAt: new Date('2023-02-23T05:06:29.716Z'),
|
||||
fileCreatedAt: new Date('2023-02-23T05:06:29.716Z'),
|
||||
@@ -77,7 +77,7 @@ export const assetStub = {
|
||||
livePhotoVideoId: null,
|
||||
tags: [],
|
||||
sharedLinks: [],
|
||||
originalFileName: 'IMG_456',
|
||||
originalFileName: 'IMG_456.jpg',
|
||||
faces: [],
|
||||
sidecarPath: null,
|
||||
isReadOnly: false,
|
||||
|
||||
Reference in New Issue
Block a user