y9v/carrierwave-base64

Upload Path Issue for Base64 Updates

ziyan-junaideen opened this issue · 6 comments

I am implementing an API using Grape mounted on Rails. I followed the instructions in the gem to implement the ability to upload a file using a Base64 string of an image. But the uploaded location seems to be messed up.

Upload location

  • Normally: public/uploads/customer/profile_picture/:id
  • Uploads to: public/tmp/1398957623-9523-4518

When I upload using a file it works. But when I use a base 64 string it doesn't.

* If this is a known issue, could you suggest a fix for this? If not could you give me a hint on what I might need to look at?*

Thanks in advance.

The files I think which are important are as follows:

# encoding: utf-8

class ProfilePictureUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provides the default URL when an image is not uploaded
  def default_url
    ActionController::Base.helpers.asset_path('users/no-profile-picture.png')
  end

  version :thumb do
    process resize_to_fit: [58, 58]
  end

  def extension_white_list
    %w(jpg jpeg png)
  end

end
class User < ActiveRecord::Base
  # blah...

  # Profile picture upload
  # mount_uploader :profile_picture, ProfilePictureUploader
  mount_base64_uploader :profile_picture, ProfilePictureUploader

  # blah...
end
class AccountApi < Grape::API

  resource :account do
    # blah....

    desc 'Upload profile picture', { headers: API::HEADER_SETTINGS }
    params do
      requires :user, type: Hash do
        requires :profile_picture, type: Rack::Multipart::UploadedFile
      end
    end
    post :profile_picture do
      profile_picture = params[:user][:profile_picture]

      status = current_user.update(profile_picture: profile_picture)

      {
        uddated:  status,
        url:      current_user.picture,
        size:     profile_picture[:tempfile].size
      }
    end

    desc 'Upload profile picture (base 64)', { headers: API::HEADER_SETTINGS }
    params do
      requires :user, type: Hash do
        requires :profile_picture, type: String
      end
    end
    post :profile_picture_base64 do
      profile_picture = params[:user][:profile_picture]

      status = current_user.update(profile_picture: profile_picture)

      {
        uddated:  status,
        url:      current_user.picture,
        size:     current_user.profile_picture.size
      }
    end

  end

end
y9v commented

@ziyan-junaideen Can you give me an example of a base64 upload? At least the beginning of the string

y9v commented

This is actually very strange. In fact the mount_base64_uploader does not influenses the store_dir in any way, it only transforms the base64-encoded string to a StringIO, and passes it further to carrierwave.

True that, I cloned the app to just play with it locally and was confused what might be causing the issue. Probably needs some close look and debugging.

Judging by '0 issues', I think this is a local issue (particular with the project) is it? I will do some debugging and if I find a reson, will come back to you.

y9v commented

Thanks! I will just close this issue for now, if you think that this is gem specific, just open it again