1
0

medias.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. class Media {
  2. constructor(data) {
  3. this.date = new Date(data.date);
  4. this.md5sum = data.md5sum;
  5. this.fixedSum = data.fixedSum;
  6. this.path = data.path;
  7. this.fileName = data.fileName;
  8. this.meta = data.meta || {};
  9. this.fixedTags = [];
  10. this.tags = [];
  11. this.writeAccess = data.accessType === 2;
  12. this.thumbnail = `/api/media/thumbnail/${data.fixedSum}.jpg`;
  13. this.original = `/api/media/original/${data.fixedSum}`;
  14. this.ui = null;
  15. this.setTags(data.fixedTags || [], data.tags || []);
  16. for (let i in this.meta) {
  17. if (this.meta[i].type === 'date')
  18. this.meta[i].value = new Date(parseInt(this.meta[i].value));
  19. else if (this.meta[i].type === 'number' || this.meta[i].type === 'octet')
  20. this.meta[i].value = parseInt(this.meta[i].value);
  21. else if (this.meta[i].type === 'string')
  22. this.meta[i].value = '' + this.meta[i].value;
  23. }
  24. }
  25. resize(maxWidth, maxHeight) {
  26. let ratio = Math.min(1, Math.max(
  27. maxWidth / (this.meta?.width?.value || maxWidth),
  28. maxHeight / (this.meta?.height?.value || maxHeight)));
  29. let result = {
  30. width: Math.floor(this.meta.width?.value *ratio),
  31. height: Math.floor(this.meta.height?.value *ratio),
  32. };
  33. if (isNaN(result.width) || isNaN(result.height) || !result.height || !result.width) {
  34. console.error("Failed to resize image ", this);
  35. return null;
  36. }
  37. return result;
  38. }
  39. setTags(fixedTags, tags) {
  40. this.tags = tags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
  41. this.fixedTags = fixedTags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
  42. }
  43. allTags() {
  44. return Array.from(new Set([...this.fixedTags, ...this.tags])).sort();
  45. }
  46. }
  47. function tryLoadMedia(md5sum) {
  48. return new Promise((ok, ko) => {
  49. $.get("/api/media/" +md5sum, data => {
  50. let item = new Media(data);
  51. MediaStorage.Instance.pushAll([item], true);
  52. ok(item);
  53. }).fail(err => {
  54. console.error("Trying to get media with md5sum " +md5sum +" failed:", err.responseText);
  55. ok(null);
  56. });
  57. });
  58. }
  59. class MediaStorage extends EventTarget
  60. {
  61. constructor() {
  62. super();
  63. this.allMeta = {};
  64. this.allMetaTypes = {};
  65. this.allTags = new Set();
  66. this.medias = [];
  67. this.oldest = null;
  68. this.newest = null;
  69. }
  70. #pushMeta(metaKey, metaVal) {
  71. if (metaKey === 'dateTime')
  72. return;
  73. if (!this.allMeta[metaKey])
  74. this.allMeta[metaKey] = new Set();
  75. this.allMeta[metaKey].add(metaVal.value);
  76. if (!this.allMetaTypes[metaKey])
  77. this.allMetaTypes[metaKey] = { type: metaVal.type, canBeEmpty: !!this.medias.length, canWrite: metaVal.canWrite };
  78. }
  79. #pushTag(tag, first) {
  80. while (tag.length && tag.endsWith('/'))
  81. tag = tag.substr(0, tag.length -1);
  82. this.allTags.add(tag);
  83. let index = tag.lastIndexOf('/');
  84. if (index >= 0)
  85. this.#pushTag(tag.substr(0, index));
  86. }
  87. #pushUnique(media) {
  88. for (let i of media.tags)
  89. this.#pushTag(i, true);
  90. for (let i of media.fixedTags)
  91. this.#pushTag(i, true);
  92. for (let key in media.meta)
  93. this.#pushMeta(key, media.meta[key]);
  94. for (let key in this.allMetaTypes)
  95. if (!media.meta[key])
  96. this.allMetaTypes[key].canBeEmpty = true;
  97. this.medias.push(media);
  98. }
  99. pushAll(arr, partialLoad) {
  100. let result = [];
  101. let reorder = false;
  102. for (let i of arr) {
  103. if (partialLoad !== true) {
  104. this.oldest = !this.oldest || this.oldest.date.getTime() > i.date.getTime() ? i : this.oldest;
  105. this.newest = !this.newest || this.newest.date.getTime() < i.date.getTime() ? i : this.newest;
  106. }
  107. if (this.medias.length && this.medias[this.medias.length -1].date.getTime() < i.date.getTime())
  108. reorder = true;
  109. if (this.medias.find(x => x.fixedSum === i.fixedSum))
  110. continue;
  111. this.#pushUnique(i);
  112. result.push(i);
  113. }
  114. for (let i of result)
  115. this.dispatchEvent(new CustomEvent("newMedia", { detail: i }));
  116. if (reorder) {
  117. this.medias.sort((a, b) => b.date.getTime() - a.date.getTime());
  118. this.dispatchEvent(new CustomEvent("rebuildMedia"));
  119. }
  120. }
  121. onFilterUpdated() {
  122. this.dispatchEvent(new CustomEvent("rebuildMedia"));
  123. }
  124. getMediaIndex(media) {
  125. return this.medias.indexOf(media);
  126. }
  127. nextMedia(current) {
  128. return this.medias[this.getMediaIndex(current) +1];
  129. }
  130. previousMedia(current) {
  131. return this.medias[this.getMediaIndex(current) -1];
  132. }
  133. getMediaBetweenIndexes(a, b) {
  134. if (a > b)
  135. return this.getMediaBetweenIndexes(b, a);
  136. return this.medias.slice(a, b +1);
  137. }
  138. getMediaBetween(a, b) {
  139. let aIndex = this.medias.indexOf(a);
  140. let bIndex = this.medias.indexOf(b);
  141. if (aIndex < 0 || bIndex < 0 || aIndex === bIndex)
  142. return [];
  143. return this.getMediaBetweenIndexes(aIndex, bIndex);
  144. }
  145. getMediaLocal(md5sum) {
  146. return this.medias.find(x => x.fixedSum === md5sum);
  147. }
  148. async getMedia(md5sum) {
  149. let media = this.medias.find(x => x.fixedSum === md5sum);
  150. if (media)
  151. return media;
  152. return await tryLoadMedia(md5sum);
  153. }
  154. setMetaValue(md5sum, key, value) {
  155. return LoadingTasks.push(() => {
  156. return new Promise(ok => {
  157. let media = this.medias.find(x => x.fixedSum === md5sum);
  158. if (!media || !media.writeAccess)
  159. return ok(false);
  160. $.ajax({
  161. url: `/api/media/${encodeURIComponent(md5sum)}/meta/${encodeURIComponent(key)}`,
  162. type: "PATCH",
  163. data: { value },
  164. success: (media) => {
  165. let meta = media.meta[key] || { type: 'string', value: value, canWrite: true };
  166. meta.value = value;
  167. this.#pushMeta(key, meta);
  168. media.meta[key] = meta;
  169. window.ReloadFilters(this);
  170. ok(true);
  171. },
  172. error: err => ok(false),
  173. });
  174. });
  175. });
  176. }
  177. removeTag(md5sum, tagName) {
  178. return LoadingTasks.push(() => {
  179. return new Promise(ok => {
  180. let media = this.medias.find(x => x.fixedSum === md5sum);
  181. if (!media || !media.writeAccess)
  182. return ok(false);
  183. $.ajax({
  184. url: `/api/media/${encodeURIComponent(md5sum)}/tag/${encodeURIComponent(tagName)}`,
  185. type: "DELETE",
  186. success: data => {
  187. media.setTags(data.fixedTags, data.tags);
  188. for (let i of data.tags)
  189. this.#pushTag(i, true);
  190. for (let i of data.fixedTags)
  191. this.#pushTag(i, true);
  192. ok(true);
  193. },
  194. error: err => ok(false),
  195. });
  196. });
  197. });
  198. }
  199. addTag(md5sum, tagName) {
  200. return LoadingTasks.push(() => {
  201. return new Promise(ok => {
  202. let media = this.medias.find(x => x.fixedSum === md5sum);
  203. if (!media || !media.writeAccess)
  204. return ok(false);
  205. $.ajax({
  206. url: `/api/media/${encodeURIComponent(md5sum)}/tag`,
  207. type: "PUT",
  208. data: { tag: tagName },
  209. success: data => {
  210. media.setTags(data.fixedTags, data.tags);
  211. for (let i of data.tags)
  212. this.#pushTag(i, true);
  213. for (let i of data.fixedTags)
  214. this.#pushTag(i, true);
  215. ok(true);
  216. },
  217. error: err => ok(false),
  218. });
  219. });
  220. });
  221. }
  222. }
  223. MediaStorage.Instance = new MediaStorage();