Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Use path for extension #773

Merged
merged 7 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions api/src/controllers/MediaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import crypto = require('crypto');
import config from '../config/main';

const multer = require('multer');
const path = require('path');

const uploadOptions = {
storage: multer.diskStorage({
destination: (req: any, file: any, cb: any) => {
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));
});
}
}),
Expand Down
59 changes: 57 additions & 2 deletions api/test/controllers/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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);
});
});
});
1 change: 1 addition & 0 deletions api/test/resources/test_file.txt
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions api/test/resources/test_file_without_extension
Original file line number Diff line number Diff line change
@@ -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.