-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-item-image.js
60 lines (50 loc) · 1.23 KB
/
upload-item-image.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
var fs = require('fs');
var imgur = require('imgur');
imgur.setClientId('e7fb665e6e4ffd4');
imgur.setAPIUrl('https://api.imgur.com/3/');
getFilesByTypeAsync('assets/featured_images/','.jpg')
.then((files)=>{
console.log(files);
//upload to imgur
return new Promise(
(resolve,reject)=>{
var imgurUrls = [];
var finals = Promise.all(files.map(imgurUploadAsync));
finals.then((data)=>{
console.log(data);
});
});
})
.then((data)=>{
// console.log(data);
});
/*---------- FUNCTIONS ----------*/
function imgurUploadAsync(path) {
return new Promise(
(resolve,reject)=>{
imgur.uploadFile(path)
.then(function (json) {
console.log(json.data.link);
resolve(json.data.link);
})
.catch(function (err) {
console.error(err.message);
reject(err.message);
});
});
}
function getFilesByTypeAsync(path,extension){
return new Promise(
(resolve,reject)=>{
var validFiles = [];
fs.readdir(path, (err, files) => {
if(err) reject(err);
files.forEach(file => {
if(file.indexOf(extension)>0){
validFiles.push(path+file);
}
});
resolve(validFiles);
});
});
}