Skip to content

Commit 697cdac

Browse files
committed
Improve error handling for file closure and clean storage directory path
1 parent 1c2d3c6 commit 697cdac

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

github/server/legacy.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ func (s *Server) handleLegacyUpload(w http.ResponseWriter, r *http.Request) {
117117
f.Seek(start, io.SeekStart)
118118
}
119119
n, copyErr := io.Copy(f, r.Body)
120-
f.Close()
120+
if err := f.Close(); err != nil && copyErr == nil {
121+
copyErr = err
122+
}
121123
if copyErr != nil {
122124
http.Error(w, "storage error", http.StatusInternalServerError)
123125
return
@@ -238,13 +240,13 @@ func (s *Server) handleLegacyListFiles(w http.ResponseWriter, r *http.Request) {
238240
// GET /artifact/{path...}
239241
func (s *Server) handleLegacyDownload(w http.ResponseWriter, r *http.Request) {
240242
fullPath := r.PathValue("path")
241-
idx := strings.IndexByte(fullPath, '/')
242-
if idx < 0 {
243+
before, after, ok := strings.Cut(fullPath, "/")
244+
if !ok {
243245
http.Error(w, "invalid path", http.StatusBadRequest)
244246
return
245247
}
246-
cidStr := fullPath[:idx]
247-
filePath := fullPath[idx+1:]
248+
cidStr := before
249+
filePath := after
248250

249251
cid, err := strconv.ParseInt(cidStr, 10, 64)
250252
if err != nil {

github/server/server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
"log"
1112
"net/http"
1213
"os"
1314
"path/filepath"
@@ -586,7 +587,11 @@ func (s *Server) handleBlobUpload(w http.ResponseWriter, r *http.Request) {
586587
http.Error(w, "storage error", http.StatusInternalServerError)
587588
return
588589
}
589-
defer f.Close()
590+
defer func() {
591+
if err := f.Close(); err != nil {
592+
log.Printf("closing blob file: %v", err)
593+
}
594+
}()
590595
if _, err := io.Copy(f, r.Body); err != nil {
591596
http.Error(w, "storage error", http.StatusInternalServerError)
592597
return
@@ -604,7 +609,11 @@ func (s *Server) handleBlobUpload(w http.ResponseWriter, r *http.Request) {
604609
http.Error(w, "storage error", http.StatusInternalServerError)
605610
return
606611
}
607-
defer f.Close()
612+
defer func() {
613+
if err := f.Close(); err != nil {
614+
log.Printf("closing blob file: %v", err)
615+
}
616+
}()
608617
if _, err := io.Copy(f, r.Body); err != nil {
609618
http.Error(w, "storage error", http.StatusInternalServerError)
610619
return

github/server/start.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ func StartServer(cfg Config) (*RunningServer, error) {
5252
cfg.OIDCSub = "repo:local/repo:ref:refs/heads/main"
5353
}
5454

55-
absDir, err := filepath.Abs(cfg.StorageDir)
56-
if err != nil {
57-
return nil, fmt.Errorf("invalid storage directory: %w", err)
58-
}
59-
cfg.StorageDir = absDir
55+
cfg.StorageDir = filepath.Clean(cfg.StorageDir)
6056

6157
if err := os.MkdirAll(cfg.StorageDir, 0o755); err != nil {
6258
return nil, fmt.Errorf("creating storage directory: %w", err)

0 commit comments

Comments
 (0)