限制每个用户上传到AWS S3的总大小(MB)(使用Shrine)(Limit total size (MBs) of uploads per user to AWS S3 (using Shrine

编程入门 行业动态 更新时间:2024-10-28 10:27:16
限制每个用户上传到AWS S3的总大小(MB)(使用Shrine)(Limit total size (MBs) of uploads per user to AWS S3 (using Shrine))

我正在开发一个Rails应用程序,允许用户通过神社将他们的图像上传到AWS S3。 到目前为止设置工作得很好 - 但是,我想知道是否有任何直接提取每个用户的上传总大小。

例如,我确实希望免费用户只允许上传最大500MB,而会员则说4GB。

在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

I am working on a Rails application that allows users to upload their images to AWS S3 via shrine. The setup so far works just fine - however, I was wondering if there is any straight forward of extracting the total size of uploads per user.

For example, I do want free users only to be allowed uploading max 500MB, while members get say 4GB.

Haven't found anything useable on Github or Google, in general, perhaps someone can share their knowledge. Cheers !

Current code:

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

最满意答案

如果要在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版本的所有图像大小,如您的示例所示。

最后,我们将当前附件大小添加到总大小,如果超过限制,我们将添加验证错误。

If you want to add the validation error message on the image column of the Photo model, you do this in Shrine validations since you can access the Photo record there.

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

First we're filtering only attachments which are present and uploaded to permanent storage (e.g. if you use backgrounding there will be a period when only the cached file is attached).

Then we're summing up all the image sizes of large versions, as in your example.

Finally, we're adding the current attachment size to the total size, and if it exceeds the limit we're adding a validation error.

更多推荐

本文发布于:2023-07-18 19:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   用户   MB   Shrine   AWS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!