(db *sql.DB, store *storage.AttachmentStore, table string)
| 61 | } |
| 62 | |
| 63 | func 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 | |
| 97 | func previewAttachment(db *sql.DB, store *storage.AttachmentStore, table string) http.HandlerFunc { |
| 98 | return func(w http.ResponseWriter, r *http.Request) { |
no test coverage detected