0

I want to upload an xls file(Libreoffice) via Shrine uploader gem, however I am getting an rollback error like File type must be one ..(There are mime types here in my initializers/shrine.rb ).. Here is my shrine.rb

 require "shrine"
require "shrine/storage/s3"

s3_options = {
  bucket:            "#{Rails.application.secrets[:aws_bucket_name]}",
  access_key_id:     "#{Rails.application.secrets[:aws_access_key_id]}",
  secret_access_key: "#{Rails.application.secrets[:aws_secret_access_key]}",
  region:            "#{Rails.application.secrets[:aws_region_name]}"
}

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
  store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
  #public_store: Shrine::Storage::S3.new(public: true, upload_options: { cache_control: "max-age=15552000" }, **s3_options)
}

Shrine.plugin :activerecord
Shrine.plugin :instrumentation
Shrine.plugin :determine_mime_type, analyzer: :marcel
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data

Shrine.plugin :presign_endpoint, presign_options: -> (request) {
  # Uppy will send the "filename" and "type" query parameters
  filename = request.params["filename"]
  type     = request.params["type"]

  {
    content_disposition:    ContentDisposition.inline(filename), # set download filename
    content_type:           type,                                # set content type (required if using DigitalOcean Spaces)
    content_length_range:   0..(10*1024*1024),                   # limit upload size to 10 MB
  }
}

Shrine.plugin :derivation_endpoint,
  secret_key: "secret",
  download_errors: [defined?(Shrine::Storage::S3) ? Aws::S3::Errors::NotFound : Errno::ENOENT]

And here is my shrine_uploader.rb

class ShrineUploader < Shrine

  plugin :processing
  plugin :validation_helpers # to validate image data
  plugin :versions
  plugin :add_metadata
  plugin :delete_raw
  plugin :recache
  plugin :default_storage, cache: :cache, store: :store

  Attacher.validate do
    validate_max_size 10.megabyte
    validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png', 'image/gif','image/tiff', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd-xls']
  end

  def generate_location(io, context)
    #type  = context[:record].class.name.downcase if context[:record]
    type  = context[:record].patron_id if context[:record]
    style = context[:version] if context[:version]
    name  = super # the default unique identifier[type, style, name].compact.join("/")
  end
end

I can upload xlsx files , however cant figure out why I cant upload xls files even if mime type is application/vnd.ms-excel.

Thanks for your help.

Hakan
  • 11
  • 4
  • It seems that Marcel is not correctly detecting the MIME type of your Excel file. Try inspecting the actual MIME type inside your `Attacher.validate` block with `puts get.mime_type`. – Janko Sep 25 '19 at 08:07
  • 1
    Thanks for your reply @janko-m. I tried to learn mime type with 'get.mime_type' and it turned out to be "application/xml" mime type.I added it to validate_mime_type_inclusion and solved the problem. – Hakan Sep 25 '19 at 14:59

3 Answers3

0
class FileUploader < Shrine
  plugin :validation_helpers
  plugin :pretty_location
  plugin :determine_mime_type

  Attacher.validate do
    validate_max_size 150.megabytes, message: "Large file sorry"
    #validate_mime_type %w[image/jpeg image/png image/jpg]
    validate_extension_inclusion %w[jpg jpeg png pdf xlsx]
  end
end

validate_extension_inclusion %w[jpg jpeg png pdf xlsx] help me <3

0

I tried to learn mime type with 'get.mime_type' and it turned out to be "application/xml" mime type.I added it to validate_mime_type_inclusion and solved the problem

Hakan
  • 11
  • 4
0

Hi I know it's a old post, but here is what I found the solution for XLS file.

Yes, even if you add "application/vnd.ms-excel" to support xls file it will fail to upload if you have condition "validate_mime_type_inclusion"

So in order to fix it, I have added the following code "application/x-ole-storage"

  MIME_TYPES = [
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/vnd.ms-excel",
    "application/x-ole-storage",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
    ]

 Attacher.validate do
      validate_mime_type_inclusion MIME_TYPES
  end