Class: SuttyMigration::WordpressXml::Attachment
- Defined in:
- lib/sutty_migration/wordpress_xml/attachment.rb
Overview
Represents an attachment or uploaded file.
Constant Summary
Constants inherited from Post
Instance Attribute Summary
Attributes inherited from Post
Instance Method Summary collapse
-
#attachment_url ⇒ String
File URL.
-
#dest ⇒ String
File destination.
-
#download(progress: true) ⇒ Boolean
Download the file if it doesn’t exist.
-
#meta ⇒ Hash
Metadata, with file information as a Hash.
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_url ⇒ String
File URL
15 16 17 |
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 15 def @attachment_url ||= attribute_value 'attachment_url' end |
#dest ⇒ String
File destination
22 23 24 |
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 22 def dest @dest ||= CGI.unescape(URI().path.sub(%r{\A/}, '')) end |
#download(progress: true) ⇒ Boolean
Download the file if it doesn’t exist. Optionally show a progress bar.
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() content_length = head.headers['content-length'].to_i progress = ProgressBar.create(title: File.basename(dest), total: content_length, output: $stderr) end Faraday.get() do |req| req..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 |
#meta ⇒ Hash
Metadata, with file information as a Hash
29 30 31 32 33 |
# File 'lib/sutty_migration/wordpress_xml/attachment.rb', line 29 def super.tap do |m| m['_wp_attachment_metadata'] = PHP.unserialize m['_wp_attachment_metadata'] end end |