CarrierWave::Data
Allows CarrierWave to process Base64 encoded file uploads.
Installation
Add this line to your application's Gemfile:
gem 'carrierwave-data'
And then execute:
$ bundle
Or install it yourself as:
$ gem install carrierwave-data
Usage
Assuming you have a User
model with a UserAvatarUploader
mounted on an avatar
attribute.
class User < ActiveRecord::Base
mount_uploader :avatar, UserAvatarUploader
end
A User
's avatar
can be set using a Base64 encoded string, as follows.
user = User.new
user.avatar_data = {
data: 'iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABuBvAAAABGdBTUE...kJggg==',
name: 'avatar',
ext: 'jpg'
}
## or
# user.avatar_data = 'iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABuBvAAAABGdBTUE...kJggg=='
user.save
user.avatar? # => true
user.avatar # => #<UserAvatarUploader:0x007f9575fddc18 @model=#<User id: 1, avatar: "2d45cced04356fcfac278ef06f76d567">, @mounted_as=:avatar, ...>
Customizations
By default, the filename of the decoded uploaded file is set to a random hex string, using SecureRandom.hex
. To customize this behaviour, override the decoded_#{mounted_as}_filename
method on your model.
class User < ActiveRecord::Base
mount_uploader :avatar, UserAvatarUploader
def decoded_avatar_filename
'avatar'
end
end
You can also customize the extension of the decoded uploaded file by overriding the decoded_#{mounted_as}_extension
method. By default, no extension is set.
class User < ActiveRecord::Base
mount_uploader :avatar, UserAvatarUploader
def decoded_avatar_extension
:png
end
end
Contributing
- Fork it ( https://github.com/RaMin0/carrierwave-data/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request