rgrinberg / opium

Sinatra like web toolkit for OCaml
MIT License
753 stars 68 forks source link

file_upload broken: example adds newline before EOF on upload #258

Open lindig opened 3 years ago

lindig commented 3 years ago

A file uploaded with the server in /example/file_upload is not identical to the original file as can be verified with a checksum. I believe a newline is added at the end of the file.

To reproduce the bug, upload a file with the demo application (either the simple or more styled one) and compare the uploaded file against the original. If this bug is indeed universal, this would render uploading images and other binary formats impossible.

lindig commented 3 years ago

Switching to https://github.com/dinosaure/multipart_form could be an option. Other people reported issues around handling of \r\n in the library that is currently used multipart-form-data library for form decoding.

lindig commented 3 years ago

I've verified this with uploading a PNG file using the simple (unstyled) upload example. The uploaded file has two extra bytes at the end, which is also evident from the file size. Can anybody confirm or reject this bug?

panglesd commented 3 years ago

I have the same issue.

An unsatisfactory fix is to remove the last two characters.

For instance, in the opium example, change the close function to this:

  let close filename file prev =
    let* () = prev in
    let* () = Lwt_unix.close file in
    let new_size = (Unix.stat filename).st_size - 2 in
    Lwt.return @@ Unix.truncate filename new_size