File upload lifecycle
File uploads turn a user-selected file into a stored file or media node. The shared flow covers selection, local checks, optional custom validation, transport, progress, failure reporting, and cleanup. Editor media adds a temporary node and keeps the source file available when a failed upload needs another attempt.
File selection
File selection starts from a native file input or from files supplied by a drop or paste event. The input can limit visible choices by MIME type, while editor routing separates images, videos, and other files so each enabled feature receives the right kind of upload. src/…/FileUploader.vue and src/…/media-drop-extension.ts support these entry points.
The standalone uploader takes the first selected file. The editor can accept several files. One image is inserted as media, several images open the image-group flow, videos are uploaded as video media, and other files use the attachment flow when that feature is available.
References: src/…/FileUploader.vue• L1-29 src/…/file-picker.ts• L1-16 src/…/media-drop-extension.ts• L59-105
File validation
Validation blocks a file before transport when the file is too large or when a component-specific rule rejects it. The shared size check reads the app limit and returns a user-facing message. FileUploader also accepts an asynchronous validation hook, so a caller can apply rules that are specific to its field.
Editor media creates an error placeholder for an over-limit file instead of starting expensive preview work. It keeps the staged file so the user can try again or choose another file. A file that passes local checks can still fail at the server or proxy.
References: src/…/fileSize.ts• L1-46 src/…/useFileUpload.ts• L197-209 src/…/types.ts• L3-43 src/…/media-upload-engine.ts• L197-220
Upload transport
The shared upload service sends the file to an upload endpoint with its storage and attachment intent. Files within the configured request size use one request. Larger files can use consecutive chunks when both the client setting and the server support chunked uploads; the final chunk produces the stored file result.
The upload result must include a safe file_url. Standalone consumers receive the stored file record. Editor media uses that URL to replace its temporary node source. Uploads default to private storage unless the caller explicitly requests a public file.
The following diagram shows how a selected file moves from validation to storage and then to its consumer.
flowchart LR
A[Selected file] --> B{Within local size limit?}
B -- No --> C[Report size error]
B -- Yes --> D{Custom validation passes?}
D -- No --> E[Report validation error]
D -- Yes --> F{Chunking needed?}
F -- No --> G[Send one request]
F -- Yes --> H[Send file chunks]
G --> I{Server accepts request?}
H --> I
I -- No --> J[Report typed upload error]
I -- Yes --> K[Return safe file URL]
References: src/…/useFileUpload.ts• L240-327 src/…/useFileUpload.ts• L418-426 src/…/media-upload-engine.ts• L96-111 src/…/media-upload-types.ts• L43-75
Progress and error state
Upload progress is shared as bytes loaded, total bytes, and a percentage. The low-level request reports progress through the upload callback. FileUploadHandler adapts that callback to start, progress, finish, and error events for FileUploader, while editor media stores progress by upload ID so each node view can display and cancel its own upload.
Failures use an UploadError kind that distinguishes a local size rejection, network failure, server response, and cancellation. The error can also carry an HTTP status, server messages, and the parsed response. UI components use this state to show an error and emit failure; a successful request clears the temporary state and emits the stored file.
The visible state follows a small state model:
| State | Meaning | User-facing result |
|---|---|---|
| Idle | No active upload | File can be selected |
| Uploading | Request or chunk sequence is active | Progress and cancel action are available |
| Success | The server returned a stored file | Success event or completed media node |
| Error | Validation, network, server, or cancellation ended the run | Error message and recovery path |
References: src/…/useFileUpload.ts• L51-98 src/…/useFileUpload.ts• L211-237 src/…/fileUploadHandler.ts• L1-53 src/…/FileUploader.vue• L76-90 src/…/media-upload-state.ts• L22-35 src/…/UploadProgressIndicator.vue• L1-31
Editor media lifecycle
Editor image and video uploads add a temporary media node before the request completes. The node has an upload ID and loading state, so the editor can show a local preview and associate later progress, success, or failure with the correct position.
The media engine stages the file, probes dimensions when useful, inserts the placeholder, and calls the host-provided upload function. On success it writes the returned URL and dimensions into the node. On failure it marks the node with an error instead of silently removing the user's work. Drop and paste flows use the same engine and sequence multiple inserts by upload ID.
This diagram shows the editor-specific lifecycle after a file passes validation.
sequenceDiagram
participant U as User
participant E as Editor
participant S as Upload state
participant X as Upload service
U->>E: Select, drop, or paste file
E->>S: Stage file and create upload ID
E->>E: Insert loading placeholder
E->>X: Upload with progress and cancel signal
X-->>S: Report byte progress
S-->>E: Show progress on media node
alt Upload succeeds
X-->>E: Return safe file URL
E->>E: Write URL and dimensions to node
E->>S: Remove staged file and progress state
else Upload fails
X-->>E: Return typed error
E->>E: Mark placeholder with error
Note over S,E: Keep staged file for recovery
end
References: src/…/media-upload-engine.ts• L169-297 src/…/media-node-ops.ts• L72-160 src/…/media-upload-state.ts• L16-35 src/…/media-drop-extension.ts• L14-26
Unfinished-media recovery
An unfinished media node is an editor-only record with a loading or error state and no final source URL. It stays in the live editor so the user can recover the upload, but serialization leaves it out of saved HTML and Markdown. This prevents unfinished media from becoming broken content downstream.
The staged file remains after a terminal upload error. A retry reads that file and starts a replacement upload. Cancellation removes the temporary node and its staged data. Successful completion removes the staged file, while destroyed editors clean up transient state without dispatching into a dead view.
References: src/…/media-upload-engine.ts• L274-295 src/…/media-upload-engine.ts• L317-340 src/…/media-node-ops.ts• L105-160 src/…/unfinished-media.test.ts• L58-149
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the client size limit, server or proxy size behavior, and displayed error wording aligned.
- Preserve the
file_urlcontract for every upload function used by editor media. - Keep upload IDs tied to staged files, progress entries, and placeholder nodes so retry and cancellation target the same upload.
- Keep chunked-upload support matched to the server version; an older server must not receive chunk pieces as independent files.
- Keep unfinished media excluded from saved HTML and Markdown while retaining it in the live editor for recovery.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| File exceeds the configured local limit | Upload work is rejected before transport | Shared size check creates a size error and editor media keeps a recovery placeholder |
| Proxy returns HTTP 413 | The server-side limit may differ from the app limit | Transport reports a server-size error with the file size |
Upload result has no safe file_url | Editor media cannot become a valid stored node | Media upload rejects the result and marks the placeholder as failed |
| Chunking is enabled against an unsupported server | Pieces can be stored as separate broken files | Enable chunking only with server support |
| User cancels an editor upload | A loading node or staged file can remain | Abort removes the node and clears transient upload state |
| Editor is destroyed during an async step | A stale view can receive an invalid document update | Upload flow checks the live editor before dispatch and cleans up state |
Key files
| File | Why to read it |
|---|---|
| src/…/useFileUpload.ts | Shared transport, size checks, chunking, progress, cancellation, and typed errors |
| src/…/fileSize.ts | Common app and server size-limit rules |
| src/…/FileUploader.vue | User-facing file selection, validation hook, state, and events |
| src/…/fileUploadHandler.ts | Event-style bridge used by the standalone uploader |
| src/…/media-upload-engine.ts | Editor media staging, upload completion, failure, retry, and cleanup |
| src/…/media-upload-state.ts | Staged-file and per-upload progress state used by media views |
| src/…/media-drop-extension.ts | Routing for dropped and pasted files |
| src/…/media-node-ops.ts | Placeholder, success, error, and cancellation effects on editor nodes |
| src/…/unfinished-media.test.ts | Guarantees for unfinished-media persistence and serialization behavior |