1
-
2
1
var url = require ( 'url' ) ;
3
2
var crypto = require ( 'crypto' ) ;
4
3
var formstream = require ( 'formstream' ) ;
@@ -58,6 +57,13 @@ Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret)
58
57
rpc . postWithoutForm ( uri , digest , onret ) ;
59
58
}
60
59
60
+ Client . prototype . fetch = function ( url , bucket , key , onret ) {
61
+ var bucketUri = getEncodedEntryUri ( bucket , key ) ;
62
+ var fetchUrl = util . urlsafeBase64Encode ( url ) ;
63
+ var uri = 'http://iovip.qbox.me/fetch/' + fetchUrl + '/to/' + bucketUri ;
64
+ var digest = util . generateAccessToken ( uri , null ) ;
65
+ rpc . postWithoutForm ( uri , digest , onret ) ;
66
+ }
61
67
62
68
function Entry ( hash , fsize , putTime , mimeType , endUser ) {
63
69
this . hash = hash || null ;
@@ -135,17 +141,35 @@ function getEncodedEntryUri(bucket, key) {
135
141
136
142
// ----- token --------
137
143
// @gist PutPolicy
138
- function PutPolicy ( scope , callbackUrl , callbackBody , returnUrl , returnBody ,
139
- asyncOps , endUser , expires , persistentOps , persistentNotifyUrl ) {
140
- this . scope = scope || null ;
141
- this . callbackUrl = callbackUrl || null ;
142
- this . callbackBody = callbackBody || null ;
143
- this . returnUrl = returnUrl || null ;
144
- this . returnBody = returnBody || null ;
145
- this . endUser = endUser || null ;
146
- this . expires = expires || 3600 ;
147
- this . persistentOps = persistentOps || null ;
148
- this . persistentNotifyUrl = persistentNotifyUrl || null ;
144
+ function PutPolicy ( putPolicyObj ) {
145
+
146
+ if ( typeof putPolicyObj !== 'object' ) {
147
+ return false ;
148
+ }
149
+
150
+ this . scope = putPolicyObj . scope || null ;
151
+ this . expires = putPolicyObj . expires || 3600 ;
152
+ this . insertOnly = putPolicyObj . insertOnly || null ;
153
+
154
+ this . saveKey = putPolicyObj . saveKey || null ;
155
+ this . endUser = putPolicyObj . endUser || null ;
156
+
157
+ this . returnUrl = putPolicyObj . returnUrl || null ;
158
+ this . returnBody = putPolicyObj . returnBody || null ;
159
+
160
+ this . callbackUrl = putPolicyObj . callbackUrl || null ;
161
+ this . callbackHost = putPolicyObj . callbackHost || null ;
162
+ this . callbackBody = putPolicyObj . callbackBody || null ;
163
+
164
+ this . persistentOps = putPolicyObj . persistentOps || null ;
165
+ this . persistentNotifyUrl = putPolicyObj . persistentNotifyUrl || null ;
166
+ this . persistentPipeline = putPolicyObj . persistentPipeline || null ;
167
+
168
+ this . fsizeLimit = putPolicyObj . fsizeLimit || null ;
169
+
170
+ this . detectMime = putPolicyObj . detectMime || null ;
171
+
172
+ this . mimeLimit = putPolicyObj . mimeLimit || null ;
149
173
}
150
174
// @endgist
151
175
@@ -161,54 +185,18 @@ PutPolicy.prototype.token = function(mac) {
161
185
return uploadToken ;
162
186
}
163
187
164
- PutPolicy . prototype . getFlags = function ( putPolicy ) {
188
+ PutPolicy . prototype . getFlags = function ( ) {
165
189
var flags = { } ;
166
- if ( this . scope != null ) {
167
- flags [ 'scope' ] = this . scope ;
168
- }
169
- if ( this . callbackUrl != null ) {
170
- flags [ 'callbackUrl' ] = this . callbackUrl ;
171
- }
172
- if ( this . callbackBody != null ) {
173
- flags [ 'callbackBody' ] = this . callbackBody ;
174
- }
175
- if ( this . returnUrl != null ) {
176
- flags [ 'returnUrl' ] = this . returnUrl ;
177
- }
178
- if ( this . returnBody != null ) {
179
- flags [ 'returnBody' ] = this . returnBody ;
180
- }
181
- if ( this . endUser != null ) {
182
- flags [ 'endUser' ] = this . endUser ;
183
- }
184
- if ( this . persistentOps != null ) {
185
- flags [ 'persistentOps' ] = this . persistentOps ;
186
- }
187
- if ( this . persistentNotifyUrl != null ) {
188
- flags [ 'persistentNotifyUrl' ] = this . persistentNotifyUrl ;
189
- }
190
- if ( this . persistentPipeline != null ) {
191
- flags [ 'persistentPipeline' ] = this . persistentPipeline ;
192
- }
193
- if ( this . mimeLimit != null ) {
194
- flags [ 'mimeLimit' ] = this . mimeLimit ;
195
- }
196
- if ( this . insertOnly != null ) {
197
- flags [ 'insertOnly' ] = this . insertOnly ;
198
- }
199
- if ( this . detectMime != null ) {
200
- flags [ 'detectMime' ] = this . detectMime ;
201
- }
202
- if ( this . saveKey != null ) {
203
- flags [ 'saveKey' ] = this . saveKey ;
190
+ var attrs = [ 'scope' , 'insertOnly' , 'saveKey' , 'endUser' , 'returnUrl' , 'returnBody' , 'callbackUrl' , 'callbackHost' , 'callbackBody' , 'callbackBodyType' , 'callbackFetchKey' , 'persistentOps' , 'persistentNotifyUrl' , 'persistentPipeline' , 'fsizeLimit' , 'detectMime' , 'mimeLimit' ] ;
191
+
192
+ for ( var i = attrs . length - 1 ; i >= 0 ; i -- ) {
193
+ if ( this [ attrs [ i ] ] !== null ) {
194
+ flags [ attrs [ i ] ] = this [ attrs [ i ] ] ;
195
+ }
204
196
}
197
+
205
198
flags [ 'deadline' ] = this . expires + Math . floor ( Date . now ( ) / 1000 ) ;
206
- if ( this . fsizeLimit != null ) {
207
- flags [ 'fsizeLimit' ] = this . fsizeLimit ;
208
- }
209
- if ( this . insertOnly != null ) {
210
- flags [ 'insertOnly' ] = this . insertOnly ;
211
- }
199
+
212
200
return flags ;
213
201
}
214
202
@@ -239,5 +227,5 @@ GetPolicy.prototype.makeRequest = function(baseUrl, mac) {
239
227
// query like '-thumbnail', '?imageMogr2/thumbnail/960x' and so on
240
228
function makeBaseUrl ( domain , key , query ) {
241
229
key = new Buffer ( key ) ;
242
- return 'http://' + domain + '/' + querystring . escape ( key ) + ( query || '' ) ;
230
+ return 'http://' + domain + '/' + querystring . escape ( key ) + ( query || '' ) ;
243
231
}
0 commit comments