I'm using mongoid-grid_fs on the following JRuby setup...
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_07-b11 +indy [Windows 7-amd64]
I've got a Sinatra application that adds image files into the database for these to be later accessed from the client API. While I haven't noticed any problems with separate XML files that I am adding to the database, I've noticed the images (JPGs) are corrupted when served back to the client.
Example of the original file before being added to the database (apologies for the size):
![original](https://camo.githubusercontent.com/7713fe0af4c2363928e61b6c74ff3a3d0ca5a4d347b1cd344d6d6673a3c660ad/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3234343139382f313031363338302f61346433363035322d306266382d313165332d393933662d3364653163363936323438382e6a7067)
And this is the result after being saved to Mongo and served back:
![jruby-1 7 4](https://camo.githubusercontent.com/445a960879118cc86382583527208e0411021c06554f75f50bc39cb901b8505e/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3234343139382f313031363431302f35353563343466632d306266392d313165332d396235372d6535653835636430363433632e6a7067)
And another, same image but after being processed through a resizer job (worse visual corruption):
![jruby-worse](https://camo.githubusercontent.com/37a6b02c88f32b683ae94c0fdfc932c9691aea28d265bf8ebe8f427e4164f343/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3234343139382f313031363434312f65363364396334362d306266392d313165332d383534392d3163376636303836303561622e6a7067)
I've investigated this by taking the core logic out my application and distilling it down to a basic script that performs the same job.
require "mongoid-grid_fs"
ENV["RACK_ENV"] = "development"
Mongoid.load! "C:/Temp/database/config.yml"
file = File.open "C:/Temp/original.jpg"
# also tested with. no change to result
# file = java.nio.file.FileSystems.default.get_path "C:/Temp/original.jpg"
db_file = Mongoid::GridFs.put file
extracted_db_file = Mongoid::GridFs.get db_file.id
File.open("C:/Temp/jruby.jpg", "wb") { |file| file.write extracted_db_file.data }
When run under jruby-1.7.4
this produces the corrupt image seen above. When run under ruby-1.9.3-p392
the image is reproduced perfectly.
I also broke down the put
and get
logic into separate scripts and run them on a combination of of JRuby vs MRI.
PUT | GET | Image OK?
JRuby | JRuby | No
JRuby | MRI | No
MRI | MRI | Yes
MRI | JRuby | Yes
So the result is that whenever JRuby performs the put
operation the image is corrupted.
Taking a look at the binary diff of the two images (using diffnow.com) there are only two differences, hex values of FF
have been deleted from the data at two points in the data. I've diffed other images and the same happens to these also.
![image_diff](https://camo.githubusercontent.com/a57901bf9e585d81f1c82cf3733381c1b8b2cec0a0a1ad939880cd3f9ff820b4/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3234343139382f313031363533352f37613632666463652d306266632d313165332d383531312d6335643265613665636237662e706e67)
I note you haven't stated any particular Ruby implementation is supported, I guess as this should Just Work™.
I'm no expert in the guts of JRuby (and I don't expect you to be either), rather I'd like someone more versed than me to take a quick look at the puts
implementation and see what might be the issue here. Also this issue is useful to others thinking of using JRuby with mongoid-grid_fs