From 453d13f9886698f431392e98afdfdd430a12c70b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Tue, 1 Aug 2023 02:19:03 +0200
Subject: [PATCH] Add step 1 and 2 (download enonce and immutable files)

---
 ExerciceChecker/.idea/vcs.xml                 |  2 +
 ExerciceChecker/src/app.ts                    | 44 ++++++++++++++-----
 ExerciceChecker/src/config/Config.ts          | 19 ++++++++
 .../src/managers/DojoBackendManager.ts        | 12 +++++
 ExerciceChecker/src/managers/HttpManager.ts   |  5 +++
 ExerciceChecker/src/shared                    |  2 +-
 ExerciceChecker/src/sharedByClients           |  2 +-
 7 files changed, 73 insertions(+), 13 deletions(-)
 create mode 100644 ExerciceChecker/src/config/Config.ts

diff --git a/ExerciceChecker/.idea/vcs.xml b/ExerciceChecker/.idea/vcs.xml
index 6c0b863..d86e73b 100644
--- a/ExerciceChecker/.idea/vcs.xml
+++ b/ExerciceChecker/.idea/vcs.xml
@@ -2,5 +2,7 @@
 <project version="4">
   <component name="VcsDirectoryMappings">
     <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+    <mapping directory="$PROJECT_DIR$/src/shared" vcs="Git" />
+    <mapping directory="$PROJECT_DIR$/src/sharedByClients" vcs="Git" />
   </component>
 </project>
\ No newline at end of file
diff --git a/ExerciceChecker/src/app.ts b/ExerciceChecker/src/app.ts
index a4f2ffd..84168a5 100644
--- a/ExerciceChecker/src/app.ts
+++ b/ExerciceChecker/src/app.ts
@@ -4,20 +4,42 @@ const path = require('node:path');
 require('dotenv').config({ path: path.join(__dirname, '../.env') });
 require('./shared/helpers/TypeScriptExtensions'); // ATTENTION : This line MUST be the second of this file
 
