mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@ -5,13 +5,18 @@ require "mini_mime"
|
||||
class UploadsController < ApplicationController
|
||||
include ExternalUploadHelpers
|
||||
|
||||
requires_login except: [:show, :show_short, :_show_secure_deprecated, :show_secure]
|
||||
requires_login except: %i[show show_short _show_secure_deprecated show_secure]
|
||||
|
||||
skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required, only: [:show, :show_short, :_show_secure_deprecated, :show_secure]
|
||||
skip_before_action :preload_json,
|
||||
:check_xhr,
|
||||
:redirect_to_login_if_required,
|
||||
only: %i[show show_short _show_secure_deprecated show_secure]
|
||||
protect_from_forgery except: :show
|
||||
|
||||
before_action :is_asset_path, :apply_cdn_headers, only: [:show, :show_short, :_show_secure_deprecated, :show_secure]
|
||||
before_action :external_store_check, only: [:_show_secure_deprecated, :show_secure]
|
||||
before_action :is_asset_path,
|
||||
:apply_cdn_headers,
|
||||
only: %i[show show_short _show_secure_deprecated show_secure]
|
||||
before_action :external_store_check, only: %i[_show_secure_deprecated show_secure]
|
||||
|
||||
SECURE_REDIRECT_GRACE_SECONDS = 5
|
||||
|
||||
@ -20,18 +25,21 @@ class UploadsController < ApplicationController
|
||||
me = current_user
|
||||
|
||||
params.permit(:type, :upload_type)
|
||||
if params[:type].blank? && params[:upload_type].blank?
|
||||
raise Discourse::InvalidParameters
|
||||
end
|
||||
raise Discourse::InvalidParameters if params[:type].blank? && params[:upload_type].blank?
|
||||
# 50 characters ought to be enough for the upload type
|
||||
type = (params[:upload_type].presence || params[:type].presence).parameterize(separator: "_")[0..50]
|
||||
type =
|
||||
(params[:upload_type].presence || params[:type].presence).parameterize(separator: "_")[0..50]
|
||||
|
||||
if type == "avatar" && !me.admin? && (SiteSetting.discourse_connect_overrides_avatar || !TrustLevelAndStaffAndDisabledSetting.matches?(SiteSetting.allow_uploaded_avatars, me))
|
||||
if type == "avatar" && !me.admin? &&
|
||||
(
|
||||
SiteSetting.discourse_connect_overrides_avatar ||
|
||||
!TrustLevelAndStaffAndDisabledSetting.matches?(SiteSetting.allow_uploaded_avatars, me)
|
||||
)
|
||||
return render json: failed_json, status: 422
|
||||
end
|
||||
|
||||
url = params[:url]
|
||||
file = params[:file] || params[:files]&.first
|
||||
url = params[:url]
|
||||
file = params[:file] || params[:files]&.first
|
||||
pasted = params[:pasted] == "true"
|
||||
for_private_message = params[:for_private_message] == "true"
|
||||
for_site_setting = params[:for_site_setting] == "true"
|
||||
@ -42,17 +50,18 @@ class UploadsController < ApplicationController
|
||||
# longer term we may change this
|
||||
hijack do
|
||||
begin
|
||||
info = UploadsController.create_upload(
|
||||
current_user: me,
|
||||
file: file,
|
||||
url: url,
|
||||
type: type,
|
||||
for_private_message: for_private_message,
|
||||
for_site_setting: for_site_setting,
|
||||
pasted: pasted,
|
||||
is_api: is_api,
|
||||
retain_hours: retain_hours
|
||||
)
|
||||
info =
|
||||
UploadsController.create_upload(
|
||||
current_user: me,
|
||||
file: file,
|
||||
url: url,
|
||||
type: type,
|
||||
for_private_message: for_private_message,
|
||||
for_site_setting: for_site_setting,
|
||||
pasted: pasted,
|
||||
is_api: is_api,
|
||||
retain_hours: retain_hours,
|
||||
)
|
||||
rescue => e
|
||||
render json: failed_json.merge(message: e.message&.split("\n")&.first), status: 422
|
||||
else
|
||||
@ -66,13 +75,11 @@ class UploadsController < ApplicationController
|
||||
uploads = []
|
||||
|
||||
if (params[:short_urls] && params[:short_urls].length > 0)
|
||||
PrettyText::Helpers.lookup_upload_urls(params[:short_urls]).each do |short_url, paths|
|
||||
uploads << {
|
||||
short_url: short_url,
|
||||
url: paths[:url],
|
||||
short_path: paths[:short_path]
|
||||
}
|
||||
end
|
||||
PrettyText::Helpers
|
||||
.lookup_upload_urls(params[:short_urls])
|
||||
.each do |short_url, paths|
|
||||
uploads << { short_url: short_url, url: paths[:url], short_path: paths[:short_path] }
|
||||
end
|
||||
end
|
||||
|
||||
render json: uploads.to_json
|
||||
@ -87,7 +94,9 @@ class UploadsController < ApplicationController
|
||||
RailsMultisite::ConnectionManagement.with_connection(params[:site]) do |db|
|
||||
return render_404 if SiteSetting.prevent_anons_from_downloading_files && current_user.nil?
|
||||
|
||||
if upload = Upload.find_by(sha1: params[:sha]) || Upload.find_by(id: params[:id], url: request.env["PATH_INFO"])
|
||||
if upload =
|
||||
Upload.find_by(sha1: params[:sha]) ||
|
||||
Upload.find_by(id: params[:id], url: request.env["PATH_INFO"])
|
||||
unless Discourse.store.internal?
|
||||
local_store = FileStore::LocalStore.new
|
||||
return render_404 unless local_store.has_been_uploaded?(upload.url)
|
||||
@ -104,21 +113,18 @@ class UploadsController < ApplicationController
|
||||
# do not serve uploads requested via XHR to prevent XSS
|
||||
return xhr_not_allowed if request.xhr?
|
||||
|
||||
if SiteSetting.prevent_anons_from_downloading_files && current_user.nil?
|
||||
return render_404
|
||||
end
|
||||
return render_404 if SiteSetting.prevent_anons_from_downloading_files && current_user.nil?
|
||||
|
||||
sha1 = Upload.sha1_from_base62_encoded(params[:base62])
|
||||
|
||||
if upload = Upload.find_by(sha1: sha1)
|
||||
if upload.secure? && SiteSetting.secure_uploads?
|
||||
return handle_secure_upload_request(upload)
|
||||
end
|
||||
return handle_secure_upload_request(upload) if upload.secure? && SiteSetting.secure_uploads?
|
||||
|
||||
if Discourse.store.internal?
|
||||
send_file_local_upload(upload)
|
||||
else
|
||||
redirect_to Discourse.store.url_for(upload, force_download: force_download?), allow_other_host: true
|
||||
redirect_to Discourse.store.url_for(upload, force_download: force_download?),
|
||||
allow_other_host: true
|
||||
end
|
||||
else
|
||||
render_404
|
||||
@ -156,7 +162,8 @@ class UploadsController < ApplicationController
|
||||
# private, so we don't want to go to the CDN url just yet otherwise we
|
||||
# will get a 403. if the upload is not secure we assume the ACL is public
|
||||
signed_secure_url = Discourse.store.signed_url_for_path(path_with_ext)
|
||||
redirect_to upload.secure? ? signed_secure_url : Discourse.store.cdn_url(upload.url), allow_other_host: true
|
||||
redirect_to upload.secure? ? signed_secure_url : Discourse.store.cdn_url(upload.url),
|
||||
allow_other_host: true
|
||||
end
|
||||
|
||||
def handle_secure_upload_request(upload, path_with_ext = nil)
|
||||
@ -167,20 +174,25 @@ class UploadsController < ApplicationController
|
||||
end
|
||||
|
||||
# defaults to public: false, so only cached by the client browser
|
||||
cache_seconds = SiteSetting.s3_presigned_get_url_expires_after_seconds - SECURE_REDIRECT_GRACE_SECONDS
|
||||
cache_seconds =
|
||||
SiteSetting.s3_presigned_get_url_expires_after_seconds - SECURE_REDIRECT_GRACE_SECONDS
|
||||
expires_in cache_seconds.seconds
|
||||
|
||||
# url_for figures out the full URL, handling multisite DBs,
|
||||
# and will return a presigned URL for the upload
|
||||
if path_with_ext.blank?
|
||||
return redirect_to Discourse.store.url_for(upload, force_download: force_download?), allow_other_host: true
|
||||
return(
|
||||
redirect_to Discourse.store.url_for(upload, force_download: force_download?),
|
||||
allow_other_host: true
|
||||
)
|
||||
end
|
||||
|
||||
redirect_to Discourse.store.signed_url_for_path(
|
||||
path_with_ext,
|
||||
expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,
|
||||
force_download: force_download?
|
||||
), allow_other_host: true
|
||||
path_with_ext,
|
||||
expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,
|
||||
force_download: force_download?,
|
||||
),
|
||||
allow_other_host: true
|
||||
end
|
||||
|
||||
def metadata
|
||||
@ -189,11 +201,11 @@ class UploadsController < ApplicationController
|
||||
raise Discourse::NotFound unless upload
|
||||
|
||||
render json: {
|
||||
original_filename: upload.original_filename,
|
||||
width: upload.width,
|
||||
height: upload.height,
|
||||
human_filesize: upload.human_filesize
|
||||
}
|
||||
original_filename: upload.original_filename,
|
||||
width: upload.width,
|
||||
height: upload.height,
|
||||
human_filesize: upload.human_filesize,
|
||||
}
|
||||
end
|
||||
|
||||
protected
|
||||
@ -207,17 +219,18 @@ class UploadsController < ApplicationController
|
||||
end
|
||||
|
||||
def validate_file_size(file_name:, file_size:)
|
||||
if file_size.zero?
|
||||
raise ExternalUploadValidationError.new(I18n.t("upload.size_zero_failure"))
|
||||
end
|
||||
raise ExternalUploadValidationError.new(I18n.t("upload.size_zero_failure")) if file_size.zero?
|
||||
|
||||
if file_size_too_big?(file_name, file_size)
|
||||
raise ExternalUploadValidationError.new(
|
||||
I18n.t(
|
||||
"upload.attachments.too_large_humanized",
|
||||
max_size: ActiveSupport::NumberHelper.number_to_human_size(SiteSetting.max_attachment_size_kb.kilobytes)
|
||||
)
|
||||
)
|
||||
I18n.t(
|
||||
"upload.attachments.too_large_humanized",
|
||||
max_size:
|
||||
ActiveSupport::NumberHelper.number_to_human_size(
|
||||
SiteSetting.max_attachment_size_kb.kilobytes,
|
||||
),
|
||||
),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@ -236,25 +249,34 @@ class UploadsController < ApplicationController
|
||||
serialized ||= (data || {}).as_json
|
||||
end
|
||||
|
||||
def self.create_upload(current_user:,
|
||||
file:,
|
||||
url:,
|
||||
type:,
|
||||
for_private_message:,
|
||||
for_site_setting:,
|
||||
pasted:,
|
||||
is_api:,
|
||||
retain_hours:)
|
||||
|
||||
def self.create_upload(
|
||||
current_user:,
|
||||
file:,
|
||||
url:,
|
||||
type:,
|
||||
for_private_message:,
|
||||
for_site_setting:,
|
||||
pasted:,
|
||||
is_api:,
|
||||
retain_hours:
|
||||
)
|
||||
if file.nil?
|
||||
if url.present? && is_api
|
||||
maximum_upload_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
||||
tempfile = FileHelper.download(
|
||||
url,
|
||||
follow_redirect: true,
|
||||
max_file_size: maximum_upload_size,
|
||||
tmp_file_name: "discourse-upload-#{type}"
|
||||
) rescue nil
|
||||
maximum_upload_size = [
|
||||
SiteSetting.max_image_size_kb,
|
||||
SiteSetting.max_attachment_size_kb,
|
||||
].max.kilobytes
|
||||
tempfile =
|
||||
begin
|
||||
FileHelper.download(
|
||||
url,
|
||||
follow_redirect: true,
|
||||
max_file_size: maximum_upload_size,
|
||||
tmp_file_name: "discourse-upload-#{type}",
|
||||
)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
filename = File.basename(URI.parse(url).path)
|
||||
end
|
||||
else
|
||||
@ -288,13 +310,14 @@ class UploadsController < ApplicationController
|
||||
# as they may be further reduced in size by UploadCreator (at this point
|
||||
# they may have already been reduced in size by preprocessors)
|
||||
def file_size_too_big?(file_name, file_size)
|
||||
!FileHelper.is_supported_image?(file_name) && file_size >= SiteSetting.max_attachment_size_kb.kilobytes
|
||||
!FileHelper.is_supported_image?(file_name) &&
|
||||
file_size >= SiteSetting.max_attachment_size_kb.kilobytes
|
||||
end
|
||||
|
||||
def send_file_local_upload(upload)
|
||||
opts = {
|
||||
filename: upload.original_filename,
|
||||
content_type: MiniMime.lookup_by_filename(upload.original_filename)&.content_type
|
||||
content_type: MiniMime.lookup_by_filename(upload.original_filename)&.content_type,
|
||||
}
|
||||
|
||||
if !FileHelper.is_inline_image?(upload.original_filename)
|
||||
@ -313,7 +336,11 @@ class UploadsController < ApplicationController
|
||||
begin
|
||||
yield
|
||||
rescue Aws::S3::Errors::ServiceError => err
|
||||
message = debug_upload_error(err, I18n.t("upload.create_multipart_failure", additional_detail: err.message))
|
||||
message =
|
||||
debug_upload_error(
|
||||
err,
|
||||
I18n.t("upload.create_multipart_failure", additional_detail: err.message),
|
||||
)
|
||||
raise ExternalUploadHelpers::ExternalUploadValidationError.new(message)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user