Class: SuttyMigration::WordpressXml::Attachment

Inherits:
Post
  • Object
show all
Defined in:
lib/sutty_migration/wordpress_xml/attachment.rb

Overview

Represents an attachment or uploaded file.

Constant Summary

Constants inherited from Post

Post::SLASH

Instance Attribute Summary

Attributes inherited from Post

#item, #wordpress

Instance Method Summary collapse

Methods inherited from Post

#attribute_value, #author, #categories, #content, #date, #description, #draft?, #id, #initialize, #inspect, #last_modified_at, #order, #password, #permalink, #published?, #slug, #tags, #title

Constructor Details

This class inherits a constructor from SuttyMigration::WordpressXml::Post

Instance Method Details

#attachment_urlString

File URL

Returns:

  • (String)


15
16
17
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 15

def attachment_url
  @attachment_url ||= attribute_value 'attachment_url'
end

#destString

File destination

Returns:

  • (String)


22
23
24
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 22

def dest
  @dest ||= CGI.unescape(URI(attachment_url).path.sub(%r{\A/}, ''))
end

#download(progress: true) ⇒ Boolean

Download the file if it doesn’t exist. Optionally show a progress bar.

Parameters:

  • :progress (Boolean)

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 40

def download(progress: true)
  return true if File.exist? dest

  ::Jekyll.logger.info 'Downloading:', dest

  FileUtils.mkdir_p File.dirname(dest)

  File.open(dest, 'w') do |f|
    if progress
      head = Faraday.head(attachment_url)
      content_length = head.headers['content-length'].to_i
      progress = ProgressBar.create(title: File.basename(dest), total: content_length, output: $stderr)
    end

    Faraday.get(attachment_url) do |req|
      req.options.on_data = proc do |chunk, downloaded_bytes|
        f.write chunk

        if progress
          progress.progress = downloaded_bytes > content_length ? content_length : downloaded_bytes
        end
      end
    end
  end

  File.exist? dest
end

#metaHash

Metadata, with file information as a Hash

Returns:

  • (Hash)


29
30
31
32
33
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 29

def meta
  super.tap do |m|
    m['_wp_attachment_metadata'] = PHP.unserialize m['_wp_attachment_metadata']
  end
end