FIX: API can provide a URL to create an upload
This commit is contained in:
parent
c91634c09a
commit
bcd98c8f0f
|
@ -7,16 +7,18 @@ class UploadsController < ApplicationController
|
|||
file = params[:file] || params[:files].first
|
||||
url = params[:url]
|
||||
|
||||
# TODO: support for API providing a URL (cf. AvatarUploadService)
|
||||
|
||||
Scheduler::Defer.later("Create Upload") do
|
||||
upload = Upload.create_for(
|
||||
current_user.id,
|
||||
file.tempfile,
|
||||
file.original_filename,
|
||||
file.tempfile.size,
|
||||
content_type: file.content_type
|
||||
)
|
||||
# API can provide a URL
|
||||
if file.nil? && url.present? && is_api?
|
||||
tempfile = FileHelper.download(url, SiteSetting.max_image_size_kb.kilobytes, "discourse-upload-#{type}") rescue nil
|
||||
filename = File.basename(URI.parse(file).path)
|
||||
else
|
||||
tempfile = file.tempfile
|
||||
filename = file.original_filename
|
||||
content_type = file.content_type
|
||||
end
|
||||
|
||||
upload = Upload.create_for(current_user.id, tempfile, filename, tempfile.size, content_type: content_type)
|
||||
|
||||
if upload.errors.empty? && current_user.admin?
|
||||
retain_hours = params[:retain_hours].to_i
|
||||
|
@ -26,6 +28,8 @@ class UploadsController < ApplicationController
|
|||
data = upload.errors.empty? ? upload : { errors: upload.errors.values.flatten }
|
||||
|
||||
MessageBus.publish("/uploads/#{type}", data.as_json, user_ids: [current_user.id])
|
||||
|
||||
tempfile.try(:close!) rescue nil
|
||||
end
|
||||
|
||||
# HACK FOR IE9 to prevent the "download dialog"
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
require_dependency "file_helper"
|
||||
|
||||
class AvatarUploadService
|
||||
|
||||
attr_accessor :source
|
||||
attr_reader :filesize, :filename, :file
|
||||
|
||||
def initialize(file, source)
|
||||
@source = source
|
||||
@file, @filename, @filesize = construct(file)
|
||||
end
|
||||
|
||||
def construct(file)
|
||||
case source
|
||||
when :url
|
||||
tmp = FileHelper.download(file, SiteSetting.max_image_size_kb.kilobytes, "discourse-avatar")
|
||||
[tmp, File.basename(URI.parse(file).path), tmp.size]
|
||||
when :image
|
||||
[file.tempfile, file.original_filename, file.tempfile.size]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,58 +0,0 @@
|
|||
require "spec_helper"
|
||||
require "avatar_upload_service"
|
||||
|
||||
describe AvatarUploadService do
|
||||
|
||||
let(:logo) { file_from_fixtures("logo.png") }
|
||||
|
||||
let(:file) do
|
||||
ActionDispatch::Http::UploadedFile.new({ filename: 'logo.png', tempfile: logo })
|
||||
end
|
||||
|
||||
let(:url) { "http://cdn.discourse.org/assets/logo.png" }
|
||||
|
||||
describe "#construct" do
|
||||
context "when avatar is in the form of a file upload" do
|
||||
let(:avatar_file) { AvatarUploadService.new(file, :image) }
|
||||
|
||||
it "should have a filesize" do
|
||||
expect(avatar_file.filesize).to be > 0
|
||||
end
|
||||
|
||||
it "should have a filename" do
|
||||
expect(avatar_file.filename).to eq("logo.png")
|
||||
end
|
||||
|
||||
it "should have a file" do
|
||||
expect(avatar_file.file).to eq(file.tempfile)
|
||||
end
|
||||
|
||||
it "should have a source as 'image'" do
|
||||
expect(avatar_file.source).to eq(:image)
|
||||
end
|
||||
end
|
||||
|
||||
context "when file is in the form of a URL" do
|
||||
let(:avatar_file) { AvatarUploadService.new(url, :url) }
|
||||
|
||||
before { FileHelper.stubs(:download).returns(logo) }
|
||||
|
||||
it "should have a filesize" do
|
||||
expect(avatar_file.filesize).to be > 0
|
||||
end
|
||||
|
||||
it "should have a filename" do
|
||||
expect(avatar_file.filename).to eq("logo.png")
|
||||
end
|
||||
|
||||
it "should have a file" do
|
||||
expect(avatar_file.file).to eq(logo)
|
||||
end
|
||||
|
||||
it "should have a source as 'url'" do
|
||||
expect(avatar_file.source).to eq(:url)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue