lib/bluedoc/export/archive.rb
# Download images in body into ../images/
Download images in body into ../images
Download images in body into .. / images
def downlod_images(body, body_html)
        images_dir = File.join(repo_dir, "images")
        FileUtils.mkdir_p(images_dir)
        doc = Nokogiri::HTML(body_html)
        doc.css("img").each do |node|
          src = node.attr("src")
          next if src.blank?
          match = src.match(/\/uploads\/(\w+)/i)
          next if match.nil?
          file_key = match[1]
          next if file_key.nil?
          blob = ActiveStorage::Blob.find_by(key: file_key)
          next if blob.blank?
          fname = File.join(images_dir, blob.filename.to_s)
          if File.exist?(fname)
            ext = File.extname(fname)
            fname = File.join(images_dir, "#{file_key}#{ext}")
          end
          begin
            File.open(fname, "w") do |f|
              blob.download { |data| f.write(data.force_encoding("UTF-8")) }
            end
          rescue => e
            logger.error "Download blob: #{blob.key} error: #{e.inspect}"
            next
          end
          new_src = "./images/#{File.basename(fname)}"
          logger.info "------- replace image src ----------"
          logger.info "- #{src}"
          logger.info "+ #{new_src}"
          body = body.gsub(src, new_src)
        end
        body
      end
def downlod_images ( body , body_html ) images_dir = File . join ( repo_dir , "images" ) FileUtils . mkdir_p ( images_dir ) doc = Nokogiri :: HTML ( body_html ) doc . css ( "img" ) . each do | node | src = node . attr ( "src" ) next if src . blank? match = src . match ( / \/ uploads \/ ( \w +) /i ) next if match . nil? file_key = match [ 1 ] next if file_key . nil? blob = ActiveStorage :: Blob . find_by ( key : file_key ) next if blob . blank? fname = File . join ( images_dir , blob . filename . to_s ) if File . exist? ( fname ) ext = File . extname ( fname ) fname = File . join ( images_dir , "#{file_key}#{ext}" ) end begin File . open ( fname , "w" ) do | f | blob . download { | data | f . write ( data . force_encoding ( "UTF-8" ) ) } end rescue => e logger . error "Download blob: #{blob.key} error: #{e.inspect}" next end new_src = "./images/#{File.basename(fname)}" logger . info "------- replace image src ----------" logger . info "- #{src}" logger . info "+ #{new_src}" body = body . gsub ( src , new_src ) end body end
Download images in body into ../images
Download images in body into .. / images
# match src is /uploads/{key} # Find blob record for get old filename # if same filename exist, rename into {key}.ext # Streaming download blob file into {fname} # replace old "/uploads/{key}" into new src "./images/{new_filename}"
body
<not_specific>
body_html
<not_specific>