Skip to content

Commit 9531c16

Browse files
authored
Drop trailing slash of prefix in #get_local_files (#425)
The local filesystem glob in `AssetSync::Storage#get_local_files` uses fuzzy matching when `config.prefix` is present. This can present a problem in some cases, as it doesn't allow for distinguishing between (e.g.) a folder called `assets/` and another folder called `assets-temp/`. A situation could arise where the latter folder has thousands/millions of files and we mistakenly publish local-only files, or worse we could clobber another directory in the bucket managed in a completely different context. This change allows developers to be more specific in their `config.prefix` by using a trailing slash for their folder name.
2 parents 4deb6ff + 923417f commit 9531c16

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/asset_sync/storage.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def get_local_files
171171

172172
log "Using: Directory Search of #{path}/#{self.config.assets_prefix}"
173173
Dir.chdir(path) do
174-
to_load = self.config.assets_prefix.present? ? "#{self.config.assets_prefix}/**/**" : '**/**'
174+
to_load = self.config.assets_prefix.present? ? File.join(self.config.assets_prefix, '/**/**') : '**/**'
175175
Dir[to_load]
176176
end
177177
end

spec/unit/storage_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require File.dirname(__FILE__) + '/../spec_helper'
2+
require 'fileutils'
23

34
describe AssetSync::Storage do
45
include_context "mock Rails without_yml"
@@ -490,4 +491,52 @@ def check_file(file)
490491
end
491492
end
492493
end
494+
495+
describe '#get_local_files' do
496+
around(:each) do |example|
497+
Dir.mktmpdir do |public_path|
498+
@public_path = public_path
499+
example.call
500+
end
501+
end
502+
503+
before(:each) do
504+
@config = AssetSync::Config.new
505+
@config.public_path = @public_path
506+
@config.prefix = 'assets'
507+
@storage = AssetSync::Storage.new(@config)
508+
509+
Dir.mkdir("#{@public_path}/assets")
510+
end
511+
512+
context 'with empty directory' do
513+
it 'has no files' do
514+
expect(@storage.get_local_files).to eq([])
515+
end
516+
end
517+
518+
context 'with non-empty directory' do
519+
before(:each) do
520+
FileUtils.touch("#{@public_path}/assets/application.js")
521+
end
522+
523+
it 'lists available files' do
524+
expect(@storage.get_local_files).to eq([
525+
'assets/application.js'
526+
])
527+
end
528+
529+
context 'with trailing slash on asset prefix' do
530+
before(:each) do
531+
@config.prefix = 'assets/'
532+
end
533+
534+
it 'lists available files with single slashes' do
535+
expect(@storage.get_local_files).to eq([
536+
'assets/application.js'
537+
])
538+
end
539+
end
540+
end
541+
end
493542
end

0 commit comments

Comments
 (0)