forked from russmatney/lambda-upload-to-vimeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (170 loc) · 4.36 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var Q = require('q');
var path = require('path');
var validate = require('lambduh-validate');
var download = require('lambduh-get-s3-object');
var mime = require('mime');
var fs = require('fs');
var req = require('request');
var vimeoCreds = {
accessToken: "499a1737e1e8401bcdd321941afad729"
}
function getFilesizeInBytes(filename) {
var stats = fs.statSync(filename);
var fileSizeInBytes = stats["size"];
return fileSizeInBytes;
}
exports.handler = function(event, context) {
event.localFilepath = "";
validate(event, {
sourceBucket: true,
sourceKey: true,
videoDescription: true,
videoTitle: true,
})
//download specified file
.then(function(event) {
console.log('downloading file');
event.localFilepath = "/tmp/" + path.basename(event.sourceKey);
return download(event, {
srcBucket: event.sourceBucket,
srcKey: event.sourceKey,
downloadFilepath: event.localFilepath
})
})
.then(function(event) {
var def = Q.defer();
console.log('waiting for a sec');
setTimeout(function() {
def.resolve(event);
}, 1000);
return def.promise;
})
//get vimeo upload 'ticket'
.then(function(event) {
var def = Q.defer();
req({
url: "https://api.vimeo.com/me/videos",
method: "POST",
json: true,
body: {
type: 'streaming'
},
headers: {
'Authorization': 'bearer ' + vimeoCreds.accessToken
}
}, function(err, response, body){
if (err) {
def.reject(err)
} else {
console.log('vimeo upload ticket recieved');
console.log(body);
event.upload_link_secure = body.upload_link_secure;
event.complete_uri = body.complete_uri;
def.resolve(event);
}
})
return def.promise;
})
//upload file to upload_link_secure
.then(function(event) {
var def = Q.defer();
var uploadReq = req({
url: event.upload_link_secure,
method: "PUT",
headers: {
'Content-Type': mime.lookup(event.localFilepath),
'Content-Length': getFilesizeInBytes(event.localFilepath)
}
})
uploadReq.on('error', function(err) {
console.log('uploadReq error');
def.reject(err);
})
uploadReq.on('end', function() {
def.resolve(event);
})
var readStream = fs.createReadStream(event.localFilepath)
readStream.on('error', function(err) {
console.log('error uploading file');
def.reject(err);
});
readStream.pipe(uploadReq);
return def.promise;
})
//confirm upload success
.then(function(event) {
var def = Q.defer();
req({
url: event.upload_link_secure,
method: "PUT",
headers: {
'Content-Length': 0,
'Content-Range': 'bytes */*'
}
}, function(err, response) {
if (err) {
def.reject(err)
} else if (response.statusCode !== 308) {
def.reject(new Error("incomplete download"))
//TODO: continue upload at byte where it left off
} else {
console.log('upload complete');
def.resolve(event)
}
})
return def.promise;
})
//'complete' the vimeo upload - DELETE to the 'complete_uri'
.then(function(event) {
var def = Q.defer();
req({
url: "https://api.vimeo.com" + event.complete_uri,
method: "DELETE",
headers: {
'Authorization': 'bearer ' + vimeoCreds.accessToken
}
}, function(err, response) {
if (err) {
def.reject(err)
} else {
event.clip_uri = response.headers.location;
def.resolve(event)
}
})
return def.promise;
})
//update video meta data
.then(function(event) {
var def = Q.defer();
req({
url: "https://api.vimeo.com" + event.clip_uri,
method: "PATCH",
json: true,
body: {
name: event.videoTitle,
description: event.videoDescription
},
headers: {
Authorization: 'bearer ' + vimeoCreds.accessToken
}
}, function(err, response, body) {
if(err) {
def.reject(err);
} else {
console.log('meta data updated');
console.log(response.statusCode);
def.resolve(event);
}
})
return def.promise;
})
.then(function(event) {
console.log(event);
context.done();
})
.fail(function(error) {
console.log('error');
console.log(error);
context.done(null, error);
})
}