TaskUpload.java 4.03 KB
Newer Older
Dominik Vayhinger's avatar
Dominik Vayhinger committed
1
2
package de.hftstuttgart.rest.v1.task;

3
4
import de.hftstuttgart.models.LegacyMoodleResult;
import de.hftstuttgart.models.ModocotResultSummary;
5
import de.hftstuttgart.utils.*;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
6
7
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
8
import org.apache.tika.Tika;
9
import org.springframework.core.env.Environment;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
10
11
12
13
14
15
16
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
17
18
19
20
21
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
22
23
24
25
26
27
28
29
30
31
32

/**
 * Rest controller for everything related to the TASK files
 */
@RestController
@RequestMapping("/v1/task")
@MultipartConfig
public class TaskUpload {
    private static final Logger LOG = LogManager.getLogger(TaskUpload.class);

    private final JGitUtil jGitUtil;
33
    private final Path testTmpPathModocot;
34
    private final ExecuteTestUtil executeTestUtil;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
35
36


37
38
39
    public TaskUpload(
        Environment env,
        JGitUtil jGitUtil,
40
        ExecuteTestUtil executeTestUtil
41
    ) {
Dominik Vayhinger's avatar
Dominik Vayhinger committed
42
        this.jGitUtil = jGitUtil;
43
        this.executeTestUtil = executeTestUtil;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
44

45
46
47
48
        // set base path for assignments to be stored
        Path p = Paths.get(
            env.getProperty("modocot.dir"),
            env.getProperty("modocot.dir.test.folder.name"));
Dominik Vayhinger's avatar
Dominik Vayhinger committed
49

50
51
52
        // set path of temporary directory on host and inside our container
        this.testTmpPathModocot = Paths.get(env.getProperty("modocot.tests.tmp.dir"));
    }
Dominik Vayhinger's avatar
Dominik Vayhinger committed
53

54
55
56
57
58
59
60
61
62
63
64
65
    @RequestMapping(method = RequestMethod.POST)
    public LegacyMoodleResult uploadAndTestFile(@RequestParam("taskFile") MultipartFile taskFileRef,
                                    @RequestParam("assignmentId") String assignmentId
    ) throws IOException, InterruptedException {
        LOG.info("submission for testing received");

        LOG.debug("creating new temporary directory");
        Path workDirectory = Files.createTempDirectory(testTmpPathModocot, "modocot");
        LOG.debug(String.format("working dir for test is: %s", workDirectory.toAbsolutePath().toString()));

        // define paths for the test, the submission and where the result is to be expected afterwards
        Path srcPath    = Paths.get(workDirectory.toAbsolutePath().toString(), "src");
Dominik Vayhinger's avatar
Dominik Vayhinger committed
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        String mimeInfo = new Tika().detect(taskFileRef.getInputStream());
        switch (mimeInfo) {
            case "text/plain":
                LOG.debug("textfile uploaded, searching for modocot config");
                // find modocot URI in config file
                Matcher config = RegexUtil.findModocotStudentConfig(taskFileRef.getInputStream());

                LOG.debug("calling repo clone");
                jGitUtil.cloneRepository(config, srcPath.toAbsolutePath().toString());
                break;

            case "application/zip":
                // TODO
                throw new RuntimeException("zip processing not yet implemented");

            default:
                String msg = String.format("couldn't process uploaded file with mime type %s", mimeInfo);
                LOG.error(msg);
                throw new RuntimeException(msg);
Dominik Vayhinger's avatar
Dominik Vayhinger committed
86
87
        }

88
89
90
        // run test
        LOG.debug("calling test execution");
        ModocotResultSummary resultSummary = executeTestUtil.runTests(assignmentId, workDirectory);
Dominik Vayhinger's avatar
Dominik Vayhinger committed
91

92
93
94
95
        // convert to moddle plugin readable format and return to moodle
        LOG.debug("convert to moodle understandable format");
        LegacyMoodleResult moodleResult = LegacyMoodleResult.convertToModdleResult(resultSummary);

96
97
98
99
        if (mimeInfo.equals("text/plain")) {
            LOG.info("check for provided Ticketsystem information");
            UnifiedTicketingUtil.reportResults(taskFileRef.getInputStream(), resultSummary);
        }
100

101
102
        LOG.info("submission tested successfully");
        return moodleResult;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
103
104
    }
}