0

We created an app on Android that a user can upload an image. The image is send through a post request on a Rails application as a bitmap. An example of what I get is this.

I want to use this hex code string in order to re-create the image and then save it to my Rails app as I could do with Paperclip.

Any tip or guidance to the right direction would be really appreciated.

1 Answers1

1

For me, this looks very much like a JPEG file (it starts with 0xFFD8FF), so you could just save the string to a file and you're done:

class UploadsController

   def create
     storage_path.open('w') {|f| f.write params[:file] }
     head :ok
   end

protected

  def storage_path
    @storage_path ||= Rails.root.join('data/uploads', current_user.id, Time.now.to_i.to_s(36) << '.jpg')
  end
end

Of course, this assumes you have a current_user method identifying a logged-in user (e.g. from Devise), and you want to save the file locally in $RAILS_ROOT/data/uploads/$USER_ID/.

For production, I'd create an Upload (or Image (or Media)) model (belongs_to :user), and move the processing logic (i.e. find storage place, convert string to file, additional checks) into that class.

DMKE
  • 4,553
  • 1
  • 31
  • 50