MCPcopy Create free account
hub / github.com/buggregator/server / downloadAttachment

Function downloadAttachment

internal/server/http/attachments.go:63–95  ·  view source on GitHub ↗
(db *sql.DB, store *storage.AttachmentStore, table string)

Source from the content-addressed store, hash-verified

61}
62
63func downloadAttachment(db *sql.DB, store *storage.AttachmentStore, table string) http.HandlerFunc {
64 return func(w http.ResponseWriter, r *http.Request) {
65 eventUUID := r.PathValue("eventUuid")
66 uuid := r.PathValue("uuid")
67
68 var name, path, mime string
69 var size int
70 var storedEventUUID string
71 err := db.QueryRowContext(r.Context(),
72 "SELECT event_uuid, name, path, size, mime FROM "+table+" WHERE uuid = ?", uuid,
73 ).Scan(&storedEventUUID, &name, &path, &size, &mime)
74 if err != nil {
75 http.Error(w, "attachment not found", 404)
76 return
77 }
78
79 if storedEventUUID != eventUUID {
80 http.Error(w, "forbidden", 403)
81 return
82 }
83
84 data, err := store.Get(path)
85 if err != nil {
86 http.Error(w, "file not found", 404)
87 return
88 }
89
90 w.Header().Set("Content-Type", "application/octet-stream")
91 w.Header().Set("Content-Disposition", "attachment; filename=\""+name+"\"")
92 w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
93 w.Write(data)
94 }
95}
96
97func previewAttachment(db *sql.DB, store *storage.AttachmentStore, table string) http.HandlerFunc {
98 return func(w http.ResponseWriter, r *http.Request) {

Callers 1

Calls 2

ScanMethod · 0.80
GetMethod · 0.45

Tested by

no test coverage detected