-
Notifications
You must be signed in to change notification settings - Fork 45
/
gallery.js
33 lines (25 loc) · 973 Bytes
/
gallery.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
const fs = require('fs');
const path = require('path');
const { loadImage } = require('canvas')
function Gallery(options) {
this.localImagesDirectory = options && options.localImagesDirectory;
this.images = {};
}
Gallery.prototype.preloadImages = function(images) {
const self = this;
const uriRegexp = /https?:\/\//;
//External images
var promises = images.filter((image) => image.match(uriRegexp))
.map((image) => loadImage(image).then((data) => self.images[image] = data));
if (this.localImagesDirectory) {
const localPromises = images.filter((image) => !image.match(uriRegexp))
.map((image) => loadImage(path.join(self.localImagesDirectory, image)).then((data) => self.images[image] = data));
promises = promises.concat(localPromises);
}
promises = promises.map((promise) => promise);
return Promise.all(promises);
}
Gallery.prototype.getImage = function(image) {
return this.images[image];
}
module.exports = Gallery;