import * as AWS from 'aws-sdk' ; import { Service } from 'typedi' ; import { aws_setup } from '../../env' ; const s3 = new AWS . S3 ( ) ; @ Service ( ) export class S3Service { public listBucker ( limit = 0 , folderName = '' ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const bucketParams = { Bucket : aws_setup . AWS_BUCKET , MaxKeys : limit , Delimiter : '/' , Prefix : folderName , } ; return new Promise ( ( resolve , reject ) => { return s3 . listObjects ( bucketParams , ( err , data ) => { if ( err ) { reject ( err ) ; } console . log ( data ) ; resolve ( data ) ; } ) ; } ) ; } public createFolder ( folderName = '' ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const bucketParams = { Bucket : aws_setup . AWS_BUCKET , Key : folderName , } ; return new Promise ( ( resolve , reject ) => { return s3 . putObject ( bucketParams , ( err , data ) => { if ( err ) { reject ( err ) ; } resolve ( data ) ; } ) ; } ) ; } public deleteFolder ( folderName = '' ) { console . log ( folderName ) ; AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const bucketParams = { Bucket : aws_setup . AWS_BUCKET , Prefix : folderName , } ; return new Promise ( ( resolve , reject ) => { s3 . listObjects ( bucketParams , ( err , data ) => { if ( err ) { reject ( err ) ; } console . log ( data ) ; const objects = data . Contents . map ( object => ( { Key : object . Key } ) ) ; return s3 . deleteObjects ( { Bucket : aws_setup . AWS_BUCKET , Delete : { Objects : objects , Quiet : true , } , } , ( error , val ) => { if ( error ) { reject ( error ) ; } resolve ( val ) ; } ) ; } ) ; } ) ; } public deleteFile ( fileName = '' ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const bucketParams = { Bucket : aws_setup . AWS_BUCKET , Key : fileName , } ; return new Promise ( ( resolve , reject ) => { return s3 . deleteObject ( bucketParams , ( err , data ) => { if ( err ) { reject ( err ) ; } resolve ( data ) ; console . log ( data ) ; } ) ; } ) ; } public resizeImage ( imgName = '' , imgPath = '' , widthString = '' , heightString = '' ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const getParams = { Bucket : aws_setup . AWS_BUCKET , Key : imgPath + imgName , } ; console . log ( getParams ) ; return new Promise ( ( resolve , reject ) => { s3 . getObject ( getParams , ( err , data ) => { if ( err ) { return reject ( err ) ; } console . log ( data ) ; if ( data ) { const gm = require ( 'gm' ) . subClass ( { imageMagick : true } ) ; return gm ( data . Body ) . resize ( widthString , heightString ) . toBuffer ( ( error , buffer ) => { if ( error ) { throw error ; } else { console . log ( 'Buffer' + Buffer . isBuffer ( buffer ) ) ; return resolve ( buffer ) ; } } ) ; } else { return resolve ( false ) ; } } ) ; } ) ; } public imageUpload ( folderName = '' , base64Image , imageType ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const params = { Bucket : aws_setup . AWS_BUCKET , Key : folderName , Body : base64Image , ACL : 'public-read' , ContentEncoding : 'base64' , ContentType : `image/${ imageType }` , } ; return new Promise ( ( resolve , reject ) => { return s3 . upload ( params , ( err , data ) => { if ( err ) { return reject ( err ) ; } const locationArray = data . Location . split ( '/' ) ; locationArray . pop ( ) ; const locationPath = locationArray . join ( '/' ) ; return resolve ( { path : locationPath } ) ; } ) ; } ) ; } public getFolder ( folderName = '' ) { AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const bucketParams = { Bucket : aws_setup . AWS_BUCKET , Prefix : folderName , Delimiter : '/' , } ; return new Promise ( ( resolve , reject ) => { return s3 . listObjects ( bucketParams , ( err , data ) => { if ( err ) { reject ( err ) ; } resolve ( data ) ; console . log ( data ) ; } ) ; } ) ; } }