mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-26 15:31:52 +00:00
9d0cafd06b
Some checks failed
Bundler Audit / security (push) Has been cancelled
Check i18n / check-i18n (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (ruby) (push) Has been cancelled
Crowdin / Upload translations / upload-translations (push) Has been cancelled
Check formatting / lint (push) Has been cancelled
CSS Linting / lint (push) Has been cancelled
Haml Linting / lint (push) Has been cancelled
JavaScript Linting / lint (push) Has been cancelled
Ruby Linting / lint (push) Has been cancelled
JavaScript Testing / test (push) Has been cancelled
Historical data migration test / test (14-alpine) (push) Has been cancelled
Historical data migration test / test (15-alpine) (push) Has been cancelled
Ruby Testing / build (production) (push) Has been cancelled
Ruby Testing / build (test) (push) Has been cancelled
Ruby Testing / test (.ruby-version) (push) Has been cancelled
Ruby Testing / test (3.1) (push) Has been cancelled
Ruby Testing / test (3.2) (push) Has been cancelled
Ruby Testing / Libvips tests (.ruby-version) (push) Has been cancelled
Ruby Testing / Libvips tests (3.1) (push) Has been cancelled
Ruby Testing / Libvips tests (3.2) (push) Has been cancelled
Ruby Testing / End to End testing (.ruby-version) (push) Has been cancelled
Ruby Testing / End to End testing (3.1) (push) Has been cancelled
Ruby Testing / End to End testing (3.2) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.10.2) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (3.1, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Has been cancelled
Ruby Testing / Elastic Search integration testing (3.2, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Has been cancelled
64 lines
1.6 KiB
Ruby
64 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Paperclip
|
|
class ResponseWithLimitAdapter < AbstractAdapter
|
|
def self.register
|
|
Paperclip.io_adapters.register self do |target|
|
|
target.is_a?(ResponseWithLimit)
|
|
end
|
|
end
|
|
|
|
def initialize(target, options = {})
|
|
super
|
|
cache_current_values
|
|
end
|
|
|
|
private
|
|
|
|
def cache_current_values
|
|
@target.response.require_limit_not_exceeded!(@target.limit)
|
|
|
|
@original_filename = truncated_filename
|
|
@tempfile = copy_to_tempfile(@target)
|
|
@content_type = ContentTypeDetector.new(@tempfile.path).detect
|
|
@size = File.size(@tempfile)
|
|
end
|
|
|
|
def copy_to_tempfile(source)
|
|
bytes_read = 0
|
|
|
|
source.response.body.each do |chunk|
|
|
bytes_read += chunk.bytesize
|
|
raise Mastodon::LengthValidationError, "Body size exceeds limit of #{source.limit}" if bytes_read > source.limit
|
|
|
|
destination.write(chunk)
|
|
chunk.clear
|
|
end
|
|
|
|
destination.rewind
|
|
destination
|
|
rescue
|
|
destination.close(true)
|
|
raise
|
|
ensure
|
|
source.response.connection.close
|
|
end
|
|
|
|
def truncated_filename
|
|
filename = filename_from_content_disposition.presence || filename_from_path.presence || 'data'
|
|
extension = File.extname(filename)
|
|
basename = File.basename(filename, extension)
|
|
[basename[...20], extension[..4]].compact_blank.join
|
|
end
|
|
|
|
def filename_from_content_disposition
|
|
disposition = @target.response.headers['content-disposition']
|
|
disposition&.match(/filename="([^"]*)"/)&.captures&.first
|
|
end
|
|
|
|
def filename_from_path
|
|
@target.response.uri.path.split('/').last
|
|
end
|
|
end
|
|
end
|