Make risurealm share and download to use new api

This commit is contained in:
kwaroran
2024-03-28 03:59:14 +09:00
parent ea6b1d20e5
commit 9f0d398d3b
2 changed files with 64 additions and 46 deletions

View File

@@ -61,7 +61,7 @@
if(!latin1.test(enNotes)){ if(!latin1.test(enNotes)){
alertError("English version of creator notes must contain only Latin-1 characters") alertError("English version of creator notes must contain only Latin-1 characters")
} }
shareRisuHub(char, { shareRisuHub2(char, {
anon: privateMode, anon: privateMode,
nsfw: nsfwMode, nsfw: nsfwMode,
tag: tags, tag: tags,
@@ -79,7 +79,7 @@
import { XIcon } from "lucide-svelte"; import { XIcon } from "lucide-svelte";
import { language } from "src/lang"; import { language } from "src/lang";
import { alertError } from "src/ts/alert"; import { alertError } from "src/ts/alert";
import { shareRisuHub } from "src/ts/characterCards"; import { shareRisuHub2 } from "src/ts/characterCards";
import { DataBase, type character } from "src/ts/storage/database"; import { DataBase, type character } from "src/ts/storage/database";
import TextInput from "../GUI/TextInput.svelte"; import TextInput from "../GUI/TextInput.svelte";
import Button from "../GUI/Button.svelte"; import Button from "../GUI/Button.svelte";

View File

@@ -730,51 +730,56 @@ export async function shareRisuHub2(char:character, arg:{
license: string license: string
anon: boolean anon: boolean
}) { }) {
char = cloneDeep(char) try {
char.license = arg.license char = cloneDeep(char)
let tagList = arg.tag.split(',') char.license = arg.license
let tagList = arg.tag.split(',')
if(arg.nsfw){
tagList.push("nsfw") if(arg.nsfw){
} tagList.push("nsfw")
await alertWait("Uploading...")
let tags = tagList.filter((v, i) => {
return (!!v) && (tagList.indexOf(v) === i)
})
char.tags = tags
const writer = new VirtualWriter()
await exportSpecV2(char, 'png', {writer: writer})
const fetchPromise = fetch(hubURL + '/hub/realm/upload', {
method: "POST",
body: writer.buf.buffer,
headers: {
"Content-Type": 'image/png',
"x-risu-api-version": "4",
"x-risu-token": get(DataBase)?.account?.token,
'x-risu-username': arg.anon ? '' : (get(DataBase)?.account?.id),
} }
})
await alertWait("Uploading...")
const res = await fetchPromise
let tags = tagList.filter((v, i) => {
if(res.status !== 200){ return (!!v) && (tagList.indexOf(v) === i)
alertError(await res.text()) })
} char.tags = tags
else{
const resJSON = await res.json()
alertNormal(resJSON.message) const writer = new VirtualWriter()
const currentChar = get(CurrentCharacter) await exportSpecV2(char, 'png', {writer: writer})
if(currentChar.type === 'group'){
return const fetchPromise = fetch(hubURL + '/hub/realm/upload', {
method: "POST",
body: writer.buf.buffer,
headers: {
"Content-Type": 'image/png',
"x-risu-api-version": "4",
"x-risu-token": get(DataBase)?.account?.token,
'x-risu-username': arg.anon ? '' : (get(DataBase)?.account?.id),
'x-risu-debug': 'true'
}
})
const res = await fetchPromise
if(res.status !== 200){
alertError(await res.text())
} }
currentChar.realmId = resJSON.id else{
const resJSON = await res.json()
alertMd(resJSON.message)
const currentChar = get(CurrentCharacter)
if(currentChar.type === 'group'){
return
}
currentChar.realmId = resJSON.id
}
} catch (error) {
alertError(`${error}`)
} }
} }
@@ -904,11 +909,24 @@ export async function downloadRisuHub(id:string) {
type: "wait", type: "wait",
msg: "Downloading..." msg: "Downloading..."
}) })
const res = await fetch(hubURL + '/hub/get/' + id) const res = await fetch(hubURL + '/hub/get/' + id, {
headers: {
"x-risu-api-version": "4"
}
})
if(res.status !== 200){ if(res.status !== 200){
alertError(await res.text()) alertError(await res.text())
return return
} }
if(res.headers.get('content-type') === 'image/png'){
await importCharacterProcess({
name: 'realm.png',
data: res.body
})
checkCharOrder()
return
}
const result = await res.json() const result = await res.json()
const data:CharacterCardV2 = result.card const data:CharacterCardV2 = result.card