REFACTOR: use an options hash instead of multiple nil-able parameters

This commit is contained in:
Régis Hanol 2014-04-15 17:15:47 +02:00
parent 8b79debde2
commit 9cd8476453
3 changed files with 8 additions and 5 deletions

View File

@ -6,7 +6,7 @@ class UploadsController < ApplicationController
file = params[:file] || params[:files].first
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?
render_serialized(upload, UploadSerializer, root: false)

View File

@ -34,7 +34,7 @@ module Jobs
hotlinked = FileHelper.download(src, @max_size, "discourse-hotlinked") rescue Discourse::InvalidParameters
if hotlinked.try(:size) <= @max_size
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
else
Rails.logger.error("Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}")

View File

@ -46,7 +46,10 @@ class Upload < ActiveRecord::Base
File.extname(original_filename)
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
sha1 = Digest::SHA1.file(file).hexdigest
# check if the file has already been uploaded
@ -67,7 +70,7 @@ class Upload < ActiveRecord::Base
url: ""
)
# 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
if FileHelper.is_image?(filename)
@ -93,7 +96,7 @@ class Upload < ActiveRecord::Base
return upload unless upload.save
# 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?
upload.url = url
upload.save