-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
515 lines (415 loc) · 14.1 KB
/
main.js
File metadata and controls
515 lines (415 loc) · 14.1 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
require('dotenv').config(); //Manage environment variables
const db = require('./my_sqlite3.js'); // Initialize database
const { EPub } = require('epub2'); // Epub metadata extraction library
const coverPath = path.join(__dirname, 'images', 'cover_placeholder.png');
const coverURL = `file://${coverPath}`;
const libraryPath = path.join(__dirname, 'library');
const os = require('os');
console.log(libraryPath)
ipcMain.handle('get-epub-buffer', async (event, filePath) => {
const buffer = fs.readFileSync(filePath);
return buffer.buffer; // Return ArrayBuffer
});
function openReaderWindow(bookPath) {
const win = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
},
});
win.webContents.openDevTools();
// Load reader.html and pass the book path via query param
win.loadFile('reader.html', {
query: { path: bookPath },
});
}
// IPC handler to open reader
ipcMain.handle('open-reader-window', (event, filePath) => {
openReaderWindow(filePath);
});
ipcMain.handle('handle-read-epub', async (event, filePath) => {
const buffer = fs.readFileSync(filePath);
return buffer.buffer; // Return ArrayBuffer to renderer
});
//Creates the main window of the application
function createWindow () {
const win = new BrowserWindow({
width: parseInt(process.env.WINDOW_WIDTH) || 800,
height: parseInt(process.env.WINDOW_HEIGHT) || 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
win.loadFile('index.html');
// Only open DevTools if enabled in environment
if (process.env.DEV_TOOLS === 'true') {
win.webContents.openDevTools();
}
}
// Handle database operations in the main process
ipcMain.handle('insert-user', (event, name) => {
try {
const stmt = db.prepare('INSERT INTO users (name) VALUES (?)');
const result = stmt.run(name);
return result.lastInsertRowid;
} catch (err) {
console.error('Error inserting user:', err);
throw err;
}
});
ipcMain.handle('get-cover-placeholder', () => {
try {
return `${coverURL}`
} catch (err) {
console.error('Error getting cover placeholder:', err);
}
});
ipcMain.handle('get-users', () => {
try {
const stmt = db.prepare('SELECT * FROM users');
return stmt.all();
} catch (err) {
console.error('Error getting users:', err);
throw err;
}
});
async function addBook(bookData){
try {
// bookData should include:
// title, author, description, filePath, coverPath, coverImage
console.log(`Adding book: ${bookData.title} by ${bookData.author}`)
const libraryBase = path.join(__dirname, 'library');
// Construct the directory path: library/author/title
const targetDir = path.join(libraryBase, bookData.author, bookData.title);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
// Create the file name: title - author.epub
const fileName = `${bookData.title} - ${bookData.author}.epub`;
const destPath = path.join(targetDir, fileName);
console.log(`Dest path: ${destPath}`);
// Copy the file if it doesn't already exist in the library
if (!fs.existsSync(destPath)) {
fs.copyFileSync(bookData.filePath, destPath);
}
// Process cover file if provided; otherwise, use a default cover image from assets
let finalCoverPath = null;
let dataUrl;
if (bookData.cover) {
let matches = null;
// Expecting coverImage to be a data URL like "data:image/png;base64,..."
// If coverImage is an object, convert it to a data URL string.
if (typeof bookData.cover === 'object' && bookData.cover.data && bookData.cover.mimeType) {
dataUrl = `data:${bookData.cover.mimeType};base64,${bookData.cover.data}`;
} else if (typeof bookData.cover === 'string') {
dataUrl = bookData.cover;
}
if(dataUrl){
matches = dataUrl.match(/^data:(.+);base64,(.+)$/);
}
if (matches) {
const mimeType = matches[1]; // e.g., image/png
const base64Data = matches[2];
const coverBuffer = Buffer.from(base64Data, 'base64');
// Determine file extension from mime type
const extension = mimeType.split('/')[1] || 'png';
const coverFileName = `${bookData.title} - ${bookData.author}.${extension}`;
finalCoverPath = path.join(targetDir, coverFileName);
// Write the cover image file
fs.writeFileSync(finalCoverPath, coverBuffer);
} else {
console.error('Invalid cover image data URL');
}
} else if (bookData.coverPath) {
// User provided a cover path - saves that file to the library
const coverFileExt = path.extname(bookData.coverPath);
const coverFileName = `${bookData.title} - ${bookData.author}${coverFileExt}`;
finalCoverPath = path.join(targetDir, coverFileName);
if (!fs.existsSync(finalCoverPath)) {
fs.copyFileSync(bookData.coverPath, finalCoverPath);
}
} else {
// Use a default cover if no cover provided
const defaultCoverSource = path.join(__dirname, 'assets', 'default_cover.png');
const coverFileName = `${bookData.title} - ${bookData.author}.png`;
finalCoverPath = path.join(targetDir, coverFileName);
if (fs.existsSync(defaultCoverSource) && !fs.existsSync(finalCoverPath)) {
fs.copyFileSync(defaultCoverSource, finalCoverPath);
}
} // End of cover file processing
console.log(`Final cover file path: ${finalCoverPath}`);
const stmt = db.prepare(`
INSERT INTO books (title, author, file_path, cover_path, description)
VALUES (?, ?, ?, ?, ?)
`);
const result = stmt.run(
bookData.title,
bookData.author,
destPath,
finalCoverPath || null,
bookData.description || null
);
return result.lastInsertRowid;
} catch (err) {
console.error('Error adding book:', err);
throw err;
}
}
// Book-related operations
ipcMain.handle('add-book', (event, bookData) => {
result = addBook(bookData);
return result;
});
ipcMain.handle('get-books', () => {
try {
const stmt = db.prepare('SELECT * FROM books ORDER BY added_date DESC');
return stmt.all();
} catch (err) {
console.error('Error getting books:', err);
throw err;
}
});
async function extractMetaData(filePath){
try {
console.log("Creating EPUB instance for:", filePath);
const epub = await EPub.createAsync(filePath, '/images/', '/chapters/');
const metadata = {
title: epub.metadata?.title || null,
author: epub.metadata?.creator || null,
language: epub.metadata?.language || null,
subject: epub.metadata?.subject || null,
description: epub.metadata?.description || null,
date: epub.metadata?.date || null,
raw: epub.metadata[EPub.SYMBOL_RAW_DATA] || null,
};
// Extract cover image if available
if (epub.metadata.cover) {
metadata.cover = await new Promise((resolve, reject) => {
epub.getImage(epub.metadata.cover, (err, data, mimeType) => {
if (err) {
console.error("Error extracting cover image:", err);
return reject(err);
}
// Convert the Buffer to a base64 string for IPC transfer.
resolve({
data: data.toString('base64'),
mimeType: mimeType,
});
});
});
} else {
metadata.cover = null;
}
return metadata;
} catch (err) {
console.error("Failed to extract metadata", err);
throw err;
}
}
ipcMain.handle('extract-metadata', async (event, filePath) => {
const metaData = extractMetaData(filePath);
console.log("Extracted metadata");
return metaData;
});
ipcMain.handle('get-book-cover', async (event, bookId) => {
try {
// Query the database for the cover_path of the book
const stmt = db.prepare('SELECT cover_path FROM books WHERE id = ?');
const row = stmt.get(bookId);
if (!row || !row.cover_path) {
// No cover image exists for this book
return null;
}
const coverPath = row.cover_path;
// Check if the cover file actually exists
if (!fs.existsSync(coverPath)) {
return null;
}
// Read the cover file as a Buffer
const coverBuffer = fs.readFileSync(coverPath);
// Determine the MIME type from the file extension
const ext = path.extname(coverPath).toLowerCase();
let mimeType = 'image/png'; // default MIME type
if (ext === '.jpg' || ext === '.jpeg') {
mimeType = 'image/jpeg';
} else if (ext === '.gif') {
mimeType = 'image/gif';
}
// Convert the Buffer to a Base64 string and construct the data URL
const base64Data = coverBuffer.toString('base64');
const dataUrl = `data:${mimeType};base64,${base64Data}`;
return dataUrl;
} catch (err) {
console.error('Error in get-book-cover:', err);
throw err;
}
});
ipcMain.handle('get-book-by-id', (event, id) => {
try {
const stmt = db.prepare('SELECT * FROM books WHERE id = ?');
return stmt.get(id);
} catch (err) {
console.error('Error getting book:', err);
throw err;
}
});
ipcMain.handle('update-book', (event, id, bookData) => {
try {
const stmt = db.prepare(`
UPDATE books
SET title = ?, author = ?, description = ?, cover_path = ?
WHERE id = ?
`);
const result = stmt.run(
bookData.title,
bookData.author,
bookData.description || null,
bookData.coverPath || null,
id
);
return result.changes > 0;
} catch (err) {
console.error('Error updating book:', err);
throw err;
}
});
ipcMain.handle('handle-epub-drag', async (event, fileData) => {
if (fileData.path) {
result = extractMetaData(fileData.path)
return { status: 'success', type: 'path' };
//Epub extractor library requires a filepath
//If we have data instead, we should save that data as a temporary file and use it to extract metadata before saving the file to the library
} else if (fileData.data) {
const arrayBuffer = fileData.data;
console.log("EPUB loaded in browser. Length:", arrayBuffer.byteLength);
// Generate a temporary file path.
const tempDir = os.tmpdir();
const tempFilePath = path.join(tempDir, `temp-${Date.now()}.epub`);
// Write the ArrayBuffer to the temporary file.
fs.writeFileSync(tempFilePath, Buffer.from(arrayBuffer));
// Now extract the metadata using the temporary file path.
let bookData;
try {
bookData = await extractMetaData(tempFilePath);
if (!bookData) {
throw new Error("Metadata extraction returned undefined");
}
} catch (error) {
console.error("Error extracting metadata:", error);
return { status: 'error', message: 'Metadata extraction failed' };
}
bookData.filePath = tempFilePath;
// console.log("bookdata = " + JSON.stringify(bookData, null, 2));
const bookId = await addBook(bookData);
// Optionally, delete the temporary file if it's no longer needed:
fs.unlinkSync(tempFilePath);
return { status: 'success', type: 'data', metadata: bookData, bookId };
} else {
return { status: 'error', message: 'Invalid file data' };
}
});
ipcMain.handle('delete-book', (event, id) => {
try {
const stmt = db.prepare('DELETE FROM books WHERE id = ?');
const result = stmt.run(id);
return result.changes > 0;
} catch (err) {
console.error('Error deleting book:', err);
throw err;
}
});
// Tag-related operations
ipcMain.handle('add-tag', (event, name) => {
try {
const stmt = db.prepare('INSERT OR IGNORE INTO tags (name) VALUES (?)');
const result = stmt.run(name);
if (result.changes === 0) {
// Tag already exists, get its ID
const getStmt = db.prepare('SELECT id FROM tags WHERE name = ?');
return getStmt.get(name).id;
}
return result.lastInsertRowid;
} catch (err) {
console.error('Error adding tag:', err);
throw err;
}
});
ipcMain.handle('get-tags', () => {
try {
const stmt = db.prepare('SELECT * FROM tags ORDER BY name');
return stmt.all();
} catch (err) {
console.error('Error getting tags:', err);
throw err;
}
});
ipcMain.handle('add-tag-to-book', (event, bookId, tagId) => {
try {
const stmt = db.prepare('INSERT OR IGNORE INTO book_tags (book_id, tag_id) VALUES (?, ?)');
const result = stmt.run(bookId, tagId);
return result.changes > 0;
} catch (err) {
console.error('Error adding tag to book:', err);
throw err;
}
});
ipcMain.handle('get-book-tags', (event, bookId) => {
try {
const stmt = db.prepare(`
SELECT t.* FROM tags t
JOIN book_tags bt ON t.id = bt.tag_id
WHERE bt.book_id = ?
ORDER BY t.name
`);
return stmt.all(bookId);
} catch (err) {
console.error('Error getting book tags:', err);
throw err;
}
});
// File selection operations
ipcMain.handle('select-epub-file', async () => {
//return the selected epub's path
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'EPUB Files', extensions: ['epub'] }]
});
if (canceled || filePaths.length === 0) {
return null;
}
const sourcePath = filePaths[0];
return {sourcePath};
});
ipcMain.handle('select-cover-image', async () => {
//returns the selected image's path
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Image Files', extensions: ['jpg', 'jpeg', 'png', 'gif'] }]
});
if (canceled || filePaths.length === 0) {
return null;
}
return filePaths[0];
});
app.whenReady().then(createWindow);
app.on('before-quit', () => {
if (serverProcess) {
serverProcess.kill();
serverProcess = null;
}
});
app.on('window-all-closed', () => {
//This checks if the platform is NOT macOS 'darwin' is the identifier for macOS in Node.js
if (process.platform !== 'darwin') {
db.close();
app.quit();
}
});