1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-28 09:33:27 +02:00

chore(server) refactor serveFile and downloadFile endpoint (#978)

This commit is contained in:
Alex 2022-11-16 00:11:16 -06:00 committed by GitHub
parent 1db255fd3e
commit e799f35dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 88 additions and 133 deletions

View File

@ -23,8 +23,7 @@ class ImageViewerService {
String fileName = p.basename(asset.originalPath);
var res = await _apiService.assetApi.downloadFileWithHttpInfo(
asset.deviceAssetId,
asset.deviceId,
asset.id,
isThumb: false,
isWeb: false,
);

View File

@ -42,7 +42,7 @@ class VideoViewerPage extends HookConsumerWidget {
final box = Hive.box(userInfoBox);
final String jwtToken = box.get(accessTokenKey);
final String videoUrl =
'${box.get(serverEndpointKey)}/asset/file?aid=${asset.deviceAssetId}&did=${asset.deviceId}';
'${box.get(serverEndpointKey)}/asset/file/${asset.id}';
return Stack(
children: [

View File

@ -28,8 +28,7 @@ class ShareService {
final fileName = basename(asset.remote!.originalPath);
final tempFile = await File('${tempDir.path}/$fileName').create();
final res = await _apiService.assetApi.downloadFileWithHttpInfo(
asset.remote!.deviceAssetId,
asset.remote!.deviceId,
asset.remote!.id,
isThumb: false,
isWeb: false,
);

View File

@ -22,7 +22,7 @@ String getAlbumThumbnailUrl(
String getImageUrl(final AssetResponseDto asset) {
final box = Hive.box(userInfoBox);
return '${box.get(serverEndpointKey)}/asset/file?aid=${asset.deviceAssetId}&did=${asset.deviceId}&isThumb=false';
return '${box.get(serverEndpointKey)}/asset/file/${asset.id}?isThumb=false';
}
String _getThumbnailUrl(

Binary file not shown.

Binary file not shown.

View File

@ -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')

View File

@ -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');

View File

@ -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

View File

@ -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",

View File

@ -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",

View File

@ -2702,19 +2702,17 @@ export const AssetApiAxiosParamCreator = function (configuration?: Configuration
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
downloadFile: async (aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'aid' is not null or undefined
assertParamExists('downloadFile', 'aid', aid)
// verify required parameter 'did' is not null or undefined
assertParamExists('downloadFile', 'did', did)
const localVarPath = `/asset/download`;
downloadFile: async (assetId: string, isThumb?: boolean, isWeb?: boolean, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'assetId' is not null or undefined
assertParamExists('downloadFile', 'assetId', assetId)
const localVarPath = `/asset/download/{assetId}`
.replace(`{${"assetId"}}`, encodeURIComponent(String(assetId)));
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
@ -2730,14 +2728,6 @@ export const AssetApiAxiosParamCreator = function (configuration?: Configuration
// http bearer authentication required
await setBearerAuthToObject(localVarHeaderParameter, configuration)
if (aid !== undefined) {
localVarQueryParameter['aid'] = aid;
}
if (did !== undefined) {
localVarQueryParameter['did'] = did;
}
if (isThumb !== undefined) {
localVarQueryParameter['isThumb'] = isThumb;
}
@ -3198,19 +3188,17 @@ export const AssetApiAxiosParamCreator = function (configuration?: Configuration
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
serveFile: async (aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'aid' is not null or undefined
assertParamExists('serveFile', 'aid', aid)
// verify required parameter 'did' is not null or undefined
assertParamExists('serveFile', 'did', did)
const localVarPath = `/asset/file`;
serveFile: async (assetId: string, isThumb?: boolean, isWeb?: boolean, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'assetId' is not null or undefined
assertParamExists('serveFile', 'assetId', assetId)
const localVarPath = `/asset/file/{assetId}`
.replace(`{${"assetId"}}`, encodeURIComponent(String(assetId)));
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
@ -3226,14 +3214,6 @@ export const AssetApiAxiosParamCreator = function (configuration?: Configuration
// http bearer authentication required
await setBearerAuthToObject(localVarHeaderParameter, configuration)
if (aid !== undefined) {
localVarQueryParameter['aid'] = aid;
}
if (did !== undefined) {
localVarQueryParameter['did'] = did;
}
if (isThumb !== undefined) {
localVarQueryParameter['isThumb'] = isThumb;
}
@ -3385,15 +3365,14 @@ export const AssetApiFp = function(configuration?: Configuration) {
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async downloadFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.downloadFile(aid, did, isThumb, isWeb, options);
async downloadFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.downloadFile(assetId, isThumb, isWeb, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
@ -3517,15 +3496,14 @@ export const AssetApiFp = function(configuration?: Configuration) {
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async serveFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.serveFile(aid, did, isThumb, isWeb, options);
async serveFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.serveFile(assetId, isThumb, isWeb, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
@ -3591,15 +3569,14 @@ export const AssetApiFactory = function (configuration?: Configuration, basePath
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
downloadFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: any): AxiosPromise<object> {
return localVarFp.downloadFile(aid, did, isThumb, isWeb, options).then((request) => request(axios, basePath));
downloadFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: any): AxiosPromise<object> {
return localVarFp.downloadFile(assetId, isThumb, isWeb, options).then((request) => request(axios, basePath));
},
/**
*
@ -3710,15 +3687,14 @@ export const AssetApiFactory = function (configuration?: Configuration, basePath
},
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
serveFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: any): AxiosPromise<object> {
return localVarFp.serveFile(aid, did, isThumb, isWeb, options).then((request) => request(axios, basePath));
serveFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: any): AxiosPromise<object> {
return localVarFp.serveFile(assetId, isThumb, isWeb, options).then((request) => request(axios, basePath));
},
/**
* Update an asset
@ -3787,16 +3763,15 @@ export class AssetApi extends BaseAPI {
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof AssetApi
*/
public downloadFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig) {
return AssetApiFp(this.configuration).downloadFile(aid, did, isThumb, isWeb, options).then((request) => request(this.axios, this.basePath));
public downloadFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig) {
return AssetApiFp(this.configuration).downloadFile(assetId, isThumb, isWeb, options).then((request) => request(this.axios, this.basePath));
}
/**
@ -3932,16 +3907,15 @@ export class AssetApi extends BaseAPI {
/**
*
* @param {string} aid
* @param {string} did
* @param {string} assetId
* @param {boolean} [isThumb]
* @param {boolean} [isWeb]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof AssetApi
*/
public serveFile(aid: string, did: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig) {
return AssetApiFp(this.configuration).serveFile(aid, did, isThumb, isWeb, options).then((request) => request(this.axios, this.basePath));
public serveFile(assetId: string, isThumb?: boolean, isWeb?: boolean, options?: AxiosRequestConfig) {
return AssetApiFp(this.configuration).serveFile(assetId, isThumb, isWeb, options).then((request) => request(this.axios, this.basePath));
}
/**

View File

@ -1,10 +1,8 @@
const _basePath = '/api';
export function getFileUrl(aid: string, did: string, isThumb?: boolean, isWeb?: boolean) {
const urlObj = new URL(`${window.location.origin}${_basePath}/asset/file`);
export function getFileUrl(assetId: string, isThumb?: boolean, isWeb?: boolean) {
const urlObj = new URL(`${window.location.origin}${_basePath}/asset/file/${assetId}`);
urlObj.searchParams.append('aid', aid);
urlObj.searchParams.append('did', did);
if (isThumb !== undefined && isThumb !== null)
urlObj.searchParams.append('isThumb', `${isThumb}`);
if (isWeb !== undefined && isWeb !== null) urlObj.searchParams.append('isWeb', `${isWeb}`);

View File

@ -101,12 +101,7 @@
$downloadAssets[imageFileName] = 0;
const { data, status } = await api.assetApi.downloadFile(
asset.deviceAssetId,
asset.deviceId,
false,
false,
{
const { data, status } = await api.assetApi.downloadFile(asset.id, false, false, {
responseType: 'blob',
onDownloadProgress: (progressEvent) => {
if (progressEvent.lengthComputable) {
@ -115,8 +110,7 @@
$downloadAssets[imageFileName] = Math.floor((current / total) * 100);
}
}
}
);
});
if (!(data instanceof Blob)) {
return;
@ -262,7 +256,7 @@
<div class="row-start-1 row-span-full col-start-1 col-span-4">
{#key asset.id}
{#if asset.type === AssetTypeEnum.Image}
<PhotoViewer assetId={asset.id} deviceId={asset.deviceId} on:close={closeViewer} />
<PhotoViewer assetId={asset.id} on:close={closeViewer} />
{:else}
<VideoViewer assetId={asset.id} on:close={closeViewer} />
{/if}

View File

@ -7,7 +7,6 @@
import Keydown from 'svelte-keydown';
export let assetId: string;
export let deviceId: string;
let assetInfo: AssetResponseDto;
let assetData: string;
@ -25,15 +24,9 @@
const loadAssetData = async () => {
try {
const { data } = await api.assetApi.serveFile(
assetInfo.deviceAssetId,
deviceId,
false,
true,
{
const { data } = await api.assetApi.serveFile(assetInfo.id, false, true, {
responseType: 'blob'
}
);
});
if (!(data instanceof Blob)) {
return;

View File

@ -24,7 +24,7 @@
const loadVideoData = async (assetInfo: AssetResponseDto) => {
isVideoLoading = true;
videoUrl = getFileUrl(assetInfo.deviceAssetId, assetInfo.deviceId, false, true);
videoUrl = getFileUrl(assetInfo.id, false, true);
return assetInfo;
};

View File

@ -31,7 +31,7 @@
const loadVideoData = async () => {
isThumbnailVideoPlaying = false;
videoUrl = getFileUrl(asset.deviceAssetId, asset.deviceId, false, true);
videoUrl = getFileUrl(asset.id, false, true);
};
const getVideoDurationInString = (currentTime: number) => {