我正在开发一个Rails应用程序,允许用户通过神社将他们的图像上传到AWS S3。到目前为止设置工作正常 - 但是,我想知道是否有任何直接提取每个用户的上传总大小。
例如,我确实希望免费用户只允许上传最大500MB,而会员可以说4GB。
Haven在Github或Google上发现任何可用的东西,一般来说,也许有人可以分享他们的知识。干杯!
当前代码:
photo.rb
class Photo < ApplicationRecord
include ImageUploader[:image]
before_create :set_name
belongs_to :employee
belongs_to :user
private
def set_name
self.name = "Photo"
end
end
image_uploader.rb
require "image_processing/mini_magick"
class ImageUploader < Shrine
include ImageProcessing::MiniMagick
plugin :determine_mime_type
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :pretty_location
plugin :processing
plugin :versions
Attacher.validate do
validate_max_size 5.megabytes, message: "is too large (max is 5 MB)"
validate_mime_type_inclusion %w[image/jpeg image/png], message: " extension is invalid. Please upload JPEGs, JPGs and PNGs only."
validate_min_width 400, message: " must have a minimum width of 400 pixel."
validate_min_height 250, message: " must have a minimum height of 250 pixel."
end
process(:store) do |io, context|
size_800 = resize_to_limit!(io.download, 800, 800)
size_300 = resize_to_limit!(io.download, 300, 300)
{original: io, large: size_800, small: size_300}
end
end
答案 0 :(得分:2)
如果要在Photo模型的image
列上添加验证错误消息,可以在Shrine验证中执行此操作,因为您可以访问那里的Photo记录。
class ImageUploader < Shrine
# ...
Attacher.validate do
record #=> #<Photo ...>
all_images = record.user.photos
.select { |photo| photo.image_attacher.stored? }
.map { |photo| photo.image[:large] }
total_size = all_images.map(&:size).inject(0, :+)
if total_size + get.size > 500.megabytes
errors << "exceeds total maximum filesize for your account (max is 500MB)"
end
# ...
end
end
首先,我们只过滤存在并上传到永久存储空间的附件(例如,如果您使用后台处理,则只会附加缓存文件的时间段。)
然后我们会对large
版本的所有图片大小进行总结,如您的示例所示。
最后,我们将当前附件大小添加到总大小,如果超出限制,我们会添加验证错误。
答案 1 :(得分:0)
所以这是我的解决方案 - 自定义验证器。非常基本,但我认为这样做(没有经过全面测试,需要赶飞机):
class Photo < ApplicationRecord
include ImageUploader[:image]
include ActiveModel::Validations
before_create :set_name
belongs_to :employee
belongs_to :user
validate :upload_size_validation
private
def set_name
self.name = "Photo"
end
def upload_size_validation
size = 0
user.photos.each do |photo|
byebug
size = size + photo.image[:large].size.to_i
end
errors[:photo] << " cannot be uploaded as you hit the quota of 5 MB" if size > 50000
end
end
如果有更好的解决方案,请分享!