This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 9809b90b authored by Daniel Crazzolara's avatar Daniel Crazzolara
Browse files

Fixed various paths to tmp files

parent ca8a1962
2 merge requests!188Merging Peer Reviewing et. al to Master,!164211 peer reviewing functionality
......@@ -98,7 +98,7 @@ public class ExerciseService {
* - Copy, commit and push files to the new repository
*
* @param exerciseInfo of the exercise to import
* @param exerciseToken of the stored exercise (in '/tmp' directory) to import
* @param exerciseToken of the stored exercise (in the temporary directory) to import
*/
public void importExercise(ArtemisExerciseInfo exerciseInfo, String exerciseToken, Integer gitlabGroupId)
throws GitLabApiException, GitAPIException, IOException, ArtemisImportError {
......@@ -109,11 +109,11 @@ public class ExerciseService {
Group group = gitlabService.getGroupById(gitlabGroupId);
String repoUrl = gitlabService.createRepository(group, exerciseInfo.getTitle());
Repository repo = gitlabService.checkoutRepo(repoUrl);
gitlabService.commitAndPushToRepo(repo, new File("/tmp", exerciseToken));
gitlabService.commitAndPushToRepo(repo, new File(System.getProperty("java.io.tmpdir"), exerciseToken));
repo.close();
try {
FileUtils.deleteDirectory(new File("/tmp", exerciseToken));
FileUtils.deleteDirectory(new File(System.getProperty("java.io.tmpdir"), exerciseToken));
FileUtils.deleteDirectory(repo.getDirectory().getParentFile());
} catch (IOException e) {
log.error("Could not remove directory: {} \n", e.getMessage());
......@@ -133,7 +133,8 @@ public class ExerciseService {
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
File exerciseDetailsFile = Objects.requireNonNull(
new File("/tmp", exerciseToken).listFiles((file, name) -> name.startsWith("Exercise-Details-") && name.endsWith(".json"))
new File(System.getProperty("java.io.tmpdir"), exerciseToken)
.listFiles((file, name) -> name.startsWith("Exercise-Details-") && name.endsWith(".json"))
)[0];
mapArtemisExerciseInfoValues(exerciseDetailsFile);
......@@ -226,10 +227,10 @@ public class ExerciseService {
private void storeZipInTmp(ZipInputStream zipInputStream, String subDir) throws IOException {
Path targetDir;
try {
targetDir = Files.createTempDirectory(subDir);
} catch (IllegalArgumentException e) { // We expect this when calling this method recursively for contained Zip files
if (subDir.startsWith(System.getProperty("java.io.tmpdir"))) {
targetDir = Files.createDirectories(Paths.get(subDir));
} else {
targetDir = Files.createDirectories(Paths.get(System.getProperty("java.io.tmpdir"), subDir));
}
ZipEntry zipEntry;
......@@ -329,7 +330,10 @@ public class ExerciseService {
ObjectMapper mapper = new ObjectMapper(
new YAMLFactory().enable(YAMLGenerator.Feature.INDENT_ARRAYS).disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
);
mapper.writeValue(new File("/tmp/" + exerciseToken, "metadata.yaml"), exerciseInfo.getExerciseInfoOnly());
mapper.writeValue(
Paths.get(System.getProperty("java.io.tmpdir"), exerciseToken, "metadata.yaml").toFile(),
exerciseInfo.getExerciseInfoOnly()
);
}
/**
......@@ -376,17 +380,19 @@ public class ExerciseService {
private void renameFiles(String exerciseToken) {
try {
File problemStatementFile = Objects.requireNonNull(
new File("/tmp", exerciseToken).listFiles((file, name) -> name.startsWith("Problem-Statement-") && name.endsWith(".md"))
new File(System.getProperty("java.io.tmpdir"), exerciseToken)
.listFiles((file, name) -> name.startsWith("Problem-Statement-") && name.endsWith(".md"))
)[0];
if (!problemStatementFile.renameTo(new File("/tmp" + File.separator + exerciseToken, "exercise.md"))) {
if (!problemStatementFile.renameTo(Paths.get(System.getProperty("java.io.tmpdir"), exerciseToken, "exercise.md").toFile())) {
log.error("Unable to rename file [{}]. Skipping..", problemStatementFile.getName());
}
File exerciseDetailsFile = Objects.requireNonNull(
new File("/tmp", exerciseToken).listFiles((file, name) -> name.startsWith("Exercise-Details-") && name.endsWith(".json"))
new File(System.getProperty("java.io.tmpdir"), exerciseToken)
.listFiles((file, name) -> name.startsWith("Exercise-Details-") && name.endsWith(".json"))
)[0];
Files.move(exerciseDetailsFile.toPath(), exerciseDetailsFile.toPath().resolveSibling("Exercise-Details.json"));
} catch (ArrayIndexOutOfBoundsException e) {
log.error("File to rename could not be found in [{}].", "/tmp" + File.separator + exerciseToken);
log.error("File to rename could not be found in [{}].", System.getProperty("java.io.tmpdir") + File.separator + exerciseToken);
} catch (IOException ioe) {
log.error("Error while renaming file: {}", ioe.getMessage());
}
......
......@@ -277,14 +277,14 @@ public class GitlabService {
*/
public Repository checkoutRepo(String repoUrl) throws ArtemisImportError {
String repoDirName = repoUrl.split("/")[repoUrl.split("/").length - 1];
File dst = new File("tmp-import", repoDirName);
File dst = new File(System.getProperty("java.io.tmpdir"), repoDirName);
if (dst.exists()) {
log.debug("Temporary directory '{}' already exists, removing it.", dst.getPath());
try {
FileUtils.deleteDirectory(dst);
} catch (IOException e) {
log.error("Unable to remove directory. Create another directory : {} \n", e.getMessage());
dst = new File("tmp-import", repoDirName + UUID.randomUUID());
dst = new File(System.getProperty("java.io.tmpdir"), repoDirName + UUID.randomUUID());
}
}
try {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment