Skip to content

Instantly share code, notes, and snippets.

Created December 10, 2014 00:48
Show Gist options
  • Save anonymous/b5aae2c9c6a10028629f to your computer and use it in GitHub Desktop.
Save anonymous/b5aae2c9c6a10028629f to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "mysql"
require "pry"
begin
con = Mysql.new '10.0.3.18', 'devuser', 'whoneedspasswords', 'devENV'
rs = con.query("SELECT direct_upload_url FROM uploads WHERE file_content_type LIKE 'image%' AND direct_thumbnail_upload_url IS NULL")
rs.each do |row|
http_url=row[0]
puts http_url
# generate all the different variables we might need later
s3_url = http_url.gsub("https", "s3").gsub("s3.amazonaws.com/", "") # s3cmd doesn't use http urls, so we need to convert it
full_filename = http_url.gsub(/.*\//, "") # extract the filename from the URL (includes spaces)
thumbnail_filename = "thumb-" + full_filename # self explanatory
pre_filename_url_http = http_url.match(/(.*\/)(.*)/)[1, 2] # this is the http url minus the filename
pre_filename_url_s3 = pre_filename_url_http[0].gsub("https", "s3").gsub("s3.amazonaws.com/", "") # this is the s3 url minus the filename
s3_thumbnail_url = pre_filename_url_s3 + thumbnail_filename # full url for s3
http_thumbnail_url = pre_filename_url_http[0] + thumbnail_filename # full http url
# debug -- lets see what those variables are
puts "s3_url: #{s3_url}"
puts "full_filename: #{full_filename}"
puts "thumbnail_filename: #{thumbnail_filename}"
puts "pre_filename_url_http #{pre_filename_url_http[0]}"
puts "pre_filename_url_s3: #{pre_filename_url_s3}"
puts "s3_thumbnail_url: #{s3_thumbnail_url}"
puts "http_thumbnail_url: #{http_thumbnail_url}"
# get image from s3
`s3cmd get "#{s3_url}" "#{full_filename}"`
# resize that fucker
`convert "#{full_filename}" -resize 340 "#{thumbnail_filename}"`
# upload the thumbnail to s3
`s3cmd put "#{thumbnail_filename}" "#{s3_thumbnail_url}"`
# remove the images
`rm "#{thumbnail_filename}" "#{full_filename}"`
# write thumbnail url to database
insert = con.query("UPDATE uploads (direct_thumbnail_upload_url) VALUES ('#{http_thumbnail_url}') WHERE direct_upload_url IS ('#{http_url}')")
binding.pry # Don't interate thru the loop
end
rescue Mysql::Error => e
puts e.errno
puts e.error
ensure
con.close if con
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment