import * as fs from 'fs-extra' ; import { Readable } from 'stream' ; import { Injectable } from '@nestjs/common' ; import { concatMap , from , Observable } from 'rxjs' ; import { drive_v3 } from 'googleapis' ; import { GoogleDriveFile , Image , ImageDimensionType , IMAGE_FILE_EXTENSION , IMAGE_MIME_TYPE , } from '@dark-rush-photography/shared/types' ; import { downloadGoogleDriveImageFile , getAzureStorageBlobPath , getAzureStorageBlobPathWithImageDimension , uploadAzureStorageStreamToBlob$ , } from '@dark-rush-photography/api/util' ; import { ConfigProvider } from './config.provider' ; @ Injectable ( ) export class ImageAddBlobProvider { constructor ( private readonly configProvider ) { } addNewImageBlob$ ( googleDrive , imageFile , image ) { return from ( downloadGoogleDriveImageFile ( googleDrive , imageFile . id ) ) . pipe ( concatMap ( ( filePath ) => uploadAzureStorageStreamToBlob$ ( fs . createReadStream ( filePath ) , IMAGE_MIME_TYPE , getAzureStorageBlobPath ( image . storageId , image . slug , IMAGE_FILE_EXTENSION ) , this . configProvider . azureStorageConnectionStringPublic , this . configProvider . azureStorageBlobContainerNamePublic ) ) ) ; } addImagePostImageBlob$ ( image , file ) { return uploadAzureStorageStreamToBlob$ ( Readable . from ( file . buffer ) , IMAGE_MIME_TYPE , getAzureStorageBlobPath ( image . storageId , image . slug , IMAGE_FILE_EXTENSION ) , this . configProvider . azureStorageConnectionStringPublic , this . configProvider . azureStorageBlobContainerNamePublic ) ; } addImageDimensionBlob$ ( storageId , slug , fileExtension , imageDimensionType , imageFilePath ) { return uploadAzureStorageStreamToBlob$ ( fs . createReadStream ( imageFilePath ) , IMAGE_MIME_TYPE , getAzureStorageBlobPathWithImageDimension ( storageId , slug , fileExtension , imageDimensionType ) , this . configProvider . azureStorageConnectionStringPublic , this . configProvider . azureStorageBlobContainerNamePublic ) ; } }