Przeglądaj źródła

Merge pull request #1828 from overleaf/msm-fix-zip-upload-ignored-extensions-js

Fixes files with full name equals to an ignored extension being ignored (JS version)

GitOrigin-RevId: 94d84c2b3e6c7e7219397b0a982117f9c88bcf5d
Eric Mc Sween 7 lat temu
rodzic
commit
15db8d3711

+ 9 - 1
services/web/app/src/Features/Uploads/FileTypeManager.js

@@ -116,12 +116,20 @@ module.exports = FileTypeManager = {
     })
     })
   },
   },
 
 
+  getExtension(fileName) {
+    const nameSplit = fileName.split('.')
+    if (nameSplit.length < 2) {
+      return undefined
+    }
+    return nameSplit.pop()
+  },
+
   shouldIgnore(path, callback) {
   shouldIgnore(path, callback) {
     if (callback == null) {
     if (callback == null) {
       callback = function(error, result) {}
       callback = function(error, result) {}
     }
     }
     const name = Path.basename(path)
     const name = Path.basename(path)
-    let extension = name.split('.').slice(-1)[0]
+    let extension = this.getExtension(name)
     if (extension != null) {
     if (extension != null) {
       extension = extension.toLowerCase()
       extension = extension.toLowerCase()
     }
     }

+ 48 - 5
services/web/test/unit/src/Uploads/FileTypeManagerTests.js

@@ -12,8 +12,7 @@
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
  */
 const sinon = require('sinon')
 const sinon = require('sinon')
-const chai = require('chai')
-const should = chai.should()
+const expect = require('chai').expect
 const modulePath = '../../../../app/src/Features/Uploads/FileTypeManager.js'
 const modulePath = '../../../../app/src/Features/Uploads/FileTypeManager.js'
 const SandboxedModule = require('sandboxed-module')
 const SandboxedModule = require('sandboxed-module')
 const isUtf8 = require('is-utf8')
 const isUtf8 = require('is-utf8')
@@ -242,7 +241,7 @@ describe('FileTypeManager', function() {
       })
       })
     })
     })
 
 
-    return describe('when the file extension is non-text', function() {
+    describe('when the file extension is non-text', function() {
       it('should return .eps files as binary', function() {
       it('should return .eps files as binary', function() {
         return this.FileTypeManager.getType(
         return this.FileTypeManager.getType(
           'file.eps',
           'file.eps',
@@ -283,7 +282,11 @@ describe('FileTypeManager', function() {
     })
     })
   })
   })
 
 
-  return describe('shouldIgnore', function() {
+  describe('shouldIgnore', function() {
+    beforeEach(function() {
+      this.stats = {}
+    })
+
     it('should ignore tex auxiliary files', function() {
     it('should ignore tex auxiliary files', function() {
       return this.FileTypeManager.shouldIgnore('file.aux', (error, ignore) =>
       return this.FileTypeManager.shouldIgnore('file.aux', (error, ignore) =>
         ignore.should.equal(true)
         ignore.should.equal(true)
@@ -316,10 +319,50 @@ describe('FileTypeManager', function() {
       )
       )
     })
     })
 
 
-    return it('should ignore the case of the extension', function() {
+    it('should ignore the case of the extension', function() {
       return this.FileTypeManager.shouldIgnore('file.AUX', (error, ignore) =>
       return this.FileTypeManager.shouldIgnore('file.AUX', (error, ignore) =>
         ignore.should.equal(true)
         ignore.should.equal(true)
       )
       )
     })
     })
+
+    it('should not ignore files with an ignored extension as full name', function() {
+      this.stats.isDirectory = sinon.stub().returns(false)
+      const fileName = this.FileTypeManager.IGNORE_EXTENSIONS[0]
+      this.FileTypeManager.shouldIgnore(fileName, (error, ignore) =>
+        ignore.should.equal(false)
+      )
+    })
+
+    it('should not ignore directories with an ignored extension as full name', function() {
+      this.stats.isDirectory = sinon.stub().returns(true)
+      const fileName = this.FileTypeManager.IGNORE_EXTENSIONS[0]
+      this.FileTypeManager.shouldIgnore(fileName, (error, ignore) =>
+        ignore.should.equal(false)
+      )
+    })
+  })
+
+  describe('getExtension', function() {
+    it('should return the extension of a file name', function() {
+      expect(this.FileTypeManager.getExtension('example.doc')).to.equal('doc')
+    })
+
+    it('should return the extension with unmodified upper and lower case characters', function() {
+      expect(this.FileTypeManager.getExtension('example.TeX')).to.equal('TeX')
+    })
+
+    it('should return the extension of a file name with multiple dots in the name', function() {
+      expect(this.FileTypeManager.getExtension('example.test.doc')).to.equal(
+        'doc'
+      )
+    })
+
+    it('should return the rest of the string when the file name starts with dot', function() {
+      expect(this.FileTypeManager.getExtension('.example.doc')).to.equal('doc')
+    })
+
+    it('should return undefined when the file name has no extension', function() {
+      expect(this.FileTypeManager.getExtension('example')).to.equal(undefined)
+    })
   })
   })
 })
 })