basic file sharing
This commit is contained in:
176
frontend/lib/api/shares.ts
Normal file
176
frontend/lib/api/shares.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
12
frontend/lib/schemas/share.ts
Normal file
12
frontend/lib/schemas/share.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { number, object, string } from 'yup';
|
||||
|
||||
export const shareSchema = object({
|
||||
password: string()
|
||||
.trim()
|
||||
.transform((s: string) => (s.length > 0 ? s : undefined))
|
||||
.optional(),
|
||||
lifetime: number()
|
||||
.positive()
|
||||
.transform((n) => (isNaN(n) ? undefined : n))
|
||||
.optional(),
|
||||
});
|
||||
Reference in New Issue
Block a user