mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: ensure a file is present when creating an upload
This commit is contained in:
@@ -50,18 +50,23 @@ class UploadsController < ApplicationController
|
|||||||
|
|
||||||
def create_upload(type, file, url)
|
def create_upload(type, file, url)
|
||||||
begin
|
begin
|
||||||
# API can provide a URL
|
# ensure we have a file
|
||||||
if file.nil? && url.present? && is_api?
|
if file.nil?
|
||||||
tempfile = FileHelper.download(url, 10.megabytes, "discourse-upload-#{type}") rescue nil
|
# API can provide a URL
|
||||||
filename = File.basename(URI.parse(url).path)
|
if url.present? && is_api?
|
||||||
|
tempfile = FileHelper.download(url, 10.megabytes, "discourse-upload-#{type}") rescue nil
|
||||||
|
filename = File.basename(URI.parse(url).path)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
tempfile = file.tempfile
|
tempfile = file.tempfile
|
||||||
filename = file.original_filename
|
filename = file.original_filename
|
||||||
content_type = file.content_type
|
content_type = file.content_type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return { errors: I18n.t("upload.file_missing") } if tempfile.nil?
|
||||||
|
|
||||||
# allow users to upload large images that will be automatically reduced to allowed size
|
# allow users to upload large images that will be automatically reduced to allowed size
|
||||||
if tempfile && File.size(tempfile.path) > 0 && SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename)
|
if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) && File.size(tempfile.path) > 0
|
||||||
attempt = 5
|
attempt = 5
|
||||||
while attempt > 0 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes
|
while attempt > 0 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes
|
||||||
OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails)
|
OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails)
|
||||||
|
|||||||
@@ -2063,6 +2063,7 @@ en:
|
|||||||
unauthorized: "Sorry, the file you are trying to upload is not authorized (authorized extensions: %{authorized_extensions})."
|
unauthorized: "Sorry, the file you are trying to upload is not authorized (authorized extensions: %{authorized_extensions})."
|
||||||
pasted_image_filename: "Pasted image"
|
pasted_image_filename: "Pasted image"
|
||||||
store_failure: "Failed to store upload #%{upload_id} for user #%{user_id}."
|
store_failure: "Failed to store upload #%{upload_id} for user #%{user_id}."
|
||||||
|
file_missing: "Sorry, you must provide a file to upload."
|
||||||
attachments:
|
attachments:
|
||||||
too_large: "Sorry, the file you are trying to upload is too big (maximum size is %{max_size_kb}KB)."
|
too_large: "Sorry, the file you are trying to upload is too big (maximum size is %{max_size_kb}KB)."
|
||||||
images:
|
images:
|
||||||
|
|||||||
@@ -82,6 +82,17 @@ describe UploadsController do
|
|||||||
expect(Upload.find(id).retain_hours).to eq(100)
|
expect(Upload.find(id).retain_hours).to eq(100)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'requires a file' do
|
||||||
|
Jobs.expects(:enqueue).never
|
||||||
|
|
||||||
|
message = MessageBus.track_publish do
|
||||||
|
xhr :post, :create, type: "composer"
|
||||||
|
end.first
|
||||||
|
|
||||||
|
expect(response.status).to eq 200
|
||||||
|
expect(message.data["errors"]).to eq(I18n.t("upload.file_missing"))
|
||||||
|
end
|
||||||
|
|
||||||
it 'properly returns errors' do
|
it 'properly returns errors' do
|
||||||
SiteSetting.stubs(:max_attachment_size_kb).returns(1)
|
SiteSetting.stubs(:max_attachment_size_kb).returns(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user