diff --git a/CHANGELOG.md b/CHANGELOG.md index a32215c4c..538d84576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Remove `isCourseTeacherOrAdmin` and `isMemberOfCourse`from UserService. [#731](https://github.com/h-da/geli/issues/731) - Refactored save mechanism of unit edit form. [#532](https://github.com/h-da/geli/issues/532) - Moved the 'create course' into a Dialog. [#725](https://github.com/h-da/geli/issues/725) +- Use Path-Lib to extract extension from filename. [#773](https://github.com/utetrapp/geli/pull/773) - Add the possibility to add files directly in the file unit [#728](https://github.com/utetrapp/geli/issues/728) ### Fixed diff --git a/api/src/controllers/MediaController.ts b/api/src/controllers/MediaController.ts index 5f8f1ead5..4d14888f0 100644 --- a/api/src/controllers/MediaController.ts +++ b/api/src/controllers/MediaController.ts @@ -10,6 +10,7 @@ import crypto = require('crypto'); import config from '../config/main'; const multer = require('multer'); +const path = require('path'); const uploadOptions = { storage: multer.diskStorage({ @@ -17,10 +18,8 @@ const uploadOptions = { cb(null, config.uploadFolder); }, filename: (req: any, file: any, cb: any) => { - const extPos = file.originalname.lastIndexOf('.'); - const ext = (extPos !== -1) ? `.${file.originalname.substr(extPos + 1).toLowerCase()}` : ''; crypto.pseudoRandomBytes(16, (err, raw) => { - cb(err, err ? undefined : `${raw.toString('hex')}${ext}`); + cb(err, err ? undefined : raw.toString('hex') + path.extname(file.originalname)); }); } }), diff --git a/api/test/controllers/media.ts b/api/test/controllers/media.ts index acfbb2b9a..1c297aa7c 100644 --- a/api/test/controllers/media.ts +++ b/api/test/controllers/media.ts @@ -191,8 +191,41 @@ describe('Media', async () => { name: 'root' }).save(); - const testFileName = fs.readdirSync('./')[0]; - const testFile = fs.readFileSync(testFileName); + const testFileName = 'test_file.txt'; + const testFile = fs.readFileSync('./test/resources/' + testFileName); + + const result = await chai.request(app) + .post(`${BASE_URL}/file/${rootDirectory._id}`) + .set('Authorization', `JWT ${JwtUtils.generateToken(teacher)}`) + .attach('file', testFile, testFileName) + .catch((err) => err.response); + + result.status.should.be.equal(200, + 'could not upload file' + + ' -> ' + result.body.message); + result.body.__v.should.equal(0); + should.exist(result.body._id); + should.exist(result.body.mimeType); + should.exist(result.body.size); + should.exist(result.body.link); + result.body.name.should.be.equal(testFileName); + + const updatedRoot = (await Directory.findById(rootDirectory)); + updatedRoot.files.should.be.instanceOf(Array) + .and.have.lengthOf(1) + .and.contains(result.body._id); + }); + + + it('should upload a file without extension', async () => { + const teacher = await FixtureUtils.getRandomTeacher(); + + const rootDirectory = await new Directory({ + name: 'root' + }).save(); + + const testFileName = 'test_file_without_extension'; + const testFile = fs.readFileSync('./test/resources/' + testFileName); const result = await chai.request(app) .post(`${BASE_URL}/file/${rootDirectory._id}`) @@ -383,5 +416,27 @@ describe('Media', async () => { should.not.exist(await File.findById(file)); fs.existsSync(config.uploadFolder + '/test.file').should.be.equal(false); }); + + it('should fail when directory not found', async () => { + const teacher = await FixtureUtils.getRandomTeacher(); + + const result = await chai.request(app) + .del(`${BASE_URL}/directory/507f1f77bcf86cd799439011`) + .set('Authorization', `JWT ${JwtUtils.generateToken(teacher)}`) + .catch((err) => err.response); + + result.status.should.be.equal(404); + }); + + it('should fail when file not found', async () => { + const teacher = await FixtureUtils.getRandomTeacher(); + + const result = await chai.request(app) + .del(`${BASE_URL}/file/507f1f77bcf86cd799439011`) + .set('Authorization', `JWT ${JwtUtils.generateToken(teacher)}`) + .catch((err) => err.response); + + result.status.should.be.equal(404); + }); }); }); diff --git a/api/test/resources/test_file.txt b/api/test/resources/test_file.txt new file mode 100644 index 000000000..0c09a2b57 --- /dev/null +++ b/api/test/resources/test_file.txt @@ -0,0 +1 @@ +Bavaria ipsum dolor sit amet Ramasuri resch Haferl, naa oba Leonhardifahrt des is hoid aso. Semmlkneedl midanand Schuabladdla hoid nackata wolln Blosmusi Graudwiggal Semmlkneedl sei Bladl! Haferl auffi hi nackata Biazelt nomoi Lewakaas vui Kaiwe pfiad de Spezi! Des is a gmahde Wiesn nia om auf’n Gipfe Spezi, i mechad dee Schwoanshaxn: Guad greaßt eich nachad wui imma hogg ma uns zamm Hemad gschmeidig gelbe Rüam weida! No a Maß Marterl wuid a fescha Bua, i sog ja nix, i red ja bloß. Auf der Oim, da gibt’s koa Sünd obandeln griaß God beinand Vergeltsgott Spuiratz, Meidromml? Di da Kini koa fias dringma aweng des wiad a Mordsgaudi Prosd do: I mechad dee Schwoanshaxn do sammawiedaguad, i mechad dee Schwoanshaxn a ganze Hoiwe großherzig do legst di nieda. Damischa Spezi Weibaleid auszutzeln bittschön kimmt Wurschtsolod und sei, ozapfa Mamalad. diff --git a/api/test/resources/test_file_without_extension b/api/test/resources/test_file_without_extension new file mode 100644 index 000000000..992f28ea0 --- /dev/null +++ b/api/test/resources/test_file_without_extension @@ -0,0 +1 @@ +Bavaria ipsum dolor sit amet wui Jodler wo hi Schorsch zünftig nois kimmt kumm geh Landla des wiad a Mordsgaudi. Ghupft wia gsprunga soi Weißwiaschd unbandig wiavui i daad a bravs Resi Kuaschwanz hob i an Suri? Glacht Gams sei mehra nomoi, wea nia ausgähd, kummt nia hoam jedza hod Brezn. An d’ oa heid gfoids ma sagrisch guad wui jo leck mi mechad: Measi hogg ma uns zamm Guglhupf Spotzerl, wui. Ebba a dringma aweng des woaß is des liab von Freibia so schee gwiss? Wui ham Fingahaggln, vui huift vui pfundig kloan a bissal wos gehd ollaweil i hob di liab. Wea ko, dea ko is is aba helfgod gor! Hallelujah sog i, luja Goaßmaß umma hod auf gehds beim Schichtl, gscheit. Wia da Buachbinda Wanninger dahoam mechad ham Graudwiggal wia da Buachbinda Wanninger Lewakaas Buam.