-import chalk       from 'chalk';
-import HttpManager from './managers/HttpManager';
+import * as fs            from 'fs';
+import chalk              from 'chalk';
+import HttpManager        from './managers/HttpManager';
+import DojoBackendManager from './managers/DojoBackendManager';
+import Config             from './config/Config';
 
 
-HttpManager.registerAxiosInterceptor();
+(async () => {
+    HttpManager.registerAxiosInterceptor();
 
 
-console.log(chalk.blue('Dojo Exercice Checker'));
+    console.log(chalk.blue('Dojo Exercice Checker'));
 
 
-// Step 1: Read the dojo enonce file from the enonce repository
-// Step 2: Download immutables files (maybe throw or show an error if the files have been modified ?) - Can be merged with step 1
-// Step 3: Run docker-compose file
-// Step 4: Wait the end of the execution of the result container
-// Step 5: Get the result from the volume
-// Step 6: Check content requirements and content size
-// Step 7: Upload and show the results
\ No newline at end of file
+    /*
+     Step 1 & 2:
+     -   Read the dojo enonce file from the enonce repository
+     -   Download immutables files (maybe throw or show an error if the files have been modified ?)
+     */
+    const exerciceEnonce = await DojoBackendManager.getExerciceEnonce();
+    if ( !exerciceEnonce ) {
+        console.error(chalk.red(`Error while getting the exercice's enonce`));
+        process.exit(1);
+    }
+
+    exerciceEnonce.immutable.forEach(immutableFile => {
+        const filePath = `${ Config.filesFolder }/${ immutableFile.file_path }`.replace(/\/\//g, '/');
+        fs.mkdirSync(path.dirname(filePath), { recursive: true });
+        fs.writeFileSync(filePath, immutableFile.content, { encoding: 'base64' });
+    });
+
+
+    // Step 3: Run docker-compose file
+    // Step 4: Wait the end of the execution of the result container
+    // Step 5: Get the result from the volume
+    // Step 6: Check content requirements and content size
+    // Step 7: Upload and show the results
+})();
+
diff --git a/ExerciceChecker/src/config/Config.ts b/ExerciceChecker/src/config/Config.ts
new file mode 100644
index 0000000..363c286
--- /dev/null
+++ b/ExerciceChecker/src/config/Config.ts
@@ -0,0 +1,19 @@
+class Config {
+    public readonly filesFolder: string;
+
+    public exercice: {
+        id: string; secret: string;
+    };
+
+    constructor() {
+        this.filesFolder = process.env.FILES_FOLDER || './';
+
+        this.exercice = {
+            id    : process.env.DOJO_EXERCICE_ID || '',
+            secret: process.env.DOJO_SECRET || ''
+        };
+    }
+}
+
+
+export default new Config();
diff --git a/ExerciceChecker/src/managers/DojoBackendManager.ts b/ExerciceChecker/src/managers/DojoBackendManager.ts
index d64a258..943bca3 100644
--- a/ExerciceChecker/src/managers/DojoBackendManager.ts
+++ b/ExerciceChecker/src/managers/DojoBackendManager.ts
@@ -1,11 +1,23 @@
 import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig';
 import ApiRoutes           from '../sharedByClients/types/ApiRoutes';
+import axios               from 'axios';
+import DojoResponse        from '../shared/types/Dojo/DojoResponse';
+import ExerciceEnonce      from '../sharedByClients/models/ExerciceEnonce';
+import Config              from '../config/Config';
 
 
 class DojoBackendManager {
     public getApiUrl(route: ApiRoutes): string {
         return `${ ClientsSharedConfig.apiURL }${ route }`;
     }
+
+    public async getExerciceEnonce(): Promise<ExerciceEnonce | undefined> {
+        try {
+            return (await axios.get<DojoResponse<ExerciceEnonce>>(this.getApiUrl(ApiRoutes.EXERCICE_ENONCE).replace('{{id}}', Config.exercice.id))).data.data;
+        } catch ( error ) {
+            return undefined;
+        }
+    }
 }
 
 
diff --git a/ExerciceChecker/src/managers/HttpManager.ts b/ExerciceChecker/src/managers/HttpManager.ts
index 9cf17d8..432e456 100644
--- a/ExerciceChecker/src/managers/HttpManager.ts
+++ b/ExerciceChecker/src/managers/HttpManager.ts
@@ -1,6 +1,7 @@
 import axios, { AxiosRequestHeaders } from 'axios';
 import FormData                       from 'form-data';
 import ClientsSharedConfig            from '../sharedByClients/config/ClientsSharedConfig';
+import Config                         from '../config/Config';
 
 
 class HttpManager {
@@ -18,9 +19,13 @@ class HttpManager {
             }
 
             if ( config.url && (config.url.indexOf(ClientsSharedConfig.apiURL) !== -1) ) {
+                config.headers['Accept-Encoding'] = 'gzip';
+
                 if ( config.data && Object.keys(config.data).length > 0 ) {
                     config.headers['Content-Type'] = 'multipart/form-data';
                 }
+
+                config.headers.Authorization = `ExerciceSecret ${ Config.exercice.secret }`;
             }
 
             return config;
diff --git a/ExerciceChecker/src/shared b/ExerciceChecker/src/shared
index c9154d4..bfca2c4 160000
--- a/ExerciceChecker/src/shared
+++ b/ExerciceChecker/src/shared
@@ -1 +1 @@
-Subproject commit c9154d42dac81311cf1957f0d75f806737849b40
+Subproject commit bfca2c401e4b5ff69b0a515fd9dcab49d36ee212
diff --git a/ExerciceChecker/src/sharedByClients b/ExerciceChecker/src/sharedByClients
index 8fe8e94..b78ee5f 160000
--- a/ExerciceChecker/src/sharedByClients
+++ b/ExerciceChecker/src/sharedByClients
@@ -1 +1 @@
-Subproject commit 8fe8e9417a527cf2182a9acc440e68b99024487e
+Subproject commit b78ee5ffda1f10ffacf8cde47fbf45e76a3d9094
-- 
GitLab