Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined Conversion Error in copy_to_local_file #86

Open
samsaradog opened this issue Mar 17, 2022 · 8 comments
Open

Undefined Conversion Error in copy_to_local_file #86

samsaradog opened this issue Mar 17, 2022 · 8 comments

Comments

@samsaradog
Copy link

Describe the bug
I'm getting an error when trying to copy a pdf file from a Paperclip::Attachment object (on S3) to a local tempfile.

To Reproduce
Steps to reproduce the behavior:

  1. Upload a PDF file to S3
  2. Create a tempfile
  3. call s3_file.copy_to_local_file
  4. See error below

Expected behavior
This error does not show up with paperclip 6.1.0, but several other errors and warnings do show up, which is why I am grateful you have made this gem available.

Screenshots

irb(main):010:0> s3_file.copy_to_local_file(:original, tmp_file)
[paperclip] copying /5270a9d4a5447bc32ad8557f380cb3eb3e12d997.pdf to local file #<File:0x000000010a05ec08>
Traceback (most recent call last):
        1: from (irb):10
Encoding::UndefinedConversionError ("\xFF" from ASCII-8BIT to UTF-8)

If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS 12.2.1
  • Ruby 2.7.5
  • Rails 6.1
  • kt-paperclip 6.4

Additional context
I found a similar problem with the paperclip gem from several years ago. Their solution involved adding the activesupport-json_encoder gem which is no longer supported. I don't have that gem in my Gemfile, so I presume they fixed it by 6.1.0.

Thanks again for all your work on this. Please let me know if there's anything else I can do to help.

@ssinghi
Copy link

ssinghi commented Apr 24, 2022

Hi @samsaradog The library is using aws s3 download_file method since version 6.2.0.
d4ef96c

I wonder if the error is due to an older version of aws s3 library, please try upgrading that.
Thanks!

@simon-isler
Copy link

@samsaradog were you able to solve the problem?

@samsaradog
Copy link
Author

Hi again - Sorry I missed your earlier response. I solved the problem by going back to paperclip, but have since started upgrading to Ruby 3.0.3, which is no longer compatible with paperclip. I am seeing the same error with aws-sdk 3.1 and kt-paperclip 6.4. Thanks again for your help! I promise I'll look back again for an answer or if you all need more information.

@ssinghi
Copy link

ssinghi commented Nov 19, 2022

@samsaradog Please try latest kt-paperclip 7.1.1

@samsaradog
Copy link
Author

@ssinghi Thanks for the reply. I'm on 7.1.1 but sadly am getting the same error. I was doing: attachment.copy_to_local_file(:original, tmp_file) but found a workaround: tmp_file.write(URI.open(attachment..expiring_url).read.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'))

Thanks again for your help.

@samsaradog
Copy link
Author

In case this helps, here's what shows up in my Gemfile.lock:

    kt-paperclip (7.1.1)
       activemodel (>= 4.2.0)
       activesupport (>= 4.2.0)
       marcel (~> 1.0.1)
       mime-types
       terrapin (~> 0.6.0)

I'm also on aws-sdk 3.1.0. Thanks again for all your help.

@samsaradog
Copy link
Author

Sorry for the churn, but it turns out my workaround above gets rid of the error, but corrupts the data it is copying. My latest workaround is to set up the encoding for the temp file as ascii-8bit: tmp_file = Tempfile.new(encoding: 'ascii-8bit')

@guldoe
Copy link

guldoe commented Sep 10, 2024

Hello @samsaradog, did you find any solution to the problem?
I really appreciate any help you can provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants