/
functions.js
134 lines (124 loc) · 5.07 KB
/
functions.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
const fs = require('fs');
const path = require('path')
const sharp = require('sharp')
const thumbnail_dir = __dirname + "/public/thumbnails/";
console.log(thumbnail_dir)
checkThumbailDir(thumbnail_dir);
function checkThumbailDir(thumbnailPath) {
if (!fs.existsSync(thumbnailPath)) {
fs.mkdirSync(thumbnailPath);
}
}
function generateGalleryThumbnails(imageDataArray) {
imageDataArray.forEach((imageData) => {
if (!imageData.thumbnailDir || !imageData.thumbnailName || !imageData.name || !imageData.fullPath) return new Error("Missing required image data");
// let thumbnailLocation = "public/" + imageData.thumbnailDir + "/gallery_" + imageData.thumbnailName;
let thumbnailLocation = thumbnail_dir + "gallery_" + imageData.thumbnailName;
console.log("Gallery thumbnail location: " + thumbnailLocation);
if (!fs.existsSync(thumbnailLocation)) {
console.log("Preparing file write for " + thumbnailLocation);
sharp(imageData.fullPath)
.resize({ height: 200 })
.toFile(thumbnailLocation)
.then(data => {
console.log(`Thumbnail generation complete (${imageData.name})`);
});
} else {
console.log(`File exists (${imageData.name}) (${thumbnailLocation})`);
}
})
}
function generateLatestThumbnail(imageData) {
if (!imageData.thumbnailDir || !imageData.thumbnailName || !imageData.name || !imageData.fullPath) return new Error("Missing required image data");
// let thumbnailLocation = "public/" + imageData.thumbnailDir + "/latest_" + imageData.thumbnailName;
let thumbnailLocation = thumbnail_dir + "latest_" + imageData.thumbnailName;
console.log("Latest thumbnail location: " + thumbnailLocation);
if (!fs.existsSync(thumbnailLocation)) {
console.log("Preparing file write for " + thumbnailLocation);
sharp(imageData.fullPath)
.resize({ height: 1000 })
.toFile(thumbnailLocation)
.then(data => {
console.log(`Latest image generation complete (${imageData.name})`);
});
} else {
console.log(`File exists (${imageData.name}) (${thumbnailLocation})`);
}
}
function getSeeds(imageDirectory) {
return new Promise(function (resolve, reject) {
getSeedImages(imageDirectory, null, imageDirectory)
.then(data => {
generateGalleryThumbnails(data);
resolve(data);
})
.catch(err => {
console.log(err)
reject(err)
});
})
}
function getLatestImage(imageDirectory) {
return new Promise(function (resolve, reject) {
getSeedImages(imageDirectory, null, imageDirectory)
.then(data => {
let latestImage = data[0];
console.log(latestImage)
generateLatestThumbnail(latestImage);
resolve(latestImage);
})
.catch(err => {
console.log(err)
reject(err)
});
})
}
function getSeedImages(dirPath, imagesArray, imageDirectory) {
return new Promise(function (resolve, reject) {
imagesArray = imagesArray || [];
let files = fs.readdirSync(dirPath);
files.forEach((file) => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
getSeedImages(dirPath + "/" + file, imagesArray, imageDirectory)
.then(data => {
imagesArray = data
})
.catch(err => {
console.log(err)
reject(err)
});
} else {
let ext = path.extname(file).split(".")[1];
if (ext == "jpeg" || ext == "jpg" || ext == "png") {
let fullPath = dirPath + '/' + file;
let imageUriPath = dirPath.split(imageDirectory)[1] + "/" + file;
let stats = fs.statSync(fullPath);
let size = Math.round(((stats.size / 1024 / 1024) + Number.EPSILON) * 100) / 100;
let imageData = {
name: file,
created: stats.birthtime,
modified: stats.mtime,
size: size + "MB",
imagePath: imageUriPath,
fullPath: fullPath,
thumbnailDir: "thumbnails",
thumbnailName: file,
directory: stats.isDirectory(),
file: stats.isFile(),
symlink: stats.isSymbolicLink()
}
imagesArray.push(imageData);
}
}
})
let imagesArraySorted = imagesArray.sort(function (a, b) {
return new Date(b.created) - new Date(a.created);
});
let reducedArray = imagesArraySorted.slice(0, 102);
resolve(reducedArray);
});
}
module.exports = {
getSeeds,
getLatestImage
}