From 038e064e60d0e71ef6c091406735ed4695f7efb3 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Thu, 1 Jun 2023 22:03:15 -0400 Subject: [PATCH] refactor(server): handle download (#2637) --- .../src/api-v1/album/album.controller.ts | 15 +------------- .../src/api-v1/asset/asset.controller.ts | 20 +++---------------- server/apps/immich/src/app.utils.ts | 10 ++++++++++ .../immich/src/constants/download.constant.ts | 3 --- 4 files changed, 14 insertions(+), 34 deletions(-) create mode 100644 server/apps/immich/src/app.utils.ts delete mode 100644 server/apps/immich/src/constants/download.constant.ts diff --git a/server/apps/immich/src/api-v1/album/album.controller.ts b/server/apps/immich/src/api-v1/album/album.controller.ts index 874435910f..b84e807a8b 100644 --- a/server/apps/immich/src/api-v1/album/album.controller.ts +++ b/server/apps/immich/src/api-v1/album/album.controller.ts @@ -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') diff --git a/server/apps/immich/src/api-v1/asset/asset.controller.ts b/server/apps/immich/src/api-v1/asset/asset.controller.ts index a042e980ec..82d9ea8baf 100644 --- a/server/apps/immich/src/api-v1/asset/asset.controller.ts +++ b/server/apps/immich/src/api-v1/asset/asset.controller.ts @@ -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() diff --git a/server/apps/immich/src/app.utils.ts b/server/apps/immich/src/app.utils.ts new file mode 100644 index 0000000000..b836e0aa92 --- /dev/null +++ b/server/apps/immich/src/app.utils.ts @@ -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; +}; diff --git a/server/apps/immich/src/constants/download.constant.ts b/server/apps/immich/src/constants/download.constant.ts deleted file mode 100644 index 70d6889f92..0000000000 --- a/server/apps/immich/src/constants/download.constant.ts +++ /dev/null @@ -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';