vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Manage release files from the CLI and browser
c51874492382d08085b6388cea898d0d60ab9375 · Keenan Nemetz · 17d ago · parent 214e7ea6
Manage release files from the CLI and browser Move the attach form to its own page, add move/remove operations to the CLI and browser, and simplify the release detail page to notes, files, and source links.
server/web/release_files.go +236
| @@ -0,0 +1,236 @@ | |||
| 1 | package web | ||
| 2 | |||
| 3 | // Release file management. Attaching has its own page because a multipart form | ||
| 4 | // is not a release note: the release page lists the files and links here for the | ||
| 5 | // file operations. Every write is the same metadata pointer write the CLI makes, | ||
| 6 | // so a file moved or removed in the browser is indistinguishable from one moved | ||
| 7 | // with `vole release move` or `vole release remove`. | ||
| 8 | |||
| 9 | import ( | ||
| 10 | "fmt" | ||
| 11 | "net/http" | ||
| 12 | |||
| 13 | "rodent.software/vole/forge/repo" | ||
| 14 | "rodent.software/vole/metadata" | ||
| 15 | ) | ||
| 16 | |||
| 17 | type releaseAttachView struct { | ||
| 18 | Page | ||
| 19 | Name string | ||
| 20 | Tag string | ||
| 21 | Artifacts []metadata.Artifact | ||
| 22 | UploadCap int64 | ||
| 23 | Error string | ||
| 24 | Locked bool | ||
| 25 | } | ||
| 26 | |||
| 27 | // ReleaseAttachPage is GET /<name>/releases/<tag>/attach. The form is offered by | ||
| 28 | // the release page as a link, not inline, because a browser upload is a page of | ||
| 29 | // its own: it has its own body limit, its own refusal messages, and a directory | ||
| 30 | // control that belongs beside the files rather than under the notes. | ||
| 31 | func (w *Web) ReleaseAttachPage(rw http.ResponseWriter, r *http.Request) { | ||
| 32 | m, ok := w.open(rw, r) | ||
| 33 | if !ok { | ||
| 34 | return | ||
| 35 | } | ||
| 36 | if _, ok := w.requireHTMLUser(rw, r); !ok { | ||
| 37 | return | ||
| 38 | } | ||
| 39 | if !w.canManageReleases(r, m) { | ||
| 40 | w.notFound(rw, r) | ||
| 41 | return | ||
| 42 | } | ||
| 43 | t, err := m.Git().Tag(r.PathValue("tag")) | ||
| 44 | if err != nil { | ||
| 45 | w.notFound(rw, r) | ||
| 46 | return | ||
| 47 | } | ||
| 48 | view := releaseAttachView{ | ||
| 49 | Page: w.page(r, m.Name+" · attach files"), | ||
| 50 | Name: m.Name, | ||
| 51 | Tag: t.Name, | ||
| 52 | UploadCap: w.attachLimit(), | ||
| 53 | } | ||
| 54 | status := 0 | ||
| 55 | if msg := w.releaseRefusal(m, t); msg != "" { | ||
| 56 | view.Locked = true | ||
| 57 | view.Error = msg | ||
| 58 | status = http.StatusConflict | ||
| 59 | if !t.Annotated { | ||
| 60 | status = http.StatusBadRequest | ||
| 61 | } | ||
| 62 | } else { | ||
| 63 | view.Artifacts = w.releaseArtifacts(r, m, t.Name) | ||
| 64 | } | ||
| 65 | w.render(rw, "release_attach", view, status) | ||
| 66 | } | ||
| 67 | |||
| 68 | type releaseFileView struct { | ||
| 69 | Page | ||
| 70 | Name string | ||
| 71 | Tag string | ||
| 72 | Path string | ||
| 73 | NewPath string | ||
| 74 | Error string | ||
| 75 | Locked bool | ||
| 76 | } | ||
| 77 | |||
| 78 | func (w *Web) releaseFileView(r *http.Request, m *repo.Meta, tag, path, newpath, msg string) releaseFileView { | ||
| 79 | return releaseFileView{ | ||
| 80 | Page: w.page(r, m.Name+" · "+path), | ||
| 81 | Name: m.Name, | ||
| 82 | Tag: tag, | ||
| 83 | Path: path, | ||
| 84 | NewPath: newpath, | ||
| 85 | Error: msg, | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | // releaseFileAt finds the attached file the form names. | ||
| 90 | func (w *Web) releaseFileAt(r *http.Request, m *repo.Meta, tag, name string) (metadata.Artifact, bool) { | ||
| 91 | for _, a := range w.releaseArtifacts(r, m, tag) { | ||
| 92 | if a.Name == name { | ||
| 93 | return a, true | ||
| 94 | } | ||
| 95 | } | ||
| 96 | return metadata.Artifact{}, false | ||
| 97 | } | ||
| 98 | |||
| 99 | // ReleaseFileEditPage is GET /<name>/releases/<tag>/files/edit?path=<path>. One | ||
| 100 | // form renames or moves the file; a second deletes it. | ||
| 101 | func (w *Web) ReleaseFileEditPage(rw http.ResponseWriter, r *http.Request) { | ||
| 102 | m, ok := w.open(rw, r) | ||
| 103 | if !ok { | ||
| 104 | return | ||
| 105 | } | ||
| 106 | if _, ok := w.requireHTMLUser(rw, r); !ok { | ||
| 107 | return | ||
| 108 | } | ||
| 109 | if !w.canManageReleases(r, m) { | ||
| 110 | w.notFound(rw, r) | ||
| 111 | return | ||
| 112 | } | ||
| 113 | t, err := m.Git().Tag(r.PathValue("tag")) | ||
| 114 | if err != nil { | ||
| 115 | w.notFound(rw, r) | ||
| 116 | return | ||
| 117 | } | ||
| 118 | name := r.URL.Query().Get("path") | ||
| 119 | if err := metadata.ValidArtifactPath(name); err != nil { | ||
| 120 | w.notFound(rw, r) | ||
| 121 | return | ||
| 122 | } | ||
| 123 | if _, found := w.releaseFileAt(r, m, t.Name, name); !found { | ||
| 124 | w.notFound(rw, r) | ||
| 125 | return | ||
| 126 | } | ||
| 127 | view := w.releaseFileView(r, m, t.Name, name, name, "") | ||
| 128 | status := 0 | ||
| 129 | if msg := w.releaseRefusal(m, t); msg != "" { | ||
| 130 | view.Locked = true | ||
| 131 | view.Error = msg | ||
| 132 | status = http.StatusConflict | ||
| 133 | if !t.Annotated { | ||
| 134 | status = http.StatusBadRequest | ||
| 135 | } | ||
| 136 | } | ||
| 137 | w.render(rw, "release_file_form", view, status) | ||
| 138 | } | ||
| 139 | |||
| 140 | // MoveReleaseFile is POST /<name>/releases/<tag>/files/edit. The old path and the | ||
| 141 | // new path are both form fields because an artifact path may contain slashes and | ||
| 142 | // spaces, which read badly in a URL and are easy to double-escape there. | ||
| 143 | func (w *Web) MoveReleaseFile(rw http.ResponseWriter, r *http.Request) { | ||
| 144 | ctx, ok := w.beginWrite(rw, r, maxSmallForm) | ||
| 145 | if !ok { | ||
| 146 | return | ||
| 147 | } | ||
| 148 | m := ctx.Repo | ||
| 149 | if !canMaintain(ctx.ACL, ctx.Name) { | ||
| 150 | w.notFound(rw, r) | ||
| 151 | return | ||
| 152 | } | ||
| 153 | tag := r.PathValue("tag") | ||
| 154 | path := r.PostFormValue("path") | ||
| 155 | newpath := r.PostFormValue("newpath") | ||
| 156 | t, err := m.Git().Tag(tag) | ||
| 157 | if err != nil { | ||
| 158 | w.notFound(rw, r) | ||
| 159 | return | ||
| 160 | } | ||
| 161 | view := w.releaseFileView(r, m, t.Name, path, newpath, "") | ||
| 162 | fail := func(status int, msg string) { | ||
| 163 | view.Error = msg | ||
| 164 | w.render(rw, "release_file_form", view, status) | ||
| 165 | } | ||
| 166 | if msg := w.releaseRefusal(m, t); msg != "" { | ||
| 167 | status := http.StatusConflict | ||
| 168 | if !t.Annotated { | ||
| 169 | status = http.StatusBadRequest | ||
| 170 | } | ||
| 171 | fail(status, msg) | ||
| 172 | return | ||
| 173 | } | ||
| 174 | if err := metadata.ValidArtifactPath(path); err != nil { | ||
| 175 | fail(http.StatusBadRequest, err.Error()) | ||
| 176 | return | ||
| 177 | } | ||
| 178 | if _, found := w.releaseFileAt(r, m, t.Name, path); !found { | ||
| 179 | fail(http.StatusBadRequest, "That file is no longer attached to this release.") | ||
| 180 | return | ||
| 181 | } | ||
| 182 | if err := metadata.MoveReleasePointer(m.Git(), metadata.Ref, t.Name, path, newpath, | ||
| 183 | fmt.Sprintf("release: %s move %s -> %s (web)", t.Name, path, newpath), ctx.Name); err != nil { | ||
| 184 | fail(http.StatusBadRequest, "cannot move the file: "+err.Error()) | ||
| 185 | return | ||
| 186 | } | ||
| 187 | w.refreshReleaseEvents(m) | ||
| 188 | w.audit("web.release.file.move", ctx.Name, m.Name, fmt.Sprintf("%s: %s -> %s", t.Name, path, newpath)) | ||
| 189 | http.Redirect(rw, r, "/"+m.Name+"/releases/"+pathURL(t.Name), http.StatusSeeOther) | ||
| 190 | } | ||
| 191 | |||
| 192 | // DeleteReleaseFile is POST /<name>/releases/<tag>/files/delete. It removes the | ||
| 193 | // pointer; the object store keeps the bytes until gc proves nothing else names | ||
| 194 | // them. | ||
| 195 | func (w *Web) DeleteReleaseFile(rw http.ResponseWriter, r *http.Request) { | ||
| 196 | ctx, ok := w.beginWrite(rw, r, maxSmallForm) | ||
| 197 | if !ok { | ||
| 198 | return | ||
| 199 | } | ||
| 200 | m := ctx.Repo | ||
| 201 | if !canMaintain(ctx.ACL, ctx.Name) { | ||
| 202 | w.notFound(rw, r) | ||
| 203 | return | ||
| 204 | } | ||
| 205 | tag := r.PathValue("tag") | ||
| 206 | path := r.PostFormValue("path") | ||
| 207 | t, err := m.Git().Tag(tag) | ||
| 208 | if err != nil { | ||
| 209 | w.notFound(rw, r) | ||
| 210 | return | ||
| 211 | } | ||
| 212 | if msg := w.releaseRefusal(m, t); msg != "" { | ||
| 213 | status := http.StatusConflict | ||
| 214 | if !t.Annotated { | ||
| 215 | status = http.StatusBadRequest | ||
| 216 | } | ||
| 217 | w.fail(rw, r, status, msg, nil) | ||
| 218 | return | ||
| 219 | } | ||
| 220 | if err := metadata.ValidArtifactPath(path); err != nil { | ||
| 221 | w.fail(rw, r, http.StatusBadRequest, err.Error(), nil) | ||
| 222 | return | ||
| 223 | } | ||
| 224 | if _, found := w.releaseFileAt(r, m, t.Name, path); !found { | ||
| 225 | w.fail(rw, r, http.StatusBadRequest, "That file is no longer attached to this release.", nil) | ||
| 226 | return | ||
| 227 | } | ||
| 228 | if err := metadata.RemoveReleasePointers(m.Git(), metadata.Ref, t.Name, []string{path}, | ||
| 229 | fmt.Sprintf("release: %s remove %s (web)", t.Name, path), ctx.Name); err != nil { | ||
| 230 | w.fail(rw, r, http.StatusBadRequest, "cannot remove the file: "+err.Error(), nil) | ||
| 231 | return | ||
| 232 | } | ||
| 233 | w.refreshReleaseEvents(m) | ||
| 234 | w.audit("web.release.file.delete", ctx.Name, m.Name, t.Name+": "+path) | ||
| 235 | http.Redirect(rw, r, "/"+m.Name+"/releases/"+pathURL(t.Name), http.StatusSeeOther) | ||
| 236 | } | ||