From 4ba99bcc04c4e1c2d9e0dca1c23dae71d6547779 Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Tue, 23 Aug 2016 19:57:48 +0200 Subject: [PATCH] FIX: Attachments can be not found --- script/import_scripts/nabble.rb | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/script/import_scripts/nabble.rb b/script/import_scripts/nabble.rb index 3e04e794b..529f46509 100644 --- a/script/import_scripts/nabble.rb +++ b/script/import_scripts/nabble.rb @@ -199,28 +199,30 @@ class ImportScripts::Nabble < ImportScripts::Base def process_attachments(txt, postid) txt.gsub!(//m) do |match| basename = Regexp.last_match[1] - fn = File.join('/tmp/nab', basename) - - binary = @client.exec("SELECT content FROM file_node WHERE name='#{basename}' AND node_id = #{postid}")[0]['content'] - File.open(fn, 'wb') { |f| - f.write(PG::Connection.unescape_bytea(binary)) - } - upload = @uploader.create_upload(0, fn, basename) - @uploader.embedded_image_html(upload) + get_attachment_upload(basename, postid) do |upload| + @uploader.embedded_image_html(upload) + end end txt.gsub!(/(.*?)<\/nabble_a>/m) do |match| basename = Regexp.last_match[1] - fn = File.join('/tmp/nab', basename) + get_attachment_upload(basename, postid) do |upload| + @uploader.attachment_html(upload, basename) + end + end + txt + end - binary = @client.exec("SELECT content FROM file_node WHERE name='#{basename}' AND node_id = #{postid}")[0]['content'] + def get_attachment_upload(basename, postid) + contents = @client.exec("SELECT content FROM file_node WHERE name='#{basename}' AND node_id = #{postid}") + if contents.any? + binary = contents[0]['content'] + fn = File.join('/tmp/nab', basename) File.open(fn, 'wb') { |f| f.write(PG::Connection.unescape_bytea(binary)) } - upload = @uploader.create_upload(0, fn, basename) - @uploader.attachment_html(upload, basename) + yield @uploader.create_upload(0, fn, basename) end - txt end def import_replies