From 4b9ba287392aad7f5e6d5e4657a183ca8a7623fe Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Thu, 14 Apr 2022 22:52:38 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Tweaking=20the=20file=20copy=20e?= =?UTF-8?q?nd=20detector=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/api.service.ts | 52 ++++++++++++--------------------- services/importqueue.service.ts | 18 ++---------- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/services/api.service.ts b/services/api.service.ts index 8a362d0..0a63d5a 100644 --- a/services/api.service.ts +++ b/services/api.service.ts @@ -140,16 +140,14 @@ export default class ApiService extends Service { }, } ); - const fileCopyDelaySeconds = 30; - const checkFileCopyComplete = (path, previousPath) => { + const fileCopyDelaySeconds = 3; + const checkEnd = (path, prev) => { fs.stat(path, async (err, stat) => { - if (err) { - throw err; - } - if ( - stat.mtime.getTime() === - previousPath.mtime.getTime() - ) { + // Replace error checking with something appropriate for your app. + if (err) throw err; + if (stat.mtime.getTime() === prev.mtime.getTime()) { + console.log("finished"); + // Move on: call whatever needs to be called to process the file. console.log( "File detected, starting import..." ); @@ -161,21 +159,21 @@ export default class ApiService extends Service { "importqueue.processImport", { fileObject: { - filePath: walkedFolder[0].filePath, + filePath: path, fileSize: walkedFolder[0].fileSize, }, } ); - } else { + } else setTimeout( - checkFileCopyComplete, - fileCopyDelaySeconds * 1000, + checkEnd, + fileCopyDelaySeconds, path, stat ); - } }); }; + fileWatcher .once("add", (path, stats) => { console.log("Watcher detected new files."); @@ -187,29 +185,17 @@ export default class ApiService extends Service { )}` ); - console.log("File copy started..."); - fs.stat(path, (err, stat) => { - if (err) { - console.log( - "Error watching file for copy completion. ERR: " + - err.message - ); - console.log( - "Error file not processed. PATH: " + - path - ); - throw err; - } + console.log("File", path, "has been added"); + + fs.stat(path, function (err, stat) { + // Replace error checking with something appropriate for your app. + if (err) throw err; setTimeout( - checkFileCopyComplete, - fileCopyDelaySeconds * 1000, + checkEnd, + fileCopyDelaySeconds, path, stat ); - client.emit("action", { - type: "LS_COMIC_ADDED", - result: path, - }); }); }) // .once( diff --git a/services/importqueue.service.ts b/services/importqueue.service.ts index 453c2b5..8a83a01 100644 --- a/services/importqueue.service.ts +++ b/services/importqueue.service.ts @@ -75,13 +75,10 @@ export default class QueueService extends Service { const result = await extractFromArchive( job.data.fileObject.filePath ); - Object.assign(result, { - fileSize: job.data.fileObject.fileSize, - }); + const { name, filePath, - fileSize, extension, cover, containedIn, @@ -113,7 +110,7 @@ export default class QueueService extends Service { rawFileDetails: { name, filePath, - fileSize, + fileSize: job.data.fileObject.fileSize, extension, containedIn, cover, @@ -218,17 +215,6 @@ export default class QueueService extends Service { } ); - await this.getQueue("process.import").on( - "drained", - async (data) => { - client.emit("action", { - type: "LS_QUEUE_DRAINED", - result: data, - }); - console.log("Drained", data); - return data; - } - ); }); }, });