How store an image from URL with Active Storage
Asked Answered
D

4

5

My question is about: How can i upload an image from URL when i'm using Active Storage. I used the code from other post here in Stackoverflow but passing through the model method, the param which i need to store in my table. The curiously situation is that i'm receiving the next error:

ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update

But when i reload the show view, from this model, the images appears stored and deployed on my posts view.

Here my code in Post model:

class Post < ApplicationRecord
  require 'open-uri'

  has_one_attached :image_one
  has_one_attached :image_two
  has_one_attached :url_image

  before_save :grab_image

  
  def grab_image(url)
    downloaded_image = open(url)
    self.url_image.attach(io: downloaded_image, filename: "map.jpg", content_type:"image/jpg")
  end
  
end

This is my code in the Edit Action of my Controller:

  def update
    @posturl = params[:post][:url_image]
    @post.grab_image(@posturl)
    respond_to do |format|
      if @post.update!(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

Apparently, the record is saved but i receive this problem

I got the following links which talks about the chance to upload images from URLs but, i don´t know what else can i do:

Active Storage Overview - EdgeGuides

@omnilord/rails-5-2-activestorage-mitigating-adoption-pitfalls

Dinahdinan answered 29/6, 2018 at 6:1 Comment(0)
F
3

Maybe this work for u:

file = open(url)
user.image.attach(io: file, filename: "temp.#{file.content_type_parse.first.split("/").last}", content_type: file.content_type_parse.first)
Faludi answered 5/7, 2018 at 21:43 Comment(3)
I will test it inside my code and will share if it works. Thanks.Dinahdinan
I found a way to convert image files to Base64. Check this link: webres-studio.com/es/blog/…Dinahdinan
This code doesn't work and has no difference from the page questionAdversative
K
2

The simplest way to do this without having to enter filename explicitly is:

require 'open-uri'

url = URI.parse("https://your-url.com/abc.mp3")
filename = File.basename(url.path)
file = URI.open
user = User.first
user.avatar.attach(io: file, filename: filename)

This automatically saves the avatar against that particular user object.

In case you are using a remote service like S3 the URL can be retrieved by:

user.avatar.service_url
Keijo answered 2/8, 2020 at 7:1 Comment(2)
service_url works for me in production, but throws an error in development I added a callback in the objects controller before_action :set_host_for_local_storage and then private ActiveStorage::Current.host = request.base_url if Rails.application.config.active_storage.service == :local and now service_url works in both dev and productionMccourt
service_url would work only when you are using an S3 like service for storing your images. You must be using your local disk for storing the assets in your development environment. Though, I liked your workaround of adding before_action to handle the same :)Keijo
S
1

Here is an example:

file_url = image[:image_url]
download = open(file_url)
IO.copy_stream(download,
user.image.attach(
  io: download,
  filename: image[:name],
  content_type: image[:content_type],
))
Standish answered 29/6, 2018 at 9:56 Comment(6)
Doesn't work for me at all. I don't know if you use this code working with Active Storage?... Please, let me know your configuration if its possible.Dinahdinan
Normal Rails 5.2 application with Ruby 2.5.1 I even have no need to require open-uri in my modelStandish
i tried to remove the open-uri because I read that since Rails 5 it was integrated but, even that, my code doesn’t work.Dinahdinan
This is the error I’m getting... ActiveSupport::MessageVerifier::InvalidSignature in PostsController#update but the most weird situation is if refresh the page, the image is stored inside the uploads folder.Dinahdinan
Seems you placed upload method in controller. That's not right, try to use your model for that.Standish
Nope, I used the structure that I put in the main question. Using controller and method model to handle this task.Dinahdinan
E
0

While editing, you need to provide the signed id of the attached blob in case the user did not make any changes to the image.

Something like the below should work. You need to make this change in your form.

<%= form.file_field :image_one, value: image_one.blog.signed_id %>
Enmity answered 12/4, 2021 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.