public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
add mime type testing to Asset

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@1596 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sun Aug 13 10:18:46 -0700 2006
commit  6bc5386313b49360d314c082a996f2323ea72b6a
tree    2e9268979d1ee79a12af5fbb32e084f6541f5776
parent  65900d7c5185e82da96446349e05f25bc552e73d
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
...
9
10
11
12
 
 
 
 
 
13
14
15
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
27
28
29
 
30
31
32
33
34
35
36
37
0
@@ -1,4 +1,22 @@
0
 class Asset < ActiveRecord::Base
0
+ # used for extra mime types that dont follow the convention
0
+ @@extra_content_types = { :audio => ['application/ogg'] }
0
+ cattr_reader :extra_content_types
0
+
0
+ class << self
0
+ def movie?(content_type)
0
+ content_type.to_s =~ /^video/
0
+ end
0
+
0
+ def audio?(content_type)
0
+ content_type.to_s =~ /^audio/ || extra_content_types[:audio].include?(content_type)
0
+ end
0
+
0
+ def document?(content_type)
0
+ !image?(content_type) && !movie?(content_type) && !audio?(content_type)
0
+ end
0
+ end
0
+
0
   belongs_to :site
0
   acts_as_attachment :storage => :file_system, :thumbnails => { :thumb => '120>', :tiny => '50>' }
0
   before_validation_on_create :set_site_from_parent
0
@@ -9,7 +27,11 @@ class Asset < ActiveRecord::Base
0
     file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path]
0
     File.join(RAILS_ROOT, 'public/assets', site.host, date_to_permalink, thumbnail_name_for(thumbnail))
0
   end
0
-
0
+
0
+ [:movie, :audio, :document].each do |content|
0
+ define_method("#{content}?") { self.class.send("#{content}?", content_type) }
0
+ end
0
+
0
   protected
0
     def date_to_permalink
0
       [created_at.year, created_at.month, created_at.day] * '/'
...
65
66
67
 
 
 
 
 
68
69
70
...
65
66
67
68
69
70
71
72
73
74
75
0
@@ -65,6 +65,11 @@ class Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ def assert_file_exists(file, message = nil)
0
+ message ||= "File not found: #{file}"
0
+ assert File.file?(file), message
0
+ end
0
+
0
   # Sets the current user in the session from the user fixtures.
0
   def login_as(user)
0
     @request.session[:user] = user ? users(user).id : nil
...
4
5
6
 
 
 
 
 
 
 
 
7
8
9
10
11
12
 
 
 
13
14
15
...
28
29
30
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
33
34
...
42
43
44
45
46
47
48
49
50
 
 
51
52
...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
 
 
18
19
20
21
22
23
...
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
...
74
75
76
 
 
 
 
 
 
77
78
79
80
0
@@ -4,12 +4,20 @@ ASSET_PATH = File.join(RAILS_ROOT, 'test/fixtures/assets')
0
 class AssetTest < Test::Unit::TestCase
0
   fixtures :sites, :assets
0
 
0
+ def test_should_upload_and_create_asset_records
0
+ assert_difference sites(:first).assets, :count do
0
+ assert_difference Asset, :count, 3 do # asset + 2 thumbnails
0
+ process_upload
0
+ end
0
+ end
0
+ end
0
+
0
   def test_should_upload_file
0
     process_upload
0
     now = Time.now
0
- assert File.file?(File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo.png'))
0
- assert File.file?(File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_thumb.png'))
0
- assert File.file?(File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_tiny.png'))
0
+ assert_file_exists File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo.png')
0
+ assert_file_exists File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_thumb.png')
0
+ assert_file_exists File.join(ASSET_PATH, sites(:first).host, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_tiny.png')
0
   end
0
 
0
   def test_should_set_site_id
0
@@ -28,7 +36,31 @@ class AssetTest < Test::Unit::TestCase
0
     a = Asset.new
0
     Technoweenie::ActsAsAttachment.content_types.each do |content_type|
0
       a.content_type = content_type
0
- assert a.image?
0
+ assert a.image?, "#{content_type} was not an image"
0
+ end
0
+ end
0
+
0
+ def test_should_report_movie_type
0
+ a = Asset.new
0
+ ['video/mpeg', 'video/quicktime'].each do |content_type|
0
+ a.content_type = content_type
0
+ assert a.movie?, "#{content_type} was not a movie"
0
+ end
0
+ end
0
+
0
+ def test_should_report_audio_type
0
+ a = Asset.new
0
+ ['audio/mpeg', 'application/ogg', 'audio/wav'].each do |content_type|
0
+ a.content_type = content_type
0
+ assert a.audio?, "#{content_type} was not audio"
0
+ end
0
+ end
0
+
0
+ def test_should_report_document_type
0
+ a = Asset.new
0
+ ['application/pdf', 'application/msword', 'text/html', 'application/x-gzip'].each do |content_type|
0
+ a.content_type = content_type
0
+ assert a.document?, "#{content_type} was not a document"
0
     end
0
   end
0
 
0
@@ -42,11 +74,7 @@ class AssetTest < Test::Unit::TestCase
0
   
0
   protected
0
     def process_upload
0
- assert_difference sites(:first).assets, :count do
0
- assert_difference Asset, :count, 3 do # asset + 2 thumbnails
0
- sites(:first).assets.create(:filename => 'logo.png', :content_type => 'image/png',
0
- :attachment_data => IO.read(File.join(RAILS_ROOT, 'public/images/logo.png')))
0
- end
0
- end
0
+ sites(:first).assets.create(:filename => 'logo.png', :content_type => 'image/png',
0
+ :attachment_data => IO.read(File.join(RAILS_ROOT, 'public/images/logo.png')))
0
     end
0
 end
...
147
148
149
150
151
152
153
154
155
156
157
 
 
 
 
 
 
 
 
158
159
160
...
147
148
149
 
 
 
 
 
 
 
 
150
151
152
153
154
155
156
157
158
159
160
0
@@ -147,14 +147,14 @@ module Technoweenie # :nodoc:
0
 
0
       # Creates or updates the thumbnail for the current attachment.
0
       def create_or_update_thumbnail(file_name_suffix, *size)
0
- thumb = thumbnail_class.find_or_create_by_thumbnail_and_parent_id file_name_suffix.to_s, id
0
- thumb.attributes = {
0
- :content_type => content_type,
0
- :filename => thumbnail_name_for(file_name_suffix),
0
- :attachment_data => resize_image_to(size)
0
- }
0
- thumb.save!
0
- thumb
0
+ returning thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) do |thumb|
0
+ thumb.attributes = {
0
+ :content_type => content_type,
0
+ :filename => thumbnail_name_for(file_name_suffix),
0
+ :attachment_data => resize_image_to(size)
0
+ }
0
+ thumb.save!
0
+ end
0
       end
0
 
0
       # This method handles the uploaded file object. If you set the field name to uploaded_data, you don't need

Comments

    No one has commented yet.