Module: X::MediaUploader
Constant Summary collapse
- MAX_RETRIES =
3- BYTES_PER_MB =
1_048_576- DEFAULT_MIME_TYPE =
"application/octet-stream".freeze
- MIME_TYPES =
%w[image/gif image/jpeg video/mp4 image/png application/x-subrip image/webp].freeze
- MIME_TYPE_MAP =
{"gif" => GIF_MIME_TYPE, "jpg" => JPEG_MIME_TYPE, "jpeg" => JPEG_MIME_TYPE, "mp4" => MP4_MIME_TYPE, "png" => PNG_MIME_TYPE, "srt" => SUBRIP_MIME_TYPE, "webp" => WEBP_MIME_TYPE}.freeze
- PROCESSING_INFO_STATES =
%w[failed succeeded].freeze
Instance Method Summary collapse
- #await_processing(client:, media:) ⇒ Object
- #await_processing!(client:, media:) ⇒ Object
- #chunked_upload(client:, file_path:, media_category:, media_type: infer_media_type(file_path, media_category), boundary: SecureRandom.hex, chunk_size_mb: 1) ⇒ Object
- #upload(client:, file_path:, media_category:, boundary: SecureRandom.hex) ⇒ Object
- #upload_binary(client:, content:, media_category:, boundary: SecureRandom.hex) ⇒ Object
Instance Method Details
#await_processing(client:, media:) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/x/media_uploader.rb', line 42 def await_processing(client:, media:) loop do status = client.get("media/upload?command=STATUS&media_id=#{media["id"]}")&.fetch("data") return status if status.nil? || !status["processing_info"] || PROCESSING_INFO_STATES.include?(status["processing_info"]["state"]) sleep status["processing_info"]["check_after_secs"].to_i end end |
#await_processing!(client:, media:) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/x/media_uploader.rb', line 51 def await_processing!(client:, media:) status = await_processing(client:, media:) raise "Media processing failed" if status&.dig("processing_info", "state") == "failed" status end |
#chunked_upload(client:, file_path:, media_category:, media_type: infer_media_type(file_path, media_category), boundary: SecureRandom.hex, chunk_size_mb: 1) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/x/media_uploader.rb', line 32 def chunked_upload(client:, file_path:, media_category:, media_type: infer_media_type(file_path, media_category), boundary: SecureRandom.hex, chunk_size_mb: 1) MediaUploadValidator.validate_file_path!(file_path:) MediaUploadValidator.validate_media_category!(media_category:) media = init(client:, file_path:, media_type:, media_category:) chunk_size = chunk_size_mb * BYTES_PER_MB append(client:, file_paths: split(file_path, chunk_size), media:, boundary:) client.post("media/upload/#{media["id"]}/finalize")&.fetch("data") end |
#upload(client:, file_path:, media_category:, boundary: SecureRandom.hex) ⇒ Object
20 21 22 23 |
# File 'lib/x/media_uploader.rb', line 20 def upload(client:, file_path:, media_category:, boundary: SecureRandom.hex) MediaUploadValidator.validate_file_path!(file_path:) upload_binary(client:, content: File.binread(file_path), media_category:, boundary:) end |
#upload_binary(client:, content:, media_category:, boundary: SecureRandom.hex) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/x/media_uploader.rb', line 25 def upload_binary(client:, content:, media_category:, boundary: SecureRandom.hex) MediaUploadValidator.validate_media_category!(media_category:) upload_body = construct_upload_body(content:, media_category:, boundary:) headers = {"Content-Type" => "multipart/form-data; boundary=#{boundary}"} client.post("media/upload", upload_body, headers:)&.fetch("data") end |