isundil hai 1 ano
pai
achega
a3081f8e46

+ 6 - 2
main.js

@@ -29,14 +29,18 @@ App.prototype.init = async function() {
         require('./model/mediaItem.js').MediaFileModel,
         require('./model/mediaItemMeta.js').MediaFileMetaModel,
         require('./model/mediaItemTag.js').MediaFileTagModel,
-        require('./model/access.js').AccessModel
+        require('./model/access.js').AccessModel,
+        require('./model/autotag.js').AutotagModel
     ]);
 }
 
 App.prototype.run = async function() {
     http.createServer(this.router).listen(CONFIG.port);
-    this.libraryManager.updateLibraries(this.databaseHelper);
+    await this.libraryManager.updateLibraries(this.databaseHelper);
     setInterval(() => { this.libraryManager.updateLibraries(this.databaseHelper); }, UPDATE_INTERVAL);
+
+    let BuilderClass = require('./src/autotagBuilder');
+    await BuilderClass.rebuildPathTags(this);
 }
 
 console.info = () => {};

+ 52 - 0
model/autotag.js

@@ -0,0 +1,52 @@
+
+const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
+
+const AUTOTAG_TYPE = {
+    path: 0,
+    meta: 1
+};
+
+function AutotagModel() {
+    DatabaseModel.call(this);
+    this.id = null;
+    this.type = AUTOTAG_TYPE.path;
+    this.typeData = "";
+    this.tag = "";
+}
+
+AutotagModel.prototype = Object.create(DatabaseModel.prototype);
+
+AutotagModel.prototype.getTableName = function() {
+    return "autotag";
+}
+
+AutotagModel.prototype.createOrUpdateBase = async function(dbHelper) {
+    await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'autotag' (
+        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+        type TINYINT NOT NULL,
+        typeData STRING NOT NULL,
+        tag STRING NOT NULL
+        )`);
+}
+
+AutotagModel.prototype.describe = function() {
+    return {
+        "id": this.id,
+        "type": this.type,
+        "typeData": this.typeData,
+        "tag": this.tag
+    };
+}
+
+AutotagModel.prototype.versionColumn = function() { return ""; }
+
+AutotagModel.prototype.fromDb = function(dbObj) {
+    this.id = dbObj["id"];
+    this.type = dbObj["type"];
+    this.typeData = dbObj["typeData"];
+    this.tag = dbObj["tag"];
+}
+
+module.exports.AutotagModel = AutotagModel;
+module.exports.AUTOTAG_TYPE = AUTOTAG_TYPE;
+

+ 6 - 2
model/mediaItemTag.js

@@ -1,11 +1,12 @@
 
 const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
 
-function MediaFileTagModel(md5sum, tag, fromMeta) {
+function MediaFileTagModel(md5sum, tag, fromMeta, fromAutotag) {
     DatabaseModel.call(this);
     this.md5sum = md5sum || "";
     this.tag = tag || "";
     this.fromMeta = fromMeta;
+    this.fromAutotag = !!fromAutotag;
 }
 
 MediaFileTagModel.prototype = Object.create(DatabaseModel.prototype);
@@ -19,6 +20,7 @@ MediaFileTagModel.prototype.createOrUpdateBase = async function(dbHelper) {
         md5sum STRING NOT NULL,
         tag varchar(32) NOT NULL,
         fromMeta BOOLEAN NOT NULL,
+        fromAutotag BOOLEAN NOT NULL,
         PRIMARY KEY (md5sum, tag))`);
 }
 
@@ -26,7 +28,8 @@ MediaFileTagModel.prototype.describe = function() {
     return {
         "md5sum": this.md5sum,
         "tag": this.tag,
-        "fromMeta": this.fromMeta
+        "fromMeta": this.fromMeta,
+        "fromAutotag": this.fromAutotag
     };
 }
 
@@ -36,6 +39,7 @@ MediaFileTagModel.prototype.fromDb = function(dbObj) {
     this.md5sum = dbObj["md5sum"];
     this.tag = dbObj["tag"];
     this.fromMeta = dbObj["fromMeta"];
+    this.fromAutotag = dbObj["fromAutotag"];
 }
 
 module.exports.MediaFileTagModel = MediaFileTagModel;

+ 2 - 2
model/mediaService.js

