Skip to content

Commit bf75d13

Browse files
committed
feat: added delete handler to server
1 parent 315dabe commit bf75d13

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

cmd/server/handlers.go

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ var langRegex = regexp.MustCompile(`^[a-z0-9]{1,20}$`)
3131

3232
const maxTTL = 365 * 24 * time.Hour
3333

34-
func (s *Server) HandleUpload(w http.ResponseWriter, r *http.Request) {
35-
authHeader := r.Header.Get("Authorization")
36-
expectedAuthHeader := "Bearer " + s.config.AuthKey
37-
if subtle.ConstantTimeCompare([]byte(authHeader), []byte(expectedAuthHeader)) != 1 {
34+
func (s *Server) authorize(w http.ResponseWriter, r *http.Request) bool {
35+
expected := "Bearer " + s.config.AuthKey
36+
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte(expected)) != 1 {
3837
http.Error(w, "Unauthorized", http.StatusUnauthorized)
38+
return false
39+
}
40+
return true
41+
}
42+
43+
func (s *Server) HandleUpload(w http.ResponseWriter, r *http.Request) {
44+
if !s.authorize(w, r) {
3945
return
4046
}
4147

@@ -93,18 +99,6 @@ func (s *Server) HandleUpload(w http.ResponseWriter, r *http.Request) {
9399
slog.Info("file uploaded", "id", id, "type", contentType, "url", fullURL)
94100
}
95101

96-
func determineExtension(contentType, lang string) string {
97-
if strings.HasPrefix(contentType, "text/") && langRegex.MatchString(lang) {
98-
return "." + lang
99-
}
100-
101-
exts, err := mime.ExtensionsByType(contentType)
102-
if len(exts) == 0 || err != nil {
103-
return ".bin"
104-
}
105-
return exts[0]
106-
}
107-
108102
func (s *Server) HandleServe(w http.ResponseWriter, r *http.Request) {
109103
id := strings.TrimPrefix(r.URL.Path, "/")
110104

@@ -123,16 +117,14 @@ func (s *Server) HandleServe(w http.ResponseWriter, r *http.Request) {
123117

124118
expiry, metaErr := s.store.GetMeta(id)
125119
if metaErr != nil && !errors.Is(metaErr, os.ErrNotExist) {
120+
slog.Error("failed to read file metadata", "id", id, "error", metaErr)
126121
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
127122
return
128123
}
129124
if metaErr == nil && time.Now().After(expiry) {
130125
if err := s.store.DeleteFile(id); err != nil {
131126
slog.Warn("failed to delete expired file", "id", id, "error", err)
132127
}
133-
if err := s.store.DeleteMeta(id); err != nil {
134-
slog.Warn("failed to delete expired file metadata", "id", id, "error", err)
135-
}
136128
http.Error(w, "Gone", http.StatusGone)
137129
return
138130
}
@@ -157,6 +149,38 @@ func (s *Server) HandleServe(w http.ResponseWriter, r *http.Request) {
157149
}
158150
}
159151

152+
func (s *Server) HandleDelete(w http.ResponseWriter, r *http.Request) {
153+
if !s.authorize(w, r) {
154+
return
155+
}
156+
157+
id := strings.TrimPrefix(r.URL.Path, "/")
158+
159+
if err := s.store.DeleteFile(id); err != nil {
160+
if errors.Is(err, os.ErrNotExist) {
161+
http.NotFound(w, r)
162+
return
163+
}
164+
slog.Error("failed to delete file", "id", id, "error", err)
165+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
166+
return
167+
}
168+
169+
w.WriteHeader(http.StatusNoContent)
170+
}
171+
172+
func determineExtension(contentType, lang string) string {
173+
if strings.HasPrefix(contentType, "text/") && langRegex.MatchString(lang) {
174+
return "." + lang
175+
}
176+
177+
exts, err := mime.ExtensionsByType(contentType)
178+
if len(exts) == 0 || err != nil {
179+
return ".bin"
180+
}
181+
return exts[0]
182+
}
183+
160184
func parseTTL(s string) (time.Duration, error) {
161185
if strings.HasSuffix(s, "d") {
162186
n, err := strconv.Atoi(strings.TrimSuffix(s, "d"))

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func main() {
2525

2626
mux.HandleFunc("POST /", server.HandleUpload)
2727
mux.HandleFunc("GET /{id}", server.HandleServe)
28+
mux.HandleFunc("DELETE /{id}", server.HandleDelete)
2829
mux.HandleFunc("GET /", http.NotFound)
2930
mux.HandleFunc("GET /favicon.ico", http.NotFound)
3031

cmd/server/storage.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ func (s *DiskStore) performCleanup() {
7575
if err := s.DeleteFile(id); err != nil && !errors.Is(err, os.ErrNotExist) {
7676
slog.Warn("sweep: failed to delete expired file", "id", id, "error", err)
7777
}
78-
if err := s.DeleteMeta(id); err != nil && !errors.Is(err, os.ErrNotExist) {
79-
slog.Warn("sweep: failed to delete expired meta", "id", id, "error", err)
80-
}
8178
return nil
8279
})
8380
if err != nil && !errors.Is(err, os.ErrNotExist) {
@@ -141,11 +138,15 @@ func (s *DiskStore) GetFile(id string) (string, bool) {
141138
func (s *DiskStore) DeleteFile(id string) error {
142139
path, ok := s.GetFile(id)
143140
if !ok {
141+
_ = s.DeleteMeta(id)
144142
return os.ErrNotExist
145143
}
146144
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
147145
return err
148146
}
147+
if err := s.DeleteMeta(id); err != nil {
148+
slog.Warn("failed to clean up meta file", "id", id, "error", err)
149+
}
149150
dir := filepath.Dir(path)
150151
_ = os.Remove(dir)
151152
_ = os.Remove(filepath.Dir(dir))

0 commit comments

Comments
 (0)