autotagBuilder.js 2.1 KB

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