rootstrap / active-storage-base64

Base64 support for ActiveStorage
https://rootstrap.com
MIT License
160 stars 16 forks source link

missing keywords: io, filename #46

Closed matedemorphy closed 4 years ago

matedemorphy commented 4 years ago

I just tried with a simple model and a simple configuration. I've allready run active storage migrations, it could be that?

santib commented 4 years ago

Hi @matedemorphy did you follow the README? If you did follow it, would you mind being more specific on the lines of code you added?

Having installed ActiveStorage and ran migrations is perfect, this gem is just an extension on top of it.

matedemorphy commented 4 years ago

@santib this is the code:

post model

class Picking < ApplicationRecord
  include ActiveStorageSupport::SupportForBase64

  has_one_base64_attached  :image
end

post controller

class Api::V1::PostsController < Api::V1::BaseController
  def create
    @post= Post.new(post_params)
    if @post.save
        @post.image.attach(data: params[:post][:image])
    end
  end

  private
    def post_params
        params.require(:post).permit(:title, :body)
     end
end

postman post request


{
  "post": {
       "title":"First Post",
    "body": "content for the post",
    "image": "/9j/4RNBRXhpZgAATU0AKg...."
    }
}
santib commented 4 years ago

@matedemorphy Great thanks for clarifying, I think your error is that the value of the params[:post][:image] should be in the data uri format instead of "/9j/4RNBRXhpZgAATU0AKg...." so you are missing a small part before the actual data.

matedemorphy commented 4 years ago

Thanks for the answer: @santib

new postman post request

{
  "post": {
       "title":"First Post",
    "body": "content for the post",
    "image": "data:image/jpeg;base64,/9j/4RNBRXhpZgAATU0AKg...."
    }
}

Now i'm getting thi error:

ActiveStorage::IntegrityError in Api::V1::PickingsController#create
Unsupported source URL: #<StringIO:0x00007fb15c7dac70>
santib commented 4 years ago

@matedemorphy hmm just to discard a common issue, is the Content-Type header application/json ?

matedemorphy commented 4 years ago

if i use strong params, like this:

  def create
    @post= Post.create(post_params)
  end

   def post_params
       params.require(:post).permit(:title, :body, :image)
    end

Then the error is this:

ActiveSupport::MessageVerifier::InvalidSignature in Api::V1::PickingsController#create

😭

santib commented 4 years ago

if you want to use strong params I think you need to do it this way

    def post_params
       params.require(:post).permit(:title, :body, image: :data)
    end

notice the data is also whitelisted.

Did you check that your Content-Type sent in postman is fine?

matedemorphy commented 4 years ago

@santib

In post headers: Content-Type: application/json

matedemorphy commented 4 years ago

following your suggestion:


 def post_params
   params.require(:post).permit(:title, :body, image: :data)
 end

I'm getting in the console: Unpermitted parameters: :image

I just realize this "error"? in console:  ```Can't verify CSRF token authenticity.```

it will have something to do?, I am trying to upload the image to Cloudinary, but I am sure that this configuration is correct
santib commented 4 years ago

oh yes seems like that could be the issue. https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html , if you are using an API I don't think you can use the Rails CSRF protection

matedemorphy commented 4 years ago

Thanks for your help @santib

Ok, i've changed

protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }

for:

protect_from_forgery unless: -> { request.format.json? }

but, still

ActiveStorage::IntegrityError in Api::V1::PostsController#create
Unsupported source URL: #<StringIO:0x00007fdcdc7b05f0>

i'm using Rails 6.0.1. This is for a React Native app, i started following this tutorial, but it didn't work out for me, so i intaled active-storage-base64 gem, to add support for base64 attachments to ActiveStorage, saddly I'm going for 2 days of being with this problem and I can't make it work 😩

santib commented 4 years ago

@matedemorphy Is it possible that those issues are related to cloudinary not to ActiveStorage itself? For example here people was getting IntegrityError because of missing credentials for AWS https://stackoverflow.com/questions/50307502/blob-error-with-active-storage-rails-5-2

Also check this https://github.com/cloudinary/cloudinary_gem/issues/366

matedemorphy commented 4 years ago

