Last active
February 12, 2023 18:23
-
-
Save rahul100885/f4c546a371dd2483800c to your computer and use it in GitHub Desktop.
Simple json file upload in rails api
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
#!/home/rahul/.rbenv/shims/ruby | |
require 'rest_client' | |
require 'base64' | |
image_data = Base64.encode64(File.open("/home/rahul/Pictures/download.jpg", "rb").read) | |
url = "http://myapiserver.com/api/user_profile/454-Rahul-Chaudhari?auth_token=XXXXXXXXXXXXXXX" | |
data = { user_profile: { picture: { content_type: "image/jpeg", filename: "DSCN436xxx0.JPG", data: image_data}}} | |
header = {content_type: "json", accept: "json", Authorization: "Token token=\"e4be11da6efca91461d4fd8fc5edb175\""} | |
RestClient.put( url, data, header) |
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 UserProfile < ActiveRecord::Base | |
has_attached_file :picture, | |
styles: { | |
original: {geometry: '1000x0>'}, | |
thumb: '100x100#', | |
large: '300x200>', | |
}, | |
convert_options: {large: "-quality 80 -strip -interlace Plane"}, | |
default_url: "http://s3-ap-southeast-1.amazonaws.com/#{ENV["S3_BUCKET"]}/user_profiles/placeholder.png", | |
path: (Rails.env == 'test' || Rails.env == 'development') ? ":rails_root/public/images/:class/:id/:style/:filename" : "/:class/:id/:style/:filename", | |
url: '/images/:class/:id/:style/:basename.:extension' | |
validates_attachment_size :picture, less_than: 5.megabytes | |
validates_attachment_content_type :picture, content_type: ['image/jpeg', 'image/png'], message: 'file type is not acceptable' | |
end |
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
module Api | |
module V1 | |
class UserProfileController < ApiController | |
load_and_authorize_resource :user, parent: false | |
def show | |
@user_profile = @user.user_profile | |
respond_with @user_profile, serializer: Api::V1::UserProfileSerializer | |
end | |
def update | |
@user_profile = @user.user_profile | |
if @user_profile.update(user_profile_params) | |
render json: {success: true, profile: Api::V1::UserProfileSerializer.new(@user_profile)}, status: :ok | |
else | |
render json: {success: false, errors: @user_profile.errors}, status: :unprocessable_entity | |
end | |
end | |
private | |
def user_profile_params | |
process_picture() | |
params.require(:user_profile).permit( :first_name, :last_name, :picture) | |
end | |
def process_picture | |
if params[:user_profile] && params[:user_profile][:picture] | |
data = StringIO.new(Base64.decode64(params[:user_profile][:picture][:data])) | |
data.class.class_eval { attr_accessor :original_filename, :content_type } | |
data.original_filename = params[:user_profile][:picture][:filename] | |
data.content_type = params[:user_profile][:picture][:content_type] | |
params[:user_profile][:picture] = data | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment