Skip to content
Snippets Groups Projects
Commit 8dae1364 authored by kelly.nguyen's avatar kelly.nguyen
Browse files

zip all assignments and exercise and add route user

parent 5910d45b
Branches
No related tags found
No related merge requests found
Pipeline #33396 failed
...@@ -80,7 +80,8 @@ class AssignmentRoutes implements RoutesManager { ...@@ -80,7 +80,8 @@ class AssignmentRoutes implements RoutesManager {
backend.post('/assignments/:assignmentNameOrUrl/corrections', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentAddCorrigeValidator), this.linkUpdateAssignmentCorrection(false).bind(this) as RequestHandler); backend.post('/assignments/:assignmentNameOrUrl/corrections', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentAddCorrigeValidator), this.linkUpdateAssignmentCorrection(false).bind(this) as RequestHandler);
backend.patch('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentUpdateCorrigeValidator), this.linkUpdateAssignmentCorrection(true).bind(this) as RequestHandler); backend.patch('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentUpdateCorrigeValidator), this.linkUpdateAssignmentCorrection(true).bind(this) as RequestHandler);
backend.delete('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unlinkAssignmentCorrection.bind(this) as RequestHandler); backend.delete('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unlinkAssignmentCorrection.bind(this) as RequestHandler);
backend.get('/assignments/:assignmentNameOrUrl/export', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.exportLightAssignment.bind(this) as RequestHandler); backend.get('/assignments/:assignmentNameOrUrl/export/:folderName', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.exportLightAssignment.bind(this) as RequestHandler);
backend.get('/assignments/:folderName/zip', SecurityMiddleware.check(false, SecurityCheckType.ASSIGNMENT_STAFF), this.zipAssignment.bind(this) as RequestHandler);
} }
// Get an assignment by its name or gitlab url // Get an assignment by its name or gitlab url
...@@ -301,27 +302,9 @@ class AssignmentRoutes implements RoutesManager { ...@@ -301,27 +302,9 @@ class AssignmentRoutes implements RoutesManager {
return req.session.sendResponse(res, StatusCodes.OK); return req.session.sendResponse(res, StatusCodes.OK);
} }
// private async exportLightAssignment(req: express.Request, res: express.Response) {
// const resDl = await GitlabManager.archiveRepository(req.boundParams.assignment!.gitlabId).then(async archive => {
// const buffer = Buffer.from(await archive.arrayBuffer());
// const zipName = req.boundParams.assignment?.name.replace(/ /g, "_") + '.tar.gz';
// fs.writeFile(zipName, buffer, (err) => {
// if (err) {
// console.error('Error saving archive:', err);
// } else {
// console.log('Archive saved successfully!');
// }
// });
// }).catch(error => {
// console.error('Error fetching archive:', error);
// });
// return req.session.sendResponse(res, StatusCodes.OK, resDl);
// }
private async exportLightAssignment(req: express.Request, res: express.Response) { private async exportLightAssignment(req: express.Request, res: express.Response) {
try { try {
const folderName = 'tmp2'; const folderName = req.params.folderName;
const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
// const parentDir = path.join('/tmp', `export_${timestamp}`); // const parentDir = path.join('/tmp', `export_${timestamp}`);
const parentDir = path.join('/tmp', folderName); const parentDir = path.join('/tmp', folderName);
...@@ -332,15 +315,13 @@ class AssignmentRoutes implements RoutesManager { ...@@ -332,15 +315,13 @@ class AssignmentRoutes implements RoutesManager {
const zipName = req.boundParams.assignment?.name.replace(/ /g, "_") + '.tar.gz'; const zipName = req.boundParams.assignment?.name.replace(/ /g, "_") + '.tar.gz';
const zipPath = path.join(parentDir, zipName); const zipPath = path.join(parentDir, zipName);
fs.writeFileSync(zipPath, buffer); fs.writeFile(zipPath, buffer, (err) => {
// fs.writeFile(zipPath, buffer, (err) => { if (err) {
// if (err) { console.error('Error saving archive:', err);
// console.error('Error saving archive:', err); } else {
// } else { console.log(`Archive ${zipName} saved successfully!`);
// console.log('Archive saved successfully!'); }
// } });
// });
// console.log('Archive saved successfully!');
// const finalZipPath = path.join('/tmp', `export_${timestamp}.zip`); // const finalZipPath = path.join('/tmp', `export_${timestamp}.zip`);
const finalZipPath = path.join('/tmp', `${folderName}.zip`); const finalZipPath = path.join('/tmp', `${folderName}.zip`);
...@@ -352,9 +333,8 @@ class AssignmentRoutes implements RoutesManager { ...@@ -352,9 +333,8 @@ class AssignmentRoutes implements RoutesManager {
archiveZip.pipe(output); archiveZip.pipe(output);
archiveZip.directory(parentDir, false); archiveZip.directory(parentDir, false);
await archiveZip.finalize(); await archiveZip.finalize();
// output.on('close', () => {
res.download(finalZipPath, `${folderName}.zip`, (err) => { res.download(finalZipPath, (err) => {
// res.download(finalZipPath, `export_${timestamp}.zip`, (err) => {
if (err) { if (err) {
console.error('Error sending zip:', err); console.error('Error sending zip:', err);
res.status(500).send('Error sending zip'); res.status(500).send('Error sending zip');
...@@ -365,12 +345,24 @@ class AssignmentRoutes implements RoutesManager { ...@@ -365,12 +345,24 @@ class AssignmentRoutes implements RoutesManager {
// fs.unlinkSync(finalZipPath); // fs.unlinkSync(finalZipPath);
} }
}); });
// });
} catch (error) { } catch (error) {
console.error('Error exporting assignment:', error); console.error('Error exporting assignment:', error);
res.status(500).send('Error exporting assignment'); res.status(500).send('Error exporting assignment');
} }
} }
private async zipAssignment(req : express.Request, res : express.Response) {
const folderName = req.params.folderName;
const zipPath = path.join('/tmp', `${folderName}.zip`);
res.download(zipPath, (err) => {
if (err) {
logger.error(`Error sending the zip : ${err}`);
} else {
console.log("Zip sent successfully");
}
});
}
} }
......
...@@ -26,6 +26,7 @@ import ExerciseManager from '../managers/ExerciseManager.js'; ...@@ -26,6 +26,7 @@ import ExerciseManager from '../managers/ExerciseManager.js';
import * as Gitlab from '@gitbeaker/rest'; import * as Gitlab from '@gitbeaker/rest';
import GitlabTreeFileType from '../shared/types/Gitlab/GitlabTreeFileType.js'; import GitlabTreeFileType from '../shared/types/Gitlab/GitlabTreeFileType.js';
import { GitbeakerRequestError } from '@gitbeaker/requester-utils'; import { GitbeakerRequestError } from '@gitbeaker/requester-utils';
import archiver from 'archiver';
class ExerciseRoutes implements RoutesManager { class ExerciseRoutes implements RoutesManager {
...@@ -70,6 +71,8 @@ class ExerciseRoutes implements RoutesManager { ...@@ -70,6 +71,8 @@ class ExerciseRoutes implements RoutesManager {
backend.get('/exercises/:exerciseIdOrUrl/assignment', SecurityMiddleware.check(false, SecurityCheckType.EXERCISE_SECRET), this.getAssignment.bind(this) as RequestHandler); backend.get('/exercises/:exerciseIdOrUrl/assignment', SecurityMiddleware.check(false, SecurityCheckType.EXERCISE_SECRET), this.getAssignment.bind(this) as RequestHandler);
backend.post('/exercises/:exerciseIdOrUrl/results', SecurityMiddleware.check(false, SecurityCheckType.EXERCISE_SECRET), ParamsValidatorMiddleware.validate(this.resultValidator), this.createResult.bind(this) as RequestHandler); backend.post('/exercises/:exerciseIdOrUrl/results', SecurityMiddleware.check(false, SecurityCheckType.EXERCISE_SECRET), ParamsValidatorMiddleware.validate(this.resultValidator), this.createResult.bind(this) as RequestHandler);
backend.get('/exercises/:exerciseIdOrUrl/export/:folderName', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.exportLightExercise.bind(this) as RequestHandler);
backend.get('/exercises/:folderName/zip', SecurityMiddleware.check(false, SecurityCheckType.ASSIGNMENT_STAFF), this.zipExercise.bind(this) as RequestHandler);
} }
private getExerciseName(assignment: Assignment, members: Array<Gitlab.UserSchema>, suffix: number): string { private getExerciseName(assignment: Assignment, members: Array<Gitlab.UserSchema>, suffix: number): string {
...@@ -255,6 +258,60 @@ class ExerciseRoutes implements RoutesManager { ...@@ -255,6 +258,60 @@ class ExerciseRoutes implements RoutesManager {
req.session.sendResponse(res, StatusCodes.OK); req.session.sendResponse(res, StatusCodes.OK);
} }
private async exportLightExercise(req: express.Request, res: express.Response) {
try {
const folderName = req.params.folderName;
const parentDir = path.join('/tmp', folderName);
fs.mkdirSync(parentDir, { recursive: true });
const archive = await GitlabManager.archiveRepository(req.boundParams.exercise!.gitlabId);
const buffer = Buffer.from(await archive.arrayBuffer());
const zipName = req.boundParams.exercise?.name.replace(/ /g, "_") + '.tar.gz';
const zipPath = path.join(parentDir, zipName);
fs.writeFile(zipPath, buffer, (err) => {
if (err) {
console.error('Error saving archive:', err);
} else {
console.log(`Archive ${zipName} saved successfully!`);
}
});
const finalZipPath = path.join('/tmp', `${folderName}.zip`);
const output = fs.createWriteStream(finalZipPath);
const archiveZip = archiver('zip', {
zlib: { level: 9 } // Compression maximale
});
archiveZip.pipe(output);
archiveZip.directory(parentDir, false);
await archiveZip.finalize();
res.download(finalZipPath, (err) => {
if (err) {
console.error('Error sending zip:', err);
res.status(500).send('Error sending zip');
} else {
console.log('Zip sent successfully!');
}
});
} catch (error) {
console.error('Error exporting assignment:', error);
res.status(500).send('Error exporting assignment');
}
}
private async zipExercise(req : express.Request, res : express.Response) {
const folderName = req.params.folderName;
const zipPath = path.join('/tmp', `${folderName}.zip`);
res.download(zipPath, (err) => {
if (err) {
logger.error(`Error sending the zip : ${err}`);
} else {
console.log("Zip sent successfully");
}
});
}
} }
......
...@@ -11,6 +11,7 @@ class UserRoutes implements RoutesManager { ...@@ -11,6 +11,7 @@ class UserRoutes implements RoutesManager {
backend.get('/users', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.getUsers.bind(this) as RequestHandler); backend.get('/users', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.getUsers.bind(this) as RequestHandler);
backend.patch('/users/:userId/role', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.changeRole.bind(this) as RequestHandler); backend.patch('/users/:userId/role', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.changeRole.bind(this) as RequestHandler);
backend.get('/users/:userId/assignments', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), this.getUsersAssignments.bind(this) as RequestHandler); backend.get('/users/:userId/assignments', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), this.getUsersAssignments.bind(this) as RequestHandler);
backend.get('/users/:userId/exercise', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), this.getUsersExercise.bind(this) as RequestHandler);
} }
private async getUsers(req: express.Request, res: express.Response) { private async getUsers(req: express.Request, res: express.Response) {
...@@ -25,8 +26,12 @@ class UserRoutes implements RoutesManager { ...@@ -25,8 +26,12 @@ class UserRoutes implements RoutesManager {
id: id, id: id,
}, },
include: { include: {
assignments: true, assignments: true
// assignments: {
// where: {
// deleted: false
// }
// }
}, // Include the assignments related to the user }, // Include the assignments related to the user
}); });
return req.session.sendResponse(res, StatusCodes.OK, user); return req.session.sendResponse(res, StatusCodes.OK, user);
...@@ -52,6 +57,19 @@ class UserRoutes implements RoutesManager { ...@@ -52,6 +57,19 @@ class UserRoutes implements RoutesManager {
return req.session.sendResponse(res, StatusCodes.FORBIDDEN); return req.session.sendResponse(res, StatusCodes.FORBIDDEN);
} }
} }
private async getUsersExercise(req: express.Request, res: express.Response) {
const id = +req.params.userId;
const user = await db.user.findUnique({
where: {
id: id,
},
include: {
exercises: true
}, // Include the assignments related to the user
});
return req.session.sendResponse(res, StatusCodes.OK, user);
}
} }
export default new UserRoutes(); export default new UserRoutes();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment