kemalcr / kemal

Fast, Effective, Simple Web Framework
https://kemalcr.com
MIT License
3.6k stars 187 forks source link

Kemal won't clean up broken file uploads #661

Open paulofrodriguez opened 1 year ago

paulofrodriguez commented 1 year ago

Description

During a upload to a kemal endpoint if the upload is interrupted for any reason, kemal keeps the temporary file saved on the disk and eliminates all the metadata information in a way that is impossible to know what file should be manually cleaned.

Steps to Reproduce

  1. create an endpoint to upload a file
  2. create the endpoint

` post "/alt" do |env|

begin

file_data = env.params.files["image1"]

temp_file_path = file_data.tempfile.path

puts temp_file_path

upload_id = "#{Time.utc.to_unix.to_s}-#{get_req_id()}"

original_file_size = env.request.headers["Content-Length"]

original_filename = file_data.filename

dest_file_path = ::File.join(["/tmp", "uploads/", file_data.filename+"_"+original_filename])

if  File.size(dest_file_path) == original_file_size
      File.rename(temp_file_path, dest_file_path)
      file_data.tempfile.delete
end

"upload complete id: #{upload_id}"

rescue ex 
    puts("na exc")

    #file_data.tempfile.delete
    File.delete(temp_file_path.to_s)

end end

add_handler PostOnlyHandler.new

Kemal.run `

  1. intiate a curl to upload a long file (using 8gb on this case): curl -X POST -F "image1=@file8" http://localhost:8000/alt -o test

  2. Press control+c to break the upload before it is finished

  3. go to the temp directory and list the files and check there is a trash segment with random name

Expected behavior: temp file should have been deleted or a filename should remains in some variable to allow manual cleaning

Actual behavior: no file name available and file is still there

Reproduces how often: 100% of times

Versions

Crystal 1.9.2 [1908c816f] (2023-07-19)

LLVM: 15.0.7 Default target: x86_64-unknown-linux-gnu

kemal: 1.4.0

Additional Information

sdogruyol commented 11 months ago

This could be a nice improvement. However currently I have no idea about how to achieve this behavior.... @straight-shoota anything on your side?

straight-shoota commented 11 months ago

I suppose temp files from file uploads should be deleted when the respective request has been processed.