filecove 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/THIRD_PARTY_NOTICES.md +63 -0
- package/dist/components/buttons/toolbar-actions.d.ts +28 -0
- package/dist/components/buttons/tooltip-icon-button.d.ts +14 -0
- package/dist/components/downloads/download-panel.d.ts +23 -0
- package/dist/components/drag/drag-preview.d.ts +24 -0
- package/dist/components/file-manager/announcer.d.ts +14 -0
- package/dist/components/file-manager/content/file-grid.d.ts +8 -0
- package/dist/components/file-manager/content/file-list.d.ts +9 -0
- package/dist/components/file-manager/content/folder-view.d.ts +83 -0
- package/dist/components/file-manager/content/item-icon.d.ts +16 -0
- package/dist/components/file-manager/content/no-sections.d.ts +11 -0
- package/dist/components/file-manager/content/search-results.d.ts +40 -0
- package/dist/components/file-manager/content/search-tabs.d.ts +29 -0
- package/dist/components/file-manager/content/trash-bar.d.ts +13 -0
- package/dist/components/file-manager/context-menu/context-menu.d.ts +20 -0
- package/dist/components/file-manager/dialogs/conflict-step.d.ts +28 -0
- package/dist/components/file-manager/dialogs/delete-dialog.d.ts +49 -0
- package/dist/components/file-manager/dialogs/image-viewer.d.ts +42 -0
- package/dist/components/file-manager/dialogs/name-dialog.d.ts +41 -0
- package/dist/components/file-manager/dialogs/paste-dialog.d.ts +39 -0
- package/dist/components/file-manager/dialogs/preview-dialog.d.ts +75 -0
- package/dist/components/file-manager/dialogs/restore-dialog.d.ts +41 -0
- package/dist/components/file-manager/dialogs/shortcuts-dialog.d.ts +36 -0
- package/dist/components/file-manager/dialogs/transfer-dialog.d.ts +71 -0
- package/dist/components/file-manager/dialogs/upload-dialog.d.ts +34 -0
- package/dist/components/file-manager/file-manager.d.ts +320 -0
- package/dist/components/file-manager/find-layer.d.ts +29 -0
- package/dist/components/file-manager/header-bar/header-bar.d.ts +65 -0
- package/dist/components/file-manager/header-bar/history-menu.d.ts +18 -0
- package/dist/components/file-manager/header-bar/path-bar.d.ts +19 -0
- package/dist/components/file-manager/header-bar/path-field.d.ts +23 -0
- package/dist/components/file-manager/header-bar/search-field.d.ts +27 -0
- package/dist/components/file-manager/header-bar/search-sections-menu.d.ts +25 -0
- package/dist/components/file-manager/header-bar/view-menu.d.ts +39 -0
- package/dist/components/file-manager/notice-bar.d.ts +36 -0
- package/dist/components/file-manager/properties-pane.d.ts +53 -0
- package/dist/components/file-manager/resize-edge.d.ts +39 -0
- package/dist/components/file-manager/selection-bar.d.ts +15 -0
- package/dist/components/file-manager/sidebar/location-item.d.ts +36 -0
- package/dist/components/file-manager/sidebar/main-menu.d.ts +23 -0
- package/dist/components/file-manager/sidebar/sidebar.d.ts +80 -0
- package/dist/components/icons/check-badge.d.ts +3 -0
- package/dist/components/icons/create-icon.d.ts +20 -0
- package/dist/components/icons/file-icon.d.ts +19 -0
- package/dist/components/icons/icon-provider.d.ts +21 -0
- package/dist/components/icons/icon-slot.d.ts +6 -0
- package/dist/components/icons/icon.d.ts +19 -0
- package/dist/components/icons/locked-icon.d.ts +9 -0
- package/dist/components/media-manager/asset-delete.d.ts +29 -0
- package/dist/components/media-manager/asset-hold.d.ts +32 -0
- package/dist/components/media-manager/asset-option.d.ts +74 -0
- package/dist/components/media-manager/asset-trail.d.ts +13 -0
- package/dist/components/media-manager/choice-list.d.ts +29 -0
- package/dist/components/media-manager/choices-pane.d.ts +50 -0
- package/dist/components/media-manager/choose-bar.d.ts +26 -0
- package/dist/components/media-manager/drop-zone.d.ts +7 -0
- package/dist/components/media-manager/group-check.d.ts +13 -0
- package/dist/components/media-manager/group-create.d.ts +20 -0
- package/dist/components/media-manager/group-path.d.ts +24 -0
- package/dist/components/media-manager/library-list.d.ts +23 -0
- package/dist/components/media-manager/media-asset.d.ts +65 -0
- package/dist/components/media-manager/media-edit-many.d.ts +33 -0
- package/dist/components/media-manager/media-fields.d.ts +31 -0
- package/dist/components/media-manager/media-filter-chips.d.ts +17 -0
- package/dist/components/media-manager/media-filters-dialog.d.ts +39 -0
- package/dist/components/media-manager/media-filters.d.ts +37 -0
- package/dist/components/media-manager/media-form.d.ts +29 -0
- package/dist/components/media-manager/media-frame.d.ts +77 -0
- package/dist/components/media-manager/media-grid.d.ts +114 -0
- package/dist/components/media-manager/media-group.d.ts +29 -0
- package/dist/components/media-manager/media-header.d.ts +80 -0
- package/dist/components/media-manager/media-manager.d.ts +309 -0
- package/dist/components/media-manager/media-move.d.ts +30 -0
- package/dist/components/media-manager/media-picker.d.ts +140 -0
- package/dist/components/media-manager/media-picture.d.ts +26 -0
- package/dist/components/media-manager/media-process.d.ts +26 -0
- package/dist/components/media-manager/media-row.d.ts +13 -0
- package/dist/components/media-manager/media-step.d.ts +33 -0
- package/dist/components/media-manager/media-tile.d.ts +15 -0
- package/dist/components/media-manager/media-upload.d.ts +46 -0
- package/dist/components/media-manager/open-button.d.ts +15 -0
- package/dist/components/media-manager/picker-frame.d.ts +33 -0
- package/dist/components/media-manager/selection-bar.d.ts +25 -0
- package/dist/components/media-manager/upload-tile.d.ts +17 -0
- package/dist/components/properties/properties-panel.d.ts +53 -0
- package/dist/components/properties/property-display.d.ts +15 -0
- package/dist/components/properties/property-editor.d.ts +26 -0
- package/dist/components/properties/property-input.d.ts +36 -0
- package/dist/components/transfers/transfer-panel.d.ts +25 -0
- package/dist/components/ui/backdrop.d.ts +7 -0
- package/dist/components/ui/button-base.d.ts +7 -0
- package/dist/components/ui/button.d.ts +18 -0
- package/dist/components/ui/checkbox.d.ts +24 -0
- package/dist/components/ui/chip.d.ts +18 -0
- package/dist/components/ui/circular-progress.d.ts +14 -0
- package/dist/components/ui/collapse.d.ts +9 -0
- package/dist/components/ui/dialog.d.ts +40 -0
- package/dist/components/ui/divider.d.ts +4 -0
- package/dist/components/ui/icon-button.d.ts +12 -0
- package/dist/components/ui/linear-progress.d.ts +13 -0
- package/dist/components/ui/link.d.ts +4 -0
- package/dist/components/ui/menu.d.ts +77 -0
- package/dist/components/ui/root-providers.d.ts +16 -0
- package/dist/components/ui/text-field.d.ts +38 -0
- package/dist/components/ui/text.d.ts +21 -0
- package/dist/components/ui/tooltip.d.ts +24 -0
- package/dist/components/uploads/upload-panel.d.ts +31 -0
- package/dist/contexts/asset-look-context.d.ts +1 -0
- package/dist/contexts/cut-context.d.ts +6 -0
- package/dist/contexts/data-context.d.ts +12 -0
- package/dist/contexts/direction-context.d.ts +2 -0
- package/dist/contexts/drag-context.d.ts +36 -0
- package/dist/contexts/icon-context.d.ts +10 -0
- package/dist/contexts/locale-context.d.ts +2 -0
- package/dist/contexts/moving-context.d.ts +7 -0
- package/dist/create-icon-D2vnnBQX.js +40 -0
- package/dist/create-icon-D2vnnBQX.js.map +1 -0
- package/dist/hooks/use-announcer.d.ts +20 -0
- package/dist/hooks/use-asset-look.d.ts +6 -0
- package/dist/hooks/use-asset-states.d.ts +12 -0
- package/dist/hooks/use-chosen-column.d.ts +16 -0
- package/dist/hooks/use-clipboard-follow.d.ts +12 -0
- package/dist/hooks/use-controlled.d.ts +18 -0
- package/dist/hooks/use-cut-ids.d.ts +3 -0
- package/dist/hooks/use-dev-checks.d.ts +16 -0
- package/dist/hooks/use-download-queue.d.ts +19 -0
- package/dist/hooks/use-dragged-ids.d.ts +3 -0
- package/dist/hooks/use-drop-target.d.ts +48 -0
- package/dist/hooks/use-entry-drag.d.ts +46 -0
- package/dist/hooks/use-files-drop.d.ts +20 -0
- package/dist/hooks/use-folder.d.ts +14 -0
- package/dist/hooks/use-fork-ref.d.ts +6 -0
- package/dist/hooks/use-form-send.d.ts +12 -0
- package/dist/hooks/use-found.d.ts +10 -0
- package/dist/hooks/use-icon-config.d.ts +2 -0
- package/dist/hooks/use-level-filters.d.ts +112 -0
- package/dist/hooks/use-level-search.d.ts +21 -0
- package/dist/hooks/use-locale.d.ts +10 -0
- package/dist/hooks/use-location-history.d.ts +42 -0
- package/dist/hooks/use-long-press.d.ts +25 -0
- package/dist/hooks/use-marquee.d.ts +35 -0
- package/dist/hooks/use-match-count.d.ts +8 -0
- package/dist/hooks/use-media-search.d.ts +44 -0
- package/dist/hooks/use-moving-ids.d.ts +3 -0
- package/dist/hooks/use-picks.d.ts +39 -0
- package/dist/hooks/use-preview-contents.d.ts +33 -0
- package/dist/hooks/use-properties.d.ts +25 -0
- package/dist/hooks/use-scroll-measure.d.ts +34 -0
- package/dist/hooks/use-search.d.ts +12 -0
- package/dist/hooks/use-selection.d.ts +29 -0
- package/dist/hooks/use-setting.d.ts +21 -0
- package/dist/hooks/use-stable-set.d.ts +6 -0
- package/dist/hooks/use-swipe.d.ts +41 -0
- package/dist/hooks/use-text-direction.d.ts +8 -0
- package/dist/hooks/use-thumbnail.d.ts +8 -0
- package/dist/hooks/use-transfer-queue.d.ts +22 -0
- package/dist/hooks/use-tree-rows.d.ts +9 -0
- package/dist/hooks/use-tree-selection.d.ts +19 -0
- package/dist/hooks/use-type-ahead.d.ts +9 -0
- package/dist/hooks/use-type-icons.d.ts +10 -0
- package/dist/hooks/use-upload-queue.d.ts +24 -0
- package/dist/hooks/use-virtual-lines.d.ts +27 -0
- package/dist/hooks/use-virtual-rows.d.ts +31 -0
- package/dist/hooks/use-wheel-zoom.d.ts +10 -0
- package/dist/icon-sets/filled.d.ts +6 -0
- package/dist/icon-sets/filled.js +76 -0
- package/dist/icon-sets/filled.js.map +1 -0
- package/dist/icon-sets/outlined.d.ts +6 -0
- package/dist/icon-sets/outlined.js +73 -0
- package/dist/icon-sets/outlined.js.map +1 -0
- package/dist/icon-sets/rounded.d.ts +6 -0
- package/dist/icon-sets/rounded.js +73 -0
- package/dist/icon-sets/rounded.js.map +1 -0
- package/dist/icon-sets/sharp.d.ts +6 -0
- package/dist/icon-sets/sharp.js +73 -0
- package/dist/icon-sets/sharp.js.map +1 -0
- package/dist/icon-sets/two-tone.d.ts +6 -0
- package/dist/icon-sets/two-tone.js +188 -0
- package/dist/icon-sets/two-tone.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +32651 -0
- package/dist/index.js.map +1 -0
- package/dist/locales/en-us.d.ts +7 -0
- package/dist/store/download-queue.d.ts +37 -0
- package/dist/store/folder-store.d.ts +79 -0
- package/dist/store/keyboard-layout.d.ts +11 -0
- package/dist/store/properties-cache.d.ts +73 -0
- package/dist/store/thumbnail-cache.d.ts +30 -0
- package/dist/store/transfer-across.d.ts +24 -0
- package/dist/store/transfer-queue.d.ts +59 -0
- package/dist/store/upload-queue.d.ts +83 -0
- package/dist/styles/arrived.d.ts +6 -0
- package/dist/styles/classes.d.ts +212 -0
- package/dist/styles/layout.d.ts +55 -0
- package/dist/styles/media-layout.d.ts +36 -0
- package/dist/styles/order.d.ts +1 -0
- package/dist/styles.css +7632 -0
- package/dist/styles.layer.css +7634 -0
- package/dist/types/action.d.ts +296 -0
- package/dist/types/adapter.d.ts +922 -0
- package/dist/types/api.d.ts +192 -0
- package/dist/types/clipboard.d.ts +45 -0
- package/dist/types/download.d.ts +41 -0
- package/dist/types/drag.d.ts +57 -0
- package/dist/types/folder.d.ts +47 -0
- package/dist/types/icon.d.ts +20 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/limits.d.ts +49 -0
- package/dist/types/locale.d.ts +1132 -0
- package/dist/types/main-menu.d.ts +20 -0
- package/dist/types/media.d.ts +631 -0
- package/dist/types/menu.d.ts +82 -0
- package/dist/types/navigation.d.ts +88 -0
- package/dist/types/preview.d.ts +54 -0
- package/dist/types/previewer.d.ts +67 -0
- package/dist/types/properties.d.ts +111 -0
- package/dist/types/property-definition.d.ts +286 -0
- package/dist/types/search.d.ts +46 -0
- package/dist/types/section.d.ts +98 -0
- package/dist/types/selection.d.ts +52 -0
- package/dist/types/slots.d.ts +218 -0
- package/dist/types/transfer.d.ts +68 -0
- package/dist/types/tree.d.ts +97 -0
- package/dist/types/undo.d.ts +133 -0
- package/dist/types/upload.d.ts +189 -0
- package/dist/types/view.d.ts +192 -0
- package/dist/utils/action-groups.d.ts +17 -0
- package/dist/utils/bidi.d.ts +15 -0
- package/dist/utils/clipboard.d.ts +30 -0
- package/dist/utils/cx.d.ts +2 -0
- package/dist/utils/dom.d.ts +36 -0
- package/dist/utils/download.d.ts +30 -0
- package/dist/utils/field-errors.d.ts +26 -0
- package/dist/utils/files.d.ts +48 -0
- package/dist/utils/format.d.ts +10 -0
- package/dist/utils/groups.d.ts +29 -0
- package/dist/utils/hidden.d.ts +3 -0
- package/dist/utils/keyboard.d.ts +21 -0
- package/dist/utils/limits.d.ts +7 -0
- package/dist/utils/media.d.ts +146 -0
- package/dist/utils/menu-position.d.ts +13 -0
- package/dist/utils/names.d.ts +21 -0
- package/dist/utils/navigation.d.ts +28 -0
- package/dist/utils/path.d.ts +33 -0
- package/dist/utils/permissions.d.ts +56 -0
- package/dist/utils/preview.d.ts +27 -0
- package/dist/utils/property-value.d.ts +41 -0
- package/dist/utils/resize.d.ts +2 -0
- package/dist/utils/search.d.ts +24 -0
- package/dist/utils/sections.d.ts +11 -0
- package/dist/utils/selection.d.ts +30 -0
- package/dist/utils/sets.d.ts +2 -0
- package/dist/utils/shortcut.d.ts +74 -0
- package/dist/utils/slots.d.ts +15 -0
- package/dist/utils/sort.d.ts +47 -0
- package/dist/utils/transfer.d.ts +48 -0
- package/dist/utils/trash.d.ts +25 -0
- package/dist/utils/undo.d.ts +63 -0
- package/dist/utils/zoom.d.ts +14 -0
- package/docs/README.md +55 -0
- package/docs/actions.md +301 -0
- package/docs/adapter.md +229 -0
- package/docs/copy-move-trash-undo.md +97 -0
- package/docs/customizing.md +247 -0
- package/docs/getting-started.md +168 -0
- package/docs/locale-and-rtl.md +58 -0
- package/docs/media-manager.md +498 -0
- package/docs/previewing.md +124 -0
- package/docs/properties.md +125 -0
- package/docs/recipes/in-memory-adapter.md +149 -0
- package/docs/recipes/rest-adapter.md +225 -0
- package/docs/recipes/s3-adapter.md +137 -0
- package/docs/search.md +73 -0
- package/docs/sections.md +119 -0
- package/docs/state-and-api.md +190 -0
- package/docs/uploads-and-downloads.md +144 -0
- package/package.json +90 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Properties
|
|
2
|
+
|
|
3
|
+
The properties panel shows what's selected in the content, or the open folder while nothing is, and follows the selection. It docks beside the content when the file manager is at least 1100px wide, and opens over it when narrower. Alt+Enter, the view menu, or `context.properties()` show it.
|
|
4
|
+
|
|
5
|
+
It works with any adapter: what the listing brings (type, size, location, dates, a picture's dimensions) is shown with nothing more. Three optional members add to it.
|
|
6
|
+
|
|
7
|
+
## The backend's own fields: `properties`
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
properties?(entry: FileEntry, options: { signal: AbortSignal }): Promise<EntryProperties>;
|
|
11
|
+
|
|
12
|
+
interface EntryProperties {
|
|
13
|
+
entry?: FileEntry; // fields the listing left out as too costly: createdAt, media...
|
|
14
|
+
groups?: PropertyGroup[]; // the backend's own fields, described
|
|
15
|
+
values?: Record<string, unknown>; // plain values, for definitions of your own (media manager)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PropertyGroup {
|
|
19
|
+
id: string;
|
|
20
|
+
title?: string;
|
|
21
|
+
fields: PropertyField[];
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Each field has an `id`, a `label` in your users' language, and a typed value the panel writes out in the `locale`:
|
|
26
|
+
|
|
27
|
+
| `type` | `value` | Extra |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `"text"` | string | `copyable`, `editable`, `multiline`, `maxLength` |
|
|
30
|
+
| `"number"` | number | shown grouped |
|
|
31
|
+
| `"size"` | bytes | shown as "4.2 MB" |
|
|
32
|
+
| `"date"` | `Date` | |
|
|
33
|
+
| `"link"` | the link's text | `href`, opened in a new tab |
|
|
34
|
+
| `"tags"` | string[] | `editable` |
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
async properties(entry, { signal }) {
|
|
38
|
+
const info = await api.info(entry.id, signal);
|
|
39
|
+
return {
|
|
40
|
+
entry: { ...entry, createdAt: new Date(info.createdAt) },
|
|
41
|
+
groups: [
|
|
42
|
+
{
|
|
43
|
+
id: "sharing",
|
|
44
|
+
title: "Sharing",
|
|
45
|
+
fields: [
|
|
46
|
+
{ id: "owner", label: "Owner", type: "text", value: info.owner },
|
|
47
|
+
{ id: "link", label: "Link", type: "link", value: "Open", href: info.publicUrl },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "details",
|
|
52
|
+
fields: [
|
|
53
|
+
{ id: "checksum", label: "SHA-256", type: "text", value: info.sha256, copyable: true },
|
|
54
|
+
{ id: "description", label: "Description", type: "text", value: info.description ?? "",
|
|
55
|
+
editable: true, multiline: true, maxLength: 500 },
|
|
56
|
+
{ id: "tags", label: "Tags", type: "tags", value: info.tags, editable: true },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
- It's called a moment after the panel comes to show one entry, so moving through a list with the arrow keys only asks where the user stops.
|
|
65
|
+
- A folder shown from its own menu, the open folder or a section's root, may come as an entry with only `id`, `name` and `kind`.
|
|
66
|
+
- Results are kept for as long as the entry is listed unchanged.
|
|
67
|
+
- Rejecting with an `Error` shows its message in place of the fields, with Try again; what the listing brought still shows.
|
|
68
|
+
|
|
69
|
+
## Editing fields: `updateProperties`
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
updateProperties?(entry: FileEntry, values: Record<string, unknown>, options: { signal: AbortSignal }):
|
|
73
|
+
Promise<EntryProperties | void>;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Fields marked `editable` (text and tags) get an Edit button and are changed in place in the panel, one at a time. `values` holds the field's new value by its `id`: a string for text, trimmed; a string array for tags, trimmed, each once, none empty.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
async updateProperties(entry, values, { signal }) {
|
|
80
|
+
await api.update(entry.id, values, signal); // e.g. { description: "New text" }
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- Resolve with the entry's properties as they now are, to show what the backend made of them, or with nothing, and the value given shows as saved.
|
|
85
|
+
- Reject with an `Error` to show its message under the field, which stays as the user left it, to try again.
|
|
86
|
+
- Without `updateProperties`, nothing is editable, whatever the fields say.
|
|
87
|
+
- Fields aren't edited in a read-only section, the trash, a folder whose permissions refuse writing, or while a move holds the entry.
|
|
88
|
+
- An editable field's `id` must be unique among the entry's groups.
|
|
89
|
+
|
|
90
|
+
## Folder totals: `measure`
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
measure?(folders: FileEntry[], options: {
|
|
94
|
+
signal: AbortSignal;
|
|
95
|
+
showHidden: boolean;
|
|
96
|
+
onProgress: (totals: EntryTotals) => void;
|
|
97
|
+
}): Promise<EntryTotals>;
|
|
98
|
+
|
|
99
|
+
interface EntryTotals {
|
|
100
|
+
files: number;
|
|
101
|
+
folders: number;
|
|
102
|
+
size: number; // bytes
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Counts what's in folders, all the way down, together: "12 files and 3 folders, totalling 4.5 MB". It's separate from `properties` because it's the slow part (S3 has no folder sizes), so the panel shows the rest while the totals count.
|
|
107
|
+
|
|
108
|
+
- The folders may be in different folders, as a search's results are.
|
|
109
|
+
- `onProgress` is optional; calling it shows the totals counting up.
|
|
110
|
+
- `showHidden` says whether hidden files are showing, so the totals can leave them out.
|
|
111
|
+
- Without `measure`, a folder shows how many entries it holds (`childCount`). The file manager never walks folders with `list` to count them.
|
|
112
|
+
|
|
113
|
+
## Actions in the panel's header
|
|
114
|
+
|
|
115
|
+
`propertiesActions` puts actions in the panel's header, for what it shows (see [Actions](actions.md#buttons-in-the-previews-and-the-properties-headers)).
|
|
116
|
+
|
|
117
|
+
## Controlling it
|
|
118
|
+
|
|
119
|
+
Whether the docked panel shows, and its width, are public state: `propertiesOpen` / `defaultPropertiesOpen` / `onPropertiesOpenChange` and `propertiesWidth` / `defaultPropertiesWidth` / `onPropertiesWidthChange` (240 to 480 px, 300 by default). See [State and the API](state-and-api.md).
|
|
120
|
+
|
|
121
|
+
To show the panel for particular entries from your own code:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
apiRef.current?.actionContext(entries)?.properties();
|
|
125
|
+
```
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# An in-memory adapter
|
|
2
|
+
|
|
3
|
+
A complete adapter over a tree kept in memory: listing, new folders, renames, copies, moves, deletes, uploads and the preview, all undoable. Use it to try filecove out before your backend is ready, in Storybook, or in tests. Everything is lost on reload.
|
|
4
|
+
|
|
5
|
+
Ids are generated, not paths, which is how most databases work; the file manager doesn't care either way.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// memory-adapter.ts
|
|
9
|
+
import type { FileAdapter, FileEntry, TransferResult } from "filecove";
|
|
10
|
+
|
|
11
|
+
interface Node {
|
|
12
|
+
entry: FileEntry;
|
|
13
|
+
parentId: string | null;
|
|
14
|
+
blob?: Blob;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const abortable = (signal: AbortSignal) => {
|
|
18
|
+
if (signal.aborted) {
|
|
19
|
+
throw new DOMException("Aborted", "AbortError");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function createMemoryAdapter(): FileAdapter {
|
|
24
|
+
const nodes = new Map<string, Node>();
|
|
25
|
+
let next = 1;
|
|
26
|
+
|
|
27
|
+
const add = (parentId: string | null, entry: Omit<FileEntry, "id">, blob?: Blob): FileEntry => {
|
|
28
|
+
const made: FileEntry = { ...entry, id: `n${next++}`, modifiedAt: new Date() };
|
|
29
|
+
nodes.set(made.id, { entry: made, parentId, blob });
|
|
30
|
+
return made;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const childrenOf = (parentId: string) => [...nodes.values()].filter((node) => node.parentId === parentId);
|
|
34
|
+
|
|
35
|
+
const freeName = (parentId: string, name: string) => {
|
|
36
|
+
const taken = new Set(childrenOf(parentId).map((node) => node.entry.name));
|
|
37
|
+
if (!taken.has(name)) return name;
|
|
38
|
+
const dot = name.lastIndexOf(".");
|
|
39
|
+
const [base, ext] = dot > 0 ? [name.slice(0, dot), name.slice(dot)] : [name, ""];
|
|
40
|
+
for (let n = 2; ; n++) {
|
|
41
|
+
const candidate = `${base} (${n})${ext}`;
|
|
42
|
+
if (!taken.has(candidate)) return candidate;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const remove = (id: string) => {
|
|
47
|
+
childrenOf(id).forEach((child) => remove(child.entry.id));
|
|
48
|
+
nodes.delete(id);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const copyInto = (node: Node, parentId: string): FileEntry => {
|
|
52
|
+
const made = add(parentId, { ...node.entry, name: freeName(parentId, node.entry.name) }, node.blob);
|
|
53
|
+
childrenOf(node.entry.id).forEach((child) => copyInto(child, made.id));
|
|
54
|
+
return made;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// The root.
|
|
58
|
+
nodes.set("root", { entry: { id: "root", name: "Files", kind: "folder" }, parentId: null });
|
|
59
|
+
const docs = add("root", { name: "Documents", kind: "folder" });
|
|
60
|
+
add(docs.id, { name: "notes.txt", kind: "file", mimeType: "text/plain" }, new Blob(["Hello from memory."]));
|
|
61
|
+
add("root", { name: "Pictures", kind: "folder" });
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
async list(folderId, { signal }) {
|
|
65
|
+
abortable(signal);
|
|
66
|
+
if (!nodes.has(folderId)) throw new Error("This folder doesn't exist any more.");
|
|
67
|
+
return { entries: childrenOf(folderId).map((node) => node.entry) };
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
async createFolder(parentId, name) {
|
|
71
|
+
if (childrenOf(parentId).some((node) => node.entry.name === name)) {
|
|
72
|
+
throw new Error(`There's already something named “${name}” here.`);
|
|
73
|
+
}
|
|
74
|
+
return add(parentId, { name, kind: "folder" });
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
async rename(entry, name) {
|
|
78
|
+
const node = nodes.get(entry.id);
|
|
79
|
+
if (!node) throw new Error("It doesn't exist any more.");
|
|
80
|
+
node.entry = { ...node.entry, name, modifiedAt: new Date() };
|
|
81
|
+
return node.entry;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async copy(entries, targetId): Promise<TransferResult> {
|
|
85
|
+
return { entries: entries.map((entry) => copyInto(nodes.get(entry.id)!, targetId)) };
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async move(entries, targetId, { conflict, onEntryDone }): Promise<TransferResult> {
|
|
89
|
+
const made = entries.map((entry) => {
|
|
90
|
+
const node = nodes.get(entry.id)!;
|
|
91
|
+
const name = conflict === "keepBoth" ? freeName(targetId, node.entry.name) : node.entry.name;
|
|
92
|
+
node.parentId = targetId;
|
|
93
|
+
node.entry = { ...node.entry, name };
|
|
94
|
+
onEntryDone(entry, node.entry);
|
|
95
|
+
return node.entry;
|
|
96
|
+
});
|
|
97
|
+
return { entries: made };
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
async delete(entries) {
|
|
101
|
+
entries.forEach((entry) => remove(entry.id));
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
async upload(file, folderId, { conflict, signal }) {
|
|
105
|
+
abortable(signal);
|
|
106
|
+
const existing = childrenOf(folderId).find((node) => node.entry.name === file.name);
|
|
107
|
+
if (existing && conflict === "skip") return; // left out
|
|
108
|
+
if (existing && conflict === "replace" && existing.entry.kind === "file") {
|
|
109
|
+
existing.blob = file;
|
|
110
|
+
existing.entry = { ...existing.entry, size: file.size, modifiedAt: new Date() };
|
|
111
|
+
return existing.entry;
|
|
112
|
+
}
|
|
113
|
+
return add(folderId, { name: freeName(folderId, file.name), kind: "file", size: file.size, mimeType: file.type }, file);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
async content(entry) {
|
|
117
|
+
const blob = nodes.get(entry.id)?.blob;
|
|
118
|
+
if (!blob) throw new Error("This file has no contents.");
|
|
119
|
+
return { blob };
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { useMemo } from "react";
|
|
127
|
+
import { FileManager } from "filecove";
|
|
128
|
+
import type { SectionGroup } from "filecove";
|
|
129
|
+
import { createMemoryAdapter } from "./memory-adapter";
|
|
130
|
+
import { fileActions } from "./file-actions"; // the set from docs/actions.md
|
|
131
|
+
|
|
132
|
+
const sections: SectionGroup[] = [{ id: "places", sections: [{ id: "files", label: "Files", icon: "home", rootId: "root" }] }];
|
|
133
|
+
|
|
134
|
+
export function Playground() {
|
|
135
|
+
const adapter = useMemo(createMemoryAdapter, []);
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<div style={{ height: "100vh" }}>
|
|
139
|
+
<FileManager adapter={adapter} sections={sections} actions={fileActions} />
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Notes:
|
|
146
|
+
|
|
147
|
+
- `copy`, `move` and `upload` resolve with what they made, so copies and moves can be undone and an uploaded file kept under another name is found.
|
|
148
|
+
- `move` keeps it simple: a real backend would merge folders on `"replace"` (see [The adapter](../adapter.md#name-clashes)).
|
|
149
|
+
- To see loading states, add a delay at the top of `list`: `await new Promise((resolve) => setTimeout(resolve, 300));`.
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# An adapter over a REST API
|
|
2
|
+
|
|
3
|
+
The usual case: your server keeps the files and exposes them over HTTP. This recipe assumes the API below; change the URLs and field names to yours. Every endpoint returns JSON, and errors as `{ "message": "..." }` with a 4xx or 5xx status.
|
|
4
|
+
|
|
5
|
+
| Endpoint | Returns |
|
|
6
|
+
|---|---|
|
|
7
|
+
| `GET /api/folders/:id/entries?cursor=` | `{ entries: ApiEntry[], nextCursor?: string, permissions?: {...} }` |
|
|
8
|
+
| `POST /api/folders/:id/folders` `{ name }` | `ApiEntry` |
|
|
9
|
+
| `PATCH /api/entries/:id` `{ name }` | `ApiEntry` |
|
|
10
|
+
| `POST /api/entries/copy` `{ ids, target, conflict }` | `{ entries: ApiEntry[] }` |
|
|
11
|
+
| `POST /api/entries/move` `{ ids, target, conflict }` | `{ entries: ApiEntry[] }` |
|
|
12
|
+
| `POST /api/entries/delete` `{ ids }` | nothing |
|
|
13
|
+
| `POST /api/entries/trash` `{ ids }` | `{ entries: ApiEntry[] }` |
|
|
14
|
+
| `POST /api/entries/restore` `{ ids, conflict }` | `{ entries: ApiEntry[] }` |
|
|
15
|
+
| `POST /api/folders/:id/files` (multipart `file`, `conflict`) | `ApiEntry` |
|
|
16
|
+
| `GET /api/entries/:id/content?download=1` | the bytes, `Content-Disposition: attachment` with `download=1` |
|
|
17
|
+
| `GET /api/entries/:id/thumbnail?size=` | an image |
|
|
18
|
+
| `POST /api/zip` `{ ids }` | `{ url }`, a temporary URL to the zip |
|
|
19
|
+
| `GET /api/search?root=&q=&cursor=&hidden=` | `{ matches: { entry: ApiEntry, trail: {id,name}[] }[], nextCursor? }` |
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
interface ApiEntry {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
type: "file" | "folder";
|
|
26
|
+
size?: number;
|
|
27
|
+
mime?: string;
|
|
28
|
+
updated_at?: string; // ISO
|
|
29
|
+
created_at?: string;
|
|
30
|
+
children?: number;
|
|
31
|
+
has_folders?: boolean;
|
|
32
|
+
permissions?: { write?: boolean; rename?: boolean; move?: boolean; delete?: boolean };
|
|
33
|
+
trashed?: { from: { id: string; name: string }[]; at: string };
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## The adapter
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// rest-adapter.ts
|
|
41
|
+
import type { FileAdapter, FileEntry } from "filecove";
|
|
42
|
+
|
|
43
|
+
const date = (value?: string) => (value ? new Date(value) : undefined);
|
|
44
|
+
|
|
45
|
+
// Your API's shape to filecove's.
|
|
46
|
+
const toEntry = (api: ApiEntry): FileEntry => ({
|
|
47
|
+
id: api.id,
|
|
48
|
+
name: api.name,
|
|
49
|
+
kind: api.type,
|
|
50
|
+
size: api.size,
|
|
51
|
+
mimeType: api.mime,
|
|
52
|
+
modifiedAt: date(api.updated_at),
|
|
53
|
+
createdAt: date(api.created_at),
|
|
54
|
+
childCount: api.children,
|
|
55
|
+
hasSubfolders: api.has_folders,
|
|
56
|
+
permissions: api.permissions,
|
|
57
|
+
trashed: api.trashed && { trail: api.trashed.from, trashedAt: date(api.trashed.at) },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export function createRestAdapter(baseUrl = "", headers: Record<string, string> = {}): FileAdapter {
|
|
61
|
+
// One place for fetch, JSON and errors. Rejects with an Error the user can read.
|
|
62
|
+
const request = async <T>(path: string, init: RequestInit & { signal: AbortSignal }): Promise<T> => {
|
|
63
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
64
|
+
...init,
|
|
65
|
+
headers: { "content-type": "application/json", ...headers, ...init.headers },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
const body = await response.json().catch(() => null);
|
|
70
|
+
throw new Error(body?.message ?? `The server answered ${response.status}.`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return response.status === 204 ? (undefined as T) : response.json();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const post = <T>(path: string, body: unknown, signal: AbortSignal) =>
|
|
77
|
+
request<T>(path, { method: "POST", body: JSON.stringify(body), signal });
|
|
78
|
+
|
|
79
|
+
const id = (value: string) => encodeURIComponent(value);
|
|
80
|
+
const ids = (entries: FileEntry[]) => entries.map((entry) => entry.id);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
async list(folderId, { signal, cursor }) {
|
|
84
|
+
const query = cursor ? `?cursor=${id(cursor)}` : "";
|
|
85
|
+
const body = await request<{ entries: ApiEntry[]; nextCursor?: string; permissions?: ApiEntry["permissions"] }>(
|
|
86
|
+
`/api/folders/${id(folderId)}/entries${query}`,
|
|
87
|
+
{ signal },
|
|
88
|
+
);
|
|
89
|
+
return { entries: body.entries.map(toEntry), nextCursor: body.nextCursor, permissions: body.permissions };
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async createFolder(parentId, name, { signal }) {
|
|
93
|
+
return toEntry(await post<ApiEntry>(`/api/folders/${id(parentId)}/folders`, { name }, signal));
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
async rename(entry, name, { signal }) {
|
|
97
|
+
return toEntry(
|
|
98
|
+
await request<ApiEntry>(`/api/entries/${id(entry.id)}`, { method: "PATCH", body: JSON.stringify({ name }), signal }),
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
// Windows-like rules, if your server stores files on a disk that has them.
|
|
103
|
+
checkName(name) {
|
|
104
|
+
if (/[\\:*?"<>|]/.test(name)) return 'A name can\'t contain \\ : * ? " < > |';
|
|
105
|
+
if (name.length > 255) return "That name is too long.";
|
|
106
|
+
return null;
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
// Resolving with what was made lets the user undo.
|
|
110
|
+
async copy(entries, targetId, { signal, conflict }) {
|
|
111
|
+
const body = await post<{ entries: ApiEntry[] }>("/api/entries/copy", { ids: ids(entries), target: targetId, conflict }, signal);
|
|
112
|
+
return { entries: body.entries.map(toEntry) };
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
async move(entries, targetId, { signal, conflict }) {
|
|
116
|
+
const body = await post<{ entries: ApiEntry[] }>("/api/entries/move", { ids: ids(entries), target: targetId, conflict }, signal);
|
|
117
|
+
return { entries: body.entries.map(toEntry) };
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
async delete(entries, { signal }) {
|
|
121
|
+
await post("/api/entries/delete", { ids: ids(entries) }, signal);
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
async trash(entries, { signal }) {
|
|
125
|
+
const body = await post<{ entries: ApiEntry[] }>("/api/entries/trash", { ids: ids(entries) }, signal);
|
|
126
|
+
return { entries: body.entries.map(toEntry) };
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
async restore(entries, { signal, conflict }) {
|
|
130
|
+
const body = await post<{ entries: ApiEntry[] }>("/api/entries/restore", { ids: ids(entries), conflict }, signal);
|
|
131
|
+
return { entries: body.entries.map(toEntry) };
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
// XMLHttpRequest, for upload progress, which fetch can't report.
|
|
135
|
+
upload(file, folderId, { signal, conflict, onProgress }) {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
const xhr = new XMLHttpRequest();
|
|
138
|
+
const form = new FormData();
|
|
139
|
+
|
|
140
|
+
form.append("file", file);
|
|
141
|
+
form.append("conflict", conflict);
|
|
142
|
+
xhr.open("POST", `${baseUrl}/api/folders/${id(folderId)}/files`);
|
|
143
|
+
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value));
|
|
144
|
+
xhr.responseType = "json";
|
|
145
|
+
xhr.upload.onprogress = (event) => onProgress(event.loaded, event.total);
|
|
146
|
+
xhr.onload = () =>
|
|
147
|
+
xhr.status < 300
|
|
148
|
+
? resolve(toEntry(xhr.response))
|
|
149
|
+
: reject(new Error(xhr.response?.message ?? "Couldn't upload the file."));
|
|
150
|
+
xhr.onerror = () => reject(new Error("The connection was lost."));
|
|
151
|
+
signal.addEventListener("abort", () => {
|
|
152
|
+
xhr.abort();
|
|
153
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
154
|
+
});
|
|
155
|
+
xhr.send(form);
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// A URL: the preview streams it, a download saves it.
|
|
160
|
+
async content(entry, { purpose }) {
|
|
161
|
+
const download = purpose === "download" ? "?download=1" : "";
|
|
162
|
+
return { url: `${baseUrl}/api/entries/${id(entry.id)}/content${download}` };
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
async download(entries, { signal }) {
|
|
166
|
+
return post<{ url: string }>("/api/zip", { ids: ids(entries) }, signal);
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
async thumbnail(entry, { size }) {
|
|
170
|
+
// Answer at once for what can't have one.
|
|
171
|
+
if (!entry.mimeType?.startsWith("image/") && entry.mimeType !== "application/pdf") {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
return { url: `${baseUrl}/api/entries/${id(entry.id)}/thumbnail?size=${size}` };
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
async search(rootId, query, { signal, cursor, showHidden }) {
|
|
178
|
+
const params = new URLSearchParams({ root: rootId, q: query, hidden: String(showHidden) });
|
|
179
|
+
if (cursor) params.set("cursor", cursor);
|
|
180
|
+
const body = await request<{ matches: { entry: ApiEntry; trail: { id: string; name: string }[] }[]; nextCursor?: string }>(
|
|
181
|
+
`/api/search?${params}`,
|
|
182
|
+
{ signal },
|
|
183
|
+
);
|
|
184
|
+
return {
|
|
185
|
+
matches: body.matches.map((match) => ({ entry: toEntry(match.entry), trail: match.trail })),
|
|
186
|
+
nextCursor: body.nextCursor,
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Using it
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
import { useMemo } from "react";
|
|
197
|
+
import { FileManager } from "filecove";
|
|
198
|
+
import type { SectionGroup } from "filecove";
|
|
199
|
+
import { createRestAdapter } from "./rest-adapter";
|
|
200
|
+
import { fileActions } from "./file-actions"; // the set from docs/actions.md
|
|
201
|
+
|
|
202
|
+
const sections: SectionGroup[] = [
|
|
203
|
+
{ id: "places", sections: [{ id: "home", label: "My files", icon: "home", rootId: "root" }] },
|
|
204
|
+
{ id: "bin", sections: [{ id: "trash", label: "Trash", icon: "delete", rootId: "trash", trash: true, showFolders: false }] },
|
|
205
|
+
];
|
|
206
|
+
|
|
207
|
+
export function Files({ token }: { token: string }) {
|
|
208
|
+
// A new adapter only when the token changes: a new one resets the file manager.
|
|
209
|
+
const adapter = useMemo(() => createRestAdapter("", { authorization: `Bearer ${token}` }), [token]);
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<div style={{ height: "100vh" }}>
|
|
213
|
+
<FileManager adapter={adapter} sections={sections} actions={fileActions} />
|
|
214
|
+
</div>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Notes
|
|
220
|
+
|
|
221
|
+
- **Authentication.** URLs handed over from `content` and `thumbnail` are loaded by the browser itself (`<img>`, `<video>`, a new tab), which sends cookies but not an `authorization` header. Either authenticate those endpoints by cookie, sign the URLs (a short-lived token in the query), or fetch the bytes yourself and resolve with `{ blob }`, which works for small files but loses streaming.
|
|
222
|
+
- **The server's job** for `"replace"`: replace files of the same name, and merge folders of the same name, all the way down. For `"keepBoth"`: pick a free name, "report (2).pdf". See [The adapter](../adapter.md#name-clashes).
|
|
223
|
+
- **Ids with slashes.** `encodeURIComponent` keeps a path id in one URL segment. If your router decodes `%2F`, send ids in the query or the body instead.
|
|
224
|
+
- **Big folders.** Return `nextCursor` and the first page shows while the rest comes.
|
|
225
|
+
- **Undo.** `copy`, `move`, `trash` and `restore` resolve with what they made, in the order given. If your API returns them in another order, sort them back.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# An adapter over S3 with presigned URLs
|
|
2
|
+
|
|
3
|
+
An S3 bucket (or R2, MinIO, any S3-compatible storage) where your server holds the credentials and the browser talks to the bucket directly with URLs your server signs. The bytes never pass through your server; listings and changes do.
|
|
4
|
+
|
|
5
|
+
Ids are **keys and prefixes**: a file is `photos/2026/beach.jpg`, a folder `photos/2026/` (ending in a slash), and the bucket's root `""`. The file manager never parses them; only this adapter and your server do.
|
|
6
|
+
|
|
7
|
+
## What your server provides
|
|
8
|
+
|
|
9
|
+
| Endpoint | Does |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `GET /api/s3/list?prefix=&cursor=` | `ListObjectsV2` with `Delimiter: "/"`: returns `{ folders: string[], files: { key, size, lastModified }[], nextCursor? }` |
|
|
12
|
+
| `POST /api/s3/upload-url` `{ key, type }` | a presigned `PUT` URL for that key |
|
|
13
|
+
| `POST /api/s3/download-url` `{ key, attachment }` | a presigned `GET` URL, with `ResponseContentDisposition: attachment` when asked |
|
|
14
|
+
| `POST /api/s3/folder` `{ prefix }` | puts an empty object at `prefix` (S3 has no folders; a marker makes an empty one show) |
|
|
15
|
+
| `POST /api/s3/delete` `{ keys }` | deletes the keys, and everything under the prefixes among them |
|
|
16
|
+
| `POST /api/s3/move` `{ keys, target }` / `/copy` | copies (and for a move deletes) each key, and everything under prefixes, into `target`; returns the new keys |
|
|
17
|
+
|
|
18
|
+
The bucket's CORS must allow your origin to `PUT` (uploads) and `GET` (the preview's text, and copies to another backend), and expose `ETag`.
|
|
19
|
+
|
|
20
|
+
## The adapter
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// s3-adapter.ts
|
|
24
|
+
import type { FileAdapter, FileEntry } from "filecove";
|
|
25
|
+
|
|
26
|
+
const nameOf = (key: string) => key.replace(/\/$/, "").split("/").pop() ?? key;
|
|
27
|
+
const isFolder = (key: string) => key.endsWith("/");
|
|
28
|
+
|
|
29
|
+
const toEntry = (key: string, size?: number, lastModified?: string): FileEntry => ({
|
|
30
|
+
id: key,
|
|
31
|
+
name: nameOf(key),
|
|
32
|
+
kind: isFolder(key) ? "folder" : "file",
|
|
33
|
+
size,
|
|
34
|
+
modifiedAt: lastModified ? new Date(lastModified) : undefined,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
async function api<T>(path: string, body: unknown, signal: AbortSignal): Promise<T> {
|
|
38
|
+
const response = await fetch(path, {
|
|
39
|
+
method: body === undefined ? "GET" : "POST",
|
|
40
|
+
headers: { "content-type": "application/json" },
|
|
41
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
42
|
+
signal,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
throw new Error((await response.json().catch(() => null))?.message ?? "The storage didn't answer.");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return response.json();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const s3Adapter: FileAdapter = {
|
|
53
|
+
async list(prefix, { signal, cursor }) {
|
|
54
|
+
const params = new URLSearchParams({ prefix });
|
|
55
|
+
if (cursor) params.set("cursor", cursor);
|
|
56
|
+
|
|
57
|
+
const body = await api<{ folders: string[]; files: { key: string; size: number; lastModified: string }[]; nextCursor?: string }>(
|
|
58
|
+
`/api/s3/list?${params}`,
|
|
59
|
+
undefined,
|
|
60
|
+
signal,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
entries: [
|
|
65
|
+
...body.folders.map((key) => toEntry(key)),
|
|
66
|
+
// The folder's own marker object is listed under its prefix; leave it out.
|
|
67
|
+
...body.files.filter((file) => file.key !== prefix).map((file) => toEntry(file.key, file.size, file.lastModified)),
|
|
68
|
+
],
|
|
69
|
+
nextCursor: body.nextCursor,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
async createFolder(parentPrefix, name, { signal }) {
|
|
74
|
+
const prefix = `${parentPrefix}${name}/`;
|
|
75
|
+
await api("/api/s3/folder", { prefix }, signal);
|
|
76
|
+
return toEntry(prefix);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
// S3 keys can hold almost anything, but "/" makes a folder.
|
|
80
|
+
checkName(name) {
|
|
81
|
+
return new TextEncoder().encode(name).length > 900 ? "That name is too long." : null;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
async copy(entries, target, { signal }) {
|
|
85
|
+
const { keys } = await api<{ keys: string[] }>("/api/s3/copy", { keys: entries.map((entry) => entry.id), target }, signal);
|
|
86
|
+
return { entries: keys.map((key, index) => ({ ...entries[index], id: key, name: nameOf(key) })) };
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
async move(entries, target, { signal }) {
|
|
90
|
+
const { keys } = await api<{ keys: string[] }>("/api/s3/move", { keys: entries.map((entry) => entry.id), target }, signal);
|
|
91
|
+
return { entries: keys.map((key, index) => ({ ...entries[index], id: key, name: nameOf(key) })) };
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
async delete(entries, { signal }) {
|
|
95
|
+
await api("/api/s3/delete", { keys: entries.map((entry) => entry.id) }, signal);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
async upload(file, prefix, { signal, onProgress }) {
|
|
99
|
+
const key = `${prefix}${file.name}`;
|
|
100
|
+
const { url } = await api<{ url: string }>("/api/s3/upload-url", { key, type: file.type }, signal);
|
|
101
|
+
|
|
102
|
+
await new Promise<void>((resolve, reject) => {
|
|
103
|
+
const xhr = new XMLHttpRequest();
|
|
104
|
+
xhr.open("PUT", url);
|
|
105
|
+
xhr.setRequestHeader("content-type", file.type || "application/octet-stream");
|
|
106
|
+
xhr.upload.onprogress = (event) => onProgress(event.loaded, event.total);
|
|
107
|
+
xhr.onload = () => (xhr.status < 300 ? resolve() : reject(new Error("The storage refused the file.")));
|
|
108
|
+
xhr.onerror = () => reject(new Error("The connection was lost."));
|
|
109
|
+
signal.addEventListener("abort", () => {
|
|
110
|
+
xhr.abort();
|
|
111
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
112
|
+
});
|
|
113
|
+
xhr.send(file);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return { id: key, name: file.name, kind: "file", size: file.size, mimeType: file.type, modifiedAt: new Date() };
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
async content(entry, { signal, purpose }) {
|
|
120
|
+
const { url } = await api<{ url: string }>(
|
|
121
|
+
"/api/s3/download-url",
|
|
122
|
+
{ key: entry.id, attachment: purpose === "download" },
|
|
123
|
+
signal,
|
|
124
|
+
);
|
|
125
|
+
return { url };
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Notes
|
|
131
|
+
|
|
132
|
+
- **Name clashes.** A `PUT` to a key that exists replaces it. This upload ignores `conflict`, so it always replaces; to keep both, have `/api/s3/upload-url` pick a free key ("beach (2).jpg") when asked with `conflict: "keepBoth"` and return it, then resolve with that key. The file manager asks the user first about the names it can see, so a clash only reaches the adapter when the folder changed meanwhile.
|
|
133
|
+
- **Renaming** is a copy and a delete on S3, and for a folder, of everything under it. Add `rename` through your server the same way as `move`, if you want it; without it, there's no Rename.
|
|
134
|
+
- **Downloads.** `attachment: true` makes the signed URL answer with `Content-Disposition: attachment`, so the browser saves it rather than opening it in the tab the file manager opens for another origin. Zipping a folder needs a server (or a worker) to build the zip: add `download` returning its URL.
|
|
135
|
+
- **Thumbnails.** S3 makes none. Generate them on upload (a Lambda, a queue) under a `thumbnails/` prefix, and add `thumbnail` returning their signed URLs, `null` for files that have none.
|
|
136
|
+
- **Folders that hold nothing but a marker** show as empty folders. Folders made implicitly by keys (`a/b/c.txt` with no `a/` object) show too, from `CommonPrefixes`.
|
|
137
|
+
- **Search.** S3 can't search. Keep an index (your database, OpenSearch) and add `search`, or leave it out: the open folder's own search still works.
|