-
Notifications
You must be signed in to change notification settings - Fork 1
/
images.js
95 lines (82 loc) · 2.48 KB
/
images.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
const fs = require("fs");
const currentDirectory = "images";
const imageDirectory = __dirname + "/" + currentDirectory;
async function loadImages(directoryPath) {
const dirEntries = await fs.promises.readdir(directoryPath, {
withFileTypes: true
});
const options = [];
const thisDir = directoryPath.replace(/^.*[\\\/]/, "");
const quizPage = {
field: thisDir,
options: []
};
for (const entry of dirEntries) {
if (entry.isFile()) {
// if config file, add config data to quizPage
if (entry.name.includes("config.json")) {
const configData = require(directoryPath + "/config.json");
quizPage.position = configData.position;
quizPage.title = configData.title;
quizPage.submitText = configData.submitText;
}
// else, add image name to options array
else {
const nameOfFile = entry.name.replace(".png", "");
quizPage.options.push({ name: nameOfFile });
}
}
if (entry.isDirectory()) {
const returned = await loadImages(imageDirectory + "/" + entry.name);
quizPage.options = quizPage.options.concat(returned);
}
}
// if we are in top level recursion, don't return images field
if (quizPage.field === currentDirectory) {
return quizPage.options;
}
return [quizPage];
}
async function getAllFiles(directoryPath) {
const dirEntries = await fs.promises.readdir(directoryPath, {
withFileTypes: true
});
let files = [];
// console.log(dirEntries);
for (const entry of dirEntries) {
// console.log(entry);
// if the entry is a file and is a png, save it to database
if (entry.isFile() && entry.name.includes(".png")) {
/** create image to be save to DB, using:
* 1) name of file w/o extenstion
* 2) binary image data
*/
const nameOfFile = entry.name.replace(".png", "");
const image = {
name: nameOfFile,
image: fs.readFileSync(directoryPath + "/" + entry.name)
};
files.push(image);
}
// if directory, recurse
if (entry.isDirectory()) {
files = files.concat(await getAllFiles(directoryPath + "/" + entry.name));
}
}
return files;
}
async function load() {
const pageData = await loadImages(imageDirectory);
// sort page data by position
pageData.sort((p1, p2) => {
return p1.position < p2.position ? -1 : 1;
});
return pageData;
}
async function databaseLoad() {
return await getAllFiles(imageDirectory);
}
module.exports = {
load,
databaseLoad
};