176 lines
4.1 KiB
TypeScript
176 lines
4.1 KiB
TypeScript
import { toast } from 'vue-sonner';
|
|
import type { DirectoryEntry, FileType } from '~/types';
|
|
import type { Warren } from '~/types/warrens';
|
|
|
|
export async function getWarrens(): Promise<Record<string, Warren>> {
|
|
const { data: arr, error } = await useFetch<Warren[]>(
|
|
getApiUrl('warrens'),
|
|
{
|
|
method: 'GET',
|
|
}
|
|
);
|
|
|
|
if (arr.value == null) {
|
|
throw error.value?.name;
|
|
}
|
|
|
|
const warrens: Record<string, Warren> = {};
|
|
|
|
for (const warren of arr.value) {
|
|
warrens[warren.id] = warren;
|
|
}
|
|
|
|
return warrens;
|
|
}
|
|
|
|
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/${warrenId}/get${rest}`),
|
|
{
|
|
method: 'GET',
|
|
}
|
|
);
|
|
|
|
if (entries.value == null) {
|
|
throw error.value?.name;
|
|
}
|
|
|
|
return entries.value;
|
|
}
|
|
|
|
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/${warrenId}/create/${rest}${directoryName}`),
|
|
{
|
|
method: 'POST',
|
|
}
|
|
);
|
|
|
|
if (status.value !== 'success') {
|
|
toast.error('Directory', {
|
|
id: 'CREATE_DIRECTORY_TOAST',
|
|
description: `Failed to create directory`,
|
|
});
|
|
return { success: false };
|
|
}
|
|
|
|
await refreshNuxtData('current-directory');
|
|
|
|
toast.success('Directory', {
|
|
id: 'CREATE_DIRECTORY_TOAST',
|
|
description: `Successfully created directory: ${directoryName}`,
|
|
});
|
|
|
|
return { success: true };
|
|
}
|
|
|
|
export async function deleteWarrenEntry(
|
|
path: string,
|
|
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/${warrenId}/delete${rest}/${directoryName}?fileType=${fileType}`
|
|
),
|
|
{
|
|
method: 'DELETE',
|
|
}
|
|
);
|
|
|
|
const toastTitle = fileType.slice(0, 1).toUpperCase() + fileType.slice(1);
|
|
|
|
if (status.value !== 'success') {
|
|
toast.error(toastTitle, {
|
|
id: 'DELETE_DIRECTORY_TOAST',
|
|
description: `Failed to delete ${fileType}`,
|
|
});
|
|
return { success: false };
|
|
}
|
|
|
|
await refreshNuxtData('current-directory');
|
|
|
|
toast.success(toastTitle, {
|
|
id: 'DELETE_DIRECTORY_TOAST',
|
|
description: `Successfully deleted ${fileType}: ${directoryName}`,
|
|
});
|
|
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 };
|
|
}
|