anusharanganathan / data2paper

Rails helper application to submit data papers from repositories to publishers
7 stars 1 forks source link

Add feature to download pre-filled template file #20

Closed anusharanganathan closed 6 years ago

anusharanganathan commented 6 years ago
anusharanganathan commented 6 years ago

See commits 8a2dfc35, 3b0b25df3253 and 12532ed9b

gem docx_replace - DocxReplace::Doc.new returns NameError: uninitialized constant DocxReplace

Show method in controller returns nil for pre-populated docx file

martyn-w commented 6 years ago

This was a fun problem to resolve!

Firstly, the gem error NameError: uninitialized constant DocxReplace I think is a red herring - try removing your rbenv gems and reinstalling them; I don't get this in the dockerised version.

Secondly, the files are definitely being sent as zero bytes, even though they are correctly generated. I have traced the problem to send_file in the show method of data2paper/app/controllers/hyrax/data_paper_template_controller.rb. It doesn't seem to work well the the temporary files we are generating. Downloading the file via curl reveals that a stream is being prematurely closed:

$ curl -v http://localhost:3000/concern/data_papers/pg15bd88s/template
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /concern/data_papers/pg15bd88s/template HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/octet-stream
< Content-Disposition: attachment; filename="JJJpaper_template_pg15bd88s_data2paper_template.docx20180222-46-7fl1qp"
< Content-Transfer-Encoding: binary
< Cache-Control: private
< Set-Cookie: _data2paper_session=aWpCSHcwRmY0ZDRvMEs1dWtvWUNrNGlMMENkZVpyd05YeEtCWFA4TWhLZHRZTDI4bHpqN055eU53NHZ4dy9saVZFMGIwcmRpR3RTb0FId2dzV1VHcWZGQTEzeEJ0anB2RzVXd1lVL01XSmZEUDVCa3dpeFNiV3k4N2VnMkQrRUVrcmc3SHBpcWloUDF3WU5HQ3oyVlpnPT0tLUFheWJ4MnlXNk5LZzhka2VXVEZHUUE9PQ%3D%3D--f9dd132634aa900f7db9d2305f5551e006f1e7e8; path=/; HttpOnly
< X-Request-Id: 794f5c9a-d521-4c41-abf8-b90f0a65dd6a
< X-Runtime: 3.917687
< Transfer-Encoding: chunked
< 
* transfer closed with outstanding read data remaining
* stopped the pause stream!
* Closing connection 0
curl: (18) transfer closed with outstanding read data remaining

However, a quick fix is to read the file into memory first and then use the send_data method instead: send_data File.read(file)

Then, everything works as expected 😄 e.g.

Template:

screen shot 2018-02-22 at 21 16 14

Generated file

screen shot 2018-02-22 at 21 16 18
emanuil-tolev commented 6 years ago

read the file into memory first

These are not expected to ever be particularly big, right? "Pre-filled template" doesn't shout "large files" to me.

anusharanganathan commented 6 years ago

Yes, the file should not be no more than a few mb. We can read it in memory. Thanks a lot @martyn-w

martyn-w commented 6 years ago

Closing as this now works - but please re-open if there is more to do.

screen shot 2018-03-03 at 17 11 29