[Git][noosfero/noosfero][master] 3 commits: attachment_fu: allow utf8 characters on filename

Rodrigo Souto gitlab at mg.gitlab.com
Thu Jun 29 09:56:45 BRT 2017


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
83f5d54e by Braulio Bhavamitra at 2017-06-27T06:18:25+00:00
attachment_fu: allow utf8 characters on filename

- - - - -
94b37f0f by Gabriel Silva at 2017-06-28T01:18:14+00:00
Extracts sanitize_filename to concern

Signed-off-by: Gabriel Silva <gabriel93.silva at gmail.com>

- - - - -
f335ae3a by Rodrigo Souto at 2017-06-29T12:55:17+00:00
Merge branch 'brauliobo/noosfero-attachment-filenames-utf8' into 'master'

Keeps special characters on filenames

Closes #157

See merge request !1258
- - - - -


9 changed files:

- app/models/application_record.rb
- app/models/concerns/upload_sanitizer.rb
- app/models/image.rb
- app/models/reported_image.rb
- app/models/thumbnail.rb
- app/models/uploaded_file.rb
- config/initializers/active_record_extensions.rb
- + test/fixtures/files/Relação com Espaço.txt
- test/unit/uploaded_file_test.rb


Changes:

=====================================
app/models/application_record.rb
=====================================
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -1,7 +1,6 @@
 class ApplicationRecord < ActiveRecord::Base
 
   extend PostgresqlAttachmentFu::ClassMethods
-  include UploadSanitizer
 
   self.abstract_class       = true
   self.store_full_sti_class = true


=====================================
app/models/concerns/upload_sanitizer.rb
=====================================
--- a/app/models/concerns/upload_sanitizer.rb
+++ b/app/models/concerns/upload_sanitizer.rb
@@ -1,12 +1,14 @@
 module UploadSanitizer
-  def self.included(base)
-    base.extend(ClassMethods)
-  end
+  extend ActiveSupport::Concern
+
+  included do
+    before_create { |file| file.filename = Environment.verify_filename(file.filename) }
 
-  module ClassMethods
-    def sanitize_filename
-      before_create { |file| file.filename = Environment.verify_filename(file.filename) }
+    def sanitize_filename filename
+      # let accents and other utf8, but remotes the extension
+      # overwrite vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb
+      filename
     end
   end
-end
 
+end


=====================================
app/models/image.rb
=====================================
--- a/app/models/image.rb
+++ b/app/models/image.rb
@@ -1,5 +1,7 @@
 class Image < ApplicationRecord
 
+  include UploadSanitizer
+
   attr_accessible :uploaded_data, :label, :remove_image
   attr_accessor :remove_image
 
@@ -9,8 +11,6 @@ class Image < ApplicationRecord
     Image.attachment_options[:max_size]
   end
 
-  sanitize_filename
-
   has_attachment :content_type => :image,
                  :storage => :file_system,
                  :path_prefix => 'public/image_uploads',


=====================================
app/models/reported_image.rb
=====================================
--- a/app/models/reported_image.rb
+++ b/app/models/reported_image.rb
@@ -1,4 +1,7 @@
 class ReportedImage < ApplicationRecord
+
+  include UploadSanitizer
+
   belongs_to :abuse_report
 
   validates_presence_of :abuse_report


=====================================
app/models/thumbnail.rb
=====================================
--- a/app/models/thumbnail.rb
+++ b/app/models/thumbnail.rb
@@ -1,5 +1,7 @@
 class Thumbnail < ApplicationRecord
 
+  include UploadSanitizer
+
   attr_accessible :uploaded_data
   # mass assigned by attachment_fu
   attr_accessible :content_type, :filename, :thumbnail_resize_options, :thumbnail, :parent_id
@@ -8,8 +10,6 @@ class Thumbnail < ApplicationRecord
     :content_type => :image, :max_size => UploadedFile.max_size, processor: 'Rmagick'
   validates_as_attachment
 
-  sanitize_filename
-
   postgresql_attachment_fu
 
 end


=====================================
app/models/uploaded_file.rb
=====================================
--- a/app/models/uploaded_file.rb
+++ b/app/models/uploaded_file.rb
@@ -7,6 +7,8 @@ require 'sdbm' unless RUBY_ENGINE == 'jruby'
 
 class UploadedFile < Article
 
+  include UploadSanitizer
+
   attr_accessible :uploaded_data, :title
 
   include Noosfero::Plugin::HotSpot
@@ -41,8 +43,6 @@ class UploadedFile < Article
     self.name = value
   end
 
-  sanitize_filename
-
   before_create do |uploaded_file|
     uploaded_file.is_image = true if uploaded_file.image?
   end


=====================================
config/initializers/active_record_extensions.rb
=====================================
--- a/config/initializers/active_record_extensions.rb
+++ b/config/initializers/active_record_extensions.rb
@@ -1,5 +1,3 @@
-require 'upload_sanitizer'
-
 module ActiveRecordExtension
 
   module ClassMethods


=====================================
test/fixtures/files/Relação com Espaço.txt
=====================================
--- /dev/null
+++ "b/test/fixtures/files/Rela\303\247\303\243o com Espa\303\247o.txt"
@@ -0,0 +1 @@
+test


=====================================
test/unit/uploaded_file_test.rb
=====================================
--- a/test/unit/uploaded_file_test.rb
+++ b/test/unit/uploaded_file_test.rb
@@ -387,4 +387,9 @@ class UploadedFileTest < ActiveSupport::TestCase
     dbm.close
   end
 
+  should 'keep special characters on filenames' do
+    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/Relação com Espaço.txt', 'image/png'), :profile => profile)
+    assert_equal file.name, 'Relação com Espaço.txt'
+  end
+
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/341a89222210a633c44cdd14b286889930d55e58...f335ae3a65b792cdf18f4107d13ed763ab37e2f2

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/341a89222210a633c44cdd14b286889930d55e58...f335ae3a65b792cdf18f4107d13ed763ab37e2f2
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170629/d5c04036/attachment-0001.html>


More information about the Noosfero-dev mailing list