mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-04 03:39:37 -05:00 
			
		
		
		
	chore: rebase main (#3103)
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
This commit is contained in:
		
							parent
							
								
									34d1f74b77
								
							
						
					
					
						commit
						2fb85f4a16
					
				@ -4,8 +4,6 @@ import {
 | 
				
			|||||||
  Controller,
 | 
					  Controller,
 | 
				
			||||||
  Delete,
 | 
					  Delete,
 | 
				
			||||||
  Get,
 | 
					  Get,
 | 
				
			||||||
  Header,
 | 
					 | 
				
			||||||
  Headers,
 | 
					 | 
				
			||||||
  HttpCode,
 | 
					  HttpCode,
 | 
				
			||||||
  HttpStatus,
 | 
					  HttpStatus,
 | 
				
			||||||
  Param,
 | 
					  Param,
 | 
				
			||||||
@ -111,39 +109,35 @@ export class AssetController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @SharedLinkRoute()
 | 
					  @SharedLinkRoute()
 | 
				
			||||||
  @Get('/file/:id')
 | 
					  @Get('/file/:id')
 | 
				
			||||||
  @Header('Cache-Control', 'private, max-age=86400, no-transform')
 | 
					 | 
				
			||||||
  @ApiOkResponse({
 | 
					  @ApiOkResponse({
 | 
				
			||||||
    content: {
 | 
					    content: {
 | 
				
			||||||
      'application/octet-stream': { schema: { type: 'string', format: 'binary' } },
 | 
					      'application/octet-stream': { schema: { type: 'string', format: 'binary' } },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  serveFile(
 | 
					  async serveFile(
 | 
				
			||||||
    @AuthUser() authUser: AuthUserDto,
 | 
					    @AuthUser() authUser: AuthUserDto,
 | 
				
			||||||
    @Headers() headers: Record<string, string>,
 | 
					    @Response() res: Res,
 | 
				
			||||||
    @Response({ passthrough: true }) res: Res,
 | 
					 | 
				
			||||||
    @Query(new ValidationPipe({ transform: true })) query: ServeFileDto,
 | 
					    @Query(new ValidationPipe({ transform: true })) query: ServeFileDto,
 | 
				
			||||||
    @Param() { id }: UUIDParamDto,
 | 
					    @Param() { id }: UUIDParamDto,
 | 
				
			||||||
  ) {
 | 
					  ) {
 | 
				
			||||||
    return this.assetService.serveFile(authUser, id, query, res, headers);
 | 
					    await this.assetService.serveFile(authUser, id, query, res);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @SharedLinkRoute()
 | 
					  @SharedLinkRoute()
 | 
				
			||||||
  @Get('/thumbnail/:id')
 | 
					  @Get('/thumbnail/:id')
 | 
				
			||||||
  @Header('Cache-Control', 'private, max-age=86400, no-transform')
 | 
					 | 
				
			||||||
  @ApiOkResponse({
 | 
					  @ApiOkResponse({
 | 
				
			||||||
    content: {
 | 
					    content: {
 | 
				
			||||||
      'image/jpeg': { schema: { type: 'string', format: 'binary' } },
 | 
					      'image/jpeg': { schema: { type: 'string', format: 'binary' } },
 | 
				
			||||||
      'image/webp': { schema: { type: 'string', format: 'binary' } },
 | 
					      'image/webp': { schema: { type: 'string', format: 'binary' } },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  getAssetThumbnail(
 | 
					  async getAssetThumbnail(
 | 
				
			||||||
    @AuthUser() authUser: AuthUserDto,
 | 
					    @AuthUser() authUser: AuthUserDto,
 | 
				
			||||||
    @Headers() headers: Record<string, string>,
 | 
					    @Response() res: Res,
 | 
				
			||||||
    @Response({ passthrough: true }) res: Res,
 | 
					 | 
				
			||||||
    @Param() { id }: UUIDParamDto,
 | 
					    @Param() { id }: UUIDParamDto,
 | 
				
			||||||
    @Query(new ValidationPipe({ transform: true })) query: GetAssetThumbnailDto,
 | 
					    @Query(new ValidationPipe({ transform: true })) query: GetAssetThumbnailDto,
 | 
				
			||||||
  ) {
 | 
					  ) {
 | 
				
			||||||
    return this.assetService.getAssetThumbnail(authUser, id, query, res, headers);
 | 
					    await this.assetService.serveThumbnail(authUser, id, query, res);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Get('/curated-objects')
 | 
					  @Get('/curated-objects')
 | 
				
			||||||
 | 
				
			|||||||
@ -28,11 +28,10 @@ import {
 | 
				
			|||||||
} from '@nestjs/common';
 | 
					} from '@nestjs/common';
 | 
				
			||||||
import { InjectRepository } from '@nestjs/typeorm';
 | 
					import { InjectRepository } from '@nestjs/typeorm';
 | 
				
			||||||
import { Response as Res } from 'express';
 | 
					import { Response as Res } from 'express';
 | 
				
			||||||
import { constants, createReadStream } from 'fs';
 | 
					import { constants } from 'fs';
 | 
				
			||||||
import fs from 'fs/promises';
 | 
					import fs from 'fs/promises';
 | 
				
			||||||
import path, { extname } from 'path';
 | 
					import path, { extname } from 'path';
 | 
				
			||||||
import sanitize from 'sanitize-filename';
 | 
					import sanitize from 'sanitize-filename';
 | 
				
			||||||
import { pipeline } from 'stream/promises';
 | 
					 | 
				
			||||||
import { QueryFailedError, Repository } from 'typeorm';
 | 
					import { QueryFailedError, Repository } from 'typeorm';
 | 
				
			||||||
import { UploadRequest } from '../../app.interceptor';
 | 
					import { UploadRequest } from '../../app.interceptor';
 | 
				
			||||||
import { IAssetRepository } from './asset-repository';
 | 
					import { IAssetRepository } from './asset-repository';
 | 
				
			||||||
@ -301,13 +300,7 @@ export class AssetService {
 | 
				
			|||||||
    return mapAsset(updatedAsset);
 | 
					    return mapAsset(updatedAsset);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getAssetThumbnail(
 | 
					  async serveThumbnail(authUser: AuthUserDto, assetId: string, query: GetAssetThumbnailDto, res: Res) {
 | 
				
			||||||
    authUser: AuthUserDto,
 | 
					 | 
				
			||||||
    assetId: string,
 | 
					 | 
				
			||||||
    query: GetAssetThumbnailDto,
 | 
					 | 
				
			||||||
    res: Res,
 | 
					 | 
				
			||||||
    headers: Record<string, string>,
 | 
					 | 
				
			||||||
  ) {
 | 
					 | 
				
			||||||
    await this.access.requirePermission(authUser, Permission.ASSET_VIEW, assetId);
 | 
					    await this.access.requirePermission(authUser, Permission.ASSET_VIEW, assetId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const asset = await this._assetRepository.get(assetId);
 | 
					    const asset = await this._assetRepository.get(assetId);
 | 
				
			||||||
@ -316,7 +309,7 @@ export class AssetService {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      return this.streamFile(this.getThumbnailPath(asset, query.format), res, headers);
 | 
					      await this.sendFile(res, this.getThumbnailPath(asset, query.format));
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
      res.header('Cache-Control', 'none');
 | 
					      res.header('Cache-Control', 'none');
 | 
				
			||||||
      this.logger.error(`Cannot create read stream for asset ${asset.id}`, 'getAssetThumbnail');
 | 
					      this.logger.error(`Cannot create read stream for asset ${asset.id}`, 'getAssetThumbnail');
 | 
				
			||||||
@ -327,42 +320,23 @@ export class AssetService {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async serveFile(
 | 
					  public async serveFile(authUser: AuthUserDto, assetId: string, query: ServeFileDto, res: Res) {
 | 
				
			||||||
    authUser: AuthUserDto,
 | 
					 | 
				
			||||||
    assetId: string,
 | 
					 | 
				
			||||||
    query: ServeFileDto,
 | 
					 | 
				
			||||||
    res: Res,
 | 
					 | 
				
			||||||
    headers: Record<string, string>,
 | 
					 | 
				
			||||||
  ) {
 | 
					 | 
				
			||||||
    // this is not quite right as sometimes this returns the original still
 | 
					    // this is not quite right as sometimes this returns the original still
 | 
				
			||||||
    await this.access.requirePermission(authUser, Permission.ASSET_VIEW, assetId);
 | 
					    await this.access.requirePermission(authUser, Permission.ASSET_VIEW, assetId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const allowOriginalFile = !!(!authUser.isPublicUser || authUser.isAllowDownload);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const asset = await this._assetRepository.getById(assetId);
 | 
					    const asset = await this._assetRepository.getById(assetId);
 | 
				
			||||||
    if (!asset) {
 | 
					    if (!asset) {
 | 
				
			||||||
      throw new NotFoundException('Asset does not exist');
 | 
					      throw new NotFoundException('Asset does not exist');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Handle Sending Images
 | 
					    const allowOriginalFile = !!(!authUser.isPublicUser || authUser.isAllowDownload);
 | 
				
			||||||
    if (asset.type == AssetType.IMAGE) {
 | 
					
 | 
				
			||||||
      try {
 | 
					    const filepath =
 | 
				
			||||||
        return this.streamFile(this.getServePath(asset, query, allowOriginalFile), res, headers);
 | 
					      asset.type === AssetType.IMAGE
 | 
				
			||||||
      } catch (e) {
 | 
					        ? this.getServePath(asset, query, allowOriginalFile)
 | 
				
			||||||
        this.logger.error(`Cannot create read stream for asset ${asset.id} ${JSON.stringify(e)}`, 'serveFile[IMAGE]');
 | 
					        : asset.encodedVideoPath || asset.originalPath;
 | 
				
			||||||
        throw new InternalServerErrorException(
 | 
					
 | 
				
			||||||
          e,
 | 
					    await this.sendFile(res, filepath);
 | 
				
			||||||
          `Cannot read thumbnail file for asset ${asset.id} - contact your administrator`,
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      try {
 | 
					 | 
				
			||||||
        return this.streamFile(asset.encodedVideoPath || asset.originalPath, res, headers);
 | 
					 | 
				
			||||||
      } catch (e: Error | any) {
 | 
					 | 
				
			||||||
        this.logger.error(`Error serving VIDEO asset=${asset.id}`, e?.stack);
 | 
					 | 
				
			||||||
        throw new InternalServerErrorException(`Failed to serve video asset ${e}`, 'ServeFile');
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async deleteAll(authUser: AuthUserDto, dto: DeleteAssetDto): Promise<DeleteAssetResponseDto[]> {
 | 
					  public async deleteAll(authUser: AuthUserDto, dto: DeleteAssetDto): Promise<DeleteAssetResponseDto[]> {
 | 
				
			||||||
@ -624,64 +598,18 @@ export class AssetService {
 | 
				
			|||||||
    return asset.resizePath;
 | 
					    return asset.resizePath;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private async streamFile(filepath: string, res: Res, headers: Record<string, string>) {
 | 
					  private async sendFile(res: Res, filepath: string): Promise<void> {
 | 
				
			||||||
    await fs.access(filepath, constants.R_OK);
 | 
					    await fs.access(filepath, constants.R_OK);
 | 
				
			||||||
    const { size, mtimeNs } = await fs.stat(filepath, { bigint: true });
 | 
					    res.set('Cache-Control', 'private, max-age=86400, no-transform');
 | 
				
			||||||
 | 
					 | 
				
			||||||
    res.header('Content-Type', mimeTypes.lookup(filepath));
 | 
					    res.header('Content-Type', mimeTypes.lookup(filepath));
 | 
				
			||||||
 | 
					    res.sendFile(filepath, { root: process.cwd() }, (error: Error) => {
 | 
				
			||||||
    const range = this.setResRange(res, headers, Number(size));
 | 
					      if (!error) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // etag
 | 
					 | 
				
			||||||
    const etag = `W/"${size}-${mtimeNs}"`;
 | 
					 | 
				
			||||||
    res.setHeader('ETag', etag);
 | 
					 | 
				
			||||||
    if (etag === headers['if-none-match']) {
 | 
					 | 
				
			||||||
      res.status(304);
 | 
					 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const stream = createReadStream(filepath, range);
 | 
					      if (error.message !== 'Request aborted') {
 | 
				
			||||||
    return await pipeline(stream, res).catch((err) => {
 | 
					        this.logger.error(`Unable to send file: ${error.name}`, error.stack);
 | 
				
			||||||
      if (err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
 | 
					 | 
				
			||||||
        this.logger.error(err);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  private setResRange(res: Res, headers: Record<string, string>, size: number) {
 | 
					 | 
				
			||||||
    if (!headers.range) {
 | 
					 | 
				
			||||||
      return {};
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /** Extracting Start and End value from Range Header */
 | 
					 | 
				
			||||||
    const [startStr, endStr] = headers.range.replace(/bytes=/, '').split('-');
 | 
					 | 
				
			||||||
    let start = parseInt(startStr, 10);
 | 
					 | 
				
			||||||
    let end = endStr ? parseInt(endStr, 10) : size - 1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!isNaN(start) && isNaN(end)) {
 | 
					 | 
				
			||||||
      start = start;
 | 
					 | 
				
			||||||
      end = size - 1;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (isNaN(start) && !isNaN(end)) {
 | 
					 | 
				
			||||||
      start = size - end;
 | 
					 | 
				
			||||||
      end = size - 1;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Handle unavailable range request
 | 
					 | 
				
			||||||
    if (start >= size || end >= size) {
 | 
					 | 
				
			||||||
      console.error('Bad Request');
 | 
					 | 
				
			||||||
      res.status(416).set({ 'Content-Range': `bytes */${size}` });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      throw new BadRequestException('Bad Request Range');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    res.status(206).set({
 | 
					 | 
				
			||||||
      'Content-Range': `bytes ${start}-${end}/${size}`,
 | 
					 | 
				
			||||||
      'Accept-Ranges': 'bytes',
 | 
					 | 
				
			||||||
      'Content-Length': end - start + 1,
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return { start, end };
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user