autotagBuilder.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const path = require('path');
  2. const { AutotagModel, AUTOTAG_TYPE } = require("../model/autotag.js");
  3. const MediaFileModel = require('../model/mediaItem.js').MediaFileModel;
  4. const MediaFileTagModel = require('../model/mediaItemTag.js').MediaFileTagModel;
  5. class AutoTagBuilder
  6. {
  7. #initialized = false;
  8. #pathTags = [];
  9. #metaTags = [];
  10. constructor()
  11. {
  12. }
  13. #normalizePath(p) {
  14. p = path.normalize(p);
  15. if (!p.endsWith('/'))
  16. p = p + '/';
  17. return p;
  18. }
  19. async reloadRules(app) {
  20. this.#pathTags = [];
  21. this.#metaTags = [];
  22. for (let proto of await app.databaseHelper.fetch(AutotagModel)) {
  23. switch (proto.type) {
  24. case AUTOTAG_TYPE.path:
  25. this.#pathTags.push({ path: this.#normalizePath(proto.typeData), tag: proto.tag }); break;
  26. case AUTOTAG_TYPE.meta:
  27. this.#metaTags.push({ meta: proto.typeData, tag: proto.tag }); break;
  28. }
  29. }
  30. this.#initialized = true;
  31. }
  32. async rebuildPathTags(app) {
  33. if (!this.#initialized)
  34. await this.reloadRules(app);
  35. await app.databaseHelper.remove(MediaFileTagModel, { fromAutotag: true });
  36. let entities = [];
  37. for (let proto of this.#pathTags) {
  38. for (let matchingFiles of await app.databaseHelper.runSql("select * from `"+MediaFileModel.prototype.getTableName.call(null) +"` where path like '" +proto.path +"%'")) {
  39. let entity = new MediaFileTagModel(matchingFiles.fixedSum, proto.tag, false, true);
  40. entities.push(entity);
  41. if (entities.length >= 100) {
  42. await app.databaseHelper.insertMultipleSameTable(entities);
  43. entities = [];
  44. }
  45. }
  46. }
  47. await app.databaseHelper.insertMultipleSameTable(entities);
  48. console.log("Done proccessing autotags");
  49. }
  50. }
  51. module.exports = new AutoTagBuilder();