spud_media 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,101 +1,136 @@
1
1
  class SpudMedia < ActiveRecord::Base
2
2
 
3
3
  has_attached_file :attachment,
4
- :storage => Spud::Media.paperclip_storage,
5
- :s3_credentials => Spud::Media.s3_credentials,
6
- :s3_permissions => lambda { |attachment, style|
7
- attachment.instance.is_protected ? 'private' : 'public-read'
8
- },
9
- :path => Spud::Media.paperclip_storage == :s3 ? Spud::Media.storage_path : lambda { |attachment|
10
- attachment.instance.is_protected ? Spud::Media.storage_path_protected : Spud::Media.storage_path
11
- },
12
- :url => Spud::Media.storage_url
4
+ :storage => Spud::Media.paperclip_storage,
5
+ :s3_credentials => Spud::Media.s3_credentials,
6
+ :s3_permissions => lambda { |attachment, style|
7
+ attachment.instance.is_protected ? 'private' : 'public-read'
8
+ },
9
+ :path => Spud::Media.paperclip_storage == :s3 ? Spud::Media.storage_path : lambda { |attachment|
10
+ attachment.instance.is_protected ? Spud::Media.storage_path_protected : Spud::Media.storage_path
11
+ },
12
+ :url => Spud::Media.storage_url,
13
+ :styles => lambda { |attachment| attachment.instance.custom_style }
13
14
 
14
- before_update :validate_permissions
15
+ attr_accessible :attachment_content_type,:attachment_file_name,:attachment_file_size,:attachment, :is_protected, :crop_x, :crop_y, :crop_w, :crop_h, :crop_s
15
16
 
16
- attr_accessible :attachment_content_type,:attachment_file_name,:attachment_file_size,:attachment, :is_protected
17
- def image_from_type
18
- if self.attachment_content_type.blank?
19
- return "spud/admin/files_thumbs/dat_thumb.png"
20
- end
17
+ validates_numericality_of :crop_x, :crop_y, :crop_w, :crop_h, :crop_s, :allow_nil => true
21
18
 
22
- if self.attachment_content_type.match(/jpeg|jpg/)
23
- return "spud/admin/files_thumbs/jpg_thumb.png"
24
- end
19
+ before_create :rename_file
20
+ before_update :validate_permissions
25
21
 
26
- if self.attachment_content_type.match(/png/)
27
- return "spud/admin/files_thumbs/png_thumb.png"
28
- end
22
+ def rename_file
23
+ # remove periods and other unsafe characters from file name to make routing easier
24
+ extension = File.extname(attachment_file_name)
25
+ filename = attachment_file_name.chomp(extension).parameterize
26
+ attachment.instance_write :file_name, filename + extension
27
+ end
29
28
 
30
- if self.attachment_content_type.match(/zip|tar|tar\.gz|gz/)
31
- return "spud/admin/files_thumbs/zip_thumb.png"
32
- end
29
+ def image_from_type
30
+ if self.attachment_content_type.blank?
31
+ return "spud/admin/files_thumbs/dat_thumb.png"
32
+
33
+ elsif self.attachment_content_type.match(/jpeg|jpg/)
34
+ return "spud/admin/files_thumbs/jpg_thumb.png"
35
+
36
+ elsif self.attachment_content_type.match(/png/)
37
+ return "spud/admin/files_thumbs/png_thumb.png"
38
+
39
+ elsif self.attachment_content_type.match(/zip|tar|tar\.gz|gz/)
40
+ return "spud/admin/files_thumbs/zip_thumb.png"
41
+
42
+ elsif self.attachment_content_type.match(/xls|xlsx/)
43
+ return "spud/admin/files_thumbs/xls_thumb.png"
44
+
45
+ elsif self.attachment_content_type.match(/doc|docx/)
46
+ return "spud/admin/files_thumbs/doc_thumb.png"
47
+
48
+ elsif self.attachment_content_type.match(/ppt|pptx/)
49
+ return "spud/admin/files_thumbs/ppt_thumb.png"
50
+
51
+ elsif self.attachment_content_type.match(/txt|text/)
52
+ return "spud/admin/files_thumbs/txt_thumb.png"
53
+
54
+ elsif self.attachment_content_type.match(/pdf|ps/)
55
+ return "spud/admin/files_thumbs/pdf_thumb.png"
56
+
57
+ elsif self.attachment_content_type.match(/mp3|wav|aac/)
58
+ return "spud/admin/files_thumbs/mp3_thumb.png"
59
+ end
60
+
61
+ return "spud/admin/files_thumbs/dat_thumb.png"
62
+ end
33
63
 
34
- if self.attachment_content_type.match(/xls|xlsx/)
35
- return "spud/admin/files_thumbs/xls_thumb.png"
36
- end
37
- if self.attachment_content_type.match(/doc|docx/)
38
- return "spud/admin/files_thumbs/doc_thumb.png"
39
- end
40
- if self.attachment_content_type.match(/ppt|pptx/)
41
- return "spud/admin/files_thumbs/ppt_thumb.png"
42
- end
43
- if self.attachment_content_type.match(/txt|text/)
44
- return "spud/admin/files_thumbs/txt_thumb.png"
45
- end
46
- if self.attachment_content_type.match(/pdf|ps/)
47
- return "spud/admin/files_thumbs/pdf_thumb.png"
48
- end
49
- if self.attachment_content_type.match(/mp3|wav|aac/)
50
- return "spud/admin/files_thumbs/mp3_thumb.png"
51
- end
64
+ def is_image?
65
+ if self.attachment_content_type.match(/jpeg|jpg|png/)
66
+ return true
67
+ else
68
+ return false
69
+ end
70
+ end
52
71
 
53
- return "spud/admin/files_thumbs/dat_thumb.png"
54
- end
72
+ def has_custom_crop?
73
+ return (crop_x && crop_y && crop_w && crop_h && crop_s)
74
+ end
55
75
 
56
- # if you are using S3, attachment.url will automatically point to the S3 url
57
- # protected files need to hit the rails middle-man first
58
- # this method will provide the correct url for either case
59
- def attachment_url
60
- if Spud::Media.paperclip_storage == :s3 && is_protected
61
- return Paperclip::Interpolations.interpolate(Spud::Media.config.storage_url, attachment, 'original')
62
- else
63
- return attachment.url
64
- end
76
+ def custom_style
77
+ if is_image? && has_custom_crop?
78
+ {:cropped => {:geometry => '', :convert_options => "-resize #{crop_s}% -crop #{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"}}
79
+ else
80
+ {}
65
81
  end
82
+ end
66
83
 
67
- # If is_protected has changed, we need to make sure we are setting the appropriate permissions
68
- # This means either moving the file in the filesystem or setting the appropriate ACL in S3
69
- def validate_permissions
70
- if Spud::Media.config.paperclip_storage == :filesystem
71
- validate_permissions_filesystem
72
- elsif Spud::Media.config.paperclip_storage == :s3
73
- validate_permissions_s3
74
- end
84
+ # if you are using S3, attachment.url will automatically point to the S3 url
85
+ # protected files need to hit the rails middle-man first
86
+ # this method will provide the correct url for either case
87
+ def attachment_url(style=nil)
88
+ # defaults to cropped style if that style exists, otherwise use original
89
+ if !style
90
+ style = (is_image? && has_custom_crop?) ? 'cropped' : 'original'
75
91
  end
92
+ if Spud::Media.paperclip_storage == :s3 && is_protected
93
+ return Paperclip::Interpolations.interpolate(Spud::Media.config.storage_url, attachment, style)
94
+ else
95
+ return attachment.url(style)
96
+ end
97
+ end
98
+
99
+ # If is_protected has changed, we need to make sure we are setting the appropriate permissions
100
+ # This means either moving the file in the filesystem or setting the appropriate ACL in S3
101
+ def validate_permissions
102
+ if Spud::Media.config.paperclip_storage == :filesystem
103
+ validate_permissions_filesystem
104
+ elsif Spud::Media.config.paperclip_storage == :s3
105
+ validate_permissions_s3
106
+ end
107
+ end
76
108
 
77
109
  private
78
110
 
79
- def validate_permissions_filesystem
80
- if is_protected
81
- old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original')
82
- new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original')
83
- else
84
- old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original')
85
- new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original')
86
- end
87
- new_base_dir = File.dirname(new_path)
88
- old_base_dir= File.dirname(old_path)
89
- if File.directory?(old_base_dir)
90
- FileUtils.mv(old_base_dir, new_base_dir)
91
- end
111
+ def validate_permissions_filesystem
112
+ if is_protected
113
+ old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original')
114
+ new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original')
115
+ else
116
+ old_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path_protected, attachment, 'original')
117
+ new_path = Paperclip::Interpolations.interpolate(Spud::Media.config.storage_path, attachment, 'original')
118
+ end
119
+ new_base_dir = File.dirname(File.dirname(new_path))
120
+ old_base_dir= File.dirname(File.dirname(old_path))
121
+ if File.directory?(old_base_dir)
122
+ FileUtils.mv(old_base_dir, new_base_dir)
92
123
  end
