From afe4ff535b49420457dd68a78f69cb0ef7286094 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Mon, 17 Feb 2025 00:23:36 +0100 Subject: [PATCH] Fix issue where merging new image content for existing posts did not work when uploadMode is set to "content" --- src/popup/pages/MergePost.vue | 7 ++++-- src/popup/pages/PopupMain.vue | 46 ++++++++++------------------------- src/utils.ts | 26 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/popup/pages/MergePost.vue b/src/popup/pages/MergePost.vue index f35a08a..c89dbce 100644 --- a/src/popup/pages/MergePost.vue +++ b/src/popup/pages/MergePost.vue @@ -5,7 +5,7 @@ import { Post, UpdatePostRequest } from "~/api/models"; import { isMobile } from "~/env"; import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models"; import { cfg, useMergeStore, usePopupStore } from "~/stores"; -import { emptyPost, getUrl } from "~/utils"; +import { emptyPost, ensurePostHasContentToken, getUrl } from "~/utils"; const props = defineProps(["siteId", "postId"]); const merge = useMergeStore(); @@ -140,7 +140,10 @@ async function mergeChanges() { } 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) { diff --git a/src/popup/pages/PopupMain.vue b/src/popup/pages/PopupMain.vue index ccc925b..9129bb4 100644 --- a/src/popup/pages/PopupMain.vue +++ b/src/popup/pages/PopupMain.vue @@ -2,7 +2,7 @@ import { useDark } from "@vueuse/core"; import { cloneDeep } from "lodash"; import { ScrapeResults } from "neo-scraper"; -import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary } from "~/utils"; +import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary, ensurePostHasContentToken } from "~/utils"; import { BrowserCommand, ScrapedPostDetails, @@ -108,6 +108,10 @@ async function grabPost() { 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 // a. user has addPageUrlToSource set to true // b. source is empty @@ -162,13 +166,9 @@ async function upload() { try { const post: ScrapedPostDetails = cloneDeep(pop.selectedPost)!; - // uploadMode "content" requires a content token to work. So ensure it is - // set. Ignore fallback "Upload as URL" mode as well - if ( - (cfg.value.alwaysUploadAsContent || post.uploadMode === "content") - && post.name !== "[fallback] Upload as URL" - ) { - await ensurePostHasContentToken(post); + // uploadMode "content" requires a content token to work. So ensure it is set. + if (post.uploadMode === "content") { + await ensurePostHasContentToken(szuru.value!, post, cfg); } const cmdData = new PostUploadCommandData(post, cloneDeep(selectedSite.value)); @@ -249,34 +249,14 @@ async function clickFindSimilar() { 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) { if (!post || !szuru.value || !cfg.value.selectedSiteId) return; // 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. let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId]; @@ -291,7 +271,7 @@ async function findSimilar(post: ScrapedPostDetails | undefined) { isSearchingForSimilarPosts.value++; try { - await ensurePostHasContentToken(post); + await ensurePostHasContentToken(selectedInstance, post, cfg); const res = await selectedInstance.reverseSearchToken(instanceSpecificData.contentToken!); diff --git a/src/utils.ts b/src/utils.ts index b078c51..0a7b9c8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import byteSize from "byte-size"; import { MicroUser, Post } from "./api/models"; import { ScrapedPostDetails, TagDetails } from "./models"; +import SzurubooruApi from "./api"; export function getUrl(root: string, ...parts: string[]): string { let url = root.replace(/\/+$/, ""); @@ -102,3 +103,28 @@ export function getPostInfoSummary(post: ScrapedPostDetails) { } 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; + } +}