@@ -133,7 +133,7 @@ module.exports.fetchOne = async function(app, md5sum, accessList) {
     let result = ((await app.databaseHelper.runSql(`
         select mediaFile.path, mediaFile.md5sum, mediaFile.date, mediaFile.fixedSum,
         mediaMeta.key as metaKey, mediaMeta.value as metaValue,
-        mediaTag.tag as mediaTag, mediaTag.fromMeta as isFixedTag
+        mediaTag.tag as mediaTag, (mediaTag.fromMeta or mediaTag.fromAutotag) as isFixedTag
         from mediaFile
         left join mediaMeta on mediaMeta.md5sum=mediaFile.fixedSum
         left join mediaTag on mediaTag.md5sum=mediaFile.fixedSum
@@ -146,7 +146,7 @@ module.exports.fetchMedias = async function(app, startTs, count) {
     let result = ((await app.databaseHelper.runSql(`
         select mediaFile.path, mediaFile.md5sum, mediaFile.date, mediaFile.fixedSum,
         mediaMeta.key as metaKey, mediaMeta.value as metaValue,
-        mediaTag.tag as mediaTag, mediaTag.fromMeta as isFixedTag
+        mediaTag.tag as mediaTag, (mediaTag.fromMeta or mediaTag.fromAutotag) as isFixedTag
         from mediaFile
         left join mediaMeta on mediaMeta.md5sum=mediaFile.fixedSum
         left join mediaTag on mediaTag.md5sum=mediaFile.fixedSum

+ 60 - 0
src/autotagBuilder.js

@@ -0,0 +1,60 @@
+
+const path = require('path');
+const { AutotagModel, AUTOTAG_TYPE } = require("../model/autotag.js");
+const MediaFileModel = require('../model/mediaItem.js').MediaFileModel;
+const MediaFileTagModel = require('../model/mediaItemTag.js').MediaFileTagModel;
+
+class AutoTagBuilder
+{
+    #initialized = false;
+    #pathTags = [];
+    #metaTags = [];
+
+    constructor()
+    {
+    }
+
+    #normalizePath(p) {
+        p = path.normalize(p);
+        if (!p.endsWith('/'))
+            p = p + '/';
+        return p;
+    }
+
+    async reloadRules(app) {
+        this.#pathTags = [];
+        this.#metaTags = [];
+
+        for (let proto of await app.databaseHelper.fetch(AutotagModel)) {
+            switch (proto.type) {
+                case AUTOTAG_TYPE.path:
+                    this.#pathTags.push({ path: this.#normalizePath(proto.typeData), tag: proto.tag }); break;
+                case AUTOTAG_TYPE.meta:
+                    this.#metaTags.push({ meta: proto.typeData, tag: proto.tag }); break;
+            }
+        }
+        this.#initialized = true;
+    }
+
+    async rebuildPathTags(app) {
+        if (!this.#initialized)
+            await this.reloadRules(app);
+        await app.databaseHelper.remove(MediaFileTagModel, { fromAutotag: true });
+        let entities = [];
+        for (let proto of this.#pathTags) {
+            for (let matchingFiles of await app.databaseHelper.runSql("select * from `"+MediaFileModel.prototype.getTableName.call(null) +"` where path like '" +proto.path +"%'")) {
+                let entity = new MediaFileTagModel(matchingFiles.fixedSum, proto.tag, false, true);
+                entities.push(entity);
+                if (entities.length >= 100) {
+                    await app.databaseHelper.insertMultipleSameTable(entities);
+                    entities = [];
+                }
+            }
+        }
+        await app.databaseHelper.insertMultipleSameTable(entities);
+        console.log("Done proccessing autotags");
+    }
+}
+
+module.exports = new AutoTagBuilder();
+

+ 5 - 2
static/public/js/access.js

@@ -29,6 +29,7 @@ $(() => {
                         type: "DELETE",
                         success: data => {
                             window.ReloadAccessList(data);
+                            MediaStorage.Instance.rebuildMetaList();
                             ok();
                         },
                         error: err => ok(false),
@@ -45,9 +46,10 @@ $(() => {
                     data: { linkIds: JSON.stringify(Array.from(linkList)) },
                     success: data => {
                         for (let i in data)
-                            this.#storedAccess.add(data[i].linkId);
+                            this.#storedAccess.add(i);
                         this.#UpdateStorage();
                         window.ReloadAccessList(data);
+                        MediaStorage.Instance.rebuildMetaList();
                         ok(data);
                     },
                     error: err => ok(false),
@@ -65,9 +67,10 @@ $(() => {
                     return new Promise(ok => {
                         $.get("/api/access/list", data => {
                             for (let i in data)
-                                this.#storedAccess.add(data[i].linkId);
+                                this.#storedAccess.add(i);
                             this.#UpdateStorage();
                             window.ReloadAccessList(data);
+                            MediaStorage.Instance.rebuildMetaList();
                             ok();
                         });
                     });

+ 4 - 0
static/public/js/chronology.js

@@ -35,6 +35,10 @@ $(() => {
         isInitialized() {
             return this.#min && this.#max;
         }
+
+        reset() {
+            this.#min = this.#max = null;
+        }
     }
 
     window.chronology = new Chronology();

+ 10 - 0
static/public/js/medias.js

@@ -69,6 +69,10 @@ class MediaStorage extends EventTarget
 {
     constructor() {
         super();
+        this.#reset();
+    }
+
+    #reset() {
         this.allMeta = {};
         this.allMetaTypes = {};
         this.allTags = new Set();
@@ -77,6 +81,12 @@ class MediaStorage extends EventTarget
         this.newest = null;
     }
 
+    rebuildMetaList() {
+        this.#reset();
+        window.chronology.reset();
+        this.downloadMetaList();
+    }
+
     downloadMetaList() {
         if (this.isLoading())
             return;

+ 1 - 1
static/public/js/uiAccess.js

@@ -43,7 +43,7 @@ loginCode.querySelector("button").addEventListener("click", () => {
 });
 
 async function logout(accessId, linkId) {
-    await AccessManager.Logout(linkId);
+    await AccessManager.Logout(accessId);
     closeLoginPopin();
 }