Skip to content

Instantly share code, notes, and snippets.

@jastkand
Created March 15, 2013 13:45
Show Gist options
  • Save jastkand/5169963 to your computer and use it in GitHub Desktop.
Save jastkand/5169963 to your computer and use it in GitHub Desktop.
Script that helps to download images from old server using url stored in database and old :host_url:
class HomeController
def download
require 'open-uri'
require 'fileutils'
icons = Icon.all
posts = Post.all
snapshots = Snapshot.all
icons.each do |icon|
create_image icon.picture.url
create_image icon.picture.url(:picture)
create_image icon.picture.url(:picture2x)
end
posts.each do |post|
create_image post.teaser.url
create_image post.teaser.url(:thumbnail)
create_image post.teaser.url(:picture)
create_image post.teaser.url(:picture2x)
end
snapshots.each do |snapshot|
create_image snapshot.snap.url
create_image snapshot.snap.url(:thumbnail)
create_image snapshot.snap.url(:thumbnail_2x)
end
end
def create_image(url)
remote_host = ':host_url:'
url.gsub!(/\?.*/, "")
name = File.basename(url)
_url = url.gsub("/#{name}", "")
path = ":local_public_folder:#{_url}"
logger.debug "#{remote_host}#{url}"
FileUtils.mkdir_p(path) unless File.exists?(path)
logger.debug "#{path}/#{name}"
open("#{path}/#{name}", 'wb') do |file|
file << open("#{remote_host}#{url}").read
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment