Skip to content

Commit

Permalink
delay delete temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong committed Oct 23, 2023
1 parent b17bff6 commit 657cbe5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Binary file added __debug_bin782867005
Binary file not shown.
16 changes: 16 additions & 0 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions route/v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 657cbe5

Please sign in to comment.