Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Blame · server/web/release_files.go

Line attribution on main · 123 line(s)

c5187449
Keenan Nemetz
17d ago
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,
fdd0b3e8
Keenan Nemetz
17d ago
6 // so a file removed in the browser is indistinguishable from one removed with
7 // `vole release remove`.
c5187449
Keenan Nemetz
17d ago
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
fdd0b3e8
Keenan Nemetz
17d ago
27 // ReleaseAttachPage is GET /<name>/releases/<tag>/attach. The page is offered by
c5187449
Keenan Nemetz
17d ago
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{
fdd0b3e8
Keenan Nemetz
17d ago
49 Page: w.page(r, m.Name+" · manage files"),
c5187449
Keenan Nemetz
17d ago
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 // releaseFileAt finds the attached file the form names.
69 func (w *Web) releaseFileAt(r *http.Request, m *repo.Meta, tag, name string) (metadata.Artifact, bool) {
70 for _, a := range w.releaseArtifacts(r, m, tag) {
71 if a.Name == name {
72 return a, true
73 }
74 }
75 return metadata.Artifact{}, false
76 }
77
78 // DeleteReleaseFile is POST /<name>/releases/<tag>/files/delete. It removes the
79 // pointer; the object store keeps the bytes until gc proves nothing else names
fdd0b3e8
Keenan Nemetz
17d ago
80 // them. The form sits on the release page and on the manage-files page, so the
81 // action is reachable without a second navigational step.
c5187449
Keenan Nemetz
17d ago
82 func (w *Web) DeleteReleaseFile(rw http.ResponseWriter, r *http.Request) {
83 ctx, ok := w.beginWrite(rw, r, maxSmallForm)
84 if !ok {
85 return
86 }
87 m := ctx.Repo
88 if !canMaintain(ctx.ACL, ctx.Name) {
89 w.notFound(rw, r)
90 return
91 }
92 tag := r.PathValue("tag")
93 path := r.PostFormValue("path")
94 t, err := m.Git().Tag(tag)
95 if err != nil {
96 w.notFound(rw, r)
97 return
98 }
99 if msg := w.releaseRefusal(m, t); msg != "" {
100 status := http.StatusConflict
101 if !t.Annotated {
102 status = http.StatusBadRequest
103 }
104 w.fail(rw, r, status, msg, nil)
105 return
106 }
107 if err := metadata.ValidArtifactPath(path); err != nil {
108 w.fail(rw, r, http.StatusBadRequest, err.Error(), nil)
109 return
110 }
111 if _, found := w.releaseFileAt(r, m, t.Name, path); !found {
112 w.fail(rw, r, http.StatusBadRequest, "That file is no longer attached to this release.", nil)
113 return
114 }
115 if err := metadata.RemoveReleasePointers(m.Git(), metadata.Ref, t.Name, []string{path},
116 fmt.Sprintf("release: %s remove %s (web)", t.Name, path), ctx.Name); err != nil {
117 w.fail(rw, r, http.StatusBadRequest, "cannot remove the file: "+err.Error(), nil)
118 return
119 }
120 w.refreshReleaseEvents(m)
121 w.audit("web.release.file.delete", ctx.Name, m.Name, t.Name+": "+path)
122 http.Redirect(rw, r, "/"+m.Name+"/releases/"+pathURL(t.Name), http.StatusSeeOther)
123 }