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:dev": "webpack --config webpack.dev.js",
|
||||||
"build-zip": "node scripts/build-zip.js",
|
"build-zip": "node scripts/build-zip.js",
|
||||||
"sign-xpi": "node scripts/sign-xpi.js",
|
"sign-xpi": "node scripts/sign-xpi.js",
|
||||||
"watch": "webpack --watch --config webpack.dev.js",
|
"watch": "webpack --watch --config webpack.dev.js"
|
||||||
"prepare": "npm run build"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.23.0",
|
"axios": "^0.23.0",
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ export class Config {
|
||||||
useOriginalSource: boolean = true;
|
useOriginalSource: boolean = true;
|
||||||
autoSearchSimilar: boolean = false;
|
autoSearchSimilar: boolean = false;
|
||||||
loadTagCounts: boolean = true;
|
loadTagCounts: boolean = true;
|
||||||
|
useContentTokens: boolean = true;
|
||||||
sites: Array<SzuruSiteConfig> = [];
|
sites: Array<SzuruSiteConfig> = [];
|
||||||
|
|
||||||
public async save() {
|
public async save() {
|
||||||
|
|
|
||||||
|
|
@ -111,3 +111,7 @@ export interface ImageSearchResult {
|
||||||
exactPost: Post | null;
|
exactPost: Post | null;
|
||||||
similarPosts: SimilarPost[];
|
similarPosts: SimilarPost[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TemporaryFileUploadResult {
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import {
|
||||||
SzuruError,
|
SzuruError,
|
||||||
ImageSearchResult,
|
ImageSearchResult,
|
||||||
TagFields,
|
TagFields,
|
||||||
SzuruNote
|
TemporaryFileUploadResult
|
||||||
} from "./SzuruTypes";
|
} from "./SzuruTypes";
|
||||||
import { PostViewModel } from "./ViewModels";
|
import { PostViewModel } from "./ViewModels";
|
||||||
|
|
||||||
|
|
@ -71,14 +71,19 @@ export default class SzuruWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createPost(post: PostViewModel): Promise<Post> {
|
async createPost(post: PostViewModel): Promise<Post> {
|
||||||
var obj = {
|
var obj = <any>{
|
||||||
tags: post.tags.map(x => x.name),
|
tags: post.tags.map(x => x.name),
|
||||||
safety: post.rating,
|
safety: post.rating,
|
||||||
source: post.source,
|
source: post.source,
|
||||||
notes: post.notes,
|
notes: post.notes
|
||||||
contentUrl: post.contentUrl
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (post.contentToken) {
|
||||||
|
obj.contentToken = post.contentToken;
|
||||||
|
} else {
|
||||||
|
obj.contentUrl = post.contentUrl;
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Create new post object");
|
console.log("Create new post object");
|
||||||
console.dir(obj);
|
console.dir(obj);
|
||||||
|
|
||||||
|
|
@ -90,6 +95,16 @@ export default class SzuruWrapper {
|
||||||
return (await this.apiPost("posts/reverse-search", obj)).data;
|
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 {
|
static createFromConfig(siteConfig: SzuruSiteConfig): SzuruWrapper | null {
|
||||||
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ export class PostViewModel {
|
||||||
tags: TagViewModel[] = [];
|
tags: TagViewModel[] = [];
|
||||||
notes: ScrapedNote[];
|
notes: ScrapedNote[];
|
||||||
contentUrl: string;
|
contentUrl: string;
|
||||||
|
contentToken: string | undefined;
|
||||||
pageUrl: string;
|
pageUrl: string;
|
||||||
contentType: ContentType;
|
contentType: ContentType;
|
||||||
rating: BooruTypes.SafetyRating;
|
rating: BooruTypes.SafetyRating;
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
<ul class="input">
|
<ul class="input">
|
||||||
<li>
|
<li>
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" name="autoSearchSimilar" v-model="autoSearchSimilar" />
|
<input type="checkbox" v-model="autoSearchSimilar" />
|
||||||
Automatically search for similar posts
|
Automatically search for similar posts
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -29,7 +29,7 @@
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" name="useOriginalSource" v-model="useOriginalSource" />
|
<input type="checkbox" v-model="useOriginalSource" />
|
||||||
Use original image source when available
|
Use original image source when available
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -39,7 +39,7 @@
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<label>
|
<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
|
Show how often a tag is used in the selected szurubooru instance
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
|
|
@ -47,6 +47,16 @@
|
||||||
the tag does not yet exist.
|
the tag does not yet exist.
|
||||||
</p>
|
</p>
|
||||||
</li>
|
</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>
|
</ul>
|
||||||
|
|
||||||
<div class="settings-footer">
|
<div class="settings-footer">
|
||||||
|
|
@ -75,6 +85,7 @@ export default Vue.extend({
|
||||||
autoSearchSimilar: false,
|
autoSearchSimilar: false,
|
||||||
loadTagCounts: false,
|
loadTagCounts: false,
|
||||||
useOriginalSource: false,
|
useOriginalSource: false,
|
||||||
|
useContentTokens: false,
|
||||||
statusText: "",
|
statusText: "",
|
||||||
statusType: "",
|
statusType: "",
|
||||||
};
|
};
|
||||||
|
|
@ -114,6 +125,7 @@ export default Vue.extend({
|
||||||
config.autoSearchSimilar = this.autoSearchSimilar;
|
config.autoSearchSimilar = this.autoSearchSimilar;
|
||||||
config.loadTagCounts = this.loadTagCounts;
|
config.loadTagCounts = this.loadTagCounts;
|
||||||
config.useOriginalSource = this.useOriginalSource;
|
config.useOriginalSource = this.useOriginalSource;
|
||||||
|
config.useContentTokens = this.useContentTokens;
|
||||||
|
|
||||||
await config.save();
|
await config.save();
|
||||||
|
|
||||||
|
|
@ -139,6 +151,7 @@ export default Vue.extend({
|
||||||
this.autoSearchSimilar = config.autoSearchSimilar;
|
this.autoSearchSimilar = config.autoSearchSimilar;
|
||||||
this.loadTagCounts = config.loadTagCounts;
|
this.loadTagCounts = config.loadTagCounts;
|
||||||
this.useOriginalSource = config.useOriginalSource;
|
this.useOriginalSource = config.useOriginalSource;
|
||||||
|
this.useContentTokens = config.useContentTokens;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ import axios, { CancelTokenSource } from "axios";
|
||||||
import { browser, Runtime, WebRequest } from "webextension-polyfill-ts";
|
import { browser, Runtime, WebRequest } from "webextension-polyfill-ts";
|
||||||
import { ScrapedPost, ScrapeResults } from "neo-scraper";
|
import { ScrapedPost, ScrapeResults } from "neo-scraper";
|
||||||
import SzuruWrapper from "../SzuruWrapper";
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
import { Post } from "../SzuruTypes";
|
import { ImageSearchResult, Post } from "../SzuruTypes";
|
||||||
import { Config, SzuruSiteConfig } from "../Config";
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
import { BrowserCommand, Message, getUrl, isChrome, encodeTagName } from "../Common";
|
import { BrowserCommand, Message, getUrl, isChrome, encodeTagName } from "../Common";
|
||||||
import { PostViewModel, TagViewModel } from "../ViewModels";
|
import { PostViewModel, TagViewModel } from "../ViewModels";
|
||||||
|
|
@ -320,7 +320,18 @@ export default Vue.extend({
|
||||||
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
|
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
|
||||||
|
|
||||||
try {
|
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) {
|
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||||
msg.content = "No similar posts found";
|
msg.content = "No similar posts found";
|
||||||
|
|
@ -493,7 +504,7 @@ export default Vue.extend({
|
||||||
if (post != undefined && post.referrer) {
|
if (post != undefined && post.referrer) {
|
||||||
console.log(`Setting referrer to '${post.referrer}' for request to '${post.contentUrl}'.`);
|
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.
|
// 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 });
|
requestHeaders.push({ name: "Referer", value: post.referrer });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,8 @@ async function uploadPost(post: PostViewModel) {
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
||||||
let error = ex as SzuruError;
|
let error = ex as SzuruError;
|
||||||
if (error) {
|
// TODO: Actual good error handling/messaging
|
||||||
|
if (error.name) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
switch (error.name) {
|
switch (error.name) {
|
||||||
case "PostAlreadyUploadedError":
|
case "PostAlreadyUploadedError":
|
||||||
|
|
@ -82,7 +83,7 @@ async function uploadPost(post: PostViewModel) {
|
||||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(msg, "error")));
|
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(msg, "error")));
|
||||||
break;
|
break;
|
||||||
default:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue