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,
});
let contentType = res.headers.get('content-type') || 'application/octet-stream';
if (res.headers.get('content-type') === 'audio/flac') {
return res.body;
return {
body: res.body,
headers: {
'Content-Type': contentType,
}
};
} else {
throw new ApiError(meta.errors.unavailable);
}

View File

@ -543,7 +543,7 @@ function showMenu(): void {
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);
}

View File

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

View File

@ -304,11 +304,11 @@ export function getNoteMenu(props: {
props.converting.value = true;
const res = await misskeyApi('notes/tts', {
noteId: appearNote.id,
});
}, undefined, undefined, true);
if (res.headers.get('Content-Type')?.startsWith('audio/')) {
props.convert.value = await res.blob();
} 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;
}

View File

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