Last active
October 26, 2022 05:48
-
-
Save oojikoo-gist/6a65c8c125b51f6cd7ae to your computer and use it in GitHub Desktop.
rails: api_file_upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageUploader < CarrierWave::Uploader::Base | |
class FilelessIO < StringIO | |
attr_accessor :original_filename | |
attr_accessor :content_type | |
end | |
before :cache, :convert_base64 | |
def convert_base64(file) | |
if file.respond_to?(:original_filename) && | |
file.original_filename.match(/^base64:/) | |
fname = file.original_filename.gsub(/^base64:/, '') | |
ctype = file.content_type | |
decoded = Base64.decode64(file.read) | |
file.file.tempfile.close! | |
decoded = FilelessIO.new(decoded) | |
decoded.original_filename = fname | |
decoded.content_type = ctype | |
file.__send__ :file=, decoded | |
end | |
file | |
end | |
end | |
# alternative | |
class ImageUploader < CarrierWave::Uploader::Base | |
# Mimick an UploadedFile. | |
class FilelessIO < StringIO | |
attr_accessor :original_filename | |
attr_accessor :content_type | |
end | |
# Param must be a hash with to 'base64_contents' and 'filename'. | |
def cache!(file) | |
if file.respond_to?(:has_key?) && file.has_key?(:base64_contents) && file.has_key?(:filename) | |
local_file = FilelessIO.new(Base64.decode64(file[:base64_contents])) | |
local_file.original_filename = file[:filename] | |
extension = File.extname(file[:filename])[1..-1] | |
local_file.content_type = Mime::Type.lookup_by_extension(extension).to_s | |
super(local_file) | |
else | |
super(file) | |
end | |
end | |
end | |
# another | |
# | |
# POST /pictures | |
# POST /pictures.json | |
# param | |
# picture {:user_id => "1", :folder_id => 1, etc., :picture_path {:file => "base64 awesomeness", :original_filename => "my file name", :filename => "my file name"}} | |
def create | |
#check if file is within picture_path | |
if params[:picture][:picture_path]["file"] | |
picture_path_params = params[:picture][:picture_path] | |
#create a new tempfile named fileupload | |
tempfile = Tempfile.new("fileupload") | |
tempfile.binmode | |
#get the file and decode it with base64 then write it to the tempfile | |
tempfile.write(Base64.decode64(picture_path_params["file"])) | |
#create a new uploaded file | |
uploaded_file = ActionDispatch::Http::UploadedFile.new(:tempfile => tempfile, :filename => picture_path_params["filename"], :original_filename => picture_path_params["original_filename"]) | |
#replace picture_path with the new uploaded file | |
params[:picture][:picture_path] = uploaded_file | |
end | |
@picture = Picture.new(params[:picture]) | |
respond_to do |format| | |
if @picture.save | |
format.html { redirect_to @picture, notice: 'Picture was successfully created.' } | |
format.json { render json: @picture, status: :created, location: @picture } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @picture.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# encoding: utf-8 | |
class FileUploader < 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 | |
#START FROM BASE64 POST LINKED ABOVE | |
class FilelessIO < StringIO | |
attr_accessor :original_filename | |
attr_accessor :content_type | |
end | |
before :cache, :convert_base64 | |
def convert_base64(file) | |
if file.respond_to?(:original_filename) && | |
file.original_filename.match(/^base64:/) | |
fname = file.original_filename.gsub(/^base64:/, '') | |
ctype = file.content_type | |
decoded = Base64.decode64(file.read) | |
file.file.tempfile.close! | |
decoded = FilelessIO.new(decoded) | |
decoded.original_filename = fname | |
decoded.content_type = ctype | |
file.__send__ :file=, decoded | |
end | |
file | |
end | |
#END FROM POST LINKED ABOVE | |
# 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}/#{model.user_id}" | |
end | |
# Provide a default URL as a default if there hasn't been a file uploaded: | |
# def default_url | |
# "/images/fallback/" + [version_name, "default.png"].compact.join('_') | |
# end | |
# Process files as they are uploaded: | |
# process :scale => [200, 300] | |
# | |
# def scale(width, height) | |
# # do something | |
# end | |
# Create different versions of your uploaded files: | |
version :thumb do | |
process :resize_to_fit => [200, 300] | |
end | |
version :web do | |
process :resize_to_fit => [1000, 1000] | |
end | |
# Add a white list of extensions which are allowed to be uploaded. | |
# For images you might use something like this: | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
# Override the filename of the uploaded files: | |
# Avoid using model.id or version_name here, see uploader/store.rb for details. | |
def filename | |
if original_filename | |
Time.new.to_i.to_s+"_"+original_filename | |
end | |
end | |
end | |
class Picture < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :folders | |
attr_accessible :user_id, :picture_name, :picture_description, | |
:folder_id, :picture_path, :file_save | |
mount_uploader :picture_path, FileUploader | |
before_save :update_pictures_attributes | |
def update_pictures_attributes | |
self.file_size = picture_path.file.size | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment