2020-05-21 05:08:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-11-30 22:09:24 -05:00
|
|
|
module WorkhorseAuthorization
|
2020-05-21 05:08:04 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include WorkhorseRequest
|
|
|
|
|
|
|
|
included do
|
|
|
|
skip_before_action :verify_authenticity_token, only: %i[authorize]
|
|
|
|
before_action :verify_workhorse_api!, only: %i[authorize]
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize
|
|
|
|
set_workhorse_internal_api_content_type
|
|
|
|
|
2020-11-30 22:09:24 -05:00
|
|
|
authorized = uploader_class.workhorse_authorize(
|
2020-05-21 05:08:04 -04:00
|
|
|
has_length: false,
|
2020-12-07 19:09:45 -05:00
|
|
|
maximum_size: maximum_size.to_i)
|
2020-05-21 05:08:04 -04:00
|
|
|
|
|
|
|
render json: authorized
|
|
|
|
rescue SocketError
|
|
|
|
render json: _("Error uploading file"), status: :internal_server_error
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def file_is_valid?(file)
|
|
|
|
return false unless file.is_a?(::UploadedFile)
|
|
|
|
|
2021-11-02 02:10:00 -04:00
|
|
|
file_extension_allowlist.include?(File.extname(file.original_filename).downcase.delete('.'))
|
2020-11-30 22:09:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def uploader_class
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2020-12-07 19:09:45 -05:00
|
|
|
def maximum_size
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2021-11-02 02:10:00 -04:00
|
|
|
def file_extension_allowlist
|
2021-10-19 08:12:07 -04:00
|
|
|
ImportExportUploader::EXTENSION_ALLOWLIST
|
2020-05-21 05:08:04 -04:00
|
|
|
end
|
|
|
|
end
|