danopia (owner)

Revisions

gist: 161524 Download_button fork
public
Public Clone URL: git://gist.github.com/161524.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'digest/md5'
 
class FileNotFoundController < ApplicationController
def index
@path = params[:path].join('/')
 
# Nail and serve images on request if they aren't nailed yet
if @path =~ /^(.*)\/.nails\/([a-z]+)\/(.+)$/
 
@folder = $1
@size = $2
@name = $3
@path = @folder + @name
 
fs_folder = RAILS_ROOT + "/public/#{@folder}"
fs_path = fs_folder + "/#{@name}"
 
@sizes ||= ['thumb', 'small', 'medium', 'large']
if @sizes.include? @size
@size_sym = @size.to_sym
else
render :action => 'filenotfound', :status => 404
return
end
 
fs_target = _resize_image fs_path, @size_sym
send_file(fs_target, :type => 'image/' + File.extname(@name).delete('.').downcase, :disposition => 'inline')
return
end
 
@fs_path = RAILS_ROOT + '/public/' + @path
if File.exists?(@fs_path) && !(params[:path].include?('.nail'))
files = Dir.entries(@fs_path).sort!
@dirs = []
@imgs = []
@files = []
@sizes = []
@img_exts = ['.gif', '.jpg', '.jpeg', '.png', '.svg', '.bmp'] # ', '. , '.'
 
@fs_nail = @fs_path + '/.nail/'
 
files.each do |file|
next if file[0,1] == '.' && file != '..'
file_path = @fs_path + '/' + file
ftype = File.ftype(file_path)
if ftype == 'file'
file_ext = File.extname(file).downcase
file_array = {:name => file, :size => File.size(file_path)}
if @img_exts.include? file_ext
_resize_image file_path, :thumb
@imgs << file_array
else
@files << file_array
end
elsif ftype == 'directory'
@dirs << file
end
end
 
@thumbnail = @imgs.size > @files.size
unless @thumbnail
@files += @imgs
@files.sort! { |a,b| a[:name] <=> b[:name] }
end
 
render :action => 'dirlist'
 
else
render :action => 'filenotfound', :status => 404
end
end
 
def _resize_image(fs_image, type)
fs_target = File.dirname(fs_image) + "/.nails/#{type}/"
FileUtils.mkdir_p fs_target
fs_target += File.basename(fs_image)
 
@nail_types ||= {
:thumb => '150x113',
:small => '320x240',
:medium => '640x480',
:large => '1280x960'
}
 
unless File.exists?(fs_target) && (File.stat(fs_target).mtime.to_i > File.stat(fs_image).mtime.to_i)
system("convert -resize #{@nail_types[type] || type} '#{fs_image}' '#{fs_target}'")
end
fs_target
end
end