Просмотр исходного кода

add missing SafePath.clean function

Brian Gough 8 лет назад
Родитель
Сommit
c6f74d24f1

+ 7 - 0
services/web/app/coffee/Features/Project/SafePath.coffee

@@ -22,6 +22,13 @@ MAX_PATH = 1024 # Maximum path length, in characters. This is fairly arbitrary.
 
 
 module.exports = SafePath =
 module.exports = SafePath =
 
 
+	clean: (filename) ->
+		filename = filename.replace BADCHAR_RX, '_'
+		# for BADFILE_RX replace any matches with an equal number of underscores
+		filename = filename.replace BADFILE_RX, (match) -> 
+			return new Array(match.length + 1).join("_")
+		return filename
+
 	isCleanFilename: (filename) ->
 	isCleanFilename: (filename) ->
 		return SafePath.isAllowedLength(filename) &&
 		return SafePath.isAllowedLength(filename) &&
 			not filename.match(BADCHAR_RX) &&
 			not filename.match(BADCHAR_RX) &&

+ 34 - 0
services/web/test/unit/coffee/Project/SafePathTests.coffee

@@ -87,3 +87,37 @@ describe 'SafePath', ->
 		it 'should not accept an empty path', ->
 		it 'should not accept an empty path', ->
 			result = @SafePath.isAllowedLength ''
 			result = @SafePath.isAllowedLength ''
 			result.should.equal false
 			result.should.equal false
+	
+	describe 'clean', ->
+		it 'should not modify a valid filename', ->
+			result = @SafePath.clean 'main.tex'
+			result.should.equal 'main.tex'
+
+		it 'should replace invalid characters with _', ->
+			result = @SafePath.clean 'foo/bar*/main.tex'
+			result.should.equal 'foo_bar__main.tex'
+			
+		it 'should replace "." with "_"', ->
+			result = @SafePath.clean '.'
+			result.should.equal '_'
+
+		it 'should replace ".." with "__"', ->
+			result = @SafePath.clean '..'
+			result.should.equal '__'
+
+		it 'should replace a single trailing space with _', ->
+			result = @SafePath.clean 'foo '
+			result.should.equal 'foo_'
+
+		it 'should replace a multiple trailing spaces with ___', ->
+			result = @SafePath.clean 'foo  '
+			result.should.equal 'foo__'
+
+		it 'should replace a single leading space with _', ->
+			result = @SafePath.clean ' foo'
+			result.should.equal '_foo'
+
+		it 'should replace a multiple leading spaces with ___', ->
+			result = @SafePath.clean '  foo'
+			result.should.equal '__foo'		
+