mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
Create avatar from file in base importer
This commit is contained in:
parent
84d9b2e473
commit
88ca838e02
1 changed files with 32 additions and 6 deletions
|
@ -9,21 +9,34 @@ module ImportScripts
|
||||||
# Expects path to be the full path and filename of the source file.
|
# Expects path to be the full path and filename of the source file.
|
||||||
# @return [Upload]
|
# @return [Upload]
|
||||||
def create_upload(user_id, path, source_filename)
|
def create_upload(user_id, path, source_filename)
|
||||||
tmp = Tempfile.new('discourse-upload')
|
tmp = copy_to_tempfile(path)
|
||||||
src = File.open(path)
|
|
||||||
FileUtils.copy_stream(src, tmp)
|
|
||||||
src.close
|
|
||||||
tmp.rewind
|
|
||||||
|
|
||||||
UploadCreator.new(tmp, source_filename).create_for(user_id)
|
UploadCreator.new(tmp, source_filename).create_for(user_id)
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error("Failed to create upload: #{e}")
|
puts "Failed to create upload: #{e}"
|
||||||
nil
|
nil
|
||||||
ensure
|
ensure
|
||||||
tmp.close rescue nil
|
tmp.close rescue nil
|
||||||
tmp.unlink rescue nil
|
tmp.unlink rescue nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
puts "Failed to upload avatar for user #{user.username}: #{avatar_path}"
|
||||||
|
puts upload.errors.inspect if upload
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
tempfile.close! if tempfile
|
||||||
|
end
|
||||||
|
|
||||||
def html_for_upload(upload, display_filename)
|
def html_for_upload(upload, display_filename)
|
||||||
if FileHelper.is_image?(upload.url)
|
if FileHelper.is_image?(upload.url)
|
||||||
embedded_image_html(upload)
|
embedded_image_html(upload)
|
||||||
|
@ -41,5 +54,18 @@ module ImportScripts
|
||||||
def attachment_html(upload, display_filename)
|
def attachment_html(upload, display_filename)
|
||||||
"<a class='attachment' href='#{upload.url}'>#{display_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
"<a class='attachment' href='#{upload.url}'>#{display_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def copy_to_tempfile(source_path)
|
||||||
|
tmp = Tempfile.new('discourse-upload')
|
||||||
|
|
||||||
|
File.open(source_path) do |source_stream|
|
||||||
|
IO.copy_stream(source_stream, tmp)
|
||||||
|
end
|
||||||
|
|
||||||
|
tmp.rewind
|
||||||
|
tmp
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue