public
Description: The kick ass (non-commercial) home for musicians and their music
Homepage: http://alonetone.com
Clone URL: git://github.com/sudara/alonetone.git
Safer and better testing of asset mp3 tags, names, and generation of 
permalinks
sudara (author)
Thu Apr 24 04:11:46 -0700 2008
commit  f37856f18942c18bfef18ec136513f0c1f3c5602
tree    3d1e781027b48849c9757c52e1061a3de83753cc
parent  487b4951510775744c16141e16bcbd7bd885308b
...
140
141
142
143
 
144
145
146
147
148
149
 
150
151
 
 
152
153
154
...
140
141
142
 
143
144
145
146
147
148
149
150
151
 
152
153
154
155
156
0
@@ -140,15 +140,17 @@ class Asset < ActiveRecord::Base
0
   
0
   def name
0
     # make sure the title is there, and if not, the filename is used...
0
- title && !title.empty? ? title : clean_filename
0
+ (title && !title.strip.blank?) ? title.strip : clean_filename
0
   end
0
   
0
   def public_mp3
0
     self.s3_url
0
   end
0
   
0
+ # never allow this to be blank, as permalinks are generated from it
0
   def clean_filename
0
- self.filename[0..-5].gsub(/-|_/,' ').titleize
0
+ clean = self.filename[0..-5].gsub(/-|_/,' ').strip.titleize
0
+ clean.blank? ? 'untitled' : clean
0
   end
0
   
0
   def clean_permalink
...
54
55
56
 
 
 
 
 
 
 
57
58
 
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
...
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
92
93
94
95
0
@@ -54,10 +54,42 @@ describe Asset, 'on upload' do
0
     @asset.name.should == 'Old Muppet Men Booing'
0
   end
0
   
0
+ it 'should still work even when tags are empty and the name is weird' do
0
+ @asset = new_track('_ .mp3')
0
+ @asset.save
0
+ @asset.permalink.should == 'untitled'
0
+ @asset.name.should == 'untitled'
0
+ end
0
+
0
   it 'should handle strange charsets / characters in title tags' do
0
     @asset = new_track('japanese-characters.mp3')
0
+ @asset.title = ''
0
     @asset.save
0
- @asset.name.should == 'Old Muppet Men Booing'
0
+ @asset.permalink.should == 'untitled' # name is still 01-\266Ե??\313"
0
+ end
0
+
0
+ it 'should handle empty name' do
0
+ @asset = new_track('japanese-characters.mp3')
0
+ @asset.save
0
+ @asset.permalink.should == "untitled" # name is 01-\266Ե??\313"
0
+ @asset.title = 'bee'
0
+ @asset.save
0
+ @asset.permalink.should == 'bee'
0
+ end
0
+
0
+ it 'should handle non-english filenames and default to untitled' do
0
+ @asset = new_track('中文測試.mp3')
0
+ @asset.save.should == true
0
+ @asset.permalink.should == 'untitled'
0
+ end
0
+
0
+ it 'should handle titles with only ???? and default to untitled' do
0
+ @asset = new_track('中文測試.mp3')
0
+ @asset.save.should == true
0
+ @asset.permalink.should == 'untitled'
0
+ @asset.title = "中文測試"
0
+ @asset.save.should == true
0
+ @asset.permalink.should == 'untitled'
0
   end
0
   
0
   it 'should use the mp3 tag1 title as name if present' do
...
10
11
12
 
13
14
15
...
10
11
12
13
14
15
16
0
@@ -10,6 +10,7 @@ Spec::Runner.configure do |config|
0
   config.use_instantiated_fixtures = false
0
   config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
0
 
0
+
0
   # == Fixtures
0
   #
0
   # You can declare fixtures for each example_group like this:
...
6
7
8
9
 
 
 
 
 
10
11
12
13
 
14
15
16
...
6
7
8
 
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -6,11 +6,16 @@ module PermalinkFu
0
     attr_accessor :translation_from
0
     
0
     def escape(str)
0
- s = Iconv.iconv(translation_to, translation_from, str).to_s
0
+ begin # this can fail when its passed japanese characters
0
+ s = Iconv.iconv(translation_to, translation_from, str).to_s
0
+ rescue Iconv::InvalidCharacter
0
+ s = 'untitled'
0
+ end
0
       s.gsub!(/\W+/, ' ') # all non-word chars to spaces
0
       s.strip! # ohh la la
0
       s.downcase! #
0
       s.gsub!(/\ +/, '-') # spaces to dashes, preferred separator char everywhere
0
+ s = (s.blank? ? 'untitled' : s)
0
       s
0
     end
0
   end

Comments

    No one has commented yet.