|
@@ -2,7 +2,9 @@ const fs = require('fs');
|
|
|
const path = require('path');
|
|
const path = require('path');
|
|
|
const mime = require("mime-types");
|
|
const mime = require("mime-types");
|
|
|
const md5Stats = require('./md5sum.js').stats;
|
|
const md5Stats = require('./md5sum.js').stats;
|
|
|
|
|
+const FileTypeManager = require('./fileTypeManager.js');
|
|
|
|
|
|
|
|
|
|
+const MediaFileModel = require("../model/mediaItem.js").MediaFileModel;
|
|
|
const MANAGED_FILES = [ ".cr2" ]; // TODO
|
|
const MANAGED_FILES = [ ".cr2" ]; // TODO
|
|
|
const BUFFER_MAXSIZE = 100;
|
|
const BUFFER_MAXSIZE = 100;
|
|
|
|
|
|
|
@@ -12,6 +14,7 @@ function File(fullPath, name) {
|
|
|
this.checksum = null;
|
|
this.checksum = null;
|
|
|
this.mimeType = null;
|
|
this.mimeType = null;
|
|
|
this.isMedia = null;
|
|
this.isMedia = null;
|
|
|
|
|
+ this.meta = {};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
File.prototype.enrich = async function() {
|
|
File.prototype.enrich = async function() {
|
|
@@ -19,11 +22,15 @@ File.prototype.enrich = async function() {
|
|
|
const lowerName = this.name.toLowerCase();
|
|
const lowerName = this.name.toLowerCase();
|
|
|
this.isMedia = this.mimeType.startsWith("image/") || this.mimeType.startsWith("video/") || !!MANAGED_FILES.find(i => lowerName.endsWith(i));
|
|
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) : "";
|
|
this.checksum = this.isMedia ? await md5Stats(this.path) : "";
|
|
|
|
|
+ this.meta = await FileTypeManager.createMeta(this.path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function Library_doUpdate(lib) {
|
|
|
|
|
|
|
+async function Library_doUpdate(dbHelper, lib) {
|
|
|
if (lib.buff.length === 0)
|
|
if (lib.buff.length === 0)
|
|
|
return;
|
|
return;
|
|
|
|
|
+ const dbItems = await dbHelper.fetchRaw("path", MediaFileModel.prototype.getTableName.call(null), { "path": lib.buff.map(i => i.path) });
|
|
|
|
|
+ lib.buff = lib.buff.filter(i => dbItems.indexOf(i.path) === -1);
|
|
|
|
|
+ console.log(lib.buff);
|
|
|
await Promise.allSettled(lib.buff.map(i => i.enrich()));
|
|
await Promise.allSettled(lib.buff.map(i => i.enrich()));
|
|
|
lib.buff = lib.buff.filter(i => !!i.checksum);
|
|
lib.buff = lib.buff.filter(i => !!i.checksum);
|
|
|
// TODO update
|
|
// TODO update
|
|
@@ -31,15 +38,15 @@ async function Library_doUpdate(lib) {
|
|
|
lib.buff = [];
|
|
lib.buff = [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function Library_depthupdate(library, dir) {
|
|
|
|
|
|
|
+async function Library_depthupdate(dbHelper, library, dir) {
|
|
|
for (let o of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
for (let o of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
|
const fullPath = path.join(dir, o.name);
|
|
const fullPath = path.join(dir, o.name);
|
|
|
if (o.isDirectory())
|
|
if (o.isDirectory())
|
|
|
- await Library_depthupdate(library, fullPath, library.buff);
|
|
|
|
|
|
|
+ await Library_depthupdate(dbHelper, library, fullPath);
|
|
|
else if (o.isFile()) {
|
|
else if (o.isFile()) {
|
|
|
library.buff.push(new File(fullPath, o.name));
|
|
library.buff.push(new File(fullPath, o.name));
|
|
|
if (library.buff.length >= BUFFER_MAXSIZE)
|
|
if (library.buff.length >= BUFFER_MAXSIZE)
|
|
|
- await Library_doUpdate(library);
|
|
|
|
|
|
|
+ await Library_doUpdate(dbHelper, library);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -52,13 +59,13 @@ function Library(path) {
|
|
|
this.path = path;
|
|
this.path = path;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-Library.prototype.updateLibrary = async function() {
|
|
|
|
|
|
|
+Library.prototype.updateLibrary = async function(dbHelper) {
|
|
|
console.log(`Starting update of library ${this.path}`);
|
|
console.log(`Starting update of library ${this.path}`);
|
|
|
this.foundMedias = [];
|
|
this.foundMedias = [];
|
|
|
this.buff = [];
|
|
this.buff = [];
|
|
|
if (Library_isValid(this))
|
|
if (Library_isValid(this))
|
|
|
- await Library_depthupdate(this, this.path);
|
|
|
|
|
- await Library_doUpdate(this)
|
|
|
|
|
|
|
+ await Library_depthupdate(dbHelper, this, this.path);
|
|
|
|
|
+ await Library_doUpdate(dbHelper, this)
|
|
|
console.log(`Done updating library ${this.path}. Managed ${this.foundMedias.length} media files.`);
|
|
console.log(`Done updating library ${this.path}. Managed ${this.foundMedias.length} media files.`);
|
|
|
}
|
|
}
|
|
|
|
|
|