2015-05-04 17:09:58 -04:00
|
|
|
require_dependency 'url_helper'
|
|
|
|
require_dependency 'file_helper'
|
|
|
|
|
|
|
|
module ImportScripts
|
|
|
|
class Uploader
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
|
|
|
# Creates an upload.
|
|
|
|
# Expects path to be the full path and filename of the source file.
|
|
|
|
# @return [Upload]
|
|
|
|
def create_upload(user_id, path, source_filename)
|
2018-06-17 16:54:50 -04:00
|
|
|
tmp = copy_to_tempfile(path)
|
2015-05-04 17:09:58 -04:00
|
|
|
|
2017-05-10 18:16:57 -04:00
|
|
|
UploadCreator.new(tmp, source_filename).create_for(user_id)
|
2015-05-04 17:09:58 -04:00
|
|
|
rescue => e
|
2018-08-12 16:02:17 -04:00
|
|
|
STDERR.puts "Failed to create upload: #{e}"
|
2015-05-04 17:09:58 -04:00
|
|
|
nil
|
|
|
|
ensure
|
|
|
|
tmp.close rescue nil
|
|
|
|
tmp.unlink rescue nil
|
|
|
|
end
|
|
|
|
|
2018-06-17 16:54:50 -04:00
|
|
|
def create_avatar(user, avatar_path)
|
|
|
|
tempfile = copy_to_tempfile(avatar_path)
|
|
|
|
filename = "avatar#{File.extname(avatar_path)}"
|
|
|
|
upload = UploadCreator.new(tempfile, filename, type: "avatar").create_for(user.id)
|
|
|
|
|
|
|
|
if upload.present? && upload.persisted?
|
|
|
|
user.create_user_avatar
|
|
|
|
user.user_avatar.update(custom_upload_id: upload.id)
|
|
|
|
user.update(uploaded_avatar_id: upload.id)
|
|
|
|
else
|
2018-08-12 16:02:17 -04:00
|
|
|
STDERR.puts "Failed to upload avatar for user #{user.username}: #{avatar_path}"
|
|
|
|
STDERR.puts upload.errors.inspect if upload
|
2018-06-17 16:54:50 -04:00
|
|
|
end
|
2018-08-12 16:02:17 -04:00
|
|
|
rescue
|
|
|
|
STDERR.puts "Failed to create avatar for user #{user.username}: #{avatar_path}"
|
2018-06-17 16:54:50 -04:00
|
|
|
ensure
|
|
|
|
tempfile.close! if tempfile
|
|
|
|
end
|
|
|
|
|
2015-05-04 17:09:58 -04:00
|
|
|
def html_for_upload(upload, display_filename)
|
2018-09-09 22:22:45 -04:00
|
|
|
if FileHelper.is_supported_image?(upload.url)
|
2015-05-04 17:09:58 -04:00
|
|
|
embedded_image_html(upload)
|
|
|
|
else
|
|
|
|
attachment_html(upload, display_filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def embedded_image_html(upload)
|
|
|
|
image_width = [upload.width, SiteSetting.max_image_width].compact.min
|
|
|
|
image_height = [upload.height, SiteSetting.max_image_height].compact.min
|
2018-06-17 16:56:14 -04:00
|
|
|
upload_name = upload.short_url || upload.url
|
|
|
|
%Q~![#{upload.original_filename}|#{image_width}x#{image_height}](#{upload_name})~
|
2015-05-04 17:09:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def attachment_html(upload, display_filename)
|
|
|
|
"<a class='attachment' href='#{upload.url}'>#{display_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
|
|
|
end
|
2018-06-17 16:54:50 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def copy_to_tempfile(source_path)
|
2018-08-12 16:26:07 -04:00
|
|
|
extension = File.extname(source_path)
|
|
|
|
tmp = Tempfile.new(['discourse-upload', extension])
|
2018-06-17 16:54:50 -04:00
|
|
|
|
|
|
|
File.open(source_path) do |source_stream|
|
|
|
|
IO.copy_stream(source_stream, tmp)
|
|
|
|
end
|
|
|
|
|
|
|
|
tmp.rewind
|
|
|
|
tmp
|
|
|
|
end
|
2015-05-04 17:09:58 -04:00
|
|
|
end
|
|
|
|
end
|