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
19 changed files with 116 additions and 175 deletions
@@ -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');