-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
321 lines (240 loc) · 9.04 KB
/
server.js
File metadata and controls
321 lines (240 loc) · 9.04 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import express from 'express';
import os from 'os';
import fs from 'fs';
import path from 'path';
import { exec } from 'child_process';
import ExifReader from 'exifreader';
import { v4 as uuidv4 } from 'uuid';
import multer from 'multer'
// Setup express
const app = express();
app.use(express.json());
app.use(express.urlencoded());
// Backend path to data
const dataRootPath = './data';
// Frontend path to data
const dataRootPathFrontend = dataRootPath.substring(1);
// Multer storage
const storage = multer.diskStorage({
destination: function(req, file, cb) {
const directory = req.body.directory_path;
cb(null, directory);
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
});
// Multer upload
const upload = multer({ storage: storage });
// Multer upload function
function uploadFiles(req, res, next) {
let numFiles = req.files.length;
let uploadedFiles = [];
req.files.forEach(function (file) {
// Do something with each file, like moving it to a custom directory
// and pushing its new path to the `uploadedFiles` array.
// You can also handle any errors that occur during the upload process.
uploadedFiles.push(file.path);
numFiles--;
// Log upload progress
console.log(`${numFiles+1} files left to upload.`);
console.log(`File uploaded: ${file.path}`);
if (numFiles === 0) {
// All files have been uploaded, so call the next middleware function
// or route handler, passing along any relevant data or results.
req.uploadedFiles = uploadedFiles;
console.log("all files uploaded")
next();
}
});
}
////////////////////////////////////////
///// Create missing caption files /////
////////////////////////////////////////
function createMissingCaptionFiles(directory){
const imageFiles = [];
const captionFiles = [];
fs.readdirSync(directory).forEach((file) => {
const filePath = path.join(directory, file);
// Check if file is an image
if (fs.statSync(filePath).isFile() && ['.jpg', '.jpeg', '.png', '.gif'].includes(path.extname(filePath).toLowerCase())) {
console.log(filePath)
imageFiles.push(file);
const captionFilePath = path.join(directory, `${path.parse(filePath).name}.txt`);
console.log("captionFilePath:", captionFilePath)
// Check if caption file already exists
if (!fs.existsSync(captionFilePath)) {
// Write empty caption to file
fs.writeFileSync(captionFilePath, '');
console.log(`Caption file created for ${file}.`);
} else {
captionFiles.push(file);
console.log(`Caption file already exists for ${file}.`);
}
}
});
// Check if image files and caption files are equal
if (imageFiles.length !== captionFiles.length) {
console.log('Image files and caption files are not equal.');
console.log('Image files:', imageFiles.length);
console.log('Caption files:', captionFiles.length);
}
}
// Set captions based on filename
function setCaptionsBasedOnFilename(directory){
fs.readdirSync(directory).forEach((file) => {
const filePath = path.join(directory, file);
// Check if file is an image
if (fs.statSync(filePath).isFile() && ['.txt'].includes(path.extname(filePath).toLowerCase())) {
let tempCaption = `${path.parse(filePath).name}`
// Remove parentheses and content
const parenthesesAndContentRegex = /\([^()]*\)/g;
tempCaption = tempCaption.replace(parenthesesAndContentRegex, '')
// Write caption to file
fs.writeFileSync(filePath, tempCaption);
console.log("tempCaption:", tempCaption)
}
});
}
function createImageCaptionPairsData(directory) {
// Get image-caption-pairs
const imageCaptionPairs = fs.readdirSync(directory)
.filter(file => /\.(jpe?g|png|gif)$/i.test(file))
.map(imageFile => ({
image_path: path.join(directory, imageFile),
caption_path: path.join(directory, `${imageFile.split('.')[0]}.txt`),
id: uuidv4(),
// exif data of image
exif: ExifReader.load(fs.readFileSync(path.join(directory, imageFile)))
}));
// Get caption content
const pairsWithCaptions = imageCaptionPairs.map(pair => {
const captionContent = fs.readFileSync(pair.caption_path, 'utf-8');
return { ...pair, caption_content: captionContent };
});
return pairsWithCaptions;
}
////////////////////////////////////////
///////////// API Endpoints ////////////
////////////////////////////////////////
// get image-caption-pairs from directory
app.get('/image-caption-pairs?:directory', (req, res) => {
const directory = req.query.directory;
// Create missing caption files
createMissingCaptionFiles(directory)
// Get image caption pairs
const imageCaptionPairs = createImageCaptionPairsData(directory)
// Send image caption pairs
res.json(imageCaptionPairs);
});
// Update single caption file
app.post('/update-caption', (req, res) => {
console.log('POST RESPONSE',req.body)
const { caption_path, caption_content } = req.body;
fs.writeFileSync(caption_path, caption_content);
res.sendStatus(200);
});
// Get directories in directoryPath and each directory name
app.get('/directories', (req, res) => {
// Get directories in directoryPath and each directory name and count of image files
const directories = fs.readdirSync(dataRootPath)
.filter(file => fs.statSync(path.join(dataRootPath, file)).isDirectory())
.map(directory => ({
directory_path: path.join(dataRootPath, directory),
directory_name: directory,
image_count: fs.readdirSync(path.join(dataRootPath, directory)).filter(file => /\.(jpe?g|png|gif)$/i.test(file)).length,
}));
res.json(directories);
});
// Endpoint to set captions based on filename
app.get('/add-filenames-to-captions?:directory', (req, res) => {
const directory = req.query.directory;
// Set captions based on filename
setCaptionsBasedOnFilename(directory)
// Create missing caption files
createMissingCaptionFiles(directory)
// Get image caption pairs
const imageCaptionPairs = createImageCaptionPairsData(directory)
// Send image caption pairs
res.json(imageCaptionPairs);
});
// Endpoint to upload files to a directory
app.post('/save-files',upload.array('files'), uploadFiles, (req, res, next) => {
// Console uploaded files
console.log("req.uploadedFiles:", req.uploadedFiles)
// Get directory path
let directoryPath = req.body.directory_path
console.log("upload path:", directoryPath)
// Create missing caption files
createMissingCaptionFiles(directoryPath)
// Create new data
let newPairsData = createImageCaptionPairsData(directoryPath)
// Respons status ok
res.status(200);
// Respons with new data including new files
res.json(newPairsData);
});
// Endpoint to create a new directory in dataRootPath
app.post('/create-directory', (req, res) => {
// Get directory name
let directoryName = req.body.directory_name
// Create directory if it doesnt already exist
if (!fs.existsSync(path.join(dataRootPath, directoryName))){
fs.mkdirSync(path.join(dataRootPath, directoryName));
console.log(`Directory ${directoryName} created.`);
} else {
console.log(`Directory ${directoryName} already exists.`);
}
// Respons status ok
res.status(200);
res.json({directory_name: directoryName});
});
// Express route handler for opening the current directory in Finder
app.post('/open-folder', (req, res) => {
console.log("req.body:", req.body)
// Get the path from the request
const currentDir = req.body.directory_path;
// Get relative path of current directory
// Get absolute path of current directory
const absolutePath = path.resolve( currentDir );
console.log("absolutePath:", absolutePath)
// Get escaped absolute path of current directory
const escapedAbsolutePath = absolutePath.replace(/(\s+)/g, '\\$1');
console.log("escapedAbsolutePath:", escapedAbsolutePath)
// Use the `open` command to open the directory in Finder
if (os.platform() === 'darwin') {
console.log('mac open folder')
exec(`open ${escapedAbsolutePath}`, (err) => {
if (err) {
console.error(err);
res.status(500).send('Failed to open folder');
return;
}
// Send a success response if the command executed successfully
res.status(200).send('Folder opened');
});
} else if (os.platform() === 'win32') {
let command = `explorer /select, "${escapedAbsolutePath}"`
console.log('windows open folder', command)
exec(command, (err) => {
if (err) {
console.error(err);
res.status(500).send('Failed to open folder');
return;
}
// Send a success response if the command executed successfully
res.status(200).send('Folder opened');
});
} else {
res.status(500).send('Unsupported platform');
}
});
// Enable serving image files in directoryPath directory
app.use(dataRootPathFrontend, express.static(dataRootPath))
console.log(dataRootPath)
app.use(express.static('public'))
console.log(dataRootPath)
// Start Server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});