From b4470cf4bdcccc526b8d2f0cbe7973cd166dbbd2 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Tue, 23 Mar 2021 22:26:42 +0800 Subject: [PATCH] fix upload large file >32M, close #98 --- httpstaticserver.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/httpstaticserver.go b/httpstaticserver.go index 2cefb05..a4335b1 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -232,22 +232,24 @@ func (s *HTTPStaticServer) hUploadOrMkdir(w http.ResponseWriter, req *http.Reque // Large file (>32MB) will store in tmp directory // The quickest operation is call os.Move instead of os.Copy + // Note: it seems not working well + var copyErr error - if osFile, ok := file.(*os.File); ok && fileExists(osFile.Name()) { - tmpUploadPath := osFile.Name() - osFile.Close() // Windows can not rename opened file - log.Printf("Move %s -> %s", tmpUploadPath, dstPath) - copyErr = os.Rename(tmpUploadPath, dstPath) - } else { - dst, err := os.Create(dstPath) - if err != nil { - log.Println("Create file:", err) - http.Error(w, "File create "+err.Error(), http.StatusInternalServerError) - return - } - _, copyErr = io.Copy(dst, file) - dst.Close() + // if osFile, ok := file.(*os.File); ok && fileExists(osFile.Name()) { + // tmpUploadPath := osFile.Name() + // osFile.Close() // Windows can not rename opened file + // log.Printf("Move %s -> %s", tmpUploadPath, dstPath) + // copyErr = os.Rename(tmpUploadPath, dstPath) + // } else { + dst, err := os.Create(dstPath) + if err != nil { + log.Println("Create file:", err) + http.Error(w, "File create "+err.Error(), http.StatusInternalServerError) + return } + _, copyErr = io.Copy(dst, file) + dst.Close() + // } if copyErr != nil { log.Println("Handle upload file:", err) http.Error(w, err.Error(), http.StatusInternalServerError)