small upload code refactor
This commit is contained in:
parent
91b3123e14
commit
0e5c9b2590
|
@ -36,7 +36,7 @@ class CategoriesController < ApplicationController
|
|||
guardian.ensure_can_create!(Category)
|
||||
|
||||
file = params[:file] || params[:files].first
|
||||
upload = Upload.create_for(current_user.id, file.tempfile, file.original_filename, File.size(file.tempfile))
|
||||
upload = Upload.create_for(current_user.id, file.tempfile, file.original_filename, file.tempfile.size)
|
||||
if upload.errors.blank?
|
||||
render json: { url: upload.url, width: upload.width, height: upload.height }
|
||||
else
|
||||
|
|
|
@ -4,7 +4,7 @@ class UploadsController < ApplicationController
|
|||
|
||||
def create
|
||||
file = params[:file] || params[:files].first
|
||||
filesize = File.size(file.tempfile)
|
||||
filesize = file.tempfile.size
|
||||
upload = Upload.create_for(current_user.id, file.tempfile, file.original_filename, filesize, { content_type: file.content_type })
|
||||
|
||||
if upload.errors.empty? && current_user.admin?
|
||||
|
|
|
@ -13,7 +13,7 @@ class UserAvatarsController < ApplicationController
|
|||
user.create_user_avatar(user_id: user.id) unless user.user_avatar
|
||||
user.user_avatar.update_gravatar!
|
||||
|
||||
render json: {upload_id: user.user_avatar.gravatar_upload_id}
|
||||
render json: { upload_id: user.user_avatar.gravatar_upload_id }
|
||||
else
|
||||
raise Discourse::NotFound
|
||||
end
|
||||
|
|
|
@ -130,12 +130,12 @@ class DiscourseSingleSignOn < SingleSignOn
|
|||
sso_record.external_avatar_url != avatar_url)
|
||||
|
||||
begin
|
||||
tempfile = FileHelper.download(avatar_url, 1.megabyte, "sso-avatar", true)
|
||||
tempfile = FileHelper.download(avatar_url, SiteSetting.max_image_size_kb.kilobytes, "sso-avatar", true)
|
||||
|
||||
ext = FastImage.type(tempfile).to_s
|
||||
tempfile.rewind
|
||||
|
||||
upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, File.size(tempfile.path), { origin: avatar_url })
|
||||
upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, tempfile.size, { origin: avatar_url })
|
||||
user.uploaded_avatar_id = upload.id
|
||||
|
||||
unless user.user_avatar
|
||||
|
|
|
@ -15,8 +15,10 @@ class OptimizedImage < ActiveRecord::Base
|
|||
thumbnail = nil
|
||||
end
|
||||
|
||||
# return the previous thumbnail if any
|
||||
return thumbnail unless thumbnail.nil?
|
||||
|
||||
# create the thumbnail otherwise
|
||||
unless thumbnail
|
||||
external_copy = Discourse.store.download(upload) if Discourse.store.external?
|
||||
original_path = if Discourse.store.external?
|
||||
external_copy.try(:path)
|
||||
|
@ -43,7 +45,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||
thumbnail = OptimizedImage.create!(
|
||||
upload_id: upload.id,
|
||||
sha1: Digest::SHA1.file(temp_path).hexdigest,
|
||||
extension: File.extname(temp_path),
|
||||
extension: extension,
|
||||
width: width,
|
||||
height: height,
|
||||
url: "",
|
||||
|
@ -66,7 +68,6 @@ class OptimizedImage < ActiveRecord::Base
|
|||
|
||||
# make sure we remove the cached copy from external stores
|
||||
external_copy.close! if Discourse.store.external?
|
||||
end
|
||||
|
||||
thumbnail
|
||||
end
|
||||
|
|
|
@ -50,46 +50,32 @@ class Upload < ActiveRecord::Base
|
|||
# - 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
|
||||
upload = Upload.find_by(sha1: sha1)
|
||||
# delete the previously uploaded file if there's been an error
|
||||
|
||||
# do we already have that upload?
|
||||
upload = find_by(sha1: sha1)
|
||||
|
||||
# make sure the previous upload has not failed
|
||||
if upload && upload.url.blank?
|
||||
upload.destroy
|
||||
upload = nil
|
||||
end
|
||||
# create the upload
|
||||
unless upload
|
||||
# initialize a new upload
|
||||
upload = Upload.new(
|
||||
user_id: user_id,
|
||||
original_filename: filename,
|
||||
filesize: filesize,
|
||||
sha1: sha1,
|
||||
url: ""
|
||||
)
|
||||
# trim the origin if any
|
||||
|
||||
# return the previous upload if any
|
||||
return upload unless upload.nil?
|
||||
|
||||
# create the upload otherwise
|
||||
upload = Upload.new
|
||||
upload.user_id = user_id
|
||||
upload.original_filename = filename
|
||||
upload.filesize = filesize
|
||||
upload.sha1 = sha1
|
||||
upload.url = ""
|
||||
upload.origin = options[:origin][0...1000] if options[:origin]
|
||||
|
||||
# check the size of the upload
|
||||
if FileHelper.is_image?(filename)
|
||||
if SiteSetting.max_image_size_kb > 0 && filesize >= SiteSetting.max_image_size_kb.kilobytes
|
||||
upload.errors.add(:base, I18n.t("upload.images.too_large", max_size_kb: SiteSetting.max_image_size_kb))
|
||||
else
|
||||
# deal with width & height for images
|
||||
upload = Upload.resize_image(filename, file, upload)
|
||||
end
|
||||
else
|
||||
if SiteSetting.max_attachment_size_kb > 0 && filesize >= SiteSetting.max_attachment_size_kb.kilobytes
|
||||
upload.errors.add(:base, I18n.t("upload.attachments.too_large", max_size_kb: SiteSetting.max_attachment_size_kb))
|
||||
end
|
||||
end
|
||||
upload = resize_image(filename, file, upload) if FileHelper.is_image?(filename)
|
||||
|
||||
# make sure there is no error
|
||||
return upload unless upload.errors.empty?
|
||||
|
||||
# create a db record (so we can use the id)
|
||||
return upload unless upload.save
|
||||
|
||||
# store the file and update its url
|
||||
|
@ -100,7 +86,6 @@ class Upload < ActiveRecord::Base
|
|||
else
|
||||
upload.errors.add(:url, I18n.t("upload.store_failure", { upload_id: upload.id, user_id: user_id }))
|
||||
end
|
||||
end
|
||||
|
||||
# return the uploaded file
|
||||
upload
|
||||
|
|
|
@ -15,22 +15,20 @@ class UserAvatar < ActiveRecord::Base
|
|||
|
||||
self.last_gravatar_download_attempt = Time.new
|
||||
gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=500&d=404"
|
||||
tempfile = FileHelper.download(gravatar_url, 1.megabyte, "gravatar")
|
||||
tempfile = FileHelper.download(gravatar_url, SiteSetting.max_image_size_kb.kilobytes, "gravatar")
|
||||
|
||||
upload = Upload.create_for(user.id, tempfile, 'gravatar.png', File.size(tempfile.path))
|
||||
upload = Upload.create_for(user.id, tempfile, 'gravatar.png', tempfile.size, { origin: gravatar_url })
|
||||
|
||||
if gravatar_upload_id != upload.id
|
||||
gravatar_upload.try(:destroy!)
|
||||
self.gravatar_upload = upload
|
||||
save!
|
||||
else
|
||||
gravatar_upload
|
||||
end
|
||||
rescue OpenURI::HTTPError
|
||||
save!
|
||||
rescue SocketError
|
||||
# skip saving, we are not connected to the net
|
||||
Rails.logger.warn "Failed to download gravatar, socket error - user id #{ user.id }"
|
||||
Rails.logger.warn "Failed to download gravatar, socket error - user id #{user.id}"
|
||||
ensure
|
||||
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
|
||||
end
|
||||
|
|
|
@ -14,9 +14,9 @@ class AvatarUploadService
|
|||
case source
|
||||
when :url
|
||||
tmp = FileHelper.download(file, SiteSetting.max_image_size_kb.kilobytes, "discourse-avatar")
|
||||
[tmp, File.basename(URI.parse(file).path), File.size(tmp)]
|
||||
[tmp, File.basename(URI.parse(file).path), tmp.size]
|
||||
when :image
|
||||
[file.tempfile, file.original_filename, File.size(file.tempfile)]
|
||||
[file.tempfile, file.original_filename, file.tempfile.size]
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -224,7 +224,7 @@ module Email
|
|||
# read attachment
|
||||
File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded }
|
||||
# create the upload for the user
|
||||
upload = Upload.create_for(user.id, tmp, attachment.filename, File.size(tmp))
|
||||
upload = Upload.create_for(user.id, tmp, attachment.filename, tmp.size)
|
||||
if upload && upload.errors.empty?
|
||||
# TODO: should use the same code as the client to insert attachments
|
||||
raw << "\n#{attachment_markdown(upload)}\n"
|
||||
|
|
|
@ -462,7 +462,7 @@ class ImportScripts::Base
|
|||
src.close
|
||||
tmp.rewind
|
||||
|
||||
Upload.create_for(user_id, tmp, source_filename, File.size(tmp))
|
||||
Upload.create_for(user_id, tmp, source_filename, tmp.size)
|
||||
ensure
|
||||
tmp.close rescue nil
|
||||
tmp.unlink rescue nil
|
||||
|
|
Loading…
Reference in New Issue