|
| 1 | +import { |
| 2 | + deleteFile, |
| 3 | + getSolidDataset, |
| 4 | + getContainedResourceUrlAll, |
| 5 | + isContainer, |
| 6 | + UrlString, |
| 7 | +} from "@inrupt/solid-client"; |
| 8 | +import { ensureTrailingSlash } from "./copyUtils"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Recursively deletes all contents of a folder, then deletes the folder itself |
| 12 | + */ |
| 13 | +async function deleteFolderContents( |
| 14 | + folderUrl: string, |
| 15 | + fetchFn: typeof fetch, |
| 16 | + visited: Set<string> = new Set() |
| 17 | +): Promise<void> { |
| 18 | + const normalizedUrl = ensureTrailingSlash(folderUrl); |
| 19 | + |
| 20 | + // Prevent infinite loops |
| 21 | + if (visited.has(normalizedUrl)) { |
| 22 | + return; |
| 23 | + } |
| 24 | + visited.add(normalizedUrl); |
| 25 | + |
| 26 | + try { |
| 27 | + const dataset = await getSolidDataset(normalizedUrl as UrlString, { fetch: fetchFn }); |
| 28 | + const containedResources = getContainedResourceUrlAll(dataset); |
| 29 | + |
| 30 | + // Delete all contained resources |
| 31 | + for (const resourceUrl of containedResources) { |
| 32 | + try { |
| 33 | + if (isContainer(resourceUrl)) { |
| 34 | + // It's a folder - recursively delete its contents first, then the folder |
| 35 | + await deleteFolderContents(resourceUrl, fetchFn, visited); |
| 36 | + // Delete the folder itself |
| 37 | + await deleteFile(resourceUrl as UrlString, { fetch: fetchFn }); |
| 38 | + |
| 39 | + // Also delete the .meta file if it exists |
| 40 | + try { |
| 41 | + const metaUrl = `${resourceUrl}.meta` as UrlString; |
| 42 | + await deleteFile(metaUrl, { fetch: fetchFn }); |
| 43 | + } catch { |
| 44 | + // Ignore if .meta file doesn't exist |
| 45 | + } |
| 46 | + } else { |
| 47 | + // It's a file - delete it |
| 48 | + await deleteFile(resourceUrl as UrlString, { fetch: fetchFn }); |
| 49 | + |
| 50 | + // Also delete the .meta file if it exists |
| 51 | + try { |
| 52 | + const metaUrl = `${resourceUrl}.meta` as UrlString; |
| 53 | + await deleteFile(metaUrl, { fetch: fetchFn }); |
| 54 | + } catch { |
| 55 | + // Ignore if .meta file doesn't exist |
| 56 | + } |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + console.warn(`Failed to delete resource ${resourceUrl}:`, error); |
| 60 | + |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (error) { |
| 64 | + console.error(`Failed to access folder ${folderUrl}:`, error); |
| 65 | + throw error; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Deletes a file resource |
| 71 | + */ |
| 72 | +export async function deleteFileResource( |
| 73 | + fileUrl: string, |
| 74 | + fetchFn: typeof fetch |
| 75 | +): Promise<void> { |
| 76 | + try { |
| 77 | + // Delete the file |
| 78 | + await deleteFile(fileUrl as UrlString, { fetch: fetchFn }); |
| 79 | + |
| 80 | + // Also try to delete the .meta file if it exists |
| 81 | + try { |
| 82 | + const metaUrl = `${fileUrl}.meta` as UrlString; |
| 83 | + await deleteFile(metaUrl, { fetch: fetchFn }); |
| 84 | + } catch { |
| 85 | + // Ignore if .meta file doesn't exist |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + console.error("Failed to delete file:", error); |
| 89 | + throw new Error( |
| 90 | + `Failed to delete file: ${error instanceof Error ? error.message : "Unknown error"}` |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Deletes a folder resource and all its contents recursively |
| 97 | + */ |
| 98 | +export async function deleteFolderResource( |
| 99 | + folderUrl: string, |
| 100 | + fetchFn: typeof fetch |
| 101 | +): Promise<void> { |
| 102 | + try { |
| 103 | + const normalizedUrl = ensureTrailingSlash(folderUrl); |
| 104 | + |
| 105 | + // First, delete all contents recursively |
| 106 | + await deleteFolderContents(normalizedUrl, fetchFn); |
| 107 | + |
| 108 | + // Then delete the folder itself |
| 109 | + await deleteFile(normalizedUrl as UrlString, { fetch: fetchFn }); |
| 110 | + |
| 111 | + // Also try to delete the .meta file if it exists |
| 112 | + try { |
| 113 | + const metaUrl = `${normalizedUrl}.meta` as UrlString; |
| 114 | + await deleteFile(metaUrl, { fetch: fetchFn }); |
| 115 | + } catch { |
| 116 | + // Ignore if .meta file doesn't exist |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + console.error("Failed to delete folder:", error); |
| 120 | + throw new Error( |
| 121 | + `Failed to delete folder: ${error instanceof Error ? error.message : "Unknown error"}` |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments