file uploads
This commit is contained in:
@@ -26,8 +26,17 @@ export async function getWarrens(): Promise<Record<string, Warren>> {
|
||||
export async function getWarrenDirectory(
|
||||
path: string
|
||||
): Promise<DirectoryEntry[]> {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [warrenId, rest] = splitOnce(path, '/');
|
||||
|
||||
if (rest == null) {
|
||||
rest = '';
|
||||
} else {
|
||||
rest = '/' + rest;
|
||||
}
|
||||
|
||||
const { data: entries, error } = await useFetch<DirectoryEntry[]>(
|
||||
getApiUrl(`warrens/${path}`),
|
||||
getApiUrl(`warrens/${warrenId}/get${rest}`),
|
||||
{
|
||||
method: 'GET',
|
||||
}
|
||||
@@ -44,8 +53,17 @@ export async function createDirectory(
|
||||
path: string,
|
||||
directoryName: string
|
||||
): Promise<{ success: boolean }> {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [warrenId, rest] = splitOnce(path, '/');
|
||||
|
||||
if (rest == null) {
|
||||
rest = '';
|
||||
} else {
|
||||
rest += '/';
|
||||
}
|
||||
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${path}/${directoryName}`),
|
||||
getApiUrl(`warrens/${warrenId}/create/${rest}${directoryName}`),
|
||||
{
|
||||
method: 'POST',
|
||||
}
|
||||
@@ -74,8 +92,19 @@ export async function deleteWarrenEntry(
|
||||
directoryName: string,
|
||||
fileType: FileType
|
||||
): Promise<{ success: boolean }> {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [warrenId, rest] = splitOnce(path, '/');
|
||||
|
||||
if (rest == null) {
|
||||
rest = '';
|
||||
} else {
|
||||
rest = '/' + rest;
|
||||
}
|
||||
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${path}/${directoryName}?fileType=${fileType}`),
|
||||
getApiUrl(
|
||||
`warrens/${warrenId}/delete${rest}/${directoryName}?fileType=${fileType}`
|
||||
),
|
||||
{
|
||||
method: 'DELETE',
|
||||
}
|
||||
@@ -99,3 +128,48 @@ export async function deleteWarrenEntry(
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function uploadToWarren(
|
||||
path: string,
|
||||
files: FileList
|
||||
): Promise<{ success: boolean }> {
|
||||
const body = new FormData();
|
||||
for (const file of files) {
|
||||
body.append('files', file);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [warrenId, rest] = splitOnce(path, '/');
|
||||
|
||||
if (rest == null) {
|
||||
rest = '';
|
||||
} else {
|
||||
rest = '/' + rest;
|
||||
}
|
||||
|
||||
const { status } = await useFetch(
|
||||
getApiUrl(`warrens/${warrenId}/upload${rest}`),
|
||||
{
|
||||
method: 'POST',
|
||||
body,
|
||||
key: 'upload-' + new Date().getTime().toString(),
|
||||
}
|
||||
);
|
||||
|
||||
if (status.value !== 'success') {
|
||||
toast.error('Upload', {
|
||||
id: 'UPLOAD_FILE_TOAST',
|
||||
description: `Failed to upload`,
|
||||
});
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
await refreshNuxtData('current-directory');
|
||||
|
||||
toast.success('Upload', {
|
||||
id: 'UPLOAD_FILE_TOAST',
|
||||
description: `Successfully uploaded ${files.length} file${files.length !== 1 ? 's' : ''}`,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user