import { Injectable , Inject } from "@nestjs/common" ; import * as uuid from "uuid" ; import { S3 } from "aws-sdk" ; import { FileType } from "../../db" ; import { PermissionContext } from "../../auth" ; import { ConfigDiToken , Config } from "../../config" ; /**
 *  Incapsulates logic of AWS S3 SDK configuration building.
 *  Refer to [AWS S3 SDK docs](@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html)
 */ @ Injectable ( ) export class S3FileStorageConfigurationFactory { public constructor ( @ Inject ( ConfigDiToken . CONFIG ) private readonly config ) { } public getDeleteConfiguration ( key ) { return { Bucket : this . config . storage . bucket , Key : key , } ; } public getMultipleDeleteConfiguration ( keys ) { return { Bucket : this . config . storage . bucket , Delete : { Objects : keys . map ( ( key ) => { return { Key : key } ; } ) , } , } ; } public getUploadConfiguration ( key , body , originalName ) { return { Bucket : this . config . storage . bucket , Key : key , Body : body , ContentDisposition : this . getContentDisposition ( originalName ) , } ; } public getDownloadConfiguration ( key ) { return { Bucket : this . config . storage . bucket , Key : key , } as S3 . Types . GetObjectRequest ; } /**
     *  Returns new storage filename to prevent collisions
     */ public getFilename ( originalFilename ) { return `${ uuid . v4 ( ) }_${ originalFilename }` ; } returns { String } public getStoragePath ( file ) { return `${ file . type }/` ; } param { String } path param { String } originalFilename returns { String } description public generateKey ( path , originalFilename ) { return `${ this . config . storage . prefix }${ path }${ this . getFilename ( originalFilename ) }` ; } param { String } originalFilename returns { String } description public getContentDisposition ( originalFilename ) { return `attachment; filename*=UTF-8''${ encodeURIComponent ( originalFilename ) }` ; } }