Fixed bugs related to URL file import and improved usability (#652)

# PR Checklist
- After uploading the character file to GitHub, conducted tests in both
the RisuAI web and Node environments.

# Description
- Fixed the bug where the character list was not updating in certain
situations after importing a URL.
- Removed the hash after import to prevent the same character from being
imported again upon refresh.
- Added exception handling for import file downloads.
This commit is contained in:
kwaroran
2024-10-22 21:52:01 +09:00
committed by GitHub

View File

@@ -300,12 +300,19 @@ export async function characterURLImport() {
const hash = location.hash
if(hash.startsWith('#import=')){
location.hash = ''
const url = hash.replace('#import=', '')
const res = await fetch(url, {
method: 'GET',
})
const data = new Uint8Array(await res.arrayBuffer())
importFile(getFileName(res), data)
try {
const res = await fetch(url, {
method: 'GET',
})
const data = new Uint8Array(await res.arrayBuffer())
await importFile(getFileName(res), data)
checkCharOrder()
} catch (error) {
alertError(language.errors.noData)
return null
}
}
if(hash.startsWith('#import_module=')){
const data = hash.replace('#import_module=', '')
@@ -381,7 +388,7 @@ export async function characterURLImport() {
for(const f of files){
const file = await f.getFile()
const data = new Uint8Array(await file.arrayBuffer())
importFile(f.name, data);
await importFile(f.name, data);
}
}
//@ts-ignore
@@ -399,7 +406,7 @@ export async function characterURLImport() {
if(files){
for(const file of files){
const data = await readFile(file)
importFile(file, data)
await importFile(file, data)
}
}
}
@@ -451,9 +458,9 @@ export async function characterURLImport() {
}
function getFileName(res : Response) : string {
return getFormContent(res.headers.get('content-disposition')) || getFromURL(res.url);
return getFromContent(res.headers.get('content-disposition')) || getFromURL(res.url);
function getFormContent(contentDisposition : string) {
function getFromContent(contentDisposition : string) {
if (!contentDisposition) return null;
const pattern = /filename\*=UTF-8''([^"';\n]+)|filename[^;\n=]*=["']?([^"';\n]+)["']?/;
const matches = contentDisposition.match(pattern);