Repository navigation
Conversation
|
|
||
| func (e *accountExports) build(ctx context.Context, owner string, job *accountExport) { | ||
| defer job.cancel() | ||
| f, err := os.CreateTemp("", "refract-account-export-*.zip") |
There was a problem hiding this comment.
Important: Exports are staged in the system temp dir (os.CreateTemp("", ...), and os.MkdirTemp("", ...) for each git bundle in internal/projects/account_export.go:61), not the server's configured <data>/tmp. On a container with a read-only root filesystem, or a small tmpfs /tmp, every export fails, and the user only sees "Could not export your data". Temp ZIPs left behind by a crash or by shutdown while a build is running (close() cancels the build but doesn't wait for it) are never cleaned up. Fix: create both under filepath.Join(dataDir, "tmp"), the folder projectsAPI.tmp already uses, and sweep leftover refract-account-export-* files at startup.
| return "", false | ||
| } | ||
| existing := e.jobs[owner] | ||
| if existing != nil && (existing.status == "preparing" || existing.status == "ready" && !existing.downloaded) { |
There was a problem hiding this comment.
Important: The archive can be stale. start hands back an existing preparing job, or a ready one that hasn't been downloaded, so the edits flushPending() saved just before this click aren't in the ZIP. Example: someone clicks Export, closes the tab before the download starts, edits for a few minutes, then clicks Export again. They get the old ZIP, which doesn't have those edits. Fix: let a new POST replace a ready job that hasn't been downloaded. For a preparing job, note when it started and rebuild if a newer request arrives (or return the existing job only when it started after the client's flush).
| } | ||
| chats := &assistant.Service{DB: ps.DB} | ||
| for _, item := range items { | ||
| if err := ps.WriteAccountProject(ctx, zw, owner, item.ID); err != nil { |
There was a problem hiding this comment.
Nit (optional): Any single project failure aborts the whole account export. That includes a trashed project whose repo directory is gone, an empty repo where git bundle create refuses, or an unreadable file. The user gets a generic failure and no data at all. Fix: record the failed project in the archive (for example an errors.json entry or a field in projects.json) and carry on with the other projects. Or at least log which project failed and say so in the UI.
| type ExportStatus = { status: 'preparing' | 'ready' | 'failed' } | ||
| const path = '/api/account/export' | ||
| let job = await api<ExportStatus>(path, { method: 'POST' }) | ||
| while (job.status === 'preparing') { |
There was a problem hiding this comment.
Nit (optional): This polling loop can't be cancelled and has no deadline on the client. If the Settings dialog is closed, it keeps polling every second for up to the server's 30-minute timeout. Then it starts a download, or shows a toast, out of nowhere. Fix: stop the loop on unmount (an AbortController or a mounted ref), or keep the job state where the user can still see it.
| return existing.status, true | ||
| } | ||
| // A small number of simultaneous git packs keeps a shared server responsive. | ||
| if e.running >= 2 { |
There was a problem hiding this comment.
Nit (optional): When two exports are already running server-wide, POST returns 503 "account export is busy". The client turns that into the generic "Could not export your data. Try again.", so the user can't tell a busy server from a real failure. Fix: queue the request, or have the UI show a separate "busy, try again in a few minutes" message for 503.
| if closeErr != nil { | ||
| return closeErr | ||
| } | ||
| return os.Link(f.Name(), path) |
There was a problem hiding this comment.
Nit (optional): os.Link fails on filesystems without hard-link support, such as exFAT/FAT USB drives, some SMB/NFS mounts, and some Docker Desktop bind mounts. On those, refract users export fails with "operation not permitted/not supported", even though the archive was written. Fix: if Link fails with ENOTSUP/EPERM, fall back to opening the destination with O_CREATE|O_EXCL, 0600, and copying the temp file into it.
| if err != nil { | ||
| return err | ||
| } | ||
| ps := &projects.Service{DB: db, Dir: filepath.Join(dataDir, "projects"), Key: key} |
There was a problem hiding this comment.
Nit (optional): The CLI export runs in a separate process. The lockRepo taken in WriteAccountProject is process-local, so it doesn't hold back a running server's checkpoints or file writes. If the server is up, git bundle and the file walk can race with its writes and produce an inconsistent archive (a half-written file, or a bundle that doesn't match files/). Fix: say in the usage/README that the server should be stopped, or take a cross-process lock (a flock on the repo directory).
| func (a *api) missing() []string { return append([]string{}, a.missingTools...) } | ||
|
|
||
| func (a *api) routes(rt *router) { | ||
| a.exports.routes(rt) |
There was a problem hiding this comment.
Nit (optional): a.exports.routes(rt) registers the export routes even when a.exports is nil. Registration works, but the first request to any /api/account/export endpoint panics on e.mu.Lock() with a nil receiver. That can happen in any test or setup that builds api without setting exports. Fix: register the routes only when a.exports != nil, as deleteAccount already checks.
| job.timer.Stop() | ||
| } | ||
| if job.path != "" { | ||
| os.Remove(job.path) |
There was a problem hiding this comment.
Nit (optional): os.Remove(job.path) ignores its error. On Windows, removing a file that downloadExport still has open (a download running when the retention timer fires, or a new export starts) fails, and the ZIP stays in the temp dir for good. Fix: log the error and retry the removal after the download closes the file, or remove it from downloadExport's defer once the job has been discarded.
Closes #81.
Summary
refract users export <email> <file>for administrators. It uses the same archive format and preserves an existing destination file.Validation
make test, TypeScript, formatting, and lint