REFACTOR: use an options hash instead of multiple nil-able parameters
This commit is contained in:
parent
8b79debde2
commit
9cd8476453
|
@ -6,7 +6,7 @@ class UploadsController < ApplicationController
|
||||||
file = params[:file] || params[:files].first
|
file = params[:file] || params[:files].first
|
||||||
|
|
||||||
filesize = File.size(file.tempfile)
|
filesize = File.size(file.tempfile)
|
||||||
upload = Upload.create_for(current_user.id, file.tempfile, file.original_filename, filesize, file.content_type)
|
upload = Upload.create_for(current_user.id, file.tempfile, file.original_filename, filesize, { content_type: file.content_type })
|
||||||
|
|
||||||
if upload.errors.empty?
|
if upload.errors.empty?
|
||||||
render_serialized(upload, UploadSerializer, root: false)
|
render_serialized(upload, UploadSerializer, root: false)
|
||||||
|
|
|
@ -34,7 +34,7 @@ module Jobs
|
||||||
hotlinked = FileHelper.download(src, @max_size, "discourse-hotlinked") rescue Discourse::InvalidParameters
|
hotlinked = FileHelper.download(src, @max_size, "discourse-hotlinked") rescue Discourse::InvalidParameters
|
||||||
if hotlinked.try(:size) <= @max_size
|
if hotlinked.try(:size) <= @max_size
|
||||||
filename = File.basename(URI.parse(src).path)
|
filename = File.basename(URI.parse(src).path)
|
||||||
upload = Upload.create_for(post.user_id, hotlinked, filename, hotlinked.size, nil, src)
|
upload = Upload.create_for(post.user_id, hotlinked, filename, hotlinked.size, { origin: src })
|
||||||
downloaded_urls[src] = upload.url
|
downloaded_urls[src] = upload.url
|
||||||
else
|
else
|
||||||
Rails.logger.error("Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}")
|
Rails.logger.error("Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}")
|
||||||
|
|
|
@ -46,7 +46,10 @@ class Upload < ActiveRecord::Base
|
||||||
File.extname(original_filename)
|
File.extname(original_filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create_for(user_id, file, filename, filesize, content_type = nil, origin = nil)
|
# options
|
||||||
|
# - content_type
|
||||||
|
# - origin
|
||||||
|
def self.create_for(user_id, file, filename, filesize, options = {})
|
||||||
# compute the sha
|
# compute the sha
|
||||||
sha1 = Digest::SHA1.file(file).hexdigest
|
sha1 = Digest::SHA1.file(file).hexdigest
|
||||||
# check if the file has already been uploaded
|
# check if the file has already been uploaded
|
||||||
|
@ -67,7 +70,7 @@ class Upload < ActiveRecord::Base
|
||||||
url: ""
|
url: ""
|
||||||
)
|
)
|
||||||
# trim the origin if any
|
# trim the origin if any
|
||||||
upload.origin = origin[0...1000] if origin
|
upload.origin = options[:origin][0...1000] if options[:origin]
|
||||||
|
|
||||||
# deal with width & height for images
|
# deal with width & height for images
|
||||||
if FileHelper.is_image?(filename)
|
if FileHelper.is_image?(filename)
|
||||||
|
@ -93,7 +96,7 @@ class Upload < ActiveRecord::Base
|
||||||
return upload unless upload.save
|
return upload unless upload.save
|
||||||
|
|
||||||
# store the file and update its url
|
# store the file and update its url
|
||||||
url = Discourse.store.store_upload(file, upload, content_type)
|
url = Discourse.store.store_upload(file, upload, options[:content_type])
|
||||||
if url.present?
|
if url.present?
|
||||||
upload.url = url
|
upload.url = url
|
||||||
upload.save
|
upload.save
|
||||||
|
|
Loading…
Reference in New Issue