124
+ end
93
125
 
94
- def validate_permissions_s3
95
- if is_protected
96
- attachment.s3_object.acl = :private
97
- else
98
- attachment.s3_object.acl = :public_read
99
- end
126
+ def validate_permissions_s3
127
+ if is_protected
128
+ attachment.s3_object(:original).acl = :private
129
+ attachment.s3_object(:cropped).acl = :private unless !has_custom_crop?
130
+ else
131
+ attachment.s3_object(:original).acl = :public_read
132
+ attachment.s3_object(:cropped).acl = :public_read unless !has_custom_crop?
100
133
  end
134
+ end
135
+
101
136
  end
@@ -1,4 +1,5 @@
1
1
  <%=content_for :head do%>
2
- <%= stylesheet_link_tag "spud/admin/media/application" %>
3
- <%end%>
2
+ <%= javascript_include_tag 'spud/admin/media' %>
3
+ <%= stylesheet_link_tag "spud/admin/media" %>
4
+ <% end %>
4
5
  <%= render :template => 'layouts/spud/admin/detail' %>
@@ -0,0 +1,50 @@
1
+ <%= error_messages_for(@media) %>
2
+
3
+ <div id="spud_media_cropper_toolbar">
4
+ <%= form_for @media, :url => spud_admin_medium_path(@media) do |f| %>
5
+
6
+ <div class="spud_media_cropper_toolbar_item">
7
+ <%= label_tag :x %>
8
+ <%= f.text_field :crop_x %>
9
+ </div>
10
+
11
+ <div class="spud_media_cropper_toolbar_item">
12
+ <%= label_tag :y %>
13
+ <%= f.text_field :crop_y %>
14
+ </div>
15
+
16
+ <div class="spud_media_cropper_toolbar_item">
17
+ <%= label_tag :width %>
18
+ <%= f.text_field :crop_w %>
19
+ </div>
20
+
21
+ <div class="spud_media_cropper_toolbar_item">
22
+ <%= label_tag :height %>
23
+ <%= f.text_field :crop_h %>
24
+ </div>
25
+
26
+ <div class="spud_media_cropper_toolbar_item">
27
+ <%= label_tag :scale, 'Size' %>
28
+ <%= f.hidden_field :crop_s %>
29
+ <a href="#" id="spud_media_cropper_resize_up"><i class="icon-plus"></i></a>
30
+ <a href="#" id="spud_media_cropper_resize_down"><i class="icon-minus"></i></a>
31
+ </div>
32
+
33
+ <div id="spud_media_cropper_toolbar_actions">
34
+ <%= f.submit 'Crop', :id => 'spud_media_cropper_submit', :class => 'btn btn-mini btn-primary' %>
35
+ <%= link_to "Don't Crop", spud_admin_media_path, :class => 'btn btn-mini' %>
36
+ </div>
37
+ <% end %>
38
+ </div>
39
+
40
+ <div id="spud_media_cropper_outer">
41
+ <div id="spud_media_cropper">
42
+ <div id="spud_media_cropper_jcrop_container">
43
+ <%= image_tag @media.attachment_url(:original), :id => 'spud_media_cropper_image' %>
44
+ </div>
45
+ </div>
46
+ </div>
47
+
48
+ <script type="text/javascript">
49
+ $(window).load(Spud.Admin.Media.edit);
50
+ </script>
@@ -12,12 +12,15 @@
12
12
  </span>
13
13
 
14
14
  <span class="edit_controls">
15
+ <% if media.is_image? %>
16
+ <%= link_to 'Crop', edit_spud_admin_medium_path(media.id), :class => 'btn' %>
17
+ <% end %>
15
18
  <% if media.is_protected %>
16
19
  <%= link_to raw('<i class="icon-lock"></i> Protected'), set_access_spud_admin_medium_path(media.id, :protected => false), :method => :put, :class => 'btn' %>
17
20
  <% else %>
18
21
  <%= link_to 'Public', set_access_spud_admin_medium_path(media.id, :protected => true), :method => :put, :class => 'btn' %>
19
22
  <% end %>
20
- <%=link_to "Remove",spud_admin_medium_path(:id => media.id),:method => :delete,:class => 'btn btn-danger',:confirm => "Are you sure you want to remove this file?"%>
23
+ <%=link_to "Remove", spud_admin_medium_path(:id => media.id),:method => :delete,:class => 'btn btn-danger',:confirm => "Are you sure you want to remove this file?"%>
21
24
  </span>
22
25
 
23
26
  <br style="clear:both;"/>
data/config/routes.rb CHANGED
@@ -7,5 +7,6 @@ Rails.application.routes.draw do
7
7
  end
8
8
  end
9
9
 
10
+ #get '/media/protected/:id/:style/:filename' => 'ProtectedMedia#show', :as => 'protected_media'
10
11
  get Spud::Media.config.storage_url => 'ProtectedMedia#show', :as => 'protected_media'
11
12
  end
@@ -0,0 +1,9 @@
1
+ class AddCroppingToSpudMedia < ActiveRecord::Migration
2
+ def change
3
+ add_column :spud_media, :crop_x, :int, :default => 0
4
+ add_column :spud_media, :crop_y, :int, :default => 0
5
+ add_column :spud_media, :crop_w, :int
6
+ add_column :spud_media, :crop_h, :int
7
+ add_column :spud_media, :crop_s, :int, :default => 100
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Spud
2
2
  module Media
3
- VERSION = "0.9.0"
3
+ VERSION = "0.9.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spud_media
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-03 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70279704590120 !ruby/object:Gem::Requirement
16
+ requirement: &70312311481820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70279704590120
24
+ version_requirements: *70312311481820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: spud_core
27
- requirement: &70279704589000 !ruby/object:Gem::Requirement
27
+ requirement: &70312311252840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: 0.9.0
36
36
  type: :runtime
37
37
  prerelease: false
38
- version_requirements: *70279704589000
38
+ version_requirements: *70312311252840
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: paperclip
41
- requirement: &70279704603420 !ruby/object:Gem::Requirement
41
+ requirement: &70312311252020 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ! '>='
@@ -46,10 +46,10 @@ dependencies:
46
46
  version: '0'
47
47
  type: :runtime
48
48
  prerelease: false
49
- version_requirements: *70279704603420
49
+ version_requirements: *70312311252020
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: mysql2
52
- requirement: &70279704602440 !ruby/object:Gem::Requirement
52
+ requirement: &70312311251640 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - ! '>='
@@ -57,10 +57,10 @@ dependencies:
57
57
  version: '0'
58
58
  type: :development
59
59
  prerelease: false
60
- version_requirements: *70279704602440
60
+ version_requirements: *70312311251640
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rspec
63
- requirement: &70279704601520 !ruby/object:Gem::Requirement
63
+ requirement: &70312311251180 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ! '>='
@@ -68,10 +68,10 @@ dependencies:
68
68
  version: '0'
69
69
  type: :development
70
70
  prerelease: false
71
- version_requirements: *70279704601520
71
+ version_requirements: *70312311251180
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: rspec-rails
74
- requirement: &70279704600080 !ruby/object:Gem::Requirement
74
+ requirement: &70312311250760 !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements:
77
77
  - - ! '>='
@@ -79,10 +79,10 @@ dependencies:
79
79
  version: '0'
80
80
  type: :development
81
81
  prerelease: false
82
- version_requirements: *70279704600080
82
+ version_requirements: *70312311250760
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: factory_girl
85
- requirement: &70279704597640 !ruby/object:Gem::Requirement
85
+ requirement: &70312311250160 !ruby/object:Gem::Requirement
86
86
  none: false
87
87
  requirements:
88
88
  - - =
@@ -90,10 +90,10 @@ dependencies:
90
90
  version: 2.5.0
91
91
  type: :development
92
92
  prerelease: false
93
- version_requirements: *70279704597640
93
+ version_requirements: *70312311250160
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: mocha
96
- requirement: &70279704611520 !ruby/object:Gem::Requirement
96
+ requirement: &70312311249660 !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
99
  - - =
@@ -101,10 +101,10 @@ dependencies:
101
101
  version: 0.10.3
102
102
  type: :development
103
103
  prerelease: false
104
- version_requirements: *70279704611520
104
+ version_requirements: *70312311249660
105
105
  - !ruby/object:Gem::Dependency
