delay delete temp file

This commit is contained in:
LinkLeong 2023-10-23 09:06:41 +01:00
parent b17bff68dd
commit 657cbe5c41
3 changed files with 22 additions and 6 deletions

BIN
__debug_bin782867005 Executable file

Binary file not shown.

View file

@ -77,6 +77,22 @@ func RMDir(src string) error {
return nil
}
func RemoveAll(dir string) error {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return os.Remove(path)
}
return nil
})
if err != nil {
return err
}
return os.Remove(dir)
}
// Open a file according to a specific mode
func Open(name string, flag int, perm os.FileMode) (*os.File, error) {
f, err := os.OpenFile(name, flag, perm)

View file

@ -581,12 +581,12 @@ func PostFileUpload(c *gin.Context) {
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
return
}
if err := file.RMDir(tempDir); err != nil {
logger.Error("error when trying to remove `"+tempDir+"`", zap.Error(err))
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
return
}
go func() {
time.Sleep(11 * time.Second)
if err := file.RMDir(tempDir); err != nil {
logger.Error("error when trying to remove `"+tempDir+"`", zap.Error(err))
}
}()
}
} else {
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o644)