Skip to content

Instantly share code, notes, and snippets.

@liangchaoboy
Created December 18, 2017 15:02
Show Gist options
  • Save liangchaoboy/11a9fa1feaebac01cc60306698024e42 to your computer and use it in GitHub Desktop.
Save liangchaoboy/11a9fa1feaebac01cc60306698024e42 to your computer and use it in GitHub Desktop.
qiniu_vframe_timely.js
var qiniu = require("qiniu");
var crypto = require('crypto');
var urllib = require('urllib');
var url = require('url');
var LineByLineReader = require('line-by-line');
var ak = '**';
var sk = '**';
vframe_10s = 'vframe/jpg/offset/10';
vframe_30s = 'vframe/jpg/offset/30';
vframe_60s = 'vframe/jpg/offset/60';
vframe_10s_key = '/img1.jpg';
vframe_30s_key = '/img2.jpg';
vframe_60s_key = '/img3.jpg';
rl = new LineByLineReader('tmp');
var i = 1
rl.on('line', function(line) {
console.log(line);
rl.pause();
urllib.request(line, function (err, data, res) {
if (err) {
throw err; // you need to handle error
}
console.log(res.statusCode);
});
setTimeout(function(args) {
console.log(i++);
console.log(new Date());
rl.resume();
}, 200);
});
rl.on('error', function(e) {
console.log(e);
});
function getUrl( urlStr, vframe_style, vframe_key){
var u = url.parse(urlStr);
console.log('u\t' + u.toString());
var key = 'img' + u.path.split('.')[0] + vframe_key;
console.log('key\t' + key);
var bucket = 'transform';
saveas_key = qiniu.util.urlsafeBase64Encode(bucket + ':' + key);
var path = u.host + u.path + '?' + vframe_style + '|saveas/' + saveas_key;
var signStr = sign(path);
var str = 'http://' + path + '/sign/' + signStr;
return str;
}
// 创建 AccessToken 凭证
function sign(signingStr){
var digest = hmacSha1(signingStr, sk);
var safeDigest = qiniu.util.base64ToUrlSafe(digest);
return ak + ':' + safeDigest;
}
// Hmac-sha1 Crypt
function hmacSha1 (encodedFlags, secretKey) {
/*
*return value already encoded with base64
* */
var hmac = crypto.createHmac('sha1', secretKey);
hmac.update(encodedFlags);
return hmac.digest('base64');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment