From 958fc5185edd946292137c18b9433643efca3d29 Mon Sep 17 00:00:00 2001 From: bradley griffiths Date: Thu, 31 Jan 2019 13:06:03 +0000 Subject: [PATCH 1/2] Ensure optional args are optional. --- .gitignore | 1 + s3direct/static/s3direct/dist/index.js | 2 +- src/index.js | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0bf773c7..1efb4c8f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ npm-debug.log .tox/ .env .cache +settings.json \ No newline at end of file diff --git a/s3direct/static/s3direct/dist/index.js b/s3direct/static/s3direct/dist/index.js index 9b91c743..3d785ebe 100644 --- a/s3direct/static/s3direct/dist/index.js +++ b/s3direct/static/s3direct/dist/index.js @@ -49,5 +49,5 @@ var t;!function(r){if("object"==typeof exports)module.exports=r();else if("funct },{"./../img/glyphicons-halflings.png":[["glyphicons-halflings.21499243.png","cxU3"],"cxU3"],"./../img/glyphicons-halflings-white.png":[["glyphicons-halflings-white.da002663.png","QCsr"],"QCsr"]}],"/krr":[function(require,module,exports) { },{}],"Focm":[function(require,module,exports) { -"use strict";var e=o(require("js-cookie")),t=o(require("sha.js")),n=o(require("evaporate")),r=o(require("spark-md5"));function o(e){return e&&e.__esModule?e:{default:e}}require("./css/bootstrap.css"),require("./css/styles.css");var a=function(e,t,n,r,o,a){var i=new XMLHttpRequest;i.open(e,t,!0),Object.keys(r).forEach(function(e){i.setRequestHeader(e,r[e])}),i.onload=function(){a(i.status,i.responseText)},i.onerror=i.onabort=function(){d(!1),s(o,"Sorry, failed to upload file.")},i.send(n)},i=function(e){return decodeURIComponent((e+"").replace(/\+/g,"%20"))},c=function(e){var t;try{t=JSON.parse(e)}catch(n){t=null}return t},u=function(e,t){e.querySelector(".bar").style.width=Math.round(100*t)+"%"},s=function(e,t){e.className="s3direct form-active",e.querySelector(".file-input").value="",alert(t)},l=0,d=function(e){var t=document.querySelector(".submit-row");if(t){var n=t.querySelectorAll("input[type=submit],button[type=submit]");!0===e?l++:l--,[].forEach.call(n,function(e){e.disabled=0!==l})}},f=function(e){d(!0),e.className="s3direct progress-active"},p=function(e,t,n,r){var o=e.querySelector(".file-link"),a=e.querySelector(".file-url");a.value=t+"/"+n+"/"+r,o.setAttribute("href",a.value),o.innerHTML=i(a.value).split("/").pop(),e.className="s3direct link-active",e.querySelector(".bar").style.width="0%",d(!1)},y=function(e){return btoa(r.default.ArrayBuffer.hash(e,!0))},m=function(e){return(0,t.default)("sha256").update(e,"utf-8").digest("hex")},v=function(t){var n=t.querySelector(".csrf-cookie-name"),r=document.querySelector("input[name=csrfmiddlewaretoken]");return r?r.value:e.default.get(n.value)},S=function(e,t,n){var r={};return e&&(r["x-amz-acl"]=e),n&&(r["x-amz-security-token"]=n),t&&(r["x-amz-server-side-encryption"]=t),r},h=function(e){var t={};return e&&(t["x-amz-security-token"]=e),t},q=function(e,t,n){return function(r,o,i,u,s){return new Promise(function(r,o){var s=new FormData,l={"X-CSRFToken":v(e)};s.append("to_sign",i),s.append("datetime",u),s.append("dest",n),a("POST",t,s,l,e,function(e,t){var n=c(t);switch(console.log(n),e){case 200:r(n.s3ObjKey);break;case 403:default:o(n.error)}})})}},g=function(e,t,r,o,a){var i={customAuthMethod:q(e,t,a),aws_key:r.access_key_id,bucket:r.bucket,aws_url:r.endpoint,awsRegion:r.region,computeContentMd5:!0,cryptoMd5Method:y,cryptoHexEncodedHash256:m,partSize:20971520,logging:!0,allowS3ExistenceOptimization:!0,s3FileCacheHoursAgo:12},c={name:r.object_key,file:o,contentType:o.type,xAmzHeadersCommon:h(r.session_token),xAmzHeadersAtInitiate:S(r.acl,r.server_side_encryption,r.session_token),notSignedHeadersAtInitiate:{"Cache-Control":r.cache_control,"Content-Disposition":r.content_disposition},progress:function(t,n){u(e,t)},warn:function(t,n,r){r.includes("InvalidAccessKeyId")&&s(e,r)}};n.default.create(i).then(function(t){f(e),t.add(c).then(function(t){p(e,r.endpoint,r.bucket,t)},function(t){return s(e,t)})})},k=function(e){var t=e.target.parentElement,n=t.querySelector(".file-input").files[0],r=t.querySelector(".file-dest").value,o=t.getAttribute("data-policy-url"),i=t.getAttribute("data-signing-url"),u=new FormData,l={"X-CSRFToken":v(t)};u.append("dest",r),u.append("name",n.name),u.append("type",n.type),u.append("size",n.size),a("POST",o,u,l,t,function(e,o){var a=c(o);switch(e){case 200:g(t,i,a,n,r);break;case 400:case 403:case 500:s(t,a.error);break;default:s(t,"Sorry, could not get upload URL.")}})},b=function(e){e.preventDefault();var t=e.target.parentElement;t.querySelector(".file-url").value="",t.querySelector(".file-input").value="",t.className="s3direct form-active"},w=function(e){var t=e.querySelector(".file-url"),n=e.querySelector(".file-input"),r=e.querySelector(".file-remove"),o=""===t.value?"form":"link";e.className="s3direct "+o+"-active",r.addEventListener("click",b,!1),n.addEventListener("change",k,!1)};document.addEventListener("DOMContentLoaded",function(e){[].forEach.call(document.querySelectorAll(".s3direct"),w)}),document.addEventListener("DOMNodeInserted",function(e){if(e.target.tagName){var t=e.target.querySelectorAll(".s3direct");[].forEach.call(t,function(e,t,n){w(e)})}}); +"use strict";var e=o(require("js-cookie")),t=o(require("sha.js")),n=o(require("evaporate")),r=o(require("spark-md5"));function o(e){return e&&e.__esModule?e:{default:e}}require("./css/bootstrap.css"),require("./css/styles.css");var a=function(e,t,n,r,o,a){var i=new XMLHttpRequest;i.open(e,t,!0),Object.keys(r).forEach(function(e){i.setRequestHeader(e,r[e])}),i.onload=function(){a(i.status,i.responseText)},i.onerror=i.onabort=function(){d(!1),s(o,"Sorry, failed to upload file.")},i.send(n)},i=function(e){return decodeURIComponent((e+"").replace(/\+/g,"%20"))},c=function(e){var t;try{t=JSON.parse(e)}catch(n){t=null}return t},u=function(e,t){e.querySelector(".bar").style.width=Math.round(100*t)+"%"},s=function(e,t){e.className="s3direct form-active",e.querySelector(".file-input").value="",alert(t)},l=0,d=function(e){var t=document.querySelector(".submit-row");if(t){var n=t.querySelectorAll("input[type=submit],button[type=submit]");!0===e?l++:l--,[].forEach.call(n,function(e){e.disabled=0!==l})}},f=function(e){d(!0),e.className="s3direct progress-active"},p=function(e,t,n,r){var o=e.querySelector(".file-link"),a=e.querySelector(".file-url");a.value=t+"/"+n+"/"+r,o.setAttribute("href",a.value),o.innerHTML=i(a.value).split("/").pop(),e.className="s3direct link-active",e.querySelector(".bar").style.width="0%",d(!1)},y=function(e){return btoa(r.default.ArrayBuffer.hash(e,!0))},m=function(e){return(0,t.default)("sha256").update(e,"utf-8").digest("hex")},v=function(t){var n=t.querySelector(".csrf-cookie-name"),r=document.querySelector("input[name=csrfmiddlewaretoken]");return r?r.value:e.default.get(n.value)},S=function(e,t,n){var r={};return e&&(r["x-amz-acl"]=e),n&&(r["x-amz-security-token"]=n),t&&(r["x-amz-server-side-encryption"]=t),r},h=function(e){var t={};return e&&(t["x-amz-security-token"]=e),t},q=function(e,t,n){return function(r,o,i,u,s){return new Promise(function(r,o){var s=new FormData,l={"X-CSRFToken":v(e)};s.append("to_sign",i),s.append("datetime",u),s.append("dest",n),a("POST",t,s,l,e,function(e,t){var n=c(t);switch(console.log(n),e){case 200:r(n.s3ObjKey);break;case 403:default:o(n.error)}})})}},g=function(e,t,r,o,a){var i={customAuthMethod:q(e,t,a),aws_key:r.access_key_id,bucket:r.bucket,aws_url:r.endpoint,awsRegion:r.region,computeContentMd5:!0,cryptoMd5Method:y,cryptoHexEncodedHash256:m,partSize:20971520,logging:!0,allowS3ExistenceOptimization:!0,s3FileCacheHoursAgo:12},c={name:r.object_key,file:o,contentType:o.type,xAmzHeadersCommon:h(r.session_token),xAmzHeadersAtInitiate:S(r.acl,r.server_side_encryption,r.session_token),progress:function(t,n){u(e,t)},warn:function(t,n,r){r.includes("InvalidAccessKeyId")&&s(e,r)}},l={};r.cache_control&&(l["Cache-Control"]=r.cache_control),r.content_disposition&&(l["Content-Disposition"]=r.content_disposition),c.notSignedHeadersAtInitiate=l,n.default.create(i).then(function(t){f(e),t.add(c).then(function(t){p(e,r.endpoint,r.bucket,t)},function(t){return s(e,t)})})},k=function(e){var t=e.target.parentElement,n=t.querySelector(".file-input").files[0],r=t.querySelector(".file-dest").value,o=t.getAttribute("data-policy-url"),i=t.getAttribute("data-signing-url"),u=new FormData,l={"X-CSRFToken":v(t)};u.append("dest",r),u.append("name",n.name),u.append("type",n.type),u.append("size",n.size),a("POST",o,u,l,t,function(e,o){var a=c(o);switch(e){case 200:g(t,i,a,n,r);break;case 400:case 403:case 500:s(t,a.error);break;default:s(t,"Sorry, could not get upload URL.")}})},b=function(e){e.preventDefault();var t=e.target.parentElement;t.querySelector(".file-url").value="",t.querySelector(".file-input").value="",t.className="s3direct form-active"},_=function(e){var t=e.querySelector(".file-url"),n=e.querySelector(".file-input"),r=e.querySelector(".file-remove"),o=""===t.value?"form":"link";e.className="s3direct "+o+"-active",r.addEventListener("click",b,!1),n.addEventListener("change",k,!1)};document.addEventListener("DOMContentLoaded",function(e){[].forEach.call(document.querySelectorAll(".s3direct"),_)}),document.addEventListener("DOMNodeInserted",function(e){if(e.target.tagName){var t=e.target.querySelectorAll(".s3direct");[].forEach.call(t,function(e,t,n){_(e)})}}); },{"js-cookie":"PhdE","sha.js":"t0b9","evaporate":"TlVp","spark-md5":"13nb","./css/bootstrap.css":"MiSu","./css/styles.css":"/krr"}]},{},["Focm"], null) \ No newline at end of file diff --git a/src/index.js b/src/index.js index 9d284223..4dac430f 100644 --- a/src/index.js +++ b/src/index.js @@ -179,10 +179,6 @@ const initiateUpload = (element, signingUrl, uploadParameters, file, dest) => { uploadParameters.server_side_encryption, uploadParameters.session_token ), - notSignedHeadersAtInitiate: { - 'Cache-Control': uploadParameters.cache_control, - 'Content-Disposition': uploadParameters.content_disposition - }, progress: (progressRatio, stats) => { updateProgressBar(element, progressRatio); }, @@ -193,6 +189,18 @@ const initiateUpload = (element, signingUrl, uploadParameters, file, dest) => { } }; + const optHeaders = {} + + if(uploadParameters.cache_control) { + optHeaders['Cache-Control'] = uploadParameters.cache_control + } + + if(uploadParameters.content_disposition) { + optHeaders['Content-Disposition'] = uploadParameters.content_disposition + } + + addConfig['notSignedHeadersAtInitiate'] = optHeaders + Evaporate .create(createConfig) .then( From 81041e950113ff41b45d641db52b83d083b9eb65 Mon Sep 17 00:00:00 2001 From: bradley griffiths Date: Thu, 31 Jan 2019 13:06:26 +0000 Subject: [PATCH 2/2] Version bump. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e968de4e..5a809793 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "django-s3direct", - "version": "1.1.0", + "version": "1.1.1", "description": "Add direct uploads to S3 functionality with a progress bar to file input fields.", "directories": { "example": "example"