106
106
  name: database_cleaner
107
- requirement: &70279704610980 !ruby/object:Gem::Requirement
107
+ requirement: &70312311249200 !ruby/object:Gem::Requirement
108
108
  none: false
109
109
  requirements:
110
110
  - - =
@@ -112,7 +112,7 @@ dependencies:
112
112
  version: 0.7.1
113
113
  type: :development
114
114
  prerelease: false
115
- version_requirements: *70279704610980
115
+ version_requirements: *70312311249200
116
116
  description: Spud Media allows you to upload files to your site and manage them in
117
117
  the spud administrative panel. It also uses paperclip and supports s3 storage
118
118
  email:
@@ -131,9 +131,17 @@ files:
131
131
  - app/assets/images/spud/admin/files_thumbs/txt_thumb.png
132
132
  - app/assets/images/spud/admin/files_thumbs/xls_thumb.png
133
133
  - app/assets/images/spud/admin/files_thumbs/zip_thumb.png
134
+ - app/assets/images/spud/admin/media/checkers.jpg
134
135
  - app/assets/images/spud/admin/media_thumb.png
135
136
  - app/assets/images/spud/admin/media_thumb@2x.png
136
- - app/assets/javascripts/spud/admin/media/application.js
137
+ - app/assets/javascripts/spud/admin/media.js
138
+ - app/assets/libs/jcrop/css/Jcrop.gif
139
+ - app/assets/libs/jcrop/css/jquery.Jcrop.css
140
+ - app/assets/libs/jcrop/css/jquery.Jcrop.min.css
141
+ - app/assets/libs/jcrop/js/jquery.color.js
142
+ - app/assets/libs/jcrop/js/jquery.Jcrop.js
143
+ - app/assets/libs/jcrop/js/jquery.Jcrop.min.js
144
+ - app/assets/libs/jcrop/js/jquery.min.js
137
145
  - app/assets/libs/tiny_mce/plugins/spud_media/blank.htm
138
146
  - app/assets/libs/tiny_mce/plugins/spud_media/css/template.css
139
147
  - app/assets/libs/tiny_mce/plugins/spud_media/editor_plugin.js
@@ -141,7 +149,7 @@ files:
141
149
  - app/assets/libs/tiny_mce/plugins/spud_media/js/template.js
142
150
  - app/assets/libs/tiny_mce/plugins/spud_media/langs/en_dlg.js
143
151
  - app/assets/libs/tiny_mce/plugins/spud_media/template.htm
144
- - app/assets/stylesheets/spud/admin/media/application.css
152
+ - app/assets/stylesheets/spud/admin/media.css
145
153
  - app/controllers/protected_media_controller.rb
146
154
  - app/controllers/spud/admin/media_controller.rb
147
155
  - app/helpers/protected_media_helper.rb
@@ -149,11 +157,13 @@ files:
149
157
  - app/helpers/spud/user_sessions_helper.rb
150
158
  - app/models/spud_media.rb
151
159
  - app/views/layouts/spud/admin/media/detail.html.erb
160
+ - app/views/spud/admin/media/edit.html.erb
152
161
  - app/views/spud/admin/media/index.html.erb
153
162
  - app/views/spud/admin/media/new.html.erb
154
163
  - config/routes.rb
155
164
  - db/migrate/20120101194256_create_spud_media.rb
156
165
  - db/migrate/20120501203325_add_protected_to_spud_media.rb
166
+ - db/migrate/20120508132153_add_cropping_to_spud_media.rb
157
167
  - lib/spud_media/configuration.rb
158
168
  - lib/spud_media/engine.rb
159
169
  - lib/spud_media/version.rb
@@ -204,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
204
214
  version: '0'
205
215
  segments:
206
216
  - 0
207
- hash: -3389397261865265902
217
+ hash: 1269224106861082199
208
218
  required_rubygems_version: !ruby/object:Gem::Requirement
209
219
  none: false
210
220
  requirements:
@@ -213,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
223
  version: '0'
214
224
  segments:
215
225
  - 0
216
- hash: -3389397261865265902
226
+ hash: 1269224106861082199
217
227
  requirements: []
218
228
  rubyforge_project:
219
229
  rubygems_version: 1.8.10
@@ -1,3 +0,0 @@
1
- img.size-50-thumb {
2
- height:50px !important;
3
- }