diff options
Diffstat (limited to 'service/http/uploads.go')
-rw-r--r-- | service/http/uploads.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/service/http/uploads.go b/service/http/uploads.go index 7610ab28..8a46f230 100644 --- a/service/http/uploads.go +++ b/service/http/uploads.go @@ -2,6 +2,8 @@ package http import ( "encoding/json" + "fmt" + "github.com/sirupsen/logrus" "io" "io/ioutil" "mime/multipart" @@ -45,13 +47,16 @@ func (u *Uploads) MarshalJSON() ([]byte, error) { // Open moves all uploaded files to temp directory, return error in case of issue with temp directory. File errors // will be handled individually. -func (u *Uploads) Open() { +func (u *Uploads) Open(log *logrus.Logger) { var wg sync.WaitGroup for _, f := range u.list { wg.Add(1) go func(f *FileUpload) { defer wg.Done() - f.Open(u.cfg) + err := f.Open(u.cfg) + if err != nil && log != nil { + log.Error(fmt.Errorf("error opening the file: error %v", err)) + } }(f) } @@ -59,10 +64,13 @@ func (u *Uploads) Open() { } // Clear deletes all temporary files. -func (u *Uploads) Clear() { +func (u *Uploads) Clear(log *logrus.Logger) { for _, f := range u.list { if f.TempFilename != "" && exists(f.TempFilename) { - os.Remove(f.TempFilename) + err := os.Remove(f.TempFilename) + if err != nil && log != nil { + log.Error(fmt.Errorf("error removing the file: error %v", err)) + } } } } @@ -99,7 +107,13 @@ func NewUpload(f *multipart.FileHeader) *FileUpload { } // Open moves file content into temporary file available for PHP. -func (f *FileUpload) Open(cfg *UploadsConfig) error { +// NOTE: +// There is 2 deferred functions, and in case of getting 2 errors from both functions +// error from close of temp file would be overwritten by error from the main file +// STACK +// DEFER FILE CLOSE (2) +// DEFER TMP CLOSE (1) +func (f *FileUpload) Open(cfg *UploadsConfig) (err error) { if cfg.Forbids(f.Name) { f.Error = UploadErrorExtension return nil @@ -110,7 +124,11 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { f.Error = UploadErrorNoFile return err } - defer file.Close() + + defer func() { + // close the main file + err = file.Close() + }() tmp, err := ioutil.TempFile(cfg.TmpDir(), "upload") if err != nil { @@ -120,7 +138,10 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { } f.TempFilename = tmp.Name() - defer tmp.Close() + defer func() { + // close the temp file + err = tmp.Close() + }() if f.Size, err = io.Copy(tmp, file); err != nil { f.Error = UploadErrorCantWrite @@ -131,10 +152,8 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { // exists if file exists. func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true + if _, err := os.Stat(path); os.IsNotExist(err) { + return false } - - return false + return true } |