1
0

medias.js 3.3 KB

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