@@ -2602,18 +2602,24 @@ private String getScriptLocation(UploadEntity.ResourceType resourceType) {
26022602 return Script .findScript (scriptsDir , scriptname );
26032603 }
26042604
2605- public UploadEntity createUploadEntity (String uuid , String metadata ) {
2605+ public UploadEntity createUploadEntity (String uuid , String metadata , long contentLength ) {
26062606 TemplateOrVolumePostUploadCommand cmd = getTemplateOrVolumePostUploadCmd (metadata );
26072607 UploadEntity uploadEntity = null ;
26082608 if (cmd == null ){
26092609 updateStateMapWithError (uuid ,"unable decode and deserialize metadata." );
26102610 throw new InvalidParameterValueException ("unable to decode and deserialize metadata" );
26112611 } else {
26122612 uuid = cmd .getEntityUUID ();
2613- if (uploadEntityStateMap .containsKey (uuid )) {
2613+ if (uploadEntityStateMap .containsKey (uuid )) {
26142614 uploadEntity = uploadEntityStateMap .get (uuid );
26152615 throw new InvalidParameterValueException ("The one time post url is already used and the upload is in " + uploadEntity .getUploadState () + " state." );
26162616 }
2617+ int maxSizeInGB = Integer .valueOf (cmd .getMaxUploadSize ());
2618+ int contentLengthInGB = getSizeInGB (contentLength );
2619+ if (contentLengthInGB > maxSizeInGB ) {
2620+ throw new InvalidParameterValueException ("Maximum file upload size exceeded. Content Length received: " + contentLengthInGB + "GB. Maximum allowed size: " +
2621+ maxSizeInGB + "GB." );
2622+ }
26172623 try {
26182624 String absolutePath = cmd .getAbsolutePath ();
26192625 uploadEntity = new UploadEntity (uuid , cmd .getEntityId (), UploadEntity .Status .IN_PROGRESS , cmd .getName (), absolutePath );
@@ -2627,20 +2633,24 @@ public UploadEntity createUploadEntity(String uuid, String metadata) {
26272633 uploadEntity .setInstallPathPrefix (installPathPrefix );
26282634 uploadEntity .setHvm (cmd .getRequiresHvm ());
26292635 uploadEntity .setChksum (cmd .getChecksum ());
2636+ uploadEntity .setMaxSizeInGB (maxSizeInGB );
26302637 // create a install dir
26312638 if (!_storage .exists (installPathPrefix )) {
26322639 _storage .mkdir (installPathPrefix );
26332640 }
26342641 uploadEntityStateMap .put (uuid , uploadEntity );
26352642 } catch (Exception e ) {
26362643 //upload entity will be null incase an exception occurs and the handler will not proceed.
2637- s_logger .debug ("exception occured while creating upload entity " + e );
2644+ s_logger .error ("exception occurred while creating upload entity " , e );
26382645 updateStateMapWithError (uuid , e .getMessage ());
26392646 }
26402647 }
26412648 return uploadEntity ;
26422649 }
26432650
2651+ private int getSizeInGB (long sizeInBytes ) {
2652+ return (int )Math .ceil (sizeInBytes * 1.0d / (1024 * 1024 * 1024 ));
2653+ }
26442654
26452655 public String postUpload (String uuid , String filename ) {
26462656 UploadEntity uploadEntity = uploadEntityStateMap .get (uuid );
@@ -2657,7 +2667,11 @@ public String postUpload(String uuid, String filename) {
26572667 //}
26582668 //dnld.setCheckSum(checkSum);
26592669
2660- int imgSizeGigs = (int )Math .ceil (_storage .getSize (fileSavedTempLocation ) * 1.0d / (1024 * 1024 * 1024 ));
2670+ int imgSizeGigs = getSizeInGB (_storage .getSize (fileSavedTempLocation ));
2671+ int maxSize = uploadEntity .getMaxSizeInGB ();
2672+ if (imgSizeGigs > maxSize ) {
2673+ throw new InvalidParameterValueException ("Maximum file upload size exceeded. Physical file size: " +imgSizeGigs +"GB. Maximum allowed size: " +maxSize +"GB." );
2674+ }
26612675 imgSizeGigs ++; // add one just in case
26622676 long timeout = (long )imgSizeGigs * installTimeoutPerGig ;
26632677 Script scr = new Script (getScriptLocation (resourceType ), timeout , s_logger );
@@ -2771,13 +2785,18 @@ private void updateStateMapWithError(String uuid,String errorMessage) {
27712785 uploadEntityStateMap .put (uuid , uploadEntity );
27722786 }
27732787
2774- public void validatePostUploadRequest (String signature , String metadata , String timeout , String hostname , String uuid ) throws InvalidParameterValueException {
2788+ public void validatePostUploadRequest (String signature , String metadata , String timeout , String hostname ,long contentLength , String uuid ) throws InvalidParameterValueException {
27752789 // check none of the params are empty
27762790 if (StringUtils .isEmpty (signature ) || StringUtils .isEmpty (metadata ) || StringUtils .isEmpty (timeout )) {
27772791 updateStateMapWithError (uuid ,"signature, metadata and expires are compulsory fields." );
27782792 throw new InvalidParameterValueException ("signature, metadata and expires are compulsory fields." );
27792793 }
27802794
2795+ //check that contentLength exists and is greater than zero
2796+ if (contentLength <= 0 ) {
2797+ throw new InvalidParameterValueException ("content length is not set in the request or has invalid value." );
2798+ }
2799+
27812800 //validate signature
27822801 String fullUrl = "https://" + hostname + "/upload/" + uuid ;
27832802 String computedSignature = EncryptionUtil .generateSignature (metadata + fullUrl + timeout , getPostUploadPSK ());
0 commit comments