medias.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. class Media {
  2. constructor(data) {
  3. this.date = new Date(data.date);
  4. this.md5sum = data.md5sum;
  5. this.path = data.path;
  6. this.fileName = data.fileName;
  7. this.meta = data.meta || {};
  8. this.tags = data.tags || [];
  9. this.thumbnail = `/api/media/thumbnail/${data.md5sum}.jpg`;
  10. this.original = `/api/media/original/${data.md5sum}`;
  11. this.ui = null;
  12. for (let i in this.meta) {
  13. if (this.meta[i].type === 'date')
  14. this.meta[i].value = new Date(parseInt(this.meta[i].value));
  15. else if (this.meta[i].type === 'number' || this.meta[i].type === 'octet')
  16. this.meta[i].value = parseInt(this.meta[i].value);
  17. else if (this.meta[i].type === 'string')
  18. this.meta[i].value = '' + this.meta[i].value;
  19. }
  20. }
  21. resize(maxWidth, maxHeight) {
  22. let ratio = Math.min(1, Math.max(
  23. maxWidth / (this.meta?.width || maxWidth),
  24. maxHeight / (this.meta?.height || maxHeight)));
  25. return {
  26. width: Math.floor(this.meta.width *ratio),
  27. height: Math.floor(this.meta.height *ratio),
  28. };
  29. }
  30. }
  31. function tryLoadMedia(md5sum) {
  32. return new Promise((ok, ko) => {
  33. $.get("/api/media/" +md5sum, data => {
  34. let item = new Media(data);
  35. MediaStorage.Instance.pushAll([item], true);
  36. ok(item);
  37. }).fail(err => {
  38. console.error("Trying to get media with md5sum " +md5sum +" failed:", err.responseText);
  39. ok(null);
  40. });
  41. });
  42. }
  43. class MediaStorage extends EventTarget
  44. {
  45. constructor() {
  46. super();
  47. this.medias = [];
  48. this.oldest = null;
  49. this.newest = null;
  50. }
  51. pushAll(arr, partialLoad) {
  52. let result = [];
  53. let reorder = false;
  54. for (let i of arr) {
  55. if (this.medias.find(x => x.md5sum === i.md5sum))
  56. continue;
  57. if (this.medias.length && this.medias[this.medias.length -1].date.getTime() < i.date.getTime())
  58. reorder = true;
  59. this.medias.push(i);
  60. if (partialLoad !== true) {
  61. this.oldest = !this.oldest || this.oldest.date.getTime() > i.date.getTime() ? i : this.oldest;
  62. this.newest = !this.newest || this.newest.date.getTime() < i.date.getTime() ? i : this.newest;
  63. }
  64. result.push(i);
  65. }
  66. for (let i of result)
  67. this.dispatchEvent(new CustomEvent("newMedia", { detail: i }));
  68. if (reorder) {
  69. this.medias.sort((a, b) => b.date.getTime() - a.date.getTime());
  70. this.dispatchEvent(new CustomEvent("rebuildMedia"));
  71. }
  72. }
  73. getMediaIndex(media) {
  74. return this.medias.indexOf(media);
  75. }
  76. nextMedia(current) {
  77. return this.medias[this.getMediaIndex(current) +1];
  78. }
  79. previousMedia(current) {
  80. return this.medias[this.getMediaIndex(current) -1];
  81. }
  82. getMediaBetweenIndexes(a, b) {
  83. if (a > b)
  84. return this.getMediaBetweenIndexes(b, a);
  85. return this.medias.slice(a, b +1);
  86. }
  87. getMediaBetween(a, b) {
  88. let aIndex = this.medias.indexOf(a);
  89. let bIndex = this.medias.indexOf(b);
  90. if (aIndex < 0 || bIndex < 0 || aIndex === bIndex)
  91. return [];
  92. return this.getMediaBetweenIndexes(aIndex, bIndex);
  93. }
  94. async getMedia(md5sum) {
  95. let media = this.medias.find(x => x.md5sum === md5sum);
  96. if (media)
  97. return media;
  98. return await tryLoadMedia(md5sum);
  99. }
  100. }
  101. MediaStorage.Instance = new MediaStorage();