I am using tinymce-rails-imageupload plugin with dragonfly.
When the image is uploaded via separate form in popup window, it behaves as expected (save image in datastore).
But when the user drag-drop or paste image into TinyMCE, the imageupload plugin allows it. I tried to find a way to disable this behavior, but apparently there is no straightforward way to disable allowing image upload, while disallowing the past/drag-drop behavior. So I gave up on that..
Now, I'm trying to save BASE64 image in TinyMCE's content.
In controller:
def store_file
#image = Resource.new :res_image => params[:file]
#image.save
render json: {
image: {
url: #image.res_image.remote_url
}
}, content_type: "text/html"
end
def create
#entry = Entry.new(params[:entry])
# iterate through tinyMCE field params[:entry][:message]
# if image tag is found
# if value of src tag starts with "data:"
# then replace it with the output of
# Resource.create_image_from_base64(extracted_base64_value)
# end if
# end if
# end iteration
begin
#entry.save!
flash[:success] = "Entry was successfully created."
redirect_to entries_path
rescue Mongoid::Errors::Validations => e
render :action => "new"
end
end
In Resource model, I would have something like:
image_accessor :res_image
field :res_image_uid, type: String
field :res_image_name, type: String
def create_image_from_base64(base_64_encoded_data)
file = File.open('temp.png', 'wb') do|f|
f.write(Base64.decode64(base_64_encoded_data))
end
resource = # create Resource with temp file
file.close
resource.res_image.remote_url
end
Questions:
How to create "Entry with file"?
Is there a better approach for handling pasted/dragged-droped base64 images in TinyMCE with dragonfly?
Even if it is an old question:
look at this: https://groups.google.com/forum/#!topic/dragonfly-users/xNWIwZf5-_Y
Related
I used to generate PDFs for my users by using the wicked_pdf gem and writing
something like the example code below:
class ThingsController < ApplicationController
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "file_name" # Excluding ".pdf" extension.
end
end
end
end
I would then have a link on a page like <%= link_to report_pdf_path(#report), "Download PDF", target: "_blank" %> that would cause the PDF to show up in the user's browser as a new tab. This is the preferred behavior I would like, but it halts all requests until the PDF is completed for the user and some of these take quite some time to generate.
So I've since offloaded the generating of the PDF to ActiveJob which works nice, but I can't figure out how to have ActiveJob open the file in a new window yet. Currently, I have it writing to the server and then updating a partial that shows the files the user has requested. Below is an example of it.
class GeneratePdfJob < ApplicationJob
queue_as :default
def perform(*args)
params = args.first
generate_pdf_document(params)
end
def generate_pdf_document(params)
html = ApplicationController.new.render_to_string(
template: 'players/board_labels.pdf.erb',
locals: { player_ids: params[:player_ids] }
)
save_to_pdf(html, params[:pdf_title], params[:user_code])
end
def save_to_pdf(html, pdf_title, user_code)
pdf = WickedPdf.new.pdf_from_string(
html,
pdf: "#{pdf_title}",
layout: 'print',
encoding: 'utf-8'
)
pdf_name = "#{pdf_title}.pdf"
pdf_dir = Rails.root.join('public','uploads','reports',"#{user_code}")
pdf_path = Rails.root.join(pdf_dir,pdf_name)
# create the folder if it doesn't exist
FileUtils.mkdir_p(pdf_dir) unless File.directory?(pdf_dir)
# create a new file
File.open(pdf_path,'wb') do |file|
file.binmode
file << pdf.force_encoding("UTF-8")
end
end
end
So how can I make a method that would replace the save_to_pdf method and instead open the file in a new tab for the user? If this isn't possible could I open a tab when I initiate the ActiveJob that is a placeholder tab and then incorporate ActionCable to display the file some how after it's been generated for the user?
Controller
Save the object
How to Use This Controller to insert image in any folder and image url store in database
def create_json
#user = User.new(userFirstName: params[:userFirstName], userLastName: params[:userLastName], userEmail: params[:userEmail], password: encrypted_password, userImage: params[:userImage])
if #user.save #if save succeeds, redirect to the index action
redirect_to(:action => 'show', id: User.last.id)
else
#if not succeeds, redirect to the index action
redirect_to(:action => 'new')
end end
User(Model) mount_uploader :userImage, AvatarUploader
UsersController -> #user = User.new(user_params)
if #user.save
redirect_to(:action => 'show', id: User.last.id)
else
render :json => data_hash2, :content_type => 'application/json'
end
class AvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end end
If You Using Web Service
<form action="http://ruby/controllername/create" lass="new_user" id="new_user" enctype="multipart/form-data" accept-charset="UTF-8" method="post">
View following method for uploading files to app/assets/images/people_profile_images folder
First upload Image like this:
#user =User.new(user_params)
#this will create user form paramaters given
uploaded_file = params[:user][:photo]
#this will add uploaded image or file in uplaoded_fie
if (uploaded_file)
File.open(Rails.root.join('app/assets', 'images/people_profile_images', uploaded_file.original_filename), 'wb') do |file|
file.write(uploaded_file.read)
#user.photo = "/assets/people_profile_images/"+uploaded_file.original_filename
end
else
#user.photo = "/assets/people_profile_images/default_avatar.png"
end
then save user
#user.save
I don't know of an approach that can cover both: image uploading and then saving the url on the database.
You can do one or the other:
1) Use a gem called paperclip that helps upload image files.
See this links:
https://github.com/thoughtbot/paperclip#ruby-and-rails
https://teamtreehouse.com/library/image-uploads-in-ruby-on-rails-41 (paperclip, carrierwave, and dragonfly)
I have used paperclip and it has worked well for me.
OR:
2) Include it as an additional field on your filename.html.erb for your image_url
One caveat, of using this though is that this can be changed and you have no control of the image that might show on your site. Thus, the image_url should be from a reputable CDN or at least from a reputable user uploading to your site.
Create a column url:string in your model. Add it as a hidden field in your form. Set its value to the same as your remote_url using .js
var urladdy = input.remoteurl.val()
input.url.val(urladdy)
This will upload image with carrier wave and also save its url as string.
I am using Carrierwave to upload an image from a remote url :
def save
uid = SecureRandom.uuid
storedir = "uploads/#{uid}"
image = Image.new
image.image.store_dir = storedir
image.remote_image_url = "a remote url here"
if image.save!
respond_to do |format|
format.js
format.json { render json: {:saved => "#{image.image_url}", :id => "#{image.id}"} }
end
end
end
the returned result is correct :
Object { saved="https://mybucket.s3-eu-west-1.amazonaws.com/uploads/58406672-c227-4cec-a846-04c443e70f33/thumb_yr169-ae2f3bc4-74f4-47ee-950c-2e06756525b9-v2.png", id="47"}
As you can see the image url is this :
https://mybucket.s3-eu-west-1.amazonaws.com/uploads/58406672-c227-4cec-a846-04c443e70f33/thumb_yr169-ae2f3bc4-74f4-47ee-950c-2e06756525b9-v2.png
and when I go to check the file, it's there in the right folder, but when I go to the Rails Console and look for that record, the result of image_url is wrong :
> Rails console
> Image.find(47).image_url
> https://mybucket.s3-eu-west-1.amazonaws.com/uploads/thumb_yr169-ae2f3bc4-74f4-47ee-950c-2e06756525b9-v2.png
The uid after /uploads/ is gone!!
any help please!!
UPDATE
Looks like carrierwave is go check store_dir in the uploader to show the image url, in my case I override it before saving the image, and I would like to use that custom store_dir
Yeah, that is because CarrierWave, like most similar libraries try to build your url dynamically.
Add this to your ImageUploader
#ImageUploader
def uid
'58406672-c227-4cec-a846-04c443e70f33'
end
def store_dir
"uploads/#{uid}"
end
#XController
def save
image = Image.new
image.remote_image_url = "a remote url here"
if image.save!
respond_to do |format|
format.js
format.json { render json: {:saved => "#{image.image_url}", :id => "#{image.id}"} }
end
end
end
UPDATE
Not totally sure of why you'd want to overide the store, but in order to do that, I think you may be better served by storing the additional details on the images table as such:
Add a column to your Image model, which may be the store_uid
In your ImageUploader, change your store_dir to use your builder or the field name, e.g if the column on Image is store_uid, with value: whatever-uid-has-been-stored-here, you could do:
def store_dir
# /uploads/whatever-uid-has-been-stored-here
"uploads/#{model.store_uid}"
end
A cleaner approach and one that's often advised is to create another uploader for each kind of images, for consistency within your application
In my Rails app, I enable users to upload images using Carrierwave and Amazon S3. I want to implement a feature that lets users edit existing images by rotating it 90 degrees.
I'm confused about where this code would go. Does it go in the image uploader file, or the image controller? And how is it called? I believe it should look something like this:
image = Image.find(params[:id])
image_obj = MiniMagick::Image.read(image.file)
image_obj.rotate(-90)
image_obj.write(image.file)
But I haven't been able to find examples to help me. If anyone can give me a pointer in the right direction, I would really appreciate it!
Edit
Thanks to deep for their thorough response! Here is what I ended up doing:
In my view:
# image.html.erb:
<%= link_to rotate_image_path(:id => image.id), :remote => true %>
In my controller:
# image_controller.rb:
def rotate
#image = Image.find(params[:id])
#image.rotated = true
#image.save
respond_to do |format|
format.js { render :nothing => true }
end
end
In my model:
# image.rb
attr_accessible :rotated
after_save :rotate_image, if: ->(obj){obj.rotated.present? && obj.rotated?}
def rotate_image
self.image_path.recreate_versions!
end
In my uploader:
# image_uploader.rb
process :rotate_img
def rotate_img
if model.rotated.present? && model.rotated?
manipulate! do |img|
img.rotate '-90'
img
end
end
end
The only real change I made was in the uploader, where I ran into errors trying to do a condition process. I put the conditional within the rotate_img method.
Here's my solution
First define a attribute accessor in your model and on update set it to true.
In your model
#image.rb
attr_accessor :rotate
In your controller
#images_controller.rb
def update
#image = Image.find(params[:id])
#image.rotate = true
#image.save
redirect_to root_path, :notice => "Bla bla bla"
end
Carrierwave provides a recreate_versions! method which will process and re-upload the image. In you case you can add a after_save callback that will trigger recreate_versions! method only if the rotate attribute is set to true.
In your model
#image.rb
after_save :rotate_image, if: ->(obj){ obj.rotate.present? and obj.rotate? }
def rotate_image
self.file.recreate_versions!
end
Now in your image uploader you can write the code to rotate a image.
#image_uploader.rb
.......
# It will replace the original image with rotated version
process :rotate_img, :if => model.rotate.present and model.rotate?
def rotate_img
manipulate! do |img|
img.rotate "90"
img #returns the manipulated image
end
end
If you don't want to replace the original image then all you have to do is to call process inside a version like
# Create different versions of your uploaded files:
version :rotated_img do
process :rotate_img
end
For some strange reason I need to save a file (normally downloaded directly) in the server. In my case I have a PDF file created with PDFKit that I need to keep.
# app/controllers/reports_controller.rb
def show
#report = Report.find(params[:id])
respond_to do |format|
format.pdf { render :text => PDFKit.new(report_url(#report)).to_pdf }
end
end
If I go to reports/1.pdf I get the report I am expecting but I want to keep it in the server.
Is there a Raily way to save a file in the server from a respond to?
Just a quick note before I get to the actual answer: It looks like you're passing a fully-qualified URL (report_url(#report)) to PDFKit.new, which is a waste--it means PDFKit has to make a request to the web server, which in turn needs to go through Rails' router and so on down the line to fetch the contents of the page. Instead you should just render the page inside your controller with render_to_string and pass it to PDFKit.new, since it will accept an HTML string. Keeping that in mind...
This is covered in the "Usage" section of PDFKit's README. You would do something like this:
def show
#report = Report.find(params[:id])
kit = PDFKit.new render_to_string(#report) # pass any options that the
pdf_file = kit.to_file '/some/file/path.pdf' # renderer needs here (same
# options as `render`).
# and if you still want to send it to the browser...
respond_to do |format|
format.pdf { render :file => pdf_file.path }
end
end