Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 206
- Log:
Initial import of Gullery, an open source photo gallery:
http://nubyonrails.com/pages/gullery
- Author:
- rool
- Date:
- Sun May 20 19:05:59 +0100 2007
- Size:
- 3785 Bytes
1 | class Asset < ActiveRecord::Base |
2 | |
3 | cattr_accessor :thumbnail_width, :thumbnail_height |
4 | |
5 | validates_presence_of :project_id, :path |
6 | validates_associated :project |
7 | |
8 | belongs_to :project |
9 | |
10 | acts_as_taggable |
11 | |
12 | # Starts with '/' but is relative to 'public' |
13 | @@asset_dir = '/system/assets' |
14 | |
15 | @@thumbnail_width = '200' |
16 | @@thumbnail_height = '120' |
17 | |
18 | # Returns the full path to this asset on disk. /Users/bert/photos/pigeon.jpg |
19 | # TODO Take size argument |
20 | def absolute_path(size=:normal) |
21 | File.expand_path("public#{self.web_path(size)}", RAILS_ROOT) |
22 | end |
23 | |
24 | # :thumb, :normal, :original |
25 | def web_path(size=:normal) |
26 | if size == :original |
27 | return path |
28 | else |
29 | path.gsub /(\..*?)$/, "_#{size.to_s}\\1" |
30 | end |
31 | end |
32 | |
33 | |
34 | # Called automatically when saved from HTML forms |
35 | def file_field=(file_field) |
36 | if !file_field.original_filename.blank? |
37 | # Make sure the directory exists for us to save into |
38 | FileUtils.mkdir_p(File.expand_path("public#{@@asset_dir}", RAILS_ROOT)) |
39 | |
40 | # Set file to unique timestamp plus original extension |
41 | self.path = "#{@@asset_dir}/#{Time.now.utc.to_i}." + file_field.original_filename.gsub(/.*\./, '') |
42 | File.open(self.absolute_path(:original), File::CREAT|File::WRONLY) { |f| f.write(file_field.read) } |
43 | |
44 | self.resize_thumbnail |
45 | self.resize_normal |
46 | end |
47 | end |
48 | |
49 | |
50 | def resize_thumbnail |
51 | image = MiniMagick::Image.from_file(self.absolute_path(:original)) |
52 | if (image.width.to_f/image.height.to_f) >= (@@thumbnail_width.to_f/@@thumbnail_height.to_f) |
53 | # Wider than tall...use height |
54 | image.combine_options do |i| |
55 | i.resize "x#{@@thumbnail_height}" |
56 | i.crop "#{@@thumbnail_width}x#{@@thumbnail_height}+0+0!" |
57 | end |
58 | else |
59 | image.combine_options do |i| |
60 | i.resize "#{@@thumbnail_width}x" |
61 | i.crop "#{@@thumbnail_width}x#{@@thumbnail_height}+0+0!" |
62 | end |
63 | end |
64 | image.write(File.expand_path("public#{self.web_path(:thumb)}", RAILS_ROOT)) |
65 | end |
66 | |
67 | |
68 | def resize_normal |
69 | image = MiniMagick::Image.from_file(self.absolute_path(:original)) |
70 | if image.width > 640 || image.height > 480 |
71 | image.combine_options do |i| |
72 | i.resize "640x480" |
73 | end |
74 | end |
75 | # TODO Refactor |
76 | image.write(File.expand_path("public#{self.web_path(:normal)}", RAILS_ROOT)) |
77 | end |
78 | |
79 | |
80 | def before_destroy |
81 | [:original, :normal, :thumb].each do |size| |
82 | begin |
83 | File.delete absolute_path(size) |
84 | rescue Errno::ENOENT => e |
85 | end |
86 | end |
87 | end |
88 | |
89 | def rotate(direction='cw') |
90 | degrees = 90 |
91 | if direction == 'ccw' |
92 | degrees = -90 |
93 | end |
94 | image = MiniMagick::Image.from_file(self.absolute_path(:original)) |
95 | image.combine_options do |i| |
96 | i.rotate degrees |
97 | end |
98 | image.write(File.expand_path("public#{self.web_path(:original)}", RAILS_ROOT)) |
99 | resize_thumbnail |
100 | resize_normal |
101 | end |
102 | |
103 | # Returns the file extension, like jpg or pdf |
104 | def extension |
105 | self.path.gsub(/.*\./, '') |
106 | end |
107 | |
108 | # Returns the http Content-Type (image/png, etc.) |
109 | # |
110 | # TODO Add more types, or get from a reference |
111 | def file_type |
112 | file_types = { |
113 | /jpe?g/i => 'image/jpeg', |
114 | /png/ => 'image/png', |
115 | /gif/ => 'image/gif' |
116 | } |
117 | file_types.keys.each do |k| |
118 | if k.match(self.extension) |
119 | return file_types[k] |
120 | end |
121 | end |
122 | nil |
123 | end |
124 | |
125 | protected |
126 | |
127 | # Fixes a 'feature' of IE where it passes the entire path instead of just the filename |
128 | def sanitize_filename(value) |
129 | #get only the filename, not the whole path |
130 | just_filename = value.gsub(/^.*(\\|\/)/, '') |
131 | #NOTE: File.basename doesn't work right with Windows paths on Unix |
132 | #INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) |
133 | #replace all none alphanumeric, underscore or periods with underscore |
134 | just_filename.gsub(/[^\w\.\-]/,'_') |
135 | end |
136 | |
137 | end |