generated from srsergiorodriguez/serie-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serieRunTasks.js
221 lines (184 loc) · 6.26 KB
/
serieRunTasks.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sharp from 'sharp';
import lunr from 'lunr';
import fs from 'fs/promises';
import cliProgress from 'cli-progress';
import { createReadStream, readdirSync, existsSync } from 'fs';
import serieConfig from './data/serie.config.js';
import process from 'process';
import csvParser from 'csv-parser';
// Puedo hacer la cli acá para determinar tareas
const mode = process.argv[2];
const localBase = "http://localhost:5173"; // Puedo dejar una opción de escoger el host
const localBuild = "http://localhost:5500";
const configFile = "serie.config.js";
const metadataFile = "metadata.csv";
const imagesFolder = "raw_images/";
const dataPath = "./data/";
const srcConfigPath = "./src/config/";
const staticDataPath = "./static/data/";
const derivativesPath = "./static/iiif/";
const thumbsPath = "./static/thumbs/";
const metadataPath = "./src/routes/data/metadata.js";
const indexPath = "./src/routes/data/searchIndex.js";
main();
async function main() {
// Delete the IIIF derivatives folder
await deleteCopiedData();
// Copy the config file to use its data in svelte
await copyData();
// Create the Collection: IIIF derivatives and adjunts metadata
await createCollection();
}
async function deleteCopiedData() {
if (existsSync(derivativesPath)) {
await fs.rm(derivativesPath, { recursive: true, force: true }, () => {});
}
if (existsSync(thumbsPath)) {
await fs.rm(thumbsPath, { recursive: true, force: true }, () => {});
}
if (existsSync(staticDataPath)) {
await fs.rm(staticDataPath, { recursive: true, force: true }, () => {});
}
if (existsSync(srcConfigPath)) {
await fs.rm(srcConfigPath, { recursive: true, force: true }, () => {});
}
processMsg("TASK: deleted previous files");
}
async function parseMetadata() {
const results = [];
await new Promise(r => {
createReadStream(dataPath + metadataFile)
.pipe(csvParser())
.on('data', data => results.push(data))
.on('end', () => r(results))
});
// VALIDATION
// Empty metadata
if (results.length <= 0) {
errorMsg("metadata", "Metadata is empty / Los metadatos están vacíos");
process.exit(1);
}
// Unique pids
if ([...new Set(results.map(d => d.pid))].length !== results.length) {
errorMsg("metadata", "There are non unique pids / Hay pids que no son únicos");
process.exit(1);
}
// Has pids and labels
for (let e of results) {
if (e.pid === undefined || e.label === undefined) {
errorMsg("metadata", "There are rows without pids or labels / Hay filas sin pids o labels");
process.exit(1);
}
}
return results
}
async function createCollection() {
const metadata = await parseMetadata();
await createMetadataJS(metadata);
await createSearchIndex(metadata);
await batchProcessImages(metadata);
processMsg("TASK: created collection elements");
}
async function createSearchIndex(metadata) {
const searchIndex = lunr(function () {
this.ref("pid");
const keysToIndex = serieConfig.pages.metadataToIndex.length <= 0 ? Object.keys(metadata[0]) : serieConfig.pages.metadataToIndex;
for (let k of keysToIndex) {
this.field(k);
}
for (let d of metadata) {
this.add(d);
}
})
await fs.writeFile(indexPath, `export const searchIndex = ${JSON.stringify(searchIndex)};`, (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
});
}
async function createMetadataJS(metadata) {
await fs.writeFile(metadataPath, `export const metadata = ${JSON.stringify(metadata)};`, (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
});
}
async function batchProcessImages(metadata) {
await mkDir(derivativesPath);
await mkDir(thumbsPath);
const imagesFilenames = readdirSync(dataPath + imagesFolder);
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
processMsg("Processing images...");
let i = 0;
progressBar.start(metadata.length, i);
for (let e of metadata) {
const regex = new RegExp(`^${e.pid}`);
const filename = imagesFilenames.find(d => regex.test(d));
if (filename === undefined) {
console.log(`Couldn't find corresponding image for ${e.pid}`);
}
// Check if valid image format
// Podría hacer esto de forma asíncrona?
await createThumbnails(`${dataPath}${imagesFolder}${filename}`, `${thumbsPath}${filename}`);
await createIIIFDerivatives(e.pid, `${dataPath}${imagesFolder}${filename}`);
i++;
progressBar.update(i);
}
progressBar.stop();
}
async function createThumbnails(inputPath, outputFolder) {
await sharp(inputPath).resize({
width: 256,
height: 256,
fit: 'cover'
})
.jpeg({
quality: 80
})
.toFile(outputFolder);
}
async function createIIIFDerivatives(pid, imageUrl) {
// Check if image format is valid
const outputFolder = `${derivativesPath}${pid}`;
const tileSize = 512; // Set the tile size
await generateIIIFLevel0ImageTiles(imageUrl, outputFolder, tileSize);
}
async function generateIIIFLevel0ImageTiles(inputPath, outputFolder, tileSize) {
// {scheme}://{server}{/prefix}/{identifier}/{region}/{size}/{rotation}/{quality}.{format}
await sharp(inputPath).tile({
size: tileSize,
layout: "iiif3",
id: mode === "dev" ? `${localBase}${serieConfig.baseurl}/iiif` : mode === "local" ? `${localBuild}/docs/iiif` : `${serieConfig.base}${serieConfig.baseurl}/iiif`
}).toFile(outputFolder, (err, info) => {
// console.log(info);
});
}
async function copyData() {
await mkDir(srcConfigPath);
await fs.copyFile(dataPath + configFile, srcConfigPath + configFile);
await mkDir(staticDataPath);
await fs.copyFile(dataPath + metadataFile, staticDataPath + metadataFile);
await mkDir(staticDataPath + imagesFolder);
const imagesFilenames = readdirSync(dataPath + imagesFolder);
for (let e of imagesFilenames) {
await fs.copyFile(dataPath + imagesFolder + e, staticDataPath + imagesFolder + e);
}
processMsg("TASK: copied new data");
}
async function mkDir(path) {
try {
await fs.mkdir(path);
} catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
}
function errorMsg(type, msg) {
console.error("\x1b[41m\x1b[30m%s\x1b[0m",`${type.toUpperCase()} ERROR: ${msg}`);
}
function processMsg(msg) {
console.log("\x1b[32m%s\x1b[0m", msg);
}