Skip to content

Commit

Permalink
Merge pull request #50 from pili-engineering/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
longbai authored Jun 30, 2016
2 parents 29b370a + e31bf72 commit adff6d3
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 1,051 deletions.
572 changes: 165 additions & 407 deletions README.md

Large diffs are not rendered by default.

396 changes: 89 additions & 307 deletions example/app.js

Large diffs are not rendered by default.

225 changes: 87 additions & 138 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

var Request = require('./request')
, Stream = require('./stream');
const fmt = require('util');
const Request = require('./request');
const Util = require('./util');
const Stream = require('./stream');

var API = {};

Expand All @@ -11,199 +13,146 @@ exports.init = function() {
if (!API.request) {
API.request = new Request();
}
};
}

// client APIs
exports.createStream = function(credentials, hub, options, fn) {
exports.createStream = function(credentials, hub, key, fn) {

var data = {
'hub': hub
'key': key
};

if (options) {
if (options.title) {
data['title'] = options.title;
}
if (options.publishKey) {
data['publishKey'] = options.publishKey;
}
if (options.publishSecurity) {
data['publishSecurity'] = options.publishSecurity;
}
}

API.request.post(credentials, '/streams', data, function(err, json) {
var path = fmt.format('/hubs/%s/streams', hub);
API.request.post(credentials, path, data, function(err, jsonData) {
if (err) {
fn(err, null);
} else {
var stream = new Stream(credentials, json);
var stream = new Stream(credentials, hub, key);
fn(null, stream);
}
});
};

exports.getStream = function(credentials, streamId, fn) {
var path = '/streams/' + streamId;

API.request.get(credentials, path, null, function(err, data) {
if (err) {
fn(err, null);
} else {
var stream = new Stream(credentials, data);
fn(null, stream);
}
});
};
}

exports.listStreams = function(credentials, hub, options, fn) {
var data = {
'hub': hub
};

var data = {};

if (options) {
if (options.marker) {
data['marker'] = options.marker;
if (options.liveonly) {
data['liveonly'] = true;
}
if (options.prefix) {
data['prefix'] = options.prefix;
}
if (options.limit) {
data['limit'] = options.limit;
}
if (options.title) {
data['title'] = options.title;
if (options.marker) {
data['marker'] = options.marker;
}
}

API.request.get(credentials, '/streams', data, function(err, jsonData) {
var path = fmt.format('/hubs/%s/streams', hub);
API.request.get(credentials, path, data, function(err, jsonData) {
if (err) {
fn(err, null, null);
} else {
var marker = null;
if (jsonData['marker'] && jsonData['marker'] != '') {
marker = jsonData['marker'];
}

var list = [];
var marker = jsonData['marker'];
var jsonList = jsonData['items'];

if (jsonList) {
jsonList.forEach(function(json) {
var stream = new Stream(credentials, json);
var stream = new Stream(credentials, hub, json['key']);
list.push(stream);
});
})
}

fn(null, marker, list);
}
});
};
}

exports.updateStream = function(credentials, streamId, options, fn) {
var path = '/streams/' + streamId;
var data = {};
exports.getStreamInfo = function(credentials, hub, key, fn) {

if (options) {
if (options.publishKey) {
data['publishKey'] = options.publishKey;
}
if (options.publishSecurity) {
data['publishSecurity'] = options.publishSecurity;
}
if (options.disabled != 'undefined') {
data['disabled'] = options.disabled;
}
}
var path = fmt.format('/hubs/%s/streams/%s', hub, Util.urlsafeBase64Encode(key));
API.request.get(credentials, path, null, fn);
}

API.request.post(credentials, path, data, function(err, data) {
if (err) {
fn(err, null);
} else {
var stream = new Stream(credentials, data);
fn(null, stream);
}
});
};
exports.streamLiveInfo = function(credentials, hub, key, fn) {

exports.getStreamSegments = function(credentials, streamId, options, fn) {
var path = '/streams/' + streamId + '/segments';
var data = {};
var path = fmt.format('/hubs/%s/streams/%s/live', hub, Util.urlsafeBase64Encode(key));
API.request.get(credentials, path, null, fn);
}

if (options) {
if (options.startTime) {
data['start'] = options.startTime;
}
if (options.endTime) {
data['end'] = options.endTime;
}
if (options.limit) {
data['limit'] = options.limit;
}
exports.disableStream = function(credentials, hub, key, disabledTill, fn) {

var path = fmt.format('/hubs/%s/streams/%s/disabled', hub, Util.urlsafeBase64Encode(key));
var data = {
'disabledTill': disabledTill
}

API.request.get(credentials, path, data, function(err, jsonObject) {
if (err) {
fn(err, null);
} else {
fn(null, jsonObject['segments']);
}
});
};
API.request.post(credentials, path, data, fn);
}

exports.getStreamStatus = function(credentials, streamId, fn) {
var path = '/streams/' + streamId + '/status';
API.request.get(credentials, path, null, fn);
};
exports.savePlayback = function(credentials, hub, key, options, fn) {

exports.deleteStream = function(credentials, streamId, fn) {
var path = '/streams/' + streamId;
API.request.delete(credentials, path, function(err, object) {
fn(err);
});
};
var path = fmt.format('/hubs/%s/streams/%s/saveas', hub, Util.urlsafeBase64Encode(key));

exports.saveStreamAs = function(credentials, streamId, name, format, start, end, options, fn) {
var path = '/streams/' + streamId + '/saveas';
var data = {};

data['name'] = name;
data['start'] = start;
data['end'] = end;

if (format) {
data['format'] = format;
}
if (options) {
if (options.notifyUrl) {
data['notifyUrl'] = options.notifyUrl;
if (options.start) {
data['start'] = options.start;
}
if (options.pipeline) {
data['pipeline'] = options.pipeline;
if (options.end) {
data['end'] = options.end;
}
}

API.request.post(credentials, path, data, function(err, responseData) {
if (err) {
fn(err, null);
} else {
fn(null, responseData);
}
});
API.request.post(credentials, path, data, fn);
}

exports.snapshotStream = function(credentials, streamId, name, format, options, fn) {
var path = '/streams/' + streamId + '/snapshot';
var data = {};
exports.publishHistory = function(credentials, hub, key, options, fn) {

data['name'] = name;
data['format'] = format;
var path = fmt.format('/hubs/%s/streams/%s/historyactivity', hub, Util.urlsafeBase64Encode(key));

var data = {};
if (options) {
if (options.time) {
data['time'] = options.time;
if (options.start) {
data['start'] = options.start;
}
if (options.notifyUrl) {
data['notifyUrl'] = options.notifyUrl;
if (options.end) {
data['end'] = options.end;
}
}

API.request.post(credentials, path, data, function(err, responseData) {
if (err) {
fn(err, null);
} else {
fn(null, responseData);
}
});
API.request.get(credentials, path, data, fn);
}

// URL ------------

exports.publishURL = function(credentials, domain, hub, key, expireAfterSec) {

var expire = Math.floor(Date.now()/1000);
var path = fmt.format('/%s/%s?e=%d', hub, key, expire);
var token = credentials.sign(path);
return fmt.format('rtmp://%s%s&token=%s', domain, path, token);
}

exports.rtmpPlayURL = function(domain, hub, key) {
return fmt.format('rtmp://%s/%s/%s', domain, hub, key);
}

exports.hdlPlayURL = function(domain, hub, key) {
return fmt.format('http://%s/%s/%s.flv', domain, hub, key);
}

exports.hlsPlayURL = function(domain, hub, key) {
return fmt.format('http://%s/%s/%s.m3u8', domain, hub, key);
}

exports.snapshotPlayURL = function(domain, hub, key) {
return fmt.format('http://%s/%s/%s.jpg', domain, hub, key);
}
12 changes: 6 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
var config = module.exports = {};

config.const = {
ORIGIN : 'ORIGIN',
SDK_VERSION : '1.5.0',
SDK_USER_AGENT : 'pili-sdk-nodejs',
DEFAULT_API_HOST : 'pili.qiniuapi.com',
DEFAULT_API_VERSION : 'v1'
ORIGIN: 'ORIGIN',
SDK_VERSION: '1.5.0',
SDK_USER_AGENT: 'pili-sdk-nodejs',
DEFAULT_API_HOST: 'pili.qiniuapi.com',
DEFAULT_API_VERSION: 'v2'
}

config.API_HOST = config.const.DEFAULT_API_HOST;
config.USE_HTTPS = false;
config.USE_HTTPS = false;
18 changes: 15 additions & 3 deletions lib/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ function Credentials(accessKey, secretKey) {
this.secretKey = secretKey;
}

Credentials.prototype.generateAccessToken = function (options, data) {
Credentials.prototype.generateAccessToken = function(options, data) {
var sign = this._signRequest(options, data);
var token = 'Qiniu' + ' ' + this.accessKey + ':' + sign;

return token;
}

Credentials.prototype._signRequest = function (options, body) {
Credentials.prototype._signRequest = function(options, body) {
var contentType = options.headers['Content-Type'];

var host = options.host;
if (options.port && options.port != 80) {
host = host + ':' + options.port;
}

var data = options.method + ' ' + options.path;
data += '\nHost: ' + options.host;
data += '\nHost: ' + host;
if (contentType) {
data += '\nContent-Type: ' + contentType;
}
Expand All @@ -34,4 +39,11 @@ Credentials.prototype._signRequest = function (options, body) {
return sageDigest;
}

Credentials.prototype.sign = function(data) {
var digest = util.hmacSha1(data, this.secretKey);
var sageDigest = util.base64ToUrlSafe(digest);
return this.accessKey + ":" + sageDigest;

}

module.exports = exports = Credentials;
19 changes: 11 additions & 8 deletions lib/hub.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
'use strict';

var Credentials = require('./credentials')
, API = require('./api');
const Credentials = require('./credentials')
const API = require('./api');
const Stream = require('./stream');

function Hub(credentials, hub) {
this.credentials = credentials;
this.hub = hub;

API.init();
};
}

Hub.prototype.createStream = function(options, fn) {
API.createStream(this.credentials, this.hub, options, fn);
Hub.prototype.newStream = function(key) {
return new Stream(this.credentials, this.hub, key);
}

Hub.prototype.getStream = function(streamId, fn) {
API.getStream(this.credentials, streamId, fn);
Hub.prototype.createStream = function(key, fn) {

API.createStream(this.credentials, this.hub, key, fn);
}

Hub.prototype.listStreams = function(options, fn) {

API.listStreams(this.credentials, this.hub, options, fn);
}

module.exports = exports = Hub;
module.exports = exports = Hub;
Loading

0 comments on commit adff6d3

Please sign in to comment.