import uniqueId from "uniqid" ; import sanitizeFilename from "sanitize-filename" ; import S3 from "aws-sdk/clients/s3" ; import { validation } from "@webiny/validation" ; const S3_BUCKET = process . env . S3_BUCKET ; const UPLOAD_MAX_FILE_SIZE_DEFAULT = 26214400 ; const sanitizeFileSizeValue = ( value , defaultValue ) => { try { validation . validateSync ( value , "required,numeric,gte:0" ) ; return value ; } catch ( e ) { return defaultValue ; } } ; export default async ( data , settings ) => { if ( ! data . type ) { data . type = "application/octet-stream" ; } const contentType = data . type ; if ( ! contentType ) { throw Error ( `File's content type could not be resolved.` ) ; } let key = sanitizeFilename ( data . name ) ; if ( key ) { key = uniqueId ( ) + "-" + key ; } if ( data . keyPrefix ) { key = `${ sanitizeFilename ( data . keyPrefix ) }-${ key }` ; } key = key . replace ( /\s/g , "" ) ; const uploadMinFileSize = sanitizeFileSizeValue ( settings . uploadMinFileSize , 0 ) ; const uploadMaxFileSize = sanitizeFileSizeValue ( settings . uploadMaxFileSize , UPLOAD_MAX_FILE_SIZE_DEFAULT ) ; const params = { Expires : 60 , Bucket : S3_BUCKET , Conditions : [ [ "content-length-range" , uploadMinFileSize , uploadMaxFileSize ] ] , Fields : { "Content-Type" : contentType , key } } ; if ( params . Fields . key . startsWith ( "/" ) ) { params . Fields . key = params . Fields . key . substr ( 1 ) ; } const s3 = new S3 ( ) ; const payload = s3 . createPresignedPost ( params ) ; return { data : payload , file : { name : key , key , type : contentType , size : data . size } } ; } ;