Skip to content

Commit 5ea71c3

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

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

github/server/legacy.go

Lines changed: 3 additions & 1 deletion
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

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)