В моем проекте я использую мини-магию, carrierwave и fog для загрузки изображения профиля пользователя в s3.
Я использую cropit (плагин jquery) для обрезки изображения вместо изображенияфайл Я получаю данные изображения в кодировке Base64 в виде параметров, но не знаю, как загрузить их в виде файла в файл s3
. Ниже приведены условия файла
user_profile_image_uploader.rb
class UserProfileImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
process resize_to_fit: [360, 87]
# 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
def filename
"#{original_filename}#{model.email.underscore}_profile_image"
end
def default_url
# For Rails 3.1+ asset pipeline compatibility:
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"test-img.jpeg"
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
%w(jpg jpeg gif png)
end
carrierwave.rb
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: Rails.application.secrets.aws_access_key_id,
aws_secret_access_key: Rails.application.secrets.aws_secret_access_key,
region: Rails.application.secrets.s3_region,
endpoint: Rails.application.secrets.s3_endpoint
}
config.fog_directory = Rails.application.secrets.aws_bucket
end
Все работало нормально, когда я просто загружал изображение в виде файла (upload_image.slim.html)
= simple_form_for(current_user, url: update_profile_path(current_user), html: { method: :put, id: :edit_user_logos }) do |f|
= f.input :profile_image, as: :file, label: "", autofocus: true, input_html: { class: "form-control input-sm user-profile-image-field user-profile-logo" }
Но теперь я использую cropit для редактирования изображения, и вместо файла я получаю изображение base64
upload_image.html.slim
.image-editor
input.cropit-image-input type="file"
.cropit-preview
input.cropit-image-zoom-input type="range"
button.update update profile img
javascript:
$('.image-editor').cropit({
exportZoom: 1.25,
imageBackground: true,
imageBackgroundBorderWidth: 50,
});
$(".update").click(function(){
var imageData = $('.image-editor').cropit('export', {
type: 'image/jpeg',
quality: 0.9,
originalSize: true,
});
$.ajax({
url: '/profile/update',
method: 'put',
data: {'user': {'profile_image': imageData } }
})
})
profile_controller.rb
def update
uploaded_io = params[:user][:profile_image]
metadata = "data:image/jpeg\;base64,"
base64_string = uploaded_io[metadata.size..-1]
blob = Base64.decode64(base64_string)
image = MiniMagick::Image.read(blob)
@user.update(profile_image: //not sure what to put here)
end
params
Parameters: {"user"=>{"profile_image"=>"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQg....
В контроллере, когда я
image.write 'file_name.png'
, оно сохраняет изображение в моем локальном компьютере, но не уверен, как его загрузитьна s3, без сохранения в моем локальном