Skip to content

Commit

Permalink
feature: save origin file name
Browse files Browse the repository at this point in the history
  • Loading branch information
artofhuman committed Feb 9, 2016
1 parent 8b3f710 commit 31abc37
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/apress/amazon_assets/concerns/base_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def prepare_file_name
return if @file_name_prepared

if remote_file_name.present?
self.origin_file_name = remote_file_name
local.instance_write(:file_name, remote_file_name)
@file_name_prepared = true
return
Expand All @@ -116,6 +117,7 @@ def prepare_file_name
sold = SecureRandom.hex.first(4)
fname = "#{sold}__#{fname.presence || SecureRandom.hex.first(6)}#{extname}"

self.origin_file_name = local_file_name
local.instance_write(:file_name, fname)
@file_name_prepared = true
end
Expand Down
2 changes: 1 addition & 1 deletion apress-amazon_assets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec-given'
spec.add_development_dependency 'rspec-collection_matchers'
spec.add_development_dependency 'factory_girl_rails', '>= 3.1'
spec.add_development_dependency 'shoulda-matchers', '~> 2.0'
spec.add_development_dependency 'shoulda-matchers', '< 3'
spec.add_development_dependency 'webmock'
spec.add_development_dependency 'appraisal', '>= 1.0.2'
spec.add_development_dependency 'combustion', '>= 0.5.3'
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20151012110558_add_origin_file_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddOriginFileName < ActiveRecord::Migration
TABLES = [:amazon_s3_assets, :amazon_s3_public_assets]

def up
TABLES.each { |t| add_column t, :origin_file_name, :string }
end

def down
TABLES.each { |t| remove_column t, :origin_file_name }
end
end
Binary file added spec/fixtures/assets/картинка.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,41 @@
before { subject.local = file }

it { expect(subject.local_file_name).to match(/[0-9a-f]{4}__test\.txt/) }
it { expect(subject.origin_file_name).to eq 'test.txt' }

context 'when remote_file_name present' do
subject { described_class.new remote_file_name: 'remote.txt' }

it { expect(subject.local_file_name).to eq subject.remote_file_name }
it { expect(subject.origin_file_name).to eq 'remote.txt' }
end

context 'when not allowed symbols in file name' do
let(:file) { File.new('spec/fixtures/assets/test(123)') }

it { expect(subject.local_file_name).to match(/[0-9a-f]{4}__test123/) }
it { expect(subject.origin_file_name).to eq 'test(123)' }
end

context 'when no allowed symbols in file name' do
let(:file) { File.new('spec/fixtures/assets/###') }

it { expect(subject.local_file_name).to match(/[0-9a-f]{4}__[0-9a-f]{6}/) }
it { expect(subject.origin_file_name).to eq '###' }
end

context 'when cyrilic filename' do
let(:file) { File.new('spec/fixtures/assets/картинка.jpg') }

it { expect(subject.local_file_name).to match(/[0-9a-f]{4}__kartinka.jpg/) }
it { expect(subject.origin_file_name).to eq 'картинка.jpg' }
end

context 'when filename has minus sign' do
let(:file) { File.new('spec/fixtures/assets/test-test.txt') }

it { expect(subject.local_file_name).to match(/[0-9a-f]{4}__test\-test.txt/) }
it { expect(subject.origin_file_name).to eq 'test-test.txt' }
end

context 'when cyrilic filename' do
Expand Down

0 comments on commit 31abc37

Please sign in to comment.