Thnaks for keep trying to help me @santib. cloudinary is ok. meanwhile don't try to uplosd using base64, wich is exactly what the app needs.

I've allready try this:

decoded_data = Base64.decode64(data)
washed_image = MiniMagick::Image.read(decoded_data)
washed_image.format("png")
object.images.attach(
io: StringIO.new(washed_image.to_blob),
filename: "image.png",
content_type: "image/jpeg"
)

and this:

  def image_io
    decoded_image = Base64.decode64(params[:post][:image])
     StringIO.new(decoded_image)
  end

same error: Unsupported source URL: #

that was yesterday, before i found your gem.

santib commented 4 years ago

@matedemorphy I think the error you are having is not because of this gem, but because of cloudinary + active storage. Why don't you try what they said in the comment in the issue I references before: io: washed_image.to_blob instead of io: StringIO.new(washed_image.to_blob) so like this

matedemorphy commented 4 years ago

@santib please could you show the complete code that you would implement

santib commented 4 years ago

I don't have the complete code because I don't even have the failing code, but from the issue in cloudinary I would try this

decoded_data = Base64.decode64(data)
washed_image = MiniMagick::Image.read(decoded_data)
washed_image.format("png")
object.images.attach(
  io: washed_image.to_blob,
  filename: "image.png",
  content_type: "image/jpeg"
)
matedemorphy commented 4 years ago

😤 undefined method `read' for # excluded from capture: DSN not set

gizipp commented 4 years ago

https://github.com/cloudinary/cloudinary_gem/issues/366#issuecomment-600620959

@matedemorphy I think the error you are having is not because of this gem, but because of cloudinary + active storage. Why don't you try what they said in the comment in the issue I references before: io: washed_image.to_blob instead of io: StringIO.new(washed_image.to_blob) so like this

Yeah, seems like it cloudinary gem issue https://github.com/cloudinary/cloudinary_gem/issues/366#issuecomment-600620959

haroldofurtado commented 4 years ago

Having here almost the same problem.

farmer controller

# POST /api/v1/farmers
      def create
        farmer = Farmer.new(farmer_params)
        if farmer.save
          render json: farmer, status: :created
        else
          render json: farmer.errors.full_messages, status: :unprocessable_entity
        end
      end

def farmer_params
        ActiveModelSerializers::Deserialization.jsonapi_parse(params)
end

My model:

has_one_base64_attached :picture

My request structure:

{
    "data": {
        "type": "farmers",
        "attributes": {
            "name": "None",
            "address": "None",
            "responsible_name": "None",
            "cell_phone": "00000000000",
            "picture": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QCkRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAABoBBQABAAAASgAAABsBBQABAAAAUgAAACgBAwABAAAAAgAAAGmHBAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAAABQAAkAcABA.....",
            "user_attributes": {
                "email": "none@none.com",
                "federal_id": "111111111",
                "password": "111111111"
            }
        }
    }
}

And I got the message:

ActiveSupport::MessageVerifier::InvalidSignature (ActiveSupport::MessageVerifier::InvalidSignature):

app/controllers/api/v1/farmers_controller.rb:30:in `create'

I checked with byebug and it returned the following:

(byebug) params.require(:data).permit(:type, attributes:[:name, :address,:responsible_name,:cell_phone,picture: :data,user_attributes: %i[id email federal_id password]])
Unpermitted parameter: :picture
<ActionController::Parameters {"type"=>"farmers", "attributes"=><ActionController::Parameters {"name"=>"Pitomba Alheia", "address"=>"Quadra 44 7961 Alameda Alícia, São Benedito do Sul, RS 50844-983", "responsible_name"=>"Pablo Lima Jr.", "cell_phone"=>"79981040537", "user_attributes"=><ActionController::Parameters {"email"=>"sssssssssss@alheia.com.br", "federal_id"=>"11ss1sssssss2cs11ss1111112", "password"=>"123456"} permitted: true>} permitted: true>} permitted: true>
(byebug) 

For some reason returns to me: Unpermitted parameter: :picture

I don't really know why!

haroldofurtado commented 4 years ago

Got the solution:

Just changed: "picture": {"data": "base64"}