refactor(server): handle download (#2637)
This commit is contained in:
parent
800f010383
commit
038e064e60
4 changed files with 14 additions and 34 deletions
|
@ -11,24 +11,11 @@ import { AlbumResponseDto } from '@app/domain';
|
|||
import { AlbumCountResponseDto } from './response-dto/album-count-response.dto';
|
||||
import { AddAssetsResponseDto } from './response-dto/add-assets-response.dto';
|
||||
import { Response as Res } from 'express';
|
||||
import {
|
||||
IMMICH_ARCHIVE_COMPLETE,
|
||||
IMMICH_ARCHIVE_FILE_COUNT,
|
||||
IMMICH_CONTENT_LENGTH_HINT,
|
||||
} from '../../constants/download.constant';
|
||||
import { DownloadDto } from '../asset/dto/download-library.dto';
|
||||
import { CreateAlbumShareLinkDto as CreateAlbumSharedLinkDto } from './dto/create-album-shared-link.dto';
|
||||
import { UseValidation } from '../../decorators/use-validation.decorator';
|
||||
import { UUIDParamDto } from '../../controllers/dto/uuid-param.dto';
|
||||
import { DownloadArchive } from '../../modules/download/download.service';
|
||||
|
||||
const handleDownload = (download: DownloadArchive, res: Res) => {
|
||||
res.attachment(download.fileName);
|
||||
res.setHeader(IMMICH_CONTENT_LENGTH_HINT, download.fileSize);
|
||||
res.setHeader(IMMICH_ARCHIVE_FILE_COUNT, download.fileCount);
|
||||
res.setHeader(IMMICH_ARCHIVE_COMPLETE, `${download.complete}`);
|
||||
return download.stream;
|
||||
};
|
||||
import { handleDownload } from '../../app.utils';
|
||||
|
||||
@ApiTags('Album')
|
||||
@Controller('album')
|
||||
|
|
|
@ -45,11 +45,6 @@ import { CheckExistingAssetsDto } from './dto/check-existing-assets.dto';
|
|||
import { CheckExistingAssetsResponseDto } from './response-dto/check-existing-assets-response.dto';
|
||||
import { UpdateAssetDto } from './dto/update-asset.dto';
|
||||
import { DownloadDto } from './dto/download-library.dto';
|
||||
import {
|
||||
IMMICH_ARCHIVE_COMPLETE,
|
||||
IMMICH_ARCHIVE_FILE_COUNT,
|
||||
IMMICH_CONTENT_LENGTH_HINT,
|
||||
} from '../../constants/download.constant';
|
||||
import { DownloadFilesDto } from './dto/download-files.dto';
|
||||
import { CreateAssetsShareLinkDto } from './dto/create-asset-shared-link.dto';
|
||||
import { SharedLinkResponseDto } from '@app/domain';
|
||||
|
@ -61,6 +56,7 @@ import { AssetBulkUploadCheckDto } from './dto/asset-check.dto';
|
|||
import { AssetBulkUploadCheckResponseDto } from './response-dto/asset-check-response.dto';
|
||||
import { AssetIdDto } from './dto/asset-id.dto';
|
||||
import { DeviceIdDto } from './dto/device-id.dto';
|
||||
import { handleDownload } from '../../app.utils';
|
||||
|
||||
function asStreamableFile({ stream, type, length }: ImmichReadStream) {
|
||||
return new StreamableFile(stream, { type, length });
|
||||
|
@ -138,12 +134,7 @@ export class AssetController {
|
|||
) {
|
||||
this.assetService.checkDownloadAccess(authUser);
|
||||
await this.assetService.checkAssetsAccess(authUser, [...dto.assetIds]);
|
||||
const { stream, fileName, fileSize, fileCount, complete } = await this.assetService.downloadFiles(dto);
|
||||
res.attachment(fileName);
|
||||
res.setHeader(IMMICH_CONTENT_LENGTH_HINT, fileSize);
|
||||
res.setHeader(IMMICH_ARCHIVE_FILE_COUNT, fileCount);
|
||||
res.setHeader(IMMICH_ARCHIVE_COMPLETE, `${complete}`);
|
||||
return stream;
|
||||
return this.assetService.downloadFiles(dto).then((download) => handleDownload(download, res));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,12 +149,7 @@ export class AssetController {
|
|||
@Response({ passthrough: true }) res: Res,
|
||||
) {
|
||||
this.assetService.checkDownloadAccess(authUser);
|
||||
const { stream, fileName, fileSize, fileCount, complete } = await this.assetService.downloadLibrary(authUser, dto);
|
||||
res.attachment(fileName);
|
||||
res.setHeader(IMMICH_CONTENT_LENGTH_HINT, fileSize);
|
||||
res.setHeader(IMMICH_ARCHIVE_FILE_COUNT, fileCount);
|
||||
res.setHeader(IMMICH_ARCHIVE_COMPLETE, `${complete}`);
|
||||
return stream;
|
||||
return this.assetService.downloadLibrary(authUser, dto).then((download) => handleDownload(download, res));
|
||||
}
|
||||
|
||||
@SharedLinkRoute()
|
||||
|
|
10
server/apps/immich/src/app.utils.ts
Normal file
10
server/apps/immich/src/app.utils.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { Response } from 'express';
|
||||
import { DownloadArchive } from './modules/download/download.service';
|
||||
|
||||
export const handleDownload = (download: DownloadArchive, res: Response) => {
|
||||
res.attachment(download.fileName);
|
||||
res.setHeader('X-Immich-Content-Length-Hint', download.fileSize);
|
||||
res.setHeader('X-Immich-Archive-File-Count', download.fileCount);
|
||||
res.setHeader('X-Immich-Archive-Complete', `${download.complete}`);
|
||||
return download.stream;
|
||||
};
|
|
@ -1,3 +0,0 @@
|
|||
export const IMMICH_CONTENT_LENGTH_HINT = 'X-Immich-Content-Length-Hint';
|
||||
export const IMMICH_ARCHIVE_FILE_COUNT = 'X-Immich-Archive-File-Count';
|
||||
export const IMMICH_ARCHIVE_COMPLETE = 'X-Immich-Archive-Complete';
|
Loading…
Reference in a new issue