lfa-wordpress-import/lib/wordpress/attachment.rb

129 lines
2.5 KiB
Ruby
Raw Normal View History

2014-03-04 22:47:53 +00:00
module WordPressImport
class Attachment
attr_reader :node
attr_reader :refinery_image
attr_reader :refinery_resource
def initialize(node)
@node = node
end
2014-03-04 22:47:53 +00:00
def title
node.xpath("title").text
end
2014-03-04 22:47:53 +00:00
def description
node.xpath("description").text
end
2014-03-04 22:47:53 +00:00
def file_name
url.split('/').last
end
2014-03-04 22:47:53 +00:00
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
2014-03-04 22:47:53 +00:00
def url
node.xpath("wp:attachment_url").text
end
2014-03-04 22:47:53 +00:00
def url_pattern
url_parts = url.split('.')
extension = url_parts.pop
url_without_extension = url_parts.join('.')
2014-03-04 22:47:53 +00:00
/#{url_without_extension}(-\d+x\d+)?\.#{extension}/
end
2014-03-04 22:47:53 +00:00
def image?
url.match /\.(png|jpg|jpeg|gif)$/
end
2014-03-04 22:47:53 +00:00
def to_refinery
if image?
to_image
else
to_resource
end
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
def replace_url
if image?
replace_image_url
else
replace_resource_url
end
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
private
2014-03-04 22:47:53 +00:00
def to_image
image = ::Image.new
image.created_at = post_date
image.image_url = url
image.save!
2014-03-04 22:47:53 +00:00
@refinery_image = image
image
end
2014-03-04 22:47:53 +00:00
def to_resource
resource = ::Resource.new
resource.created_at = post_date
resource.file_url = url
resource.save!
2014-03-04 22:47:53 +00:00
@refinery_resource = resource
resource
end
2014-03-04 22:47:53 +00:00
def replace_image_url
replace_image_url_in_blog_posts
replace_image_url_in_pages
end
2014-03-04 22:47:53 +00:00
def replace_resource_url
replace_resource_url_in_blog_posts
replace_resource_url_in_pages
end
2014-03-04 22:47:53 +00:00
def replace_image_url_in_blog_posts
replace_url_in_blog_posts(refinery_image.image.url)
end
def replace_image_url_in_pages
replace_url_in_pages(refinery_image.image.url)
end
2014-03-04 22:47:53 +00:00
def replace_resource_url_in_blog_posts
replace_url_in_blog_posts(refinery_resource.file.url)
end
def replace_resource_url_in_pages
replace_url_in_pages(refinery_resource.file.url)
end
2014-03-04 22:47:53 +00:00
def replace_url_in_blog_posts(new_url)
::BlogPost.all.each do |post|
if (! post.body.empty?) && post.body.include?(url)
post.body = post.body.gsub(url_pattern, new_url)
post.save!
end
end
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
def replace_url_in_pages(new_url)
::Page.all.each do |page|
page.parts.each do |part|
if (! part.body.to_s.blank?) && part.body.include?(url)
part.body = part.body.gsub(url_pattern, new_url)
part.save!
end
end
end
end
2014-03-04 22:47:53 +00:00
end
end