import * as AWS from 'aws-sdk' ; import { Authorized , Body , Get , JsonController , Post , QueryParam , Req , Res } from 'routing-controllers' ; import { FolderNameRequest } from './requests/CreateFolderNameRequest' ; import { FileNameRequest } from './requests/CreateFileNameRequest' ; import { S3Service } from '../services/S3Service' ; import { ImageService } from '../services/ImageService' ; import { aws_setup , env } from '../../env' ; AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY , region : aws_setup . AWS_DEFAULT_REGION , } ) ; @ JsonController ( '/media' ) export class MediaController { constructor ( private s3Service , private imageService ) { } api apiGroup apiHeader apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/bucket-object-list' ) @ Authorized ( ) public async ObjectList ( @ QueryParam ( 'folderName' ) folderName , @ QueryParam ( 'limit' ) limit , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'working' ) ; console . log ( 'S3 folderName:- ' + folderName ) ; console . log ( 'S3 Limit:- ' + limit ) ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . listBucker ( limit , folderName ) ; } else { val = await this . imageService . listFolders ( limit , folderName ) ; } console . log ( val ) ; if ( val ) { const successResponse = { status : 1 , message : 'Successfully get bucket object list' , data : val , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/create-folder' ) @ Authorized ( ) public async CreateFolder ( @ Body ( { validate : true } ) folderNameValidation , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'working' ) ; console . log ( 'S3 folderName:- ' + folderNameValidation . folderName ) ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . createFolder ( folderNameValidation . folderName ) ; } else { val = await this . imageService . createFolder ( folderNameValidation . folderName ) ; } if ( val ) { const successResponse = { status : 1 , message : 'Successfully created folder' , data : val , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-folder' ) @ Authorized ( ) public async DeleteFolder ( @ Body ( { validate : true } ) folderNameValidation , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'working' ) ; console . log ( 'S3 folderName:- ' + folderNameValidation . folderName ) ; const val = await this . s3Service . deleteFolder ( folderNameValidation . folderName ) ; console . log ( val ) ; if ( val ) { const successResponse = { status : 1 , message : 'Successfully deleted folder' , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/delete-file' ) @ Authorized ( ) public async DeleteFile ( @ QueryParam ( 'fileName' ) fileName , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'working' ) ; console . log ( 'S3 fileName:- ' + fileName ) ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . deleteFile ( fileName ) ; } else { val = await this . imageService . deleteFile ( fileName ) ; } console . log ( val ) ; if ( val ) { const successResponse = { status : 1 , message : 'Successfully deleted file' , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/upload-file' ) @ Authorized ( ) public async uploadFile ( @ Body ( { validate : true } ) fileNameRequest , @ Req ( ) request , @ Res ( ) response ) { const base64 = fileNameRequest . image ; const path = fileNameRequest . path ; AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY } ) ; const base64Data = new Buffer ( base64 . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; const type = base64 . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . imageUpload ( ( path === '' ? name : path + name ) , base64Data , type ) ; } else { val = await this . imageService . imageUpload ( ( path === '' ? name : path + name ) , base64Data ) ; } console . log ( val ) ; const successResponse = { status : 1 , message : 'Image successfully uploaded' , data : { image : name , path : val . path , } , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/image-resize' ) public async image_resize ( @ QueryParam ( 'width' ) width , @ QueryParam ( 'height' ) height , @ QueryParam ( 'name' ) name , @ QueryParam ( 'path' ) path , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'Dim' + width + height ) ; const widthString = width ; const heightString = height ; const imgPath = path ; const imgName = name ; console . log ( 'Dim' + width + height ) ; console . log ( 'Path' + imgPath ) ; console . log ( 'filename' + imgName ) ; const ext = imgName . split ( '.' ) ; console . log ( 'ext ' + ext [ 1 ] ) ; if ( ext [ 1 ] === 'jpg' || ext [ 1 ] === 'jpeg' || ext [ 1 ] === 'png' ) { let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . resizeImage ( imgName , imgPath , widthString , heightString ) ; } else { val = await this . imageService . resizeImage ( imgName , imgPath , widthString , heightString ) ; } if ( val ) { return new Promise ( ( ) => { response . writeHead ( 200 , { 'Content-Type' : 'image/jpeg' } ) ; response . write ( val , 'binary' ) ; response . end ( undefined , 'binary' ) ; } ) ; } else { return response . status ( 400 ) . send ( { status : 0 , message : 'Only allow jpg/jpeg/png format image!' } ) ; } } else { return response . status ( 400 ) . send ( { status : 0 , message : 'Only allow jpg/jpeg/png format image!' } ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/search-folder' ) @ Authorized ( ) public async getFolder ( @ QueryParam ( 'folderName' ) folderName , @ Req ( ) request , @ Res ( ) response ) { console . log ( 'working' ) ; console . log ( 'S3 folderName:- ' + folderName ) ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . getFolder ( folderName ) ; } else { val = await this . imageService . getFolder ( folderName ) ; } console . log ( val ) ; if ( val ) { const successResponse = { status : 1 , message : 'Successfully got folder details' , data : val , } ; return response . status ( 200 ) . send ( successResponse ) ; } } }