mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-12-08 20:29:05 +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 }) => {
|
||||
|
||||
File diff suppressed because one or more lines are too long
24
server/package-lock.json
generated
24
server/package-lock.json
generated
@@ -33,6 +33,7 @@
|
||||
"diskusage": "^1.1.3",
|
||||
"dotenv": "^14.2.0",
|
||||
"exifr": "^7.1.3",
|
||||
"fdir": "^5.3.0",
|
||||
"fluent-ffmpeg": "^2.1.2",
|
||||
"geo-tz": "^7.0.2",
|
||||
"i18n-iso-countries": "^7.5.0",
|
||||
@@ -5583,6 +5584,19 @@
|
||||
"bser": "2.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/fdir": {
|
||||
"version": "5.3.0",
|
||||
"resolved": "https://registry.npmjs.org/fdir/-/fdir-5.3.0.tgz",
|
||||
"integrity": "sha512-BtE53+jaa7nNHT+gPdfU6cFAXOJUWDs2b5GFox8dtl6zLXmfNf/N6im69b9nqNNwDyl27mpIWX8qR7AafWzSdQ==",
|
||||
"peerDependencies": {
|
||||
"picomatch": "2.x"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"picomatch": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/figures": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz",
|
||||
@@ -8885,7 +8899,7 @@
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
|
||||
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"engines": {
|
||||
"node": ">=8.6"
|
||||
},
|
||||
@@ -15772,6 +15786,12 @@
|
||||
"bser": "2.1.1"
|
||||
}
|
||||
},
|
||||
"fdir": {
|
||||
"version": "5.3.0",
|
||||
"resolved": "https://registry.npmjs.org/fdir/-/fdir-5.3.0.tgz",
|
||||
"integrity": "sha512-BtE53+jaa7nNHT+gPdfU6cFAXOJUWDs2b5GFox8dtl6zLXmfNf/N6im69b9nqNNwDyl27mpIWX8qR7AafWzSdQ==",
|
||||
"requires": {}
|
||||
},
|
||||
"figures": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz",
|
||||
@@ -18309,7 +18329,7 @@
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
|
||||
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
|
||||
"dev": true
|
||||
"devOptional": true
|
||||
},
|
||||
"pirates": {
|
||||
"version": "4.0.5",
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
"diskusage": "^1.1.3",
|
||||
"dotenv": "^14.2.0",
|
||||
"exifr": "^7.1.3",
|
||||
"fdir": "^5.3.0",
|
||||
"fluent-ffmpeg": "^2.1.2",
|
||||
"geo-tz": "^7.0.2",
|
||||
"i18n-iso-countries": "^7.5.0",
|
||||
@@ -63,8 +64,8 @@
|
||||
"local-reverse-geocoder": "^0.12.5",
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^3.0.3",
|
||||
"openid-client": "^5.2.1",
|
||||
"nest-commander": "^3.3.0",
|
||||
"openid-client": "^5.2.1",
|
||||
"passport": "^0.6.0",
|
||||
"passport-jwt": "^4.0.0",
|
||||
"pg": "^8.7.1",
|
||||
@@ -144,4 +145,4 @@
|
||||
"^@app/system-config(|/.*)$": "<rootDir>/libs/system-config/src/$1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user