This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (149 loc) · 5.04 KB
/
index.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
require('v8-compile-cache');
const config = require('./config.json');
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
const fs = require('fs');
const fetch = require('node-fetch');
const cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: config.tokens.cloudinary.name,
api_key: config.tokens.cloudinary.key,
api_secret: config.tokens.cloudinary.secret
});
let win;
app.whenReady().then(() => {
win = new BrowserWindow({
width: 1000,
height: 800,
title: 'Mue Uploader',
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
frame: false
});
win.loadFile('public/index.html');
});
// ipc
let file;
let raw = false;
ipcMain.on('addFile', async (event) => {
let filePath = dialog.showOpenDialogSync({ properties: ['openFile'], filters: [{ name: 'Photos', extensions: ['jpg', 'jpeg', 'png', 'cr3', 'cr2', 'dng', 'raf', 'fff', 'rwl', 'nef', 'rw2', 'x3f', 'arw'] }] });
if (filePath === undefined) {
return;
}
file = filePath[0];
const extension = filePath[0].substring(filePath[0].lastIndexOf('.') + 1);
const rawTypes = ['cr3', 'cr2', 'dng', 'raf', 'fff', 'rwl', 'nef', 'rw2', 'x3f', 'arw'];
if (rawTypes.includes(extension.toLowerCase())) {
// only require modules when we need to
const extractd = require('extractd');
const jpg = await extractd.generate(filePath[0], {
destination: './'
});
file = jpg.preview;
raw = true;
}
let metadata;
try {
const exif = require('exifr');
metadata = await exif.parse(file);
} catch (e) {
return event.sender.send('message', 'Failed to get image data');
}
event.sender.send('addedFile', metadata, file);
});
let filename, category, id;
let busy = false;
ipcMain.on('upload', (event, arg) => {
busy = true;
const crypto = require('crypto');
filename = crypto.randomBytes(8).toString('hex');
// compress
const sharp = require('sharp');
sharp(fs.readFileSync(file), { quality: 85, reductionEffort: 6 })
.toFile(`./${filename}.webp`, (err) => {
if (err) {
return event.sender.send('message', 'Failed to compress image');
}
if (raw) {
fs.unlinkSync(file);
}
// upload
cloudinary.v2.uploader.upload(`./${filename}.webp`, {
folder: 'photos/' + arg.category.toLowerCase(),
use_filename: true
}, async (err, res) => {
fs.unlinkSync(`./${filename}.webp`);
if (err) {
return event.sender.send('message', 'Failed to upload image');
}
cloudinary_id = res.asset_id;
category = arg.category;
// add to db
const location = arg.location.charAt(0).toUpperCase() + arg.location.slice(1)
let data = await fetch(`${config.api_url}/images/add?filename=${filename}&photographer=${arg.photographer}&category=${arg.category}&location=${location}&camera=${arg.camera_model}`, {
headers: {
'Authorization': config.tokens.api
}
});
data = await data.json();
if (data.status === 500) {
event.sender.send('message', 'Failed to add image to database');
} else {
id = data.id;
event.sender.send('message', 'Uploaded successfully');
}
busy = false;
});
});
});
ipcMain.on('undo', (event) => {
busy = true;
cloudinary.v2.uploader.destroy('photos/' + category.toLowerCase() + '/' + filename, {
invalidate: true
}, async (err) => {
if (err) {
return event.sender.send('Failed to delete image');
}
let data = await fetch(`${config.api_url}/images/delete?id=${id}`, {
headers: {
'Authorization': config.tokens.api
}
});
data = await data.json();
if (data.status === 500) {
event.sender.send('message', 'Failed to delete image from database');
} else {
event.sender.send('message', 'Removed successfully');
}
busy = false;
});
});
ipcMain.on('titlebar', (_event, arg) => {
if (busy === true) {
return dialog.showErrorBox('Error', 'Cannot close while busy! Please wait until all actions have finished before closing.');
}
switch (arg) {
case 'minimize':
win.minimize();
break;
case 'maximise':
if (!win.isMaximized()) {
win.maximize();
} else {
win.unmaximize();
}
break;
case 'close':
// remove raw files if not uploaded
fs.readdirSync('./').forEach((file) => {
if (file.endsWith('.jpg')) {
fs.unlinkSync(file);
}
});
win.close();
break;
default:
break;
}
});