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

3
import de.hftstuttgart.models.ResultSummary;
4
import de.hftstuttgart.utils.*;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
5
6
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
7
import org.apache.tika.Tika;
8
import org.springframework.core.env.Environment;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
9
10
11
12
13
14
15
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;
16
17
18
19
20
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
21
22
23
24
25
26
27
28
29
30
31

/**
 * 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;
32
    private final Path testTmpPathModocot;
33
    private final ExecuteTestUtil executeTestUtil;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
34

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

43
        // set path of temporary directory on host and inside our container
Lukas Wiest's avatar
Lukas Wiest committed
44
        this.testTmpPathModocot = Paths.get(env.getProperty("tests.tmp.dir"));
45
    }
Dominik Vayhinger's avatar
Dominik Vayhinger committed
46

47
    @RequestMapping(method = RequestMethod.POST)
48
49
    public ResultSummary uploadAndTestFile(@RequestParam("taskFile") MultipartFile taskFileRef,
                                           @RequestParam("assignmentId") String assignmentId
50
51
52
53
54
55
56
57
58
    ) 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
59

60
61
62
63
64
65
66
67
68
69
70
71
        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":
72
73
74
                LOG.debug("zip archive uploaded, extracting content as student submission");
                ArchiveUtil.extractProjectFromZip(taskFileRef.getInputStream(), srcPath.toAbsolutePath());
                break;
75
76
77
78
79

            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
80
81
        }

82
83
        // run test
        LOG.debug("calling test execution");
84
        ResultSummary resultSummary = executeTestUtil.runTests(assignmentId, workDirectory);
Dominik Vayhinger's avatar
Dominik Vayhinger committed
85

86
87
88
89
        if (mimeInfo.equals("text/plain")) {
            LOG.info("check for provided Ticketsystem information");
            UnifiedTicketingUtil.reportResults(taskFileRef.getInputStream(), resultSummary);
        }
90

91
        LOG.info("submission tested successfully");
92
        return resultSummary;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
93
94
    }
}