From 9cd8476453650b19a3bddbf172d3a7232e0979b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 15 Apr 2014 17:15:47 +0200 Subject: [PATCH] REFACTOR: use an options hash instead of multiple nil-able parameters --- app/controllers/uploads_controller.rb | 2 +- app/jobs/regular/pull_hotlinked_images.rb | 2 +- app/models/upload.rb | 9 ++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index ec0c2783eb7..b94ec93ecae 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -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) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index 39ce1c79435..1e179af299b 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -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}") diff --git a/app/models/upload.rb b/app/models/upload.rb index e91fdd9ac30..fc630df618a 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -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