immich/server/src/services/asset-upload.service.ts

406 lines
15 KiB
TypeScript
Raw Normal View History

2025-09-24 13:56:46 -04:00
import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common';
import { Response } from 'express';
2025-09-24 13:56:46 -04:00
import { createHash } from 'node:crypto';
import { extname, join } from 'node:path';
2025-09-28 18:37:16 -04:00
import { setTimeout } from 'node:timers/promises';
2025-09-24 13:56:46 -04:00
import { StorageCore } from 'src/cores/storage.core';
import { AuthDto } from 'src/dtos/auth.dto';
import { GetUploadStatusDto, ResumeUploadDto, StartUploadDto } from 'src/dtos/upload.dto';
import { AssetStatus, AssetType, AssetVisibility, JobName, StorageFolder } from 'src/enum';
import { AuthenticatedRequest } from 'src/middleware/auth.guard';
2025-09-24 13:56:46 -04:00
import { BaseService } from 'src/services/base.service';
import { isAssetChecksumConstraint } from 'src/utils/database';
import { mimeTypes } from 'src/utils/mime-types';
export const MAX_RUFH_INTEROP_VERSION = 8;
2025-09-29 04:31:47 -04:00
2025-09-24 13:56:46 -04:00
@Injectable()
export class AssetUploadService extends BaseService {
async startUpload(req: AuthenticatedRequest, response: Response, dto: StartUploadDto): Promise<void> {
this.logger.verboseFn(() => `Starting upload: ${JSON.stringify(dto)}`);
const { isComplete, assetData, uploadLength, contentLength, version } = dto;
if (isComplete && uploadLength !== undefined && uploadLength !== contentLength) {
2025-09-28 18:37:16 -04:00
return this.sendInconsistentLengthProblem(response);
}
2025-09-24 13:56:46 -04:00
const assetId = this.cryptoRepository.randomUUID();
const folder = StorageCore.getNestedFolder(StorageFolder.Upload, req.auth.user.id, assetId);
const extension = extname(assetData.filename);
2025-09-24 13:56:46 -04:00
const path = join(folder, `${assetId}${extension}`);
const type = mimeTypes.assetType(path);
2025-09-28 18:37:16 -04:00
2025-09-24 13:56:46 -04:00
if (type === AssetType.Other) {
throw new BadRequestException(`${assetData.filename} is an unsupported file type`);
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
this.validateQuota(req.auth, uploadLength ?? contentLength);
2025-09-24 13:56:46 -04:00
try {
await this.assetRepository.createWithMetadata(
{
id: assetId,
ownerId: req.auth.user.id,
libraryId: null,
checksum: dto.checksum,
originalPath: path,
deviceAssetId: assetData.deviceAssetId,
deviceId: assetData.deviceId,
fileCreatedAt: assetData.fileCreatedAt,
fileModifiedAt: assetData.fileModifiedAt,
localDateTime: assetData.fileCreatedAt,
type: mimeTypes.assetType(path),
isFavorite: assetData.isFavorite,
duration: assetData.duration || null,
visibility: assetData.visibility || AssetVisibility.Timeline,
originalFileName: assetData.filename,
status: AssetStatus.Partial,
},
assetData.metadata,
);
2025-09-24 13:56:46 -04:00
} catch (error: any) {
if (isAssetChecksumConstraint(error)) {
const duplicate = await this.assetRepository.getUploadAssetIdByChecksum(req.auth.user.id, dto.checksum);
2025-09-24 13:56:46 -04:00
if (!duplicate) {
throw new InternalServerErrorException('Error locating duplicate for checksum constraint');
}
2025-09-28 18:37:16 -04:00
if (duplicate.status !== AssetStatus.Partial) {
return this.sendAlreadyCompletedProblem(response);
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
const location = `/api/upload/${duplicate.id}`;
response.status(201).setHeader('Location', location).setHeader('Upload-Limit', 'min-size=0').send();
2025-09-24 13:56:46 -04:00
return;
}
this.logger.error(`Error creating upload asset record: ${error.message}`);
response.status(500).send('Error creating upload asset record');
return;
}
2025-09-28 18:37:16 -04:00
const location = `/api/upload/${assetId}`;
if (version <= MAX_RUFH_INTEROP_VERSION) {
this.sendInterimResponse(response, location, version);
2025-09-29 04:31:47 -04:00
}
2025-09-28 18:37:16 -04:00
2025-09-24 13:56:46 -04:00
await this.storageRepository.mkdir(folder);
let checksumBuffer: Buffer | undefined;
const writeStream = this.storageRepository.createWriteStream(path);
if (isComplete) {
const hash = createHash('sha1');
req.on('data', (chunk: Buffer) => hash.update(chunk));
2025-09-24 13:56:46 -04:00
writeStream.on('finish', () => (checksumBuffer = hash.digest()));
}
writeStream.on('error', (error) => {
this.logger.error(`Failed to write chunk to ${path}: ${error.message}`);
if (!response.headersSent) {
2025-09-28 18:37:16 -04:00
response.status(500).setHeader('Location', location).send();
2025-09-24 13:56:46 -04:00
}
});
writeStream.on('finish', async () => {
2025-09-24 13:56:46 -04:00
if (!isComplete) {
2025-09-28 18:37:16 -04:00
return response.status(201).setHeader('Location', location).setHeader('Upload-Limit', 'min-size=0').send();
2025-09-24 13:56:46 -04:00
}
this.logger.log(`Finished upload to ${path}`);
if (dto.checksum.compare(checksumBuffer!) !== 0) {
2025-09-28 18:37:16 -04:00
return this.sendChecksumMismatchResponse(response, assetId, path);
}
try {
await this.onComplete({ assetId, path, size: contentLength, fileModifiedAt: assetData.fileModifiedAt });
} finally {
this.setCompleteHeader(response, dto.version, true);
response.status(200).setHeader('Location', location).setHeader('Upload-Limit', 'min-size=0').send();
}
2025-09-24 13:56:46 -04:00
});
req.on('error', (error) => {
2025-09-24 13:56:46 -04:00
this.logger.error(`Failed to read request body: ${error.message}`);
writeStream.end();
if (!response.headersSent) {
2025-09-28 18:37:16 -04:00
response.status(500).setHeader('Location', location).send();
2025-09-24 13:56:46 -04:00
}
});
let receivedLength = 0;
req.on('data', (chunk: Buffer) => {
2025-09-24 13:56:46 -04:00
if (receivedLength + chunk.length > contentLength) {
writeStream.destroy();
req.destroy();
2025-09-24 13:56:46 -04:00
response.status(400).send('Received more data than specified in content-length');
return this.onCancel(assetId, path);
2025-09-24 13:56:46 -04:00
}
receivedLength += chunk.length;
if (!writeStream.write(chunk)) {
req.pause();
writeStream.once('drain', () => req.resume());
2025-09-24 13:56:46 -04:00
}
});
req.on('end', () => {
2025-09-28 18:37:16 -04:00
if (receivedLength === contentLength) {
return writeStream.end();
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
this.logger.error(`Received ${receivedLength} bytes when expecting ${contentLength} for ${assetId}`);
writeStream.destroy();
this.onCancel(assetId, path);
2025-09-24 13:56:46 -04:00
});
}
resumeUpload(req: AuthenticatedRequest, response: Response, id: string, dto: ResumeUploadDto): Promise<void> {
this.logger.verboseFn(() => `Resuming upload for ${id}: ${JSON.stringify(dto)}`);
const { isComplete, uploadLength, uploadOffset, contentLength, version } = dto;
if (isComplete && uploadLength !== undefined && uploadLength !== contentLength) {
this.sendInconsistentLengthProblem(response);
return Promise.resolve();
}
2025-09-28 18:37:16 -04:00
if (version && version >= 6 && req.headers['content-type'] !== 'application/partial-upload') {
2025-09-28 18:37:16 -04:00
throw new BadRequestException('Content-Type must be application/partial-upload for PATCH requests');
}
return this.databaseRepository.withUuidLock(id, async () => {
const asset = await this.assetRepository.getCompletionMetadata(id, req.auth.user.id);
2025-09-24 13:56:46 -04:00
if (!asset) {
response.status(404).send('Asset not found');
return;
}
if (asset.status !== AssetStatus.Partial) {
2025-09-28 18:37:16 -04:00
return this.sendAlreadyCompletedProblem(response);
}
if (uploadOffset === null) {
2025-09-28 18:37:16 -04:00
throw new BadRequestException('Missing Upload-Offset header');
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
const { path } = asset;
2025-09-24 13:56:46 -04:00
const expectedOffset = await this.getCurrentOffset(path);
if (expectedOffset !== uploadOffset) {
this.setCompleteHeader(response, version, false);
return this.sendOffsetMismatchProblem(response, expectedOffset, uploadOffset);
2025-09-24 13:56:46 -04:00
}
const newLength = uploadOffset + contentLength;
2025-09-24 13:56:46 -04:00
2025-09-28 18:37:16 -04:00
// If upload length is provided, validate we're not exceeding it
if (uploadLength !== undefined && newLength > uploadLength) {
2025-09-28 18:37:16 -04:00
response.status(400).send('Upload would exceed declared length');
return;
}
this.validateQuota(req.auth, newLength);
2025-09-28 18:37:16 -04:00
// Empty PATCH without Upload-Complete
if (contentLength === 0 && !isComplete) {
this.setCompleteHeader(response, version, false);
2025-09-29 04:31:47 -04:00
response.status(204).setHeader('Upload-Offset', expectedOffset.toString()).send();
2025-09-24 13:56:46 -04:00
return;
}
const writeStream = this.storageRepository.createOrAppendWriteStream(path);
2025-09-28 18:37:16 -04:00
let receivedLength = 0;
2025-09-24 13:56:46 -04:00
writeStream.on('error', (error) => {
this.logger.error(`Failed to write chunk to ${path}: ${error.message}`);
if (!response.headersSent) {
response.status(500).send('Failed to write chunk');
}
});
writeStream.on('finish', async () => {
2025-09-28 18:37:16 -04:00
const currentOffset = await this.getCurrentOffset(path);
if (!isComplete) {
this.setCompleteHeader(response, version, false);
2025-09-29 04:31:47 -04:00
return response.status(204).setHeader('Upload-Offset', currentOffset.toString()).send();
2025-09-28 18:37:16 -04:00
}
this.logger.log(`Finished upload to ${path}`);
const checksum = await this.cryptoRepository.hashFile(path);
if (asset.checksum.compare(checksum) !== 0) {
return this.sendChecksumMismatchResponse(response, id, path);
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
try {
await this.onComplete({ assetId: id, path, size: currentOffset, fileModifiedAt: asset.fileModifiedAt });
} finally {
this.setCompleteHeader(response, version, true);
response.status(200).setHeader('Upload-Offset', currentOffset.toString()).send();
}
2025-09-24 13:56:46 -04:00
});
req.on('data', (chunk: Buffer) => {
2025-09-24 13:56:46 -04:00
if (receivedLength + chunk.length > contentLength) {
this.logger.error(`Received more data than specified in content-length for upload to ${path}`);
2025-09-28 18:37:16 -04:00
writeStream.destroy();
req.destroy();
2025-09-28 18:37:16 -04:00
response.status(400).send('Received more data than specified in content-length');
return this.onCancel(id, path);
2025-09-24 13:56:46 -04:00
}
receivedLength += chunk.length;
if (!writeStream.write(chunk)) {
req.pause();
writeStream.once('drain', () => req.resume());
2025-09-24 13:56:46 -04:00
}
});
req.on('end', () => {
2025-09-28 18:37:16 -04:00
if (receivedLength === contentLength) {
return writeStream.end();
2025-09-24 13:56:46 -04:00
}
this.logger.error(`Received ${receivedLength} bytes when expecting ${contentLength} for ${id}`);
2025-09-28 18:37:16 -04:00
writeStream.destroy();
return this.onCancel(id, path);
2025-09-24 13:56:46 -04:00
});
});
}
2025-09-29 03:40:24 -04:00
async cancelUpload(auth: AuthDto, assetId: string, response: Response): Promise<void> {
const asset = await this.assetRepository.getCompletionMetadata(assetId, auth.user.id);
if (!asset) {
response.status(404).send('Asset not found');
return;
}
if (asset.status !== AssetStatus.Partial) {
return this.sendAlreadyCompletedProblem(response);
}
await this.onCancel(assetId, asset.path);
2025-09-29 03:40:24 -04:00
response.status(204).send();
}
async getUploadStatus(auth: AuthDto, response: Response, id: string, { version }: GetUploadStatusDto) {
return this.databaseRepository.withUuidLock(id, async () => {
const asset = await this.assetRepository.getCompletionMetadata(id, auth.user.id);
2025-09-24 13:56:46 -04:00
if (!asset) {
response.status(404).send('Asset not found');
return;
}
2025-09-28 18:37:16 -04:00
const offset = await this.getCurrentOffset(asset.path);
const isComplete = asset.status !== AssetStatus.Partial;
2025-09-24 13:56:46 -04:00
this.setCompleteHeader(response, version, isComplete);
2025-09-24 13:56:46 -04:00
response
.status(204)
2025-09-28 18:37:16 -04:00
.setHeader('Upload-Offset', offset.toString())
.setHeader('Cache-Control', 'no-store')
.setHeader('Upload-Limit', 'min-size=0')
2025-09-24 13:56:46 -04:00
.send();
});
}
2025-09-28 18:37:16 -04:00
async getUploadOptions(response: Response): Promise<void> {
response.status(204).setHeader('Upload-Limit', 'min-size=0').setHeader('Allow', 'POST, OPTIONS').send();
}
private async onComplete(data: { assetId: string; path: string; size: number; fileModifiedAt: Date }): Promise<void> {
const { assetId, path, size, fileModifiedAt } = data;
this.logger.debug('Completing upload for asset', assetId);
2025-09-24 13:56:46 -04:00
const jobData = { name: JobName.AssetExtractMetadata, data: { id: assetId, source: 'upload' } } as const;
2025-09-28 18:37:16 -04:00
await this.withRetry(() => this.assetRepository.setCompleteWithSize(assetId, size));
try {
await this.withRetry(() => this.storageRepository.utimes(path, new Date(), fileModifiedAt));
} catch (error: any) {
this.logger.error(`Failed to update times for ${path}: ${error.message}`);
}
2025-09-28 18:37:16 -04:00
await this.withRetry(() => this.jobRepository.queue(jobData));
}
private async onCancel(assetId: string, path: string): Promise<void> {
this.logger.debug('Cancelling upload for asset', assetId);
2025-09-28 18:37:16 -04:00
await this.withRetry(() => this.storageRepository.unlink(path));
await this.withRetry(() => this.assetRepository.remove({ id: assetId }));
}
2025-09-29 04:31:47 -04:00
private sendInterimResponse(response: Response, location: string, interopVersion: number): void {
2025-09-28 18:37:16 -04:00
const socket = response.socket;
if (socket && !socket.destroyed) {
// Express doesn't understand interim responses, so write directly to socket
socket.write(
2025-09-29 04:31:47 -04:00
'HTTP/1.1 104 Upload Resumption Supported\r\n' +
2025-09-28 18:37:16 -04:00
`Location: ${location}\r\n` +
2025-09-29 04:31:47 -04:00
`Upload-Draft-Interop-Version: ${interopVersion}\r\n\r\n`,
2025-09-28 18:37:16 -04:00
);
}
}
private sendInconsistentLengthProblem(response: Response): void {
response.status(400).contentType('application/problem+json').send({
type: 'https://iana.org/assignments/http-problem-types#inconsistent-upload-length',
2025-09-28 18:37:16 -04:00
title: 'inconsistent length values for upload',
});
}
private sendAlreadyCompletedProblem(response: Response): void {
response.status(400).contentType('application/problem+json').send({
type: 'https://iana.org/assignments/http-problem-types#completed-upload',
2025-09-28 18:37:16 -04:00
title: 'upload is already completed',
});
}
private sendOffsetMismatchProblem(response: Response, expected: number, actual: number): void {
2025-09-29 04:31:47 -04:00
response.status(409).contentType('application/problem+json').setHeader('Upload-Offset', expected.toString()).send({
type: 'https://iana.org/assignments/http-problem-types#mismatching-upload-offset',
title: 'offset from request does not match offset of resource',
'expected-offset': expected,
'provided-offset': actual,
});
2025-09-28 18:37:16 -04:00
}
private sendChecksumMismatchResponse(response: Response, assetId: string, path: string): Promise<void> {
this.logger.warn(`Removing upload asset ${assetId} due to checksum mismatch`);
response.status(460).send('Checksum mismatch');
return this.onCancel(assetId, path);
2025-09-24 13:56:46 -04:00
}
2025-09-28 18:37:16 -04:00
private async withRetry<T>(operation: () => Promise<T>, retries: number = 2, delay: number = 100): Promise<T> {
2025-09-24 13:56:46 -04:00
let lastError: any;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await operation();
} catch (error: any) {
lastError = error;
}
2025-09-28 18:37:16 -04:00
if (attempt < retries) {
await setTimeout(delay);
}
2025-09-24 13:56:46 -04:00
}
throw lastError;
}
2025-09-28 18:37:16 -04:00
private validateQuota(auth: AuthDto, size: number) {
2025-09-24 13:56:46 -04:00
if (auth.user.quotaSizeInBytes === null) {
return;
}
if (auth.user.quotaSizeInBytes < auth.user.quotaUsageInBytes + size) {
throw new BadRequestException('Quota has been exceeded!');
}
}
private async getCurrentOffset(path: string): Promise<number> {
try {
const stat = await this.storageRepository.stat(path);
return stat.size;
} catch (error: any) {
if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') {
return 0;
}
throw error;
}
}
2025-09-29 04:31:47 -04:00
private setCompleteHeader(response: Response, interopVersion: number | null, isComplete: boolean): void {
if (!interopVersion) {
return;
}
if (interopVersion > 3) {
response.setHeader('Upload-Complete', isComplete ? '?1' : '?0');
} else {
response.setHeader('Upload-Incomplete', isComplete ? '?0' : '?1');
}
}
2025-09-24 13:56:46 -04:00
}