Skip to content

Commit

Permalink
Merge pull request #78 from patrickhener/fix-file-handle
Browse files Browse the repository at this point in the history
Fixing issue #77, closing the file handle correctly
  • Loading branch information
patrickhener authored Jul 25, 2024
2 parents 6f4834d + 2643234 commit 1c72a11
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 6 additions & 1 deletion httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"os"
"runtime"
"time"

"github.com/gorilla/mux"
Expand All @@ -24,7 +25,11 @@ func (fs *FileServer) SetupMux(mux *mux.Router, what string) string {
var addr string
switch what {
case modeWeb:
mux.Methods(http.MethodPost).HandlerFunc(fs.upload)
mux.Methods(http.MethodPost).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.upload(w, r)
// Run garbage collector to make sure file handle is synced and close properly
runtime.GC()
})
mux.PathPrefix("/").HandlerFunc(fs.handler)

addr = fmt.Sprintf("%+v:%+v", fs.IP, fs.Port)
Expand Down
3 changes: 3 additions & 0 deletions httpserver/updown.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (fs *FileServer) upload(w http.ResponseWriter, req *http.Request) {
logger.Warnf("Error opening file: %+v", err)
}
defer func() {
if err := osFile.Sync(); err != nil {
logger.Errorf("error syncing file: %+v", err)
}
if err := osFile.Close(); err != nil {
logger.Errorf("error closing file: %+v", err)
}
Expand Down

0 comments on commit 1c72a11

Please sign in to comment.