Skip to content

Export everything of an account - #149

Open
Lorena-J wants to merge 1 commit into
mainfrom
task/h4-proposal-export-everything-of-an-account
Open

Lorena-J wants to merge 1 commit into
mainfrom
task/h4-proposal-export-everything-of-an-account

Conversation

@Lorena-J

Copy link
Copy Markdown
Collaborator

Closes #81.

Summary

  • Add Settings → Account → Export my data. Refract prepares the ZIP in the background and the browser downloads it when it is ready.
  • Include active and trashed projects, private templates, working files, Git bundles, chats, and selected account settings. Exclude stored credentials, session tokens, provider endpoints, and raw provider errors.
  • Add 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
  • Playwright download test in Chrome and WebKit
  • Integration tests verify that a Git bundle can be cloned, trashed projects are marked, and archives exclude fixture credentials and data from another account


func (e *accountExports) build(ctx context.Context, owner string, job *accountExport) {
defer job.cancel()
f, err := os.CreateTemp("", "refract-account-export-*.zip")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread web/src/Settings.tsx
type ExportStatus = { status: 'preparing' | 'ready' | 'failed' }
const path = '/api/account/export'
let job = await api<ExportStatus>(path, { method: 'POST' })
while (job.status === 'preparing') {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/refract/users_cmd.go
if err != nil {
return err
}
ps := &projects.Service{DB: db, Dir: filepath.Join(dataDir, "projects"), Key: key}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread cmd/refract/api.go
func (a *api) missing() []string { return append([]string{}, a.missingTools...) }

func (a *api) routes(rt *router) {
a.exports.routes(rt)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: export everything of an account

2 participants