fix(server): remove the asset row when an upload fails after creating it (#30349)
This commit is contained in:
@@ -317,6 +317,12 @@ describe(AssetMediaService.name, () => {
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
|
||||
expect(mocks.asset.create).not.toHaveBeenCalled();
|
||||
expect(mocks.asset.remove).not.toHaveBeenCalled();
|
||||
expect(mocks.job.queue).toHaveBeenCalledWith({
|
||||
name: JobName.FileDelete,
|
||||
data: { files: [file.originalPath, undefined] },
|
||||
});
|
||||
expect(mocks.event.emit).not.toHaveBeenCalled();
|
||||
expect(mocks.user.updateUsage).not.toHaveBeenCalledWith(authStub.user1.user.id, file.size);
|
||||
expect(mocks.storage.utimes).not.toHaveBeenCalledWith(
|
||||
file.originalPath,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { BadRequestException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
|
||||
import sanitize from 'sanitize-filename';
|
||||
import { StorageCore } from 'src/cores/storage.core';
|
||||
import { AuthSharedLink } from 'src/database';
|
||||
import { Asset, AuthSharedLink } from 'src/database';
|
||||
import {
|
||||
AssetBulkUploadCheckResponseDto,
|
||||
AssetMediaResponseDto,
|
||||
@@ -128,6 +128,7 @@ export class AssetMediaService extends BaseService {
|
||||
file: UploadFile,
|
||||
sidecarFile?: UploadFile,
|
||||
): Promise<AssetMediaResponseDto> {
|
||||
let asset: Asset | undefined;
|
||||
try {
|
||||
await this.requireAccess({
|
||||
auth,
|
||||
@@ -145,7 +146,7 @@ export class AssetMediaService extends BaseService {
|
||||
);
|
||||
}
|
||||
|
||||
const asset = await this.assetRepository.create({
|
||||
asset = await this.assetRepository.create({
|
||||
ownerId: auth.user.id,
|
||||
libraryId: null,
|
||||
|
||||
@@ -215,6 +216,11 @@ export class AssetMediaService extends BaseService {
|
||||
return { status: AssetMediaStatus.DUPLICATE, id: duplicateId };
|
||||
}
|
||||
|
||||
// clean up the asset row if one was created
|
||||
if (asset) {
|
||||
await this.assetRepository.remove({ id: asset.id });
|
||||
}
|
||||
|
||||
this.logger.error(`Error uploading file ${error}`, error?.stack);
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user