This commit is contained in:
MomentQYC 2024-10-04 23:46:39 +08:00
parent 73ae524e9c
commit 0356af4175
5 changed files with 39 additions and 23 deletions

View File

@ -99,8 +99,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
timeout: 60000, timeout: 60000,
}); });
let contentType = res.headers.get('content-type') || 'application/octet-stream';
if (res.headers.get('content-type') === 'audio/flac') { if (res.headers.get('content-type') === 'audio/flac') {
return res.body; return {
body: res.body,
headers: {
'Content-Type': contentType,
}
};
} else { } else {
throw new ApiError(meta.errors.unavailable); throw new ApiError(meta.errors.unavailable);
} }

View File

@ -543,7 +543,7 @@ function showMenu(): void {
return; return;
} }
const { menu, cleanup } = getNoteMenu({ note: note.value, translating, translation, isDeleted, currentClip: currentClip?.value }); const { menu, cleanup } = getNoteMenu({ note: note.value, translating, translation, convert, converting, isDeleted, currentClip: currentClip?.value });
os.popupMenu(menu, menuButton.value).then(focus).finally(cleanup); os.popupMenu(menu, menuButton.value).then(focus).finally(cleanup);
} }

View File

@ -24,12 +24,12 @@ SPDX-License-Identifier: AGPL-3.0-only
</MkFolder> </MkFolder>
<MkFolder> <MkFolder>
<template #label>Text-To-Speech</template> <template #label>Text-To-Speech</template>
<div class="_gaps_m"> <div class="_gaps_m">
<MkInput v-model="hfAuthKey"> <MkInput v-model="hfAuthKey">
<template #prefix><i class="ti ti-key"></i></template> <template #prefix><i class="ti ti-key"></i></template>
<template #label>HuggingFace Auth Key</template> <template #label>HuggingFace Auth Key</template>
</MkInput> </MkInput>
<MkButton primary @click="save_deepl">Save</MkButton>
</div> </div>
</MkFolder> </MkFolder>
</FormSuspense> </FormSuspense>

View File

@ -304,11 +304,11 @@ export function getNoteMenu(props: {
props.converting.value = true; props.converting.value = true;
const res = await misskeyApi('notes/tts', { const res = await misskeyApi('notes/tts', {
noteId: appearNote.id, noteId: appearNote.id,
}); }, undefined, undefined, true);
if (res.headers.get('Content-Type')?.startsWith('audio/')) { if (res.headers.get('Content-Type')?.startsWith('audio/')) {
props.convert.value = await res.blob(); props.convert.value = await res.blob();
} else { } else {
console.error('API did not return audio data.'); console.error('API did not return audio data.',res.headers.get('Content-Type') , await res.text());
} }
props.converting.value = false; props.converting.value = false;
} }

View File

@ -20,7 +20,8 @@ export function misskeyApi<
data: P = {} as any, data: P = {} as any,
token?: string | null | undefined, token?: string | null | undefined,
signal?: AbortSignal, signal?: AbortSignal,
): Promise<_ResT> { returnResponse: boolean = false
): Promise<_ResT | Response> {
if (endpoint.includes('://')) throw new Error('invalid endpoint'); if (endpoint.includes('://')) throw new Error('invalid endpoint');
pendingApiRequestsCount.value++; pendingApiRequestsCount.value++;
@ -28,7 +29,7 @@ export function misskeyApi<
pendingApiRequestsCount.value--; pendingApiRequestsCount.value--;
}; };
const promise = new Promise<_ResT>((resolve, reject) => { const promise = new Promise<_ResT | Response>((resolve, reject) => {
// Append a credential // Append a credential
if ($i) (data as any).i = $i.token; if ($i) (data as any).i = $i.token;
if (token !== undefined) (data as any).i = token; if (token !== undefined) (data as any).i = token;
@ -44,14 +45,17 @@ export function misskeyApi<
}, },
signal, signal,
}).then(async (res) => { }).then(async (res) => {
const body = res.status === 204 ? null : await res.json(); if (returnResponse) {
resolve(res);
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve(undefined as _ResT); // void -> undefined
} else { } else {
reject(body.error); const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve(undefined as _ResT); // void -> undefined
} else {
reject(body.error);
}
} }
}).catch(reject); }).catch(reject);
}); });
@ -70,7 +74,8 @@ export function misskeyApiGet<
>( >(
endpoint: E, endpoint: E,
data: P = {} as any, data: P = {} as any,
): Promise<_ResT> { returnResponse: boolean = false
): Promise<_ResT | Response> {
pendingApiRequestsCount.value++; pendingApiRequestsCount.value++;
const onFinally = () => { const onFinally = () => {
@ -79,21 +84,24 @@ export function misskeyApiGet<
const query = new URLSearchParams(data as any); const query = new URLSearchParams(data as any);
const promise = new Promise<_ResT>((resolve, reject) => { const promise = new Promise<_ResT | Response>((resolve, reject) => {
// Send request // Send request
window.fetch(`${apiUrl}/${endpoint}?${query}`, { window.fetch(`${apiUrl}/${endpoint}?${query}`, {
method: 'GET', method: 'GET',
credentials: 'omit', credentials: 'omit',
cache: 'default', cache: 'default',
}).then(async (res) => { }).then(async (res) => {
const body = res.status === 204 ? null : await res.json(); if (returnResponse) {
resolve(res);
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve(undefined as _ResT); // void -> undefined
} else { } else {
reject(body.error); const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve(undefined as _ResT); // void -> undefined
} else {
reject(body.error);
}
} }
}).catch(reject); }).catch(reject);
}); });
@ -102,3 +110,4 @@ export function misskeyApiGet<
return promise; return promise;
} }