mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
chore(server) refactor serveFile and downloadFile endpoint (#978)
This commit is contained in:
@@ -131,13 +131,13 @@ export class AssetController {
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/download')
|
||||
@Get('/download/:assetId')
|
||||
async downloadFile(
|
||||
@GetAuthUser() authUser: AuthUserDto,
|
||||
@Response({ passthrough: true }) res: Res,
|
||||
@Query(new ValidationPipe({ transform: true })) query: ServeFileDto,
|
||||
@Param('assetId') assetId: string,
|
||||
): Promise<any> {
|
||||
return this.assetService.downloadFile(query, res);
|
||||
return this.assetService.downloadFile(query, assetId, res);
|
||||
}
|
||||
|
||||
@Get('/download-library')
|
||||
@@ -154,14 +154,15 @@ export class AssetController {
|
||||
return stream;
|
||||
}
|
||||
|
||||
@Get('/file')
|
||||
@Get('/file/:assetId')
|
||||
@Header('Cache-Control', 'max-age=300')
|
||||
async serveFile(
|
||||
@Headers() headers: Record<string, string>,
|
||||
@GetAuthUser() authUser: AuthUserDto,
|
||||
@Response({ passthrough: true }) res: Res,
|
||||
@Query(new ValidationPipe({ transform: true })) query: ServeFileDto,
|
||||
@Param('assetId') assetId: string,
|
||||
): Promise<any> {
|
||||
return this.assetService.serveFile(authUser, query, res, headers);
|
||||
return this.assetService.serveFile(assetId, query, res, headers);
|
||||
}
|
||||
|
||||
@Get('/thumbnail/:assetId')
|
||||
|
||||
@@ -107,22 +107,6 @@ export class AssetService {
|
||||
return assets.map((asset) => mapAsset(asset));
|
||||
}
|
||||
|
||||
// TODO - Refactor this to get asset by its own id
|
||||
private async findAssetOfDevice(deviceId: string, assetId: string): Promise<AssetResponseDto> {
|
||||
const rows = await this.assetRepository.query(
|
||||
'SELECT * FROM assets a WHERE a."deviceAssetId" = $1 AND a."deviceId" = $2',
|
||||
[assetId, deviceId],
|
||||
);
|
||||
|
||||
if (rows.lengh == 0) {
|
||||
throw new NotFoundException('Not Found');
|
||||
}
|
||||
|
||||
const assetOnDevice = rows[0] as AssetEntity;
|
||||
|
||||
return mapAsset(assetOnDevice);
|
||||
}
|
||||
|
||||
public async getAssetById(authUser: AuthUserDto, assetId: string): Promise<AssetResponseDto> {
|
||||
const asset = await this._assetRepository.getById(assetId);
|
||||
|
||||
@@ -150,10 +134,10 @@ export class AssetService {
|
||||
return this.downloadService.downloadArchive(dto.name || `library`, assets);
|
||||
}
|
||||
|
||||
public async downloadFile(query: ServeFileDto, res: Res) {
|
||||
public async downloadFile(query: ServeFileDto, assetId: string, res: Res) {
|
||||
try {
|
||||
let fileReadStream = null;
|
||||
const asset = await this.findAssetOfDevice(query.did, query.aid);
|
||||
const asset = await this._assetRepository.getById(assetId);
|
||||
|
||||
// Download Video
|
||||
if (asset.type === AssetType.VIDEO) {
|
||||
@@ -251,9 +235,9 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
public async serveFile(authUser: AuthUserDto, query: ServeFileDto, res: Res, headers: any) {
|
||||
public async serveFile(assetId: string, query: ServeFileDto, res: Res, headers: any) {
|
||||
let fileReadStream: ReadStream;
|
||||
const asset = await this.findAssetOfDevice(query.did, query.aid);
|
||||
const asset = await this._assetRepository.getById(assetId);
|
||||
|
||||
if (!asset) {
|
||||
throw new NotFoundException('Asset does not exist');
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Transform } from 'class-transformer';
|
||||
import { IsBoolean, IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { IsBoolean, IsOptional } from 'class-validator';
|
||||
|
||||
export class ServeFileDto {
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({ type: String, title: 'Device Asset ID' })
|
||||
aid!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({ type: String, title: 'Device ID' })
|
||||
did!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Transform(({ value }) => {
|
||||
|
||||
Reference in New Issue
Block a user