Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added thumbnail generation

  • Loading branch information...
commit 8dfb7800f9264eba873860cd7db1a7a2e5fbceab 1 parent 10a1a14
@farrspace authored
View
88 foto.rb
@@ -4,28 +4,106 @@
require 'sass'
require 'haml'
require 'coffee-script'
+require 'quick_magick'
set :port, 3000
+class Album
+ attr_accessor :name
+
+ def initialize(name)
+ @name = name
+ end
+
+ def link
+ "/photos/#{@name}/"
+ end
+end
+
+class Photo
+ attr_accessor :album, :filename
+
+ def initialize(album, filename)
+ @album = album
+ @filename = filename
+ end
+
+ def title
+ @filename.split(".")[0..-2].join(".")
+ end
+
+ def thumbnail
+ "/photos/#{@album}/thumbnail/#{@filename}"
+ end
+
+ def full
+ "/media/#{@album}/#{@filename}"
+ end
+
+ def link
+ "/photos/#{@album}/#{@filename}"
+ end
+
+end
+
helpers do
# Usage: partial :foo
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
+
+ def list_galleries
+ Dir['public/media/*/'].map{|directory| directory.split('/').last }
+ end
+
+ def create_thumb_dirs
+ galleries = list_galleries
+
+ galleries.each do |gallery|
+ d = "public/thumbs/#{gallery}"
+ if Dir[d] == nil
+ Dir.mkdir(d)
+ end
+ end
+ end
end
-get '/' do
- @galleries = Dir['public/media/*/'].map{|directory| directory.split('/').last}
+get '/photos/?' do
+ @galleries = list_galleries
haml :index
end
-get '/gallery/:album' do
+get '/photos/:album/?' do
@album = params[:album]
- @photos = Dir.glob("public/media/" + @album + "/*.jpg").map{|file| file.split('/').last}
- @urls = @photos.map{|url| "/media/" + @album + "/" + url}
+ files = Dir.glob("public/media/" + @album + "/*.jpg").map{|file| file.split('/').last}
+
+ @photos = files.map {|filename| Photo.new(@album, filename) }
+
haml :gallery
end
+get '/photos/:album/:photo' do
+ @album = Album.new(params[:album])
+ @photo = Photo.new(params[:album], params[:photo])
+ haml :photo
+end
+
+get '/photos/:album/thumbnail/:filename' do
+ content_type "image/jpeg"
+ file = "public/media/" + params[:album] + "/" + params[:filename]
+ thumb = "public/thumbs/" + params[:album] + "/" + params[:filename]
+
+ create_thumb_dirs
+
+ if ! File.exists?(thumb)
+ image = QuickMagick::Image.read(file).first
+ image.resize("300x300")
+ image.save(thumb)
+ end
+
+ File.open(thumb, 'rb').read
+end
+
get '/css/style.css' do
scss :style
end
View
BIN  public/thumbs/Animals/alpacas.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Animals/bison.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Animals/brown bears.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Animals/giraffe.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Animals/sheep.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Cities/seattle.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Cities/thames_central_lond.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Landscapes/field.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Landscapes/mountains.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/thumbs/Landscapes/smoky mountains.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
6 views/album.haml
@@ -1,5 +1,5 @@
%ul.album
- - @urls.each do |url|
+ - @photos.each do |photo|
%li
- %a{:href => url}
- %img{:src => url}
+ %a{:href => photo.link}
+ %img{:src => photo.thumbnail}
View
2  views/index.haml
@@ -5,6 +5,6 @@
- if @galleries
- @galleries.each do |gallery|
%li
- %a{:href=>"/gallery/"+gallery}= gallery
+ %a{:href=>"/photos/"+gallery}= gallery
- else
%li No galleries here.
View
6 views/photo.haml
@@ -0,0 +1,6 @@
+%h1
+ %a{:href => @photo.link}> #{@photo.title}
+ , a photo in
+ %a{:href => @album.link} #{@album.name}
+.full
+ %img{:src => @photo.thumbnail}
View
3  views/style.scss
@@ -29,9 +29,6 @@ body{
li{
list-style: none;
display: inline-block;
- img{
- max-width: 300px;
- }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.