import { Service } from 'typedi' ; import * as path from 'path' ; import * as fs from 'fs' ; @ Service ( ) export class ImageService { public async listFolders ( limit = 0 , folderName = '' ) { const directoryPath = path . join ( process . cwd ( ) , 'uploads' + '/' + folderName ) ; console . log ( directoryPath ) ; console . log ( __dirname ) ; console . log ( process . cwd ( ) ) ; const files = await this . readDir ( directoryPath ) ; const contents = [ ] ; const commonPrefix = [ ] ; for ( const file of files ) { const pathfile = path . resolve ( directoryPath , file ) ; const isDir = await this . isDirCheck ( pathfile ) ; if ( isDir ) { commonPrefix . push ( { Prefix : folderName ? folderName + file + '/' : file + '/' , } ) ; } else { contents . push ( { Key : folderName ? folderName + file : file , } ) ; } } return new Promise ( ( resolve , reject ) => { const outputResponse = { } ; outputResponse . Name = 'uploads' ; outputResponse . Prefix = folderName ; outputResponse . Delimiter = 100 ; outputResponse . IsTruncated = 'uploads' ; outputResponse . Marker = '' ; outputResponse . Contents = contents ; outputResponse . CommonPrefixes = commonPrefix ; resolve ( outputResponse ) ; } ) ; } public createFolder ( folderName = '' ) { const directoryPath = path . join ( process . cwd ( ) , 'uploads' + '/' + folderName ) ; return new Promise ( ( resolve , reject ) => { if ( fs . existsSync ( directoryPath ) ) { resolve ( { ETAG : new Date ( ) } ) ; } fs . mkdir ( directoryPath , { recursive : true } , ( err ) => { if ( err ) { reject ( err ) ; } resolve ( { ETAG : new Date ( ) } ) ; } ) ; } ) ; } public imageUpload ( folderName = '' , base64Image ) { const directoryPath = path . join ( process . cwd ( ) , 'uploads' + '/' + folderName ) ; return new Promise ( ( resolve , reject ) => { fs . writeFile ( directoryPath , base64Image , 'base64' , ( err ) => { if ( err ) { reject ( err ) ; } const locationArray = directoryPath . split ( '/' ) ; locationArray . pop ( ) ; const locationPath = locationArray . join ( '/' ) ; resolve ( { success : true , path : locationPath } ) ; } ) ; } ) ; } public async resizeImage ( imgName = '' , imgPath = '' , widthString = '' , heightString = '' ) { const directoryPath = path . join ( process . cwd ( ) , 'uploads' + '/' + imgPath + imgName ) ; console . log ( directoryPath ) ; return new Promise ( ( resolve , reject ) => { const gm = require ( 'gm' ) . subClass ( { imageMagick : true } ) ; return gm ( directoryPath ) . resize ( widthString , heightString ) . toBuffer ( ( error , buffer ) => { if ( error ) { reject ( error ) ; } else { console . log ( 'Buffer' + Buffer . isBuffer ( buffer ) ) ; resolve ( buffer ) ; } } ) ; } ) ; } public async isDirCheck ( pathfile ) { return new Promise < boolean > ( ( subresolve , subreject ) => { fs . stat ( pathfile , ( error , stat ) => { console . log ( stat ) ; console . log ( stat . isDirectory ( ) ) ; if ( stat && stat . isDirectory ( ) ) { subresolve ( true ) ; } else { subresolve ( false ) ; } } ) ; } ) ; } public async readDir ( pathfile ) { return new Promise < any > ( ( subresolve , subreject ) => { fs . readdir ( pathfile , ( error , files ) => { if ( error ) { subreject ( error ) ; } subresolve ( files ) ; } ) ; } ) ; } public deleteFile ( fileName = '' ) { const directoryPath = path . join ( process . cwd ( ) , 'uploads' + '/' + fileName ) ; return new Promise ( ( resolve , reject ) => { fs . unlink ( directoryPath , ( err ) => { if ( err ) { reject ( err ) ; } resolve ( { success : true , message : 'successfully deleted file' } ) ; } ) ; } ) ; } public async getFolder ( folderName = '' ) { return new Promise ( async ( resolve , reject ) => { const pathName = path . join ( process . cwd ( ) , 'uploads' ) ; const files = await this . readDir ( pathName ) ; const contents = [ ] ; const commonPrefix = [ ] ; if ( folderName !== '' ) { files . forEach ( async ( file ) => { if ( file . includes ( folderName ) === true ) { commonPrefix . push ( { Prefix : file + '/' } ) ; } } ) ; } else { console . log ( files ) ; for ( const file of files ) { const pathfile = path . resolve ( path . join ( process . cwd ( ) , 'uploads' + '/' + file ) ) ; const isDir = await this . isDirCheck ( pathfile ) ; if ( isDir ) { commonPrefix . push ( { Prefix : file + '/' , } ) ; } else { contents . push ( { Key : file , } ) ; } } } const outputResponse = { } ; outputResponse . IsTruncated = false ; outputResponse . Name = 'uploads' ; outputResponse . Content = contents ; outputResponse . Prefix = folderName ; outputResponse . CommonPrefixes = commonPrefix ; resolve ( outputResponse ) ; } ) ; } }