Use temp upload for reverse searching and subsequent post creation
parent
4ef09a3099
commit
76d1b1e768
|
|
@ -12,8 +12,7 @@
|
|||
"build:dev": "webpack --config webpack.dev.js",
|
||||
"build-zip": "node scripts/build-zip.js",
|
||||
"sign-xpi": "node scripts/sign-xpi.js",
|
||||
"watch": "webpack --watch --config webpack.dev.js",
|
||||
"prepare": "npm run build"
|
||||
"watch": "webpack --watch --config webpack.dev.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.23.0",
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ export class Config {
|
|||
useOriginalSource: boolean = true;
|
||||
autoSearchSimilar: boolean = false;
|
||||
loadTagCounts: boolean = true;
|
||||
useContentTokens: boolean = true;
|
||||
sites: Array<SzuruSiteConfig> = [];
|
||||
|
||||
public async save() {
|
||||
|
|
|
|||
|
|
@ -111,3 +111,7 @@ export interface ImageSearchResult {
|
|||
exactPost: Post | null;
|
||||
similarPosts: SimilarPost[];
|
||||
}
|
||||
|
||||
export interface TemporaryFileUploadResult {
|
||||
token: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import {
|
|||
SzuruError,
|
||||
ImageSearchResult,
|
||||
TagFields,
|
||||
SzuruNote
|
||||
TemporaryFileUploadResult
|
||||
} from "./SzuruTypes";
|
||||
import { PostViewModel } from "./ViewModels";
|
||||
|
||||
|
|
@ -71,14 +71,19 @@ export default class SzuruWrapper {
|
|||
}
|
||||
|
||||
async createPost(post: PostViewModel): Promise<Post> {
|
||||
var obj = {
|
||||
var obj = <any>{
|
||||
tags: post.tags.map(x => x.name),
|
||||
safety: post.rating,
|
||||
source: post.source,
|
||||
notes: post.notes,
|
||||
contentUrl: post.contentUrl
|
||||
notes: post.notes
|
||||
};
|
||||
|
||||
if (post.contentToken) {
|
||||
obj.contentToken = post.contentToken;
|
||||
} else {
|
||||
obj.contentUrl = post.contentUrl;
|
||||
}
|
||||
|
||||
console.log("Create new post object");
|
||||
console.dir(obj);
|
||||
|
||||
|
|
@ -90,6 +95,16 @@ export default class SzuruWrapper {
|
|||
return (await this.apiPost("posts/reverse-search", obj)).data;
|
||||
}
|
||||
|
||||
async reverseSearchToken(contentToken: string): Promise<ImageSearchResult> {
|
||||
var obj = { contentToken };
|
||||
return (await this.apiPost("posts/reverse-search", obj)).data;
|
||||
}
|
||||
|
||||
async uploadTempFile(contentUrl: string): Promise<TemporaryFileUploadResult> {
|
||||
var obj = { contentUrl };
|
||||
return (await this.apiPost("uploads", obj)).data;
|
||||
}
|
||||
|
||||
static createFromConfig(siteConfig: SzuruSiteConfig): SzuruWrapper | null {
|
||||
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ export class PostViewModel {
|
|||
tags: TagViewModel[] = [];
|
||||
notes: ScrapedNote[];
|
||||
contentUrl: string;
|
||||
contentToken: string | undefined;
|
||||
pageUrl: string;
|
||||
contentType: ContentType;
|
||||
rating: BooruTypes.SafetyRating;
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<ul class="input">
|
||||
<li>
|
||||
<label>
|
||||
<input type="checkbox" name="autoSearchSimilar" v-model="autoSearchSimilar" />
|
||||
<input type="checkbox" v-model="autoSearchSimilar" />
|
||||
Automatically search for similar posts
|
||||
</label>
|
||||
<p class="hint">
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
</li>
|
||||
<li>
|
||||
<label>
|
||||
<input type="checkbox" name="useOriginalSource" v-model="useOriginalSource" />
|
||||
<input type="checkbox" v-model="useOriginalSource" />
|
||||
Use original image source when available
|
||||
</label>
|
||||
<p class="hint">
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
</li>
|
||||
<li>
|
||||
<label>
|
||||
<input type="checkbox" name="loadTagCounts" v-model="loadTagCounts" />
|
||||
<input type="checkbox" v-model="loadTagCounts" />
|
||||
Show how often a tag is used in the selected szurubooru instance
|
||||
</label>
|
||||
<p class="hint">
|
||||
|
|
@ -47,6 +47,16 @@
|
|||
the tag does not yet exist.
|
||||
</p>
|
||||
</li>
|
||||
<li>
|
||||
<label>
|
||||
<input type="checkbox" v-model="loadTagCounts" />
|
||||
Use temporary files when reverse searching and creating posts
|
||||
</label>
|
||||
<p class="hint">
|
||||
When reverse searching for a post this will (temporarily) upload the post to the server. This makes creating
|
||||
the post faster because then the file already exists on the server.
|
||||
</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="settings-footer">
|
||||
|
|
@ -75,6 +85,7 @@ export default Vue.extend({
|
|||
autoSearchSimilar: false,
|
||||
loadTagCounts: false,
|
||||
useOriginalSource: false,
|
||||
useContentTokens: false,
|
||||
statusText: "",
|
||||
statusType: "",
|
||||
};
|
||||
|
|
@ -114,6 +125,7 @@ export default Vue.extend({
|
|||
config.autoSearchSimilar = this.autoSearchSimilar;
|
||||
config.loadTagCounts = this.loadTagCounts;
|
||||
config.useOriginalSource = this.useOriginalSource;
|
||||
config.useContentTokens = this.useContentTokens;
|
||||
|
||||
await config.save();
|
||||
|
||||
|
|
@ -139,6 +151,7 @@ export default Vue.extend({
|
|||
this.autoSearchSimilar = config.autoSearchSimilar;
|
||||
this.loadTagCounts = config.loadTagCounts;
|
||||
this.useOriginalSource = config.useOriginalSource;
|
||||
this.useContentTokens = config.useContentTokens;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ import axios, { CancelTokenSource } from "axios";
|
|||
import { browser, Runtime, WebRequest } from "webextension-polyfill-ts";
|
||||
import { ScrapedPost, ScrapeResults } from "neo-scraper";
|
||||
import SzuruWrapper from "../SzuruWrapper";
|
||||
import { Post } from "../SzuruTypes";
|
||||
import { ImageSearchResult, Post } from "../SzuruTypes";
|
||||
import { Config, SzuruSiteConfig } from "../Config";
|
||||
import { BrowserCommand, Message, getUrl, isChrome, encodeTagName } from "../Common";
|
||||
import { PostViewModel, TagViewModel } from "../ViewModels";
|
||||
|
|
@ -320,7 +320,18 @@ export default Vue.extend({
|
|||
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
|
||||
|
||||
try {
|
||||
const res = await this.szuru.reverseSearch(this.selectedPost.contentUrl);
|
||||
let res: ImageSearchResult | undefined;
|
||||
|
||||
if (this.config?.useContentTokens) {
|
||||
let tmpRes = await this.szuru.uploadTempFile(this.selectedPost.contentUrl);
|
||||
// TODO: Error handling?
|
||||
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
|
||||
this.selectedPost.contentToken = tmpRes.token;
|
||||
|
||||
res = await this.szuru.reverseSearchToken(tmpRes.token);
|
||||
} else {
|
||||
res = await this.szuru.reverseSearch(this.selectedPost.contentUrl);
|
||||
}
|
||||
|
||||
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||
msg.content = "No similar posts found";
|
||||
|
|
@ -493,7 +504,7 @@ export default Vue.extend({
|
|||
if (post != undefined && post.referrer) {
|
||||
console.log(`Setting referrer to '${post.referrer}' for request to '${post.contentUrl}'.`);
|
||||
// TODO: If the headers already have a 'Referer' header this will _not_ override that.
|
||||
// Though as far as I am aware the 'img' tag doesn't set this header.
|
||||
// As far as I am aware the 'img' tag doesn't set this header, so it shouldn't be a problem.
|
||||
requestHeaders.push({ name: "Referer", value: post.referrer });
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -72,7 +72,8 @@ async function uploadPost(post: PostViewModel) {
|
|||
} catch (ex) {
|
||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
||||
let error = ex as SzuruError;
|
||||
if (error) {
|
||||
// TODO: Actual good error handling/messaging
|
||||
if (error.name) {
|
||||
console.error(error);
|
||||
switch (error.name) {
|
||||
case "PostAlreadyUploadedError":
|
||||
|
|
@ -82,7 +83,7 @@ async function uploadPost(post: PostViewModel) {
|
|||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(msg, "error")));
|
||||
break;
|
||||
default:
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex.message, "error")));
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex?.message ?? ex, "error")));
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue