(db *sql.DB, store *storage.AttachmentStore, table string)
| 95 | } |
| 96 | |
| 97 | func previewAttachment(db *sql.DB, store *storage.AttachmentStore, table string) http.HandlerFunc { |
| 98 | return func(w http.ResponseWriter, r *http.Request) { |
| 99 | eventUUID := r.PathValue("eventUuid") |
| 100 | uuid := r.PathValue("uuid") |
| 101 | |
| 102 | var name, path, mime string |
| 103 | var storedEventUUID string |
| 104 | err := db.QueryRowContext(r.Context(), |
| 105 | "SELECT event_uuid, name, path, mime FROM "+table+" WHERE uuid = ?", uuid, |
| 106 | ).Scan(&storedEventUUID, &name, &path, &mime) |
| 107 | if err != nil { |
| 108 | http.Error(w, "attachment not found", 404) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | if storedEventUUID != eventUUID { |
| 113 | http.Error(w, "forbidden", 403) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | data, err := store.Get(path) |
| 118 | if err != nil { |
| 119 | http.Error(w, "file not found", 404) |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | w.Header().Set("Content-Type", mime) |
| 124 | w.Write(data) |
| 125 | } |
| 126 | } |
| 127 |
no test coverage detected