|
|
@@ -6,15 +6,15 @@ class Media {
|
|
|
this.path = data.path;
|
|
|
this.fileName = data.fileName;
|
|
|
this.meta = data.meta || {};
|
|
|
- this.fixedTags = data.fixedTags || [];
|
|
|
- this.tags = data.tags || [];
|
|
|
+ this.fixedTags = [];
|
|
|
+
|
|
|
+ this.tags = [];
|
|
|
this.writeAccess = data.accessType === 2;
|
|
|
this.thumbnail = `/api/media/thumbnail/${data.md5sum}.jpg`;
|
|
|
this.original = `/api/media/original/${data.md5sum}`;
|
|
|
this.ui = null;
|
|
|
|
|
|
- this.tags = this.tags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
|
|
|
- this.fixedTags = this.fixedTags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
|
|
|
+ this.setTags(data.fixedTags || [], data.tags || []);
|
|
|
|
|
|
for (let i in this.meta) {
|
|
|
if (this.meta[i].type === 'date')
|
|
|
@@ -36,6 +36,11 @@ class Media {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ setTags(fixedTags, tags) {
|
|
|
+ this.tags = tags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
|
|
|
+ this.fixedTags = fixedTags.reduce((acc, tag) => { acc.add(tag.replaceAll(/\/\/+/gi, '/')); return acc; }, new Set());
|
|
|
+ }
|
|
|
+
|
|
|
allTags() {
|
|
|
return Array.from(new Set([...this.fixedTags, ...this.tags])).sort();
|
|
|
}
|
|
|
@@ -164,7 +169,7 @@ class MediaStorage extends EventTarget
|
|
|
}
|
|
|
|
|
|
setMetaValue(md5sum, key, value) {
|
|
|
- LoadingTasks.push(() => {
|
|
|
+ return LoadingTasks.push(() => {
|
|
|
return new Promise(ok => {
|
|
|
let media = this.medias.find(x => x.md5sum === md5sum);
|
|
|
if (!media || !media.writeAccess)
|
|
|
@@ -173,7 +178,7 @@ class MediaStorage extends EventTarget
|
|
|
url: `/api/media/${encodeURIComponent(md5sum)}/meta/${encodeURIComponent(key)}`,
|
|
|
type: "PATCH",
|
|
|
data: { value },
|
|
|
- success: () => {
|
|
|
+ success: (media) => {
|
|
|
let meta = media.meta[key] || { type: 'string', value: value, canWrite: true };
|
|
|
meta.value = value;
|
|
|
this.#pushMeta(key, meta);
|
|
|
@@ -186,6 +191,53 @@ class MediaStorage extends EventTarget
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ removeTag(md5sum, tagName) {
|
|
|
+ return LoadingTasks.push(() => {
|
|
|
+ return new Promise(ok => {
|
|
|
+ let media = this.medias.find(x => x.md5sum === md5sum);
|
|
|
+ if (!media || !media.writeAccess)
|
|
|
+ return ok(false);
|
|
|
+ $.ajax({
|
|
|
+ url: `/api/media/${encodeURIComponent(md5sum)}/tag/${encodeURIComponent(tagName)}`,
|
|
|
+ type: "DELETE",
|
|
|
+ success: data => {
|
|
|
+ media.setTags(data.fixedTags, data.tags);
|
|
|
+ for (let i of data.tags)
|
|
|
+ this.#pushTag(i, true);
|
|
|
+ for (let i of data.fixedTags)
|
|
|
+ this.#pushTag(i, true);
|
|
|
+ ok(true);
|
|
|
+ },
|
|
|
+ error: err => ok(false),
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ addTag(md5sum, tagName) {
|
|
|
+ return LoadingTasks.push(() => {
|
|
|
+ return new Promise(ok => {
|
|
|
+ let media = this.medias.find(x => x.md5sum === md5sum);
|
|
|
+ if (!media || !media.writeAccess)
|
|
|
+ return ok(false);
|
|
|
+ $.ajax({
|
|
|
+ url: `/api/media/${encodeURIComponent(md5sum)}/tag`,
|
|
|
+ type: "PUT",
|
|
|
+ data: { tag: tagName },
|
|
|
+ success: data => {
|
|
|
+ media.setTags(data.fixedTags, data.tags);
|
|
|
+ for (let i of data.tags)
|
|
|
+ this.#pushTag(i, true);
|
|
|
+ for (let i of data.fixedTags)
|
|
|
+ this.#pushTag(i, true);
|
|
|
+ ok(true);
|
|
|
+ },
|
|
|
+ error: err => ok(false),
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
MediaStorage.Instance = new MediaStorage();
|