class SuttyMigration::WordpressXml::Attachment

Represents an attachment or uploaded file.

Public Instance Methods

attachment_url() click to toggle source

File URL

@return [String]

   # File lib/sutty_migration/wordpress_xml/attachment.rb
15 def attachment_url
16   @attachment_url ||= attribute_value 'attachment_url'
17 end
dest() click to toggle source

File destination

@return [String]

   # File lib/sutty_migration/wordpress_xml/attachment.rb
22 def dest
23   @dest ||= URI(attachment_url).path.sub(%r{\A/}, '')
24 end
download(progress: true) click to toggle source

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

@param :progress [Boolean] @return [Boolean]

   # File lib/sutty_migration/wordpress_xml/attachment.rb
40 def download(progress: true)
41   return true if File.exist? dest
42 
43   ::Jekyll.logger.info "Downloading #{dest}"
44 
45   FileUtils.mkdir_p File.dirname(dest)
46 
47   File.open(dest, 'w') do |f|
48     if progress
49       head = Faraday.head(attachment_url)
50       content_length = head.headers['content-length'].to_i
51       progress = ProgressBar.create(title: File.basename(dest), total: content_length, output: $stderr)
52     end
53 
54     Faraday.get(attachment_url) do |req|
55       req.options.on_data = proc do |chunk, downloaded_bytes|
56         f.write chunk
57 
58         if progress
59           progress.progress = downloaded_bytes > content_length ? content_length : downloaded_bytes
60         end
61       end
62     end
63   end
64 
65   File.exist? dest
66 end
meta() click to toggle source

Metadata, with file information as a Hash

@return [Hash]

Calls superclass method
   # File lib/sutty_migration/wordpress_xml/attachment.rb
29 def meta
30   super.tap do |m|
31     m['_wp_attachment_metadata'] = PHP.unserialize m['_wp_attachment_metadata']
32   end
33 end