Fix issue where merging new image content for existing posts did not work when uploadMode is set to "content"
parent
9e07ee1594
commit
afe4ff535b
|
|
@ -5,7 +5,7 @@ import { Post, UpdatePostRequest } from "~/api/models";
|
||||||
import { isMobile } from "~/env";
|
import { isMobile } from "~/env";
|
||||||
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
|
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
|
||||||
import { cfg, useMergeStore, usePopupStore } from "~/stores";
|
import { cfg, useMergeStore, usePopupStore } from "~/stores";
|
||||||
import { emptyPost, getUrl } from "~/utils";
|
import { emptyPost, ensurePostHasContentToken, getUrl } from "~/utils";
|
||||||
|
|
||||||
const props = defineProps(["siteId", "postId"]);
|
const props = defineProps(["siteId", "postId"]);
|
||||||
const merge = useMergeStore();
|
const merge = useMergeStore();
|
||||||
|
|
@ -140,7 +140,10 @@ async function mergeChanges() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imageToKeep.value == "new") {
|
if (imageToKeep.value == "new") {
|
||||||
req.contentUrl = scrapedPost?.contentUrl;
|
// In practice this should not be needed, because finding similar posts requires the use of content tokens.
|
||||||
|
// So if a similar post is found, it means that a contentToken has already been set.
|
||||||
|
ensurePostHasContentToken(szuru, scrapedPost, cfg);
|
||||||
|
req.contentToken = scrapedPost.instanceSpecificData[cfg.value.selectedSiteId!].contentToken!;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.safety && !req.source && !(req.contentUrl || req.contentToken) && req.tags === undefined) {
|
if (!req.safety && !req.source && !(req.contentUrl || req.contentToken) && req.tags === undefined) {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
import { useDark } from "@vueuse/core";
|
import { useDark } from "@vueuse/core";
|
||||||
import { cloneDeep } from "lodash";
|
import { cloneDeep } from "lodash";
|
||||||
import { ScrapeResults } from "neo-scraper";
|
import { ScrapeResults } from "neo-scraper";
|
||||||
import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary } from "~/utils";
|
import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary, ensurePostHasContentToken } from "~/utils";
|
||||||
import {
|
import {
|
||||||
BrowserCommand,
|
BrowserCommand,
|
||||||
ScrapedPostDetails,
|
ScrapedPostDetails,
|
||||||
|
|
@ -108,6 +108,10 @@ async function grabPost() {
|
||||||
vm.tags.splice(0);
|
vm.tags.splice(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cfg.value.alwaysUploadAsContent && vm.name !== "[fallback] Upload as URL") {
|
||||||
|
vm.uploadMode = "content";
|
||||||
|
}
|
||||||
|
|
||||||
// Add current pageUrl to the source if either
|
// Add current pageUrl to the source if either
|
||||||
// a. user has addPageUrlToSource set to true
|
// a. user has addPageUrlToSource set to true
|
||||||
// b. source is empty
|
// b. source is empty
|
||||||
|
|
@ -162,13 +166,9 @@ async function upload() {
|
||||||
try {
|
try {
|
||||||
const post: ScrapedPostDetails = cloneDeep(pop.selectedPost)!;
|
const post: ScrapedPostDetails = cloneDeep(pop.selectedPost)!;
|
||||||
|
|
||||||
// uploadMode "content" requires a content token to work. So ensure it is
|
// uploadMode "content" requires a content token to work. So ensure it is set.
|
||||||
// set. Ignore fallback "Upload as URL" mode as well
|
if (post.uploadMode === "content") {
|
||||||
if (
|
await ensurePostHasContentToken(szuru.value!, post, cfg);
|
||||||
(cfg.value.alwaysUploadAsContent || post.uploadMode === "content")
|
|
||||||
&& post.name !== "[fallback] Upload as URL"
|
|
||||||
) {
|
|
||||||
await ensurePostHasContentToken(post);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cmdData = new PostUploadCommandData(post, <SzuruSiteConfig>cloneDeep(selectedSite.value));
|
const cmdData = new PostUploadCommandData(post, <SzuruSiteConfig>cloneDeep(selectedSite.value));
|
||||||
|
|
@ -249,34 +249,14 @@ async function clickFindSimilar() {
|
||||||
if (pop.selectedPost) return await findSimilar(pop.selectedPost);
|
if (pop.selectedPost) return await findSimilar(pop.selectedPost);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ensurePostHasContentToken(post: ScrapedPostDetails) {
|
|
||||||
if (!szuru.value || !cfg.value.selectedSiteId) return;
|
|
||||||
|
|
||||||
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
|
|
||||||
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
|
|
||||||
|
|
||||||
if (!instanceSpecificData) {
|
|
||||||
console.error("instanceSpecificData is undefined. This should never happen!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const uploadMode =
|
|
||||||
cfg.value.alwaysUploadAsContent && post.name !== "[fallback] Upload as URL" ?
|
|
||||||
'content' : post.uploadMode;
|
|
||||||
let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl, uploadMode);
|
|
||||||
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
|
|
||||||
instanceSpecificData.contentToken = tmpRes.token;
|
|
||||||
} catch (ex) {
|
|
||||||
instanceSpecificData.genericError = "Couldn't upload content. " + getErrorMessage(ex);
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function findSimilar(post: ScrapedPostDetails | undefined) {
|
async function findSimilar(post: ScrapedPostDetails | undefined) {
|
||||||
if (!post || !szuru.value || !cfg.value.selectedSiteId) return;
|
if (!post || !szuru.value || !cfg.value.selectedSiteId) return;
|
||||||
|
|
||||||
// TODO: This code might not work.
|
// TODO: This code might not work.
|
||||||
|
// Why is toRaw used?
|
||||||
|
// I guess it could happen that the selectedInstance changes halfway during this function?
|
||||||
|
// And in that case toRaw would ensure we have the original selected instance (as it was at the start of this function).
|
||||||
|
// In theory this sounds correct, but idk if it is needed in practice.
|
||||||
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
|
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
|
||||||
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
|
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
|
||||||
|
|
||||||
|
|
@ -291,7 +271,7 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
|
||||||
isSearchingForSimilarPosts.value++;
|
isSearchingForSimilarPosts.value++;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await ensurePostHasContentToken(post);
|
await ensurePostHasContentToken(selectedInstance, post, cfg);
|
||||||
|
|
||||||
const res = await selectedInstance.reverseSearchToken(instanceSpecificData.contentToken!);
|
const res = await selectedInstance.reverseSearchToken(instanceSpecificData.contentToken!);
|
||||||
|
|
||||||
|
|
|
||||||
26
src/utils.ts
26
src/utils.ts
|
|
@ -1,6 +1,7 @@
|
||||||
import byteSize from "byte-size";
|
import byteSize from "byte-size";
|
||||||
import { MicroUser, Post } from "./api/models";
|
import { MicroUser, Post } from "./api/models";
|
||||||
import { ScrapedPostDetails, TagDetails } from "./models";
|
import { ScrapedPostDetails, TagDetails } from "./models";
|
||||||
|
import SzurubooruApi from "./api";
|
||||||
|
|
||||||
export function getUrl(root: string, ...parts: string[]): string {
|
export function getUrl(root: string, ...parts: string[]): string {
|
||||||
let url = root.replace(/\/+$/, "");
|
let url = root.replace(/\/+$/, "");
|
||||||
|
|
@ -102,3 +103,28 @@ export function getPostInfoSummary(post: ScrapedPostDetails) {
|
||||||
}
|
}
|
||||||
return parts.join(" / ");
|
return parts.join(" / ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function ensurePostHasContentToken(selectedInstance: SzurubooruApi, post: ScrapedPostDetails, cfg: any) {
|
||||||
|
if (!selectedInstance || !cfg.value.selectedSiteId) return;
|
||||||
|
|
||||||
|
const instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
|
||||||
|
|
||||||
|
if (!instanceSpecificData) {
|
||||||
|
console.error("instanceSpecificData is undefined. This should never happen!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instanceSpecificData.contentToken) {
|
||||||
|
console.log("[ensurePostHasContentToken] contentToken is already set.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const tmpRes = await selectedInstance.uploadTempFile(post.contentUrl, post.uploadMode);
|
||||||
|
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
|
||||||
|
instanceSpecificData.contentToken = tmpRes.token;
|
||||||
|
} catch (ex) {
|
||||||
|
instanceSpecificData.genericError = "Couldn't upload content. " + getErrorMessage(ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue