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

PDF upload corrupted #52

Closed
xevix opened this issue Mar 17, 2016 · 3 comments
Closed

PDF upload corrupted #52

xevix opened this issue Mar 17, 2016 · 3 comments

Comments

@xevix
Copy link

xevix commented Mar 17, 2016

Using Rails 4 and the ActiveRecord Shrine plugin, there seems to be file corruption in PDF uploads.

At some point between being a Rack file coming into Rails and the process method being called, the copy made by Shrine into the cache is being corrupted consistently.

# Original
$ ll /tmp/*.pdf
-rw-------. 1 foo bar 2520603 Mar 17 16:28 /tmp/RackMultipart20160317-60267-1082pik.pdf

# Shrine's Cache
$ ll /project/public/uploads/cache/
... snip ...
-rw-r--r--. 1 501 foo 2520347 Mar 17 16:28 27f745231e058660d965d47bfe44fce7eb30a2c96e837227d4917567f1d1.pdf
... snip ...

Filesizes

Original: 2520603
Shrine: 2520347

I've had a bit of trouble following the stacktrace to see at what exact point the io param of process copies the original Rack file to see what could be causing this, so if you know where please let me know and I can keep hunting. Thanks.

File used: https://www.gnu.org/software/emacs/manual/pdf/emacs.pdf

@janko
Copy link
Member

janko commented Mar 17, 2016

What plugins are you using? Could you include your uploader, and mention any other plugins that you're globally loading?

Also, are you using the latest version (1.3.0)? Because it looks like the PDF wasn't rewinded, and before 1.3.0 there were two bugs; one was that store_dimensions plugin wouldn't rewind the IO if it wasn't an image (but you probably don't have it loaded if you're uploading PDFs), other was that IO wasn't rewinded with determine_mime_type plugin when the :mimemagic analyzer was used.

@xevix
Copy link
Author

xevix commented Mar 17, 2016

Ah, sure. I'm on Shrine 1.2.0. I'll try updating and see if it's still an issue.

# initializers/shrine.rb

require "shrine/storage/file_system"

Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"),
}

Shrine.plugin :activerecord

# image_uploader.rb

require "image_processing/mini_magick"

class ImageUploader < Shrine
  MAX_IMAGE_SIZE_MB = 50

  include ImageProcessing::MiniMagick

  plugin :determine_mime_type
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :validation_helpers
  plugin :versions, names: [:original, :thumb, :large]
  plugin(:default_url) { |_|  '/img/preview-not-available.jpg' }

  Attacher.validate do
    validate_max_size MAX_IMAGE_SIZE_MB.megabytes, message: "is too large (max is #{MAX_IMAGE_SIZE_MB} MB)"
    validate_mime_type_inclusion ['image/jpeg', 'image/png', 'image/gif', 'application/pdf']
  end

  def process(io, context)
    case context[:phase]
      when :store
        thumb = if io.mime_type == 'application/pdf'
          with_minimagick(io.download) do |pdf|
            # BUG: accessing `pdf` fails to open the file, presumably because it's corrupted
            pdf.pages[0].resize("200x200")
          end
        else
          resize_to_limit!(io.download, 200, 200)
        end
        large = resize_to_limit!(io.download, 800, 800)
        { original: io, thumb: thumb, large: large }
    end
  end
end

@xevix
Copy link
Author

xevix commented Mar 17, 2016

Using 1.3.0 seems to have fixed the issue, thanks! Closing.

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

2 participants