|
|
@@ -0,0 +1,66 @@
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+const mime = require("mime-types");
|
|
|
+const md5Stats = require('./md5sum.js').stats;
|
|
|
+
|
|
|
+const MANAGED_FILES = [ ".cr2" ]; // TODO
|
|
|
+const BUFFER_MAXSIZE = 100;
|
|
|
+
|
|
|
+function File(fullPath, name) {
|
|
|
+ this.name = name;
|
|
|
+ this.path = fullPath;
|
|
|
+ this.checksum = null;
|
|
|
+ this.mimeType = null;
|
|
|
+ this.isMedia = null;
|
|
|
+}
|
|
|
+
|
|
|
+File.prototype.enrich = async function() {
|
|
|
+ this.mimeType = mime.lookup(this.name) || "";
|
|
|
+ const lowerName = this.name.toLowerCase();
|
|
|
+ this.isMedia = this.mimeType.startsWith("image/") || this.mimeType.startsWith("video/") || !!MANAGED_FILES.find(i => lowerName.endsWith(i));
|
|
|
+ this.checksum = this.isMedia ? await md5Stats(this.path) : "";
|
|
|
+}
|
|
|
+
|
|
|
+async function Library_doUpdate(lib) {
|
|
|
+ if (lib.buff.length === 0)
|
|
|
+ return;
|
|
|
+ await Promise.allSettled(lib.buff.map(i => i.enrich()));
|
|
|
+ lib.buff = lib.buff.filter(i => !!i.checksum);
|
|
|
+ // TODO update
|
|
|
+ lib.foundMedias = lib.foundMedias.concat(lib.buff);
|
|
|
+ lib.buff = [];
|
|
|
+}
|
|
|
+
|
|
|
+async function Library_depthupdate(library, dir) {
|
|
|
+ for (let o of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
|
+ const fullPath = path.join(dir, o.name);
|
|
|
+ if (o.isDirectory())
|
|
|
+ await Library_depthupdate(library, fullPath, library.buff);
|
|
|
+ else if (o.isFile()) {
|
|
|
+ library.buff.push(new File(fullPath, o.name));
|
|
|
+ if (library.buff.length >= BUFFER_MAXSIZE)
|
|
|
+ await Library_doUpdate(library);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function Library_isValid(_this) {
|
|
|
+ return fs.existsSync(_this.path);
|
|
|
+}
|
|
|
+
|
|
|
+function Library(path) {
|
|
|
+ this.path = path;
|
|
|
+}
|
|
|
+
|
|
|
+Library.prototype.updateLibrary = async function() {
|
|
|
+ console.log(`Starting update of library ${this.path}`);
|
|
|
+ this.foundMedias = [];
|
|
|
+ this.buff = [];
|
|
|
+ if (Library_isValid(this))
|
|
|
+ await Library_depthupdate(this, this.path);
|
|
|
+ await Library_doUpdate(this)
|
|
|
+ console.log(`Done updating library ${this.path}. Managed ${this.foundMedias.length} media files.`);
|
|
|
+}
|
|
|
+
|
|
|
+module.exports.Library = Library;
|
|
|
+
|