-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
41 lines (37 loc) · 1016 Bytes
/
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
const createUrl = require('./lib/create-url');
const downloadAndSave = require('./lib/download-and-save');
const parsePage = require('./lib/parse-page');
module.exports = save;
/**
* @param {string} urlOrMediaId
* @return {Promise}
*/
function save(urlOrMediaId, dir) {
return new Promise((resolve, reject) => {
const url = createUrl(urlOrMediaId);
parsePage(url)
.then(post => {
const downloadUrl = post.downloadUrl;
const filename = post.filename;
const isVideo = post.isVideo;
const mimeType = post.mimeType;
const file = `${dir}/${filename}`;
downloadAndSave(downloadUrl, file).then(() => {
return resolve({
file,
mimeType,
url,
label: isVideo ? 'video' : 'photo',
source: downloadUrl
});
});
})
.catch(err => {
return reject({
url,
error: err,
message: `Download failed`
});
});
});
}