Class: Dscf::Core::FileStorage::Uploader
- Inherits:
-
Object
- Object
- Dscf::Core::FileStorage::Uploader
- Defined in:
- app/services/dscf/core/file_storage/uploader.rb
Overview
Handles file uploads with validation
Constant Summary collapse
- DEFAULT_MAX_SIZE =
5.megabytes
- DEFAULT_ALLOWED_TYPES =
%w[ application/pdf image/png image/jpeg image/webp ].freeze
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Uploader
constructor
A new instance of Uploader.
-
#success? ⇒ Boolean
Check if last upload was successful.
-
#upload(file, **options) ⇒ Hash?
Upload a single file.
-
#upload_many(files, **options) ⇒ Array<Hash>
Upload multiple files.
Constructor Details
#initialize(options = {}) ⇒ Uploader
Returns a new instance of Uploader.
18 19 20 21 22 23 24 |
# File 'app/services/dscf/core/file_storage/uploader.rb', line 18 def initialize( = {}) @max_size = [:max_size] || DEFAULT_MAX_SIZE @allowed_types = [:allowed_types] || DEFAULT_ALLOWED_TYPES @record_class = [:record_class] @client = [:client] || Client.new(record_class: @record_class) @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'app/services/dscf/core/file_storage/uploader.rb', line 16 def errors @errors end |
Instance Method Details
#success? ⇒ Boolean
Check if last upload was successful
64 65 66 |
# File 'app/services/dscf/core/file_storage/uploader.rb', line 64 def success? @errors.empty? end |
#upload(file, **options) ⇒ Hash?
Upload a single file
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/services/dscf/core/file_storage/uploader.rb', line 30 def upload(file, **) @errors = [] return nil unless validate_file(file) result = @client.upload(file, filename: [:filename]) { file_key: result[:file_key], filename: result[:filename], content_type: result[:content_type], size: result[:size], metadata: [:metadata] || {} } rescue Client::UploadError => e @errors << e. nil rescue StandardError => e Rails.logger.error("FileStorage::Uploader error: #{e.}") @errors << "An unexpected error occurred during upload" nil end |
#upload_many(files, **options) ⇒ Array<Hash>
Upload multiple files
57 58 59 60 61 |
# File 'app/services/dscf/core/file_storage/uploader.rb', line 57 def upload_many(files, **) Array(files).filter_map do |file| upload(file, **) end end |