mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-04 12:51:41 -05:00
2dd01c61b0
- Move some methods into their own classes in order to make it easier to reuse them outside of classes extending the base importer. For compatibility reasons the old methods are still in the base importer and delegate to the new objects. The following methods and hashes were extracted: - all the lookup maps for existing and imported data - all the methods used for uploads and attachments - No need to store failed users and groups. This information wasn't used anyway. - Print progress instead of category names when importing categories. - Allow importers to override if bbcode_to_md should be used (until now it always used ARGV) - Allow importers to add additional site settings that automatically get restored after the importer finishes. - Show how many posts and messages are imported per minute. This should help detecting when the import is slowing down and needs to be restarted. - Use max_image_width and max_image_height from settings instead of hard-coded values for uploaded images.
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
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)
|
|
tmp = Tempfile.new('discourse-upload')
|
|
src = File.open(path)
|
|
FileUtils.copy_stream(src, tmp)
|
|
src.close
|
|
tmp.rewind
|
|
|
|
Upload.create_for(user_id, tmp, source_filename, tmp.size)
|
|
rescue => e
|
|
Rails.logger.error("Failed to create upload: #{e}")
|
|
nil
|
|
ensure
|
|
tmp.close rescue nil
|
|
tmp.unlink rescue nil
|
|
end
|
|
|
|
def html_for_upload(upload, display_filename)
|
|
if FileHelper.is_image?(upload.url)
|
|
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
|
|
%Q[<img src="#{upload.url}" width="#{image_width}" height="#{image_height}"><br/>]
|
|
end
|
|
|
|
def attachment_html(upload, display_filename)
|
|
"<a class='attachment' href='#{upload.url}'>#{display_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
|
end
|
|
end
|
|
end
|