Files
warren/frontend/lib/api/shares.ts
2025-08-29 15:32:23 +02:00

177 lines
4.2 KiB
TypeScript

import type { ApiResponse } from '~/shared/types/api';
import type { Share } from '~/shared/types/shares';
import { getApiHeaders } from '.';
import type { DirectoryEntry } from '~/shared/types';
export async function getShare(
shareId: string
): Promise<
{ success: true; share: Share; file: DirectoryEntry } | { success: false }
> {
const { data } = await useFetch<
ApiResponse<{ share: Share; file: DirectoryEntry }>
>(getApiUrl('warrens/files/get_share'), {
method: 'POST',
headers: getApiHeaders(false),
body: JSON.stringify({
shareId: shareId,
}),
});
if (data.value == null) {
return {
success: false,
};
}
const { share, file } = data.value.data;
return {
success: true,
share,
file,
};
}
export async function createShare(
warrenId: string,
path: string,
password: string | null,
lifetime: number | null
): Promise<{ success: true; share: Share } | { success: false }> {
const { data } = await useFetch<ApiResponse<Share>>(
getApiUrl('warrens/files/create_share'),
{
method: 'POST',
headers: getApiHeaders(),
body: JSON.stringify({
warrenId: warrenId,
path: path,
lifetime: lifetime,
password: password,
}),
}
);
if (data.value == null) {
return {
success: false,
};
}
return { success: true, share: data.value.data };
}
export async function listShares(
warrenId: string,
path: string
): Promise<{ success: true; shares: Share[] } | { success: false }> {
const { data } = await useFetch<ApiResponse<Share[]>>(
getApiUrl('warrens/files/list_shares'),
{
method: 'POST',
headers: getApiHeaders(),
body: JSON.stringify({
warrenId: warrenId,
path: path,
}),
}
);
if (data.value == null) {
return {
success: false,
};
}
return { success: true, shares: data.value.data };
}
export async function deleteShare(
warrenId: string,
shareId: string
): Promise<{ success: true; share: Share } | { success: false }> {
const { data } = await useFetch<ApiResponse<Share>>(
getApiUrl('warrens/files/delete_share'),
{
method: 'POST',
headers: getApiHeaders(),
body: JSON.stringify({
warrenId: warrenId,
shareId: shareId,
}),
}
);
if (data.value == null) {
return {
success: false,
};
}
return { success: true, share: data.value.data };
}
export async function listShareFiles(
shareId: string,
path: string,
password: string | null
): Promise<
| { success: true; files: DirectoryEntry[]; parent: DirectoryEntry | null }
| { success: false }
> {
const { data } = await useFetch<
ApiResponse<{ files: DirectoryEntry[]; parent: DirectoryEntry | null }>
>(getApiUrl('warrens/files/ls_share'), {
method: 'POST',
headers: getApiHeaders(),
body: JSON.stringify({
shareId: shareId,
path: path,
password: password,
}),
});
if (data.value == null) {
return {
success: false,
};
}
const { files, parent } = data.value.data;
return { success: true, files, parent };
}
export async function fetchShareFile(
shareId: string,
path: string,
password: string | null
): Promise<{ success: true; data: Blob } | { success: false }> {
const { data } = await useFetch<Blob>(
getApiUrl(`warrens/files/cat_share?shareId=${shareId}&path=${path}`),
{
method: 'GET',
headers:
password != null
? {
'X-Share-Password': password,
}
: {},
responseType: 'blob',
cache: 'default',
}
);
if (data.value == null) {
return {
success: false,
};
}
return {
success: true,
data: data.value,
};
}