-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.js
41 lines (35 loc) · 1.58 KB
/
populate.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
var url = require('url');
var path = require('path');
var sharp = require('sharp');
var fetch = require('./fetch.js')
var persistence = require('./persistence.js')
module.exports = {
database: function() {
persistence.refresh(populate_database);
}
};
function populate_database() {
fetch.json('http://54.152.221.29/images.json', function(data) {
for (let i = 0; i < data.images.length; i++) {
fetch.image(data.images[i].url, function (body) {
let original_url = data.images[i].url;
let original_basename = path.basename(url.parse(original_url).pathname);
let small_url = 'small_' + original_basename;
let medium_url = 'medium_' + original_basename;
let large_url = 'large_' + original_basename;
persistence.store_urls(original_url, small_url, medium_url, large_url);
let imageSmall = sharp(body).resize(320,240).toBuffer();
let imageMedium = sharp(body).resize(384,288).toBuffer();
let imageLarge = sharp(body).resize(640,480).toBuffer();
let promises = [imageSmall, imageMedium, imageLarge];
Promise.all(promises).then(values =>
{
persistence.store_image(small_url, values[0]);
persistence.store_image(medium_url, values[1]);
persistence.store_image(large_url, values[2]);
}
);
})
}
});
}