@@ -16,6 +16,7 @@ exports.BatchItemRet = BatchItemRet;
1616exports . BatchStatItemRet = BatchStatItemRet ;
1717
1818exports . PutPolicy = PutPolicy ;
19+ exports . PutPolicy2 = PutPolicy2 ;
1920exports . GetPolicy = GetPolicy ;
2021exports . makeBaseUrl = makeBaseUrl ;
2122
@@ -141,7 +142,48 @@ function getEncodedEntryUri(bucket, key) {
141142
142143// ----- token --------
143144// @gist PutPolicy
144- function PutPolicy ( putPolicyObj ) {
145+ function PutPolicy ( scope , callbackUrl , callbackBody , returnUrl , returnBody ,
146+ asyncOps , endUser , expires , persistentOps , persistentNotifyUrl ) {
147+ this . scope = scope || null ;
148+ this . callbackUrl = callbackUrl || null ;
149+ this . callbackBody = callbackBody || null ;
150+ this . returnUrl = returnUrl || null ;
151+ this . returnBody = returnBody || null ;
152+ this . endUser = endUser || null ;
153+ this . expires = expires || 3600 ;
154+ this . persistentOps = persistentOps || null ;
155+ this . persistentNotifyUrl = persistentNotifyUrl || null ;
156+ }
157+ // @endgist
158+
159+ PutPolicy . prototype . token = function ( mac ) {
160+ if ( mac == null ) {
161+ mac = new Mac ( conf . ACCESS_KEY , conf . SECRET_KEY ) ;
162+ }
163+ var flags = this . getFlags ( ) ;
164+ var encodedFlags = util . urlsafeBase64Encode ( JSON . stringify ( flags ) ) ;
165+ var encoded = util . hmacSha1 ( encodedFlags , mac . secretKey ) ;
166+ var encodedSign = util . base64ToUrlSafe ( encoded ) ;
167+ var uploadToken = mac . accessKey + ':' + encodedSign + ':' + encodedFlags ;
168+ return uploadToken ;
169+ }
170+
171+ PutPolicy . prototype . getFlags = function ( ) {
172+ var flags = { } ;
173+ var attrs = [ 'scope' , 'insertOnly' , 'saveKey' , 'endUser' , 'returnUrl' , 'returnBody' , 'callbackUrl' , 'callbackHost' , 'callbackBody' , 'callbackBodyType' , 'callbackFetchKey' , 'persistentOps' , 'persistentNotifyUrl' , 'persistentPipeline' , 'fsizeLimit' , 'detectMime' , 'mimeLimit' ] ;
174+
175+ for ( var i = attrs . length - 1 ; i >= 0 ; i -- ) {
176+ if ( this [ attrs [ i ] ] !== null ) {
177+ flags [ attrs [ i ] ] = this [ attrs [ i ] ] ;
178+ }
179+ }
180+
181+ flags [ 'deadline' ] = this . expires + Math . floor ( Date . now ( ) / 1000 ) ;
182+
183+ return flags ;
184+ }
185+
186+ function PutPolicy2 ( putPolicyObj ) {
145187
146188 if ( typeof putPolicyObj !== 'object' ) {
147189 return false ;
@@ -171,9 +213,8 @@ function PutPolicy(putPolicyObj) {
171213
172214 this . mimeLimit = putPolicyObj . mimeLimit || null ;
173215}
174- // @endgist
175216
176- PutPolicy . prototype . token = function ( mac ) {
217+ PutPolicy2 . prototype . token = function ( mac ) {
177218 if ( mac == null ) {
178219 mac = new Mac ( conf . ACCESS_KEY , conf . SECRET_KEY ) ;
179220 }
@@ -185,7 +226,7 @@ PutPolicy.prototype.token = function(mac) {
185226 return uploadToken ;
186227}
187228
188- PutPolicy . prototype . getFlags = function ( ) {
229+ PutPolicy2 . prototype . getFlags = function ( ) {
189230 var flags = { } ;
190231 var attrs = [ 'scope' , 'insertOnly' , 'saveKey' , 'endUser' , 'returnUrl' , 'returnBody' , 'callbackUrl' , 'callbackHost' , 'callbackBody' , 'callbackBodyType' , 'callbackFetchKey' , 'persistentOps' , 'persistentNotifyUrl' , 'persistentPipeline' , 'fsizeLimit' , 'detectMime' , 'mimeLimit' ] ;
191232
0 commit comments