Ver código fonte

Missing files

isundil 2 anos atrás
pai
commit
821f7a436d
2 arquivos alterados com 87 adições e 0 exclusões
  1. 66 0
      src/library.js
  2. 21 0
      src/libraryManager.js

+ 66 - 0
src/library.js

@@ -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;
+

+ 21 - 0
src/libraryManager.js

@@ -0,0 +1,21 @@
+
+const CONFIG = require('./config.js');
+const Library = require('./library.js').Library;
+
+function LibraryManager() {
+    this.libraries = [];
+
+    for (let i of CONFIG.photoLibraries)
+        this.push(i);
+}
+
+LibraryManager.prototype.push = function(path) {
+    this.libraries.push(new Library(path));
+}
+
+LibraryManager.prototype.updateLibraries = function() {
+    return Promise.allSettled(this.libraries.map(i => i.updateLibrary()));
+}
+
+module.exports.LibraryManager = new LibraryManager();
+