Created
January 22, 2011 10:28
-
-
Save stammy/791030 to your computer and use it in GitHub Desktop.
dirty hack of a rakefile I use to upload new images to cloudfront for jekyll blog posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# when I write new blog posts I usually have a temporary folder called new_post on my desktop | |
# that I place images I want to upload to amazon s3 (which is distributed as CF) and then | |
# get the appropriate URLs to put the images in my blog post | |
# I usually save files as some_slug.png and some_slug_1200.png | |
# with the latter being a larger version (1200px wide) of the first. | |
# a typical upload will include 20 files. 2 versions of some (sm and lg), and some orphans | |
# so this compares filenames and if it detects a larger version of a file, | |
# it links the the smaller image to the larger image (i use fancyzoom on my site) | |
# if not, it just links to the image | |
desc 'upload imgs to cloudfront' | |
task :cloudfront do | |
puts 'uploading images in ~/Desktop/new_post/ to cf' | |
post_dir = "/Users/Stammy/Desktop/new_post/" | |
month = Time.new.strftime("%m") | |
year = Time.new.strftime("%Y") | |
sh "s3cmd put --acl-public --guess-mime-type #{post_dir}* s3://pstam-cloud/uploads/#{year}/#{month}/" | |
# create URLs for handy copying | |
# detect large version of same image and link it to smaller version | |
# or just provide img src to orphan if no larger version | |
# works b/c Dir.glob returns files alpha by extension | |
# was going to refactor to look cleaner.. but it works just fine | |
puts "Uploaded. Here are your CF URLs \n\n" | |
Dir.chdir(post_dir) | |
img_urls = '' | |
images = Dir.glob("*.{png,gif,jpg}") | |
images.each_with_index do |image, index| | |
cur = image | |
desc = cur.gsub('pstam_','').gsub('_',' ')[0...-4].capitalize | |
if !images[index+1].nil? | |
nxt = images[index+1] | |
if cur.gsub(/_1[0-9]00/,'')[0...-4] == nxt.gsub(/_1[0-9]00/,'')[0...-4] | |
if /_1[0-9]00/.match(image) | |
large = cur | |
small = nxt | |
elsif /_1[0-9]00/.match(nxt) | |
large = nxt | |
small = cur | |
end | |
img_urls += <<-HTML | |
<div class="center"><a href="http://turbo.paulstamatiou.com/uploads/#{year}/#{month}/#{large}" title="#{desc}"><img src="http://turbo.paulstamatiou.com/uploads/#{year}/#{month}/#{small}" alt="#{desc}"/></a></div>\n | |
HTML | |
elsif !(/_1[0-9]00/.match(image)) | |
img_urls += <<-HTML | |
<div class="center"><img src="http://turbo.paulstamatiou.com/uploads/#{year}/#{month}/#{cur}" alt="#{desc}"/></div>\n | |
HTML | |
end | |
else | |
# if last | |
img_urls += <<-HTML | |
<div class="center"><img src="http://turbo.paulstamatiou.com/uploads/#{year}/#{month}/#{cur}" alt="#{desc}"/></div>\n | |
HTML | |
end | |
end | |
puts img_urls | |
filename = (0...8).map{65.+(rand(25)).chr}.join + "_imgurls_tmp.txt" | |
path = File.join("/tmp", filename) | |
File.open(path, 'w') do |file| | |
file.puts img_urls | |
end | |
system "open -a textmate #{path}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment