-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
114 lines (102 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('native-promise-only')
var querystring = require('querystring')
var nodeify = require('nodeify')
var http = require('./src/http')
var compactObject = require('./src/compactObject')
var sign = require('./src/signature')
var FD = require("form-data")
if (typeof FormData === 'undefined' && FD) {
FormData = FD
}
var Mediaflow = function(host) {
if (typeof host !== 'string') {
throw new Error("Mediaflow requires a hostname argument")
}
this.host = host
this.username = null
this.key = null
}
Mediaflow.prototype.url = function(method, url, opts) {
var options = {
host: this.host,
port: 80,
path: url,
headers: {}
}
if (method === 'GET') {
var query = querystring.stringify(compactObject(opts))
if (query) options.path += '?' + query
}
if (this.username) {
var signature = sign(opts, this.key)
options.headers['X-Keymedia-Username'] = this.username
options.headers['X-Keymedia-Signature'] = signature
}
return options
}
Mediaflow.prototype.auth = function(username, key) {
this.username = username
this.key = key
return this
}
Mediaflow.prototype.media = function(id, callback) {
var url = this.url('GET', '/media/' + id + '.json')
var p = new Promise(function(resolve,reject){
http.getJSON(url, function(err, data) {
return err ? reject(err) : resolve(data.media)
})
})
return nodeify(p, callback)
}
Mediaflow.prototype.upload = function(file, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
var formData = new FormData()
formData.append('file', file)
for (var key in options) {
if (key == 'attributes') {
for (var attrKey in options[key]) {
formData.append('attributes[' + attrKey + ']', options[key][attrKey])
}
}
else if (Array.isArray(options[key])) {
options[key].forEach(function(val) {
formData.append(key + '[]', val)
})
}
else {
formData.append(key, options[key])
}
}
var url = this.url('POST', '/media.json', options)
var p = new Promise(function(resolve, reject) {
http.postJSON(url, formData, function(err, data) {
return err ? reject(err) : resolve(data.media)
})
})
return nodeify(p, callback)
}
Mediaflow.prototype.search = function(query, callback) {
var url = this.url('GET', '/media.json', {q: query})
var p = new Promise(function(resolve, reject) {
http.getJSON(url, function(err, data) {
return err ? reject(err) : resolve(data)
})
})
return nodeify(p, callback)
}
Mediaflow.prototype.findByTag = function(tag, callback)
{
var url = this.url('GET', '/media.json', {tags : tag})
var p = new Promise(function(resolve, reject)
{
http.getJSON(url, function(err, data)
{
return err ? reject(err) : resolve(data)
})
})
return nodeify(p, callback)
}
module.exports = Mediaflow