Skip to content

Commit 18dc27f

Browse files
committed
Merge pull request #16 from qiniupd/8429_support_private_bucket
try to support private bucket
2 parents ccf02f1 + 05beb4a commit 18dc27f

File tree

9 files changed

+114
-31
lines changed

9 files changed

+114
-31
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
install : all
22

33
all :
4+
grunt
45
node demo/server.js

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ qiniu-js-sdk
5959
var uploader = Qiniu.uploader({
6060
runtimes: 'html5,flash,html4', //上传模式,依次退化
6161
browse_button: 'pickfiles', //上传选择的点选按钮,**必需**
62-
uptoken_url: '/token', //Ajax请求upToken的Url,**强烈建议设置**(服务端提供)
62+
uptoken_url: '/uptoken', //Ajax请求upToken的Url,**强烈建议设置**(服务端提供)
63+
// downtoken_url: '/downtoken',
64+
// Ajax请求downToken的Url,私有空间时使用,JS-SDK将向该地址POST文件的key和domain,服务端返回的JSON必须包含`url`字段,`url`值为该文件的下载地址
6365
// uptoken : '<Your upload token>', //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成
6466
// unique_names: true, // 默认 false,key为文件名。若开启该选项,SDK会为每个文件自动生成key(文件名)
6567
// save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`,则开启,SDK在前端将不对key进行任何处理
@@ -301,6 +303,8 @@ qiniu-js-sdk
301303
302304
* 安装 [Nodejs](http://nodejs.org/download/)、[npm](https://www.npmjs.org/)
303305
306+
* `npm install -g grunt-cli`,安装 Grunt
307+
304308
* `npm install`,安装七牛 Node.js SDK、Express
305309
306310
* 获取源代码:
@@ -316,12 +320,15 @@ qiniu-js-sdk
316320
'ACCESS_KEY': '<Your Access Key>',
317321
'SECRET_KEY': '<Your Secret Key>',
318322
'Bucket_Name': '<Your Bucket Name>',
319-
'Port': 18080
323+
'Port': 18080,
324+
'Uptoken_Url': '<Your Uptoken_Url>',
325+
'Domain': '<Your Bucket Domain>'
320326
}
321327
322328
```
323329
324-
* 在`demo`目录运行`node server.js` 或者 在根目录运行`make`启动
330+
* 在根目录运行`make`启动
331+
325332
* 访问`http://127.0.0.1:18080/`或`http://localhost:18080/`
326333
327334
## 说明

demo/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module.exports = {
33
'SECRET_KEY': '<Your Secret Key>',
44
'Bucket_Name': '<Your Bucket Name>',
55
'Port': 18080,
6-
'Uptoken_Url': '/token',
6+
'Uptoken_Url': '/uptoken',
77
'Domain': 'http://qiniu-plupload.qiniudn.com/'
8-
}
8+
};

demo/js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/*global FileProgress */
44
/*global hljs */
55

6+
67
$(function() {
78
var uploader = Qiniu.uploader({
89
runtimes: 'html5,flash,html4',
@@ -15,6 +16,7 @@ $(function() {
1516
chunk_size: '4mb',
1617
uptoken_url: $('#uptoken_url').val(),
1718
domain: $('#domain').val(),
19+
// downtoken_url: '/downtoken',
1820
// unique_names: true,
1921
// save_key: true,
2022
// x_vars: {

demo/js/ui.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,18 @@ FileProgress.prototype.setComplete = function(up, info) {
190190
var td = this.fileProgressWrapper.find('td:eq(2) .progress');
191191

192192
var res = $.parseJSON(info);
193-
var domain = up.getOption('domain');
194-
var url = domain + encodeURI(res.key);
195-
var link = domain + res.key;
196-
var str = "<div><strong>Link:</strong><a href=" + url + " target='_blank' > " + link + "</a></div>" +
197-
"<div class=hash><strong>Hash:</strong>" + res.hash + "</div>";
193+
var url;
194+
if (res.url) {
195+
url = res.url;
196+
str = "<div><strong>Link:</strong><a href=" + res.url + " target='_blank' > " + res.url + "</a></div>" +
197+
"<div class=hash><strong>Hash:</strong>" + res.hash + "</div>";
198+
} else {
199+
var domain = up.getOption('domain');
200+
url = domain + encodeURI(res.key);
201+
var link = domain + res.key;
202+
str = "<div><strong>Link:</strong><a href=" + url + " target='_blank' > " + link + "</a></div>" +
203+
"<div class=hash><strong>Hash:</strong>" + res.hash + "</div>";
204+
}
198205

199206
td.html(str).removeClass().next().next('.status').hide();
200207

@@ -240,12 +247,14 @@ FileProgress.prototype.setComplete = function(up, info) {
240247
Wrapper.append(imgWrapper);
241248

242249
var img = new Image();
243-
$(img).attr('src', url + imageView);
250+
if (!/imageView/.test(url)) {
251+
url += imageView
252+
}
253+
$(img).attr('src', url);
244254

245255
var height_space = 340;
246256
$(img).on('load', function() {
247-
248-
showImg.attr('src', url + imageView);
257+
showImg.attr('src', url);
249258

250259
linkWrapper.attr('href', url).attr('title', '查看原图');
251260

demo/server.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ app.configure(function() {
77
app.use(express.static(__dirname + '/'));
88
});
99

10+
1011
app.set('views', __dirname + '/views');
1112
app.engine('html', require('ejs').renderFile);
1213

14+
app.use(express.urlencoded());
1315

14-
app.use(function(req, res, next) {
15-
req.headers['if-none-match'] = 'no-match-for-this';
16-
next();
17-
});
18-
19-
app.get('/token', function(req, res, next) {
16+
app.get('/uptoken', function(req, res, next) {
2017
var token = uptoken.token();
2118
res.header("Cache-Control", "max-age=0, private, must-revalidate");
2219
res.header("Pragma", "no-cache");
@@ -28,6 +25,40 @@ app.get('/token', function(req, res, next) {
2825
}
2926
});
3027

28+
app.post('/downtoken', function(req, res) {
29+
30+
var key = req.body.key,
31+
domain = req.body.domain;
32+
33+
//trim 'http://'
34+
if (domain.indexOf('http://') != -1) {
35+
domain = domain.substr(7);
36+
}
37+
//trim 'https://'
38+
if (domain.indexOf('https://') != -1) {
39+
domain = domain.substr(8);
40+
}
41+
//trim '/' if the domain's last char is '/'
42+
if (domain.lastIndexOf('/') === domain.length - 1) {
43+
domain = domain.substr(0, domain.length - 1);
44+
}
45+
46+
var baseUrl = qiniu.rs.makeBaseUrl(domain, key);
47+
var deadline = 3600 + Math.floor(Date.now() / 1000);
48+
49+
baseUrl += '?e=' + deadline;
50+
var signature = qiniu.util.hmacSha1(baseUrl, config.SECRET_KEY);
51+
var encodedSign = qiniu.util.base64ToUrlSafe(signature);
52+
var downloadToken = config.ACCESS_KEY + ':' + encodedSign;
53+
54+
if (downloadToken) {
55+
res.json({
56+
downtoken: downloadToken,
57+
url: baseUrl + '&token=' + downloadToken
58+
})
59+
}
60+
});
61+
3162
app.get('/', function(req, res) {
3263
res.render('index.html', {
3364
domain: config.Domain,
@@ -41,4 +72,6 @@ qiniu.conf.SECRET_KEY = config.SECRET_KEY;
4172
var uptoken = new qiniu.rs.PutPolicy(config.Bucket_Name);
4273

4374

44-
app.listen(config.Port);
75+
app.listen(config.Port, function() {
76+
console.log('Listening on port %d', config.Port);
77+
});

demo/views/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1 class="text-left col-md-12 ">
2020
<a class="btn btn-default view_code" id="show_code">
2121
查看初始化代码
2222
</a>
23-
<a class="btn btn-default view_github" href="https://github.com/sunln/qiniu-js-sdk" target="_blank">
23+
<a class="btn btn-default view_github" href="https://github.com/qiniupd/qiniu-js-sdk" target="_blank">
2424
<img src="http://qtestbucket.qiniudn.com/GitHub-Mark-32px.png">
2525
View Source on Github
2626
</a>

src/qiniu.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ function QiniuJsSDK() {
451451
errorObj = that.parseJSON(errorObj.error);
452452
errorText = errorObj.error || 'file exists';
453453
} catch (e) {
454-
// console.log(e);
454+
throw ('invalid json format');
455455
}
456456
break;
457457
case 631:
@@ -493,6 +493,42 @@ function QiniuJsSDK() {
493493

494494
uploader.bind('FileUploaded', (function(_FileUploaded_Handler) {
495495
return function(up, file, info) {
496+
497+
var last_step = function(up, file, info) {
498+
if (op.downtoken_url) {
499+
var ajax_downtoken = that.createAjax();
500+
ajax_downtoken.open('POST', op.downtoken_url, true);
501+
ajax_downtoken.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
502+
ajax_downtoken.onreadystatechange = function() {
503+
if (ajax_downtoken.readyState === 4) {
504+
if (ajax_downtoken.status === 200) {
505+
var res_downtoken;
506+
try {
507+
res_downtoken = that.parseJSON(ajax_downtoken.responseText);
508+
} catch (e) {
509+
throw ('invalid json format');
510+
}
511+
var info_extended = {};
512+
plupload.extend(info_extended, that.parseJSON(info), res_downtoken);
513+
if (_FileUploaded_Handler) {
514+
_FileUploaded_Handler(up, file, JSON.stringify(info_extended));
515+
}
516+
} else {
517+
uploader.trigger('Error', {
518+
status: ajax_downtoken.status,
519+
response: ajax_downtoken.responseText,
520+
file: file,
521+
code: plupload.HTTP_ERROR
522+
});
523+
}
524+
}
525+
};
526+
ajax_downtoken.send('key=' + that.parseJSON(info).key + '&domain=' + op.domain);
527+
} else if (_FileUploaded_Handler) {
528+
_FileUploaded_Handler(up, file, info);
529+
}
530+
};
531+
496532
var res = that.parseJSON(info.response);
497533
ctx = ctx ? ctx : res.ctx;
498534
if (ctx) {
@@ -523,15 +559,11 @@ function QiniuJsSDK() {
523559
ajax.open('POST', url, true);
524560
ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
525561
ajax.setRequestHeader('Authorization', 'UpToken ' + that.token);
526-
ajax.send(ctx);
527562
ajax.onreadystatechange = function() {
528563
if (ajax.readyState === 4) {
529564
if (ajax.status === 200) {
530565
var info = ajax.responseText;
531-
532-
if (_FileUploaded_Handler) {
533-
_FileUploaded_Handler(up, file, info);
534-
}
566+
last_step(up, file, info);
535567

536568
} else {
537569
uploader.trigger('Error', {
@@ -543,10 +575,9 @@ function QiniuJsSDK() {
543575
}
544576
}
545577
};
578+
ajax.send(ctx);
546579
} else {
547-
if (_FileUploaded_Handler) {
548-
_FileUploaded_Handler(up, file, info.response);
549-
}
580+
last_step(up, file, info.response);
550581
}
551582

552583
};

0 commit comments

Comments
 (0)