Coder Social home page Coder Social logo

Comments (5)

james-em avatar james-em commented on May 29, 2024 1

@janko
The bug will not be fixed?

While the code samples given above are useful, if copied into the following template: https://github.com/shrinerb/shrine/blob/master/SELF_CONTAINED_EXAMPLE.md - it will be inordinately helpful.

require "active_record"
require "shrine"
require "shrine/storage/memory"
require "down"

Shrine.storages = {
  cache: Shrine::Storage::Memory.new,
  store: Shrine::Storage::Memory.new,
}

Shrine.plugin :activerecord           # loads Active Record integration
Shrine.plugin :cached_attachment_data # enables retaining cached file across form redisplays
Shrine.plugin :restore_cached_data    # extracts metadata for assigned cached files
Shrine.plugin :determine_mime_type    # determine and store the actual MIME type of the file analyzed from file content
Shrine.plugin :validation_helpers, default_messages: {
  max_size: ->(max) { I18n.t("errors.file.max_size") },
  mime_type_inclusion: ->(list) { I18n.t("errors.file.mime_type_inclusion", list: list) }
}


class MyUploader < Shrine
  Attacher.validate do
    validate_mime_type(
      %w[application/pdf],
      message: I18n.t("activerecord.errors.messages.invalid_mime_type")
    )
    validate_max_size 2 * 1024 * 1024 * 1024 # 2GB
  end
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection.create_table(:posts) { |t| t.text :image_data }

class Post < ActiveRecord::Base
  include MyUploader::Attachment(:image)
end

puts "\n====== Without remove_invalid ======\n"

post = Post.new(image: Down.download("https://avatars.githubusercontent.com/u/63726983"))

puts "Image presence: #{post.image.present? ? "yes" : "no"}"
puts "Image Data: #{post.image_data}"
puts(post.valid? ? "Post is valid" : "Post is not valid")

puts("Saving: #{post.save ? "yes" : "no"}")


puts "\n\n====== With remove_invalid ======\n"

Shrine.plugin :remove_invalid         # remove and delete files that failed validation

post = Post.new(image: Down.download("https://avatars.githubusercontent.com/u/63726983"))

puts "Image presence: #{post.image.present? ? "yes" : "no"}"
puts "Image Data: #{post.image_data}"
puts(post.valid? ? "Post is valid" : "Post is not valid")

puts("Saving: #{post.save ? "yes" : "no"}")
source 'https://rubygems.org' do
    gem "activerecord"
    gem "sqlite3"
    gem "down"
    gem "shrine"
end

Output

====== Without remove_invalid ======
Image presence: yes
Image Data: {"id":"2a36893c33c72860814cda65893be303","storage":"cache","metadata":{"filename":"63726983","size":1540,"mime_type":"image/png"}}
Post is not valid
Saving: no


====== With remove_invalid ======
Image presence: no
Image Data: {"id":"a057c4be23af093520f126c1e30b4c3c","storage":"cache","metadata":{"filename":"63726983","size":1540,"mime_type":"image/png"}}
Post is not valid
Saving: no

from shrine.

janko avatar janko commented on May 29, 2024 1

Thanks for providing a reproducible example, I will investigate.

from shrine.

janko avatar janko commented on May 29, 2024

Could you post a self-contained script reproducing the issue from the template included in the contributing guide?

from shrine.

james-em avatar james-em commented on May 29, 2024

@janko

The bug will not be fixed?

from shrine.

benkoshy avatar benkoshy commented on May 29, 2024

@janko

The bug will not be fixed?

While the code samples given above are useful, if copied into the following template:
https://github.com/shrinerb/shrine/blob/master/SELF_CONTAINED_EXAMPLE.md - it will be inordinately helpful.

from shrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.