-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
89 lines (72 loc) · 2.55 KB
/
upload.js
File metadata and controls
89 lines (72 loc) · 2.55 KB
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
var request = require('request'),
fs = require('fs'),
IMAGE_FILE_NAME = 'image.png',
ACCESS_TOKEN = 'MY ACCESS TOKEN HERE',
uploadRoot = 'https://api.thinglink.com/api/upload',
formData = {
contentType:'image/png' //"image/jpeg", "image/png", "image/gif"
},
headers = {
'Authorization': 'Bearer ' + ACCESS_TOKEN
},
uploadId;
request.post({
url: uploadRoot,
formData: formData,
headers: headers
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('Getting upload id failed:', err);
}
uploadId = JSON.parse(body).results.id;
console.log(uploadId);
fs.stat(IMAGE_FILE_NAME, function(error, stat) {
headers['Content-Length'] = stat.size;
console.log(stat.size);
fs.createReadStream(IMAGE_FILE_NAME).pipe(request.put({
url: uploadRoot + '/' + uploadId,
headers: headers
}, function(err, response, body) {
console.log(response.statusCode);
console.log(response.headers['location']);
if (err) {
return console.error('upload failed:', err);
}
headers['Content-Type'] = 'application/json';
delete headers['Content-Length'];
request.post({
url: 'https://api.thinglink.com/api/scene',
headers: headers,
body: JSON.stringify({
"tags":[
{
"description":"Thing #1",
"link":"http:\/\/www.thinglink.com\/",
"state":"thing1",
"nubbin":"red",
"box":{
"y1":0.5,
"y2":0.5,
"x2":0,
"x1":0
}
},
{
"description":"Thing #2",
"picture":response.headers['location'],
"box":{
"y1":1.0,
"y2":1.0,
"x2":0.5,
"x1":0.5
}
}
],
"title":"Wolverine",
"visibility":"public",
"url": response.headers['location']
})
})
}));
});
});