Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to delete file promptly after ctx.FileAttachment(file) #1113

Open
anyfig opened this issue May 14, 2024 · 5 comments
Open

how to delete file promptly after ctx.FileAttachment(file) #1113

anyfig opened this issue May 14, 2024 · 5 comments
Labels
question Further information is requested

Comments

@anyfig
Copy link

anyfig commented May 14, 2024

Describe the Question
hello, friends, delete a file after download it is a very common requirement in business, i want to impl it in hertz, and meet this problem, err: The process cannot access the file because it is being used by another process.
Reproducible Code
in java, its ok to delete file after download it.

public void fileDownload(String fileName, Boolean delete, HttpServletResponse response){
    FileUtils.writeBytes(baseDir+fileName, response.getOutputStream());
    if (delete){
        FileUtils.deleteFile(baseDir+fileName);
    }
}

in hertz, I tried two ways, neither work. (err: The process cannot access the file because it is being used by another process)

func FileDownload1(ctx context.Context, c *app.RequestContext) {
    if req.Delete {
        go func() {
            time.Sleep(15 * time.Second)
            DeleteFile(baseDir+req.FileName)
        }()
    }
    c.FileAttachment(baseDir+req.FileName, req.FileName)
}
func FileDownload2(ctx context.Context, c *app.RequestContext) {
    if req.Delete {
        defer DeleteFile(baseDir+req.FileName)
    }
    c.FileAttachment(baseDir+req.FileName, req.FileName)
}

Additional context
in FileDownload1, when time.Sleep(enough_time), file would be deleted successfully, but the time is uncontrollable.

@li-jin-gou
Copy link
Member

c.FileAttachment(baseDir+req.FileName, req.FileName)
DeleteFile(baseDir+req.FileName)

@anyfig
Copy link
Author

anyfig commented May 14, 2024

c.FileAttachment(baseDir+req.FileName, req.FileName) DeleteFile(baseDir+req.FileName)

不行啊
c.FileAttachment()后跟DeleteFile()或defer DeleteFile()都一样
222

@li-jin-gou
Copy link
Member

看了一下代码,因为文件正在传输,hertz在读取文件,所以立刻删除失败了,需要等请求结束才能删除成功。

@anyfig
Copy link
Author

anyfig commented May 15, 2024

看了一下代码,因为文件正在传输,hertz在读取文件,所以立刻删除失败了,需要等请求结束才能删除成功。

c.FileAttachment()函数的文件传输好像是在另一个goroutine中,什么时候完成下载还没办法判断。o(╥﹏╥)o,有没有其他办法

@li-jin-gou
Copy link
Member

看了一下代码,因为文件正在传输,hertz在读取文件,所以立刻删除失败了,需要等请求结束才能删除成功。

c.FileAttachment()函数的文件传输好像是在另一个goroutine中,什么时候完成下载还没办法判断。o(╥﹏╥)o,有没有其他办法

一起研究下看看,可以飞书群联系下我

@li-jin-gou li-jin-gou added the question Further information is requested label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants