import { toast } from 'vue-sonner'; import type { DirectoryEntry, FileType } from '~/types'; import type { Warren } from '~/types/warrens'; export async function getWarrens(): Promise> { const { data: arr, error } = await useFetch( getApiUrl('warrens'), { method: 'GET', } ); if (arr.value == null) { throw error.value?.name; } const warrens: Record = {}; for (const warren of arr.value) { warrens[warren.id] = warren; } return warrens; } export async function getWarrenDirectory( path: string ): Promise { // eslint-disable-next-line prefer-const let [warrenId, rest] = splitOnce(path, '/'); if (rest == null) { rest = ''; } else { rest = '/' + rest; } const { data: entries, error } = await useFetch( 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, onProgress: ((loaded: number, total: number) => void) | undefined ): 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 xhr = new XMLHttpRequest(); xhr.open('POST', getApiUrl(`warrens/${warrenId}/upload${rest}`)); xhr.upload.onprogress = (e) => { onProgress?.(e.loaded, e.total); }; const promise = new Promise((res, rej) => { xhr.onreadystatechange = (_) => { if (xhr.readyState !== 4) { return; } if (xhr.status === 200) { res(); } else { rej(); } }; }); xhr.send(body); try { await promise; } catch { 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 }; }