Skip to content

Commit c2a5b43

Browse files
committedAug 3, 2022
implement read() for ProblemLocalConnection
1 parent 4fa1411 commit c2a5b43

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed
 

‎app/controllers/Upload.java

+1-19
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,7 @@ public Result uploadFiles(Http.Request request, String problem, String editKey)
7373
}
7474
}
7575

76-
private void saveProblem(String problem, Map<Path, byte[]> problemFiles) throws IOException {
77-
// boolean isOnS3 = s3conn.isOnS3("ext");
78-
// if (isOnS3) {
79-
// byte[] problemZip = Util.zip(problemFiles);
80-
// s3conn.putToS3(problemZip, repo, problem);
81-
// } else {
82-
// Path extDir = java.nio.file.Path.of(config.getString("com.horstmann.codecheck.repo.ext"));
83-
// Path problemDir = extDir.resolve(problem);
84-
// com.horstmann.codecheck.Util.deleteDirectory(problemDir); // Delete any prior contents so that it is replaced by new zip file
85-
// Files.createDirectories(problemDir);
86-
// problemDir = problemDir.resolve("problem.zip");
87-
88-
// // for (Map.Entry<Path, byte[]> entry : problemFiles.entrySet()) {
89-
// // Path p = problemDir.resolve(entry.getKey());
90-
// // Files.write(p, entry.getValue());
91-
// // }
92-
// byte[] problemZip = Util.zip(problemFiles);
93-
// org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(problemDir.toString()), problemZip);
94-
// }
76+
private void saveProblem(String problem, Map<Path, byte[]> problemFiles) throws IOException {
9577
byte[] problemZip = Util.zip(problemFiles);
9678
probConn.write(problemZip, repo, problem);
9779
}

‎app/models/CodeCheck.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package models;
22

3+
import models.ProblemConnection.ProblemConnector;
4+
35
import java.io.ByteArrayOutputStream;
46
import java.io.File;
57
import java.io.IOException;
@@ -38,12 +40,13 @@ public class CodeCheck {
3840
private static Logger.ALogger logger = Logger.of("com.horstmann.codecheck");
3941
private Config config;
4042
private S3Connection s3conn;
43+
private ProblemConnector probConn;
4144
private JarSigner signer;
4245
private ResourceLoader resourceLoader;
4346

44-
@Inject public CodeCheck(Config config, S3Connection s3conn, Environment playEnv) {
47+
@Inject public CodeCheck(Config config, ProblemConnector probConn, Environment playEnv) {
4548
this.config = config;
46-
this.s3conn = s3conn;
49+
this.probConn = probConn;
4750
resourceLoader = new ResourceLoader() {
4851
@Override
4952
public InputStream loadResource(String path) throws IOException {
@@ -128,16 +131,10 @@ private String replaceParametersInFile(String contents, ScriptEngine engine) thr
128131
}
129132

130133
public Map<Path, byte[]> loadProblem(String repo, String problemName) throws IOException {
131-
if (s3conn.isOnS3(repo)) {
132-
return Util.unzip(s3conn.readFromS3(repo, problemName));
133-
} else {
134-
Path repoPath = Path.of(config.getString("com.horstmann.codecheck.repo."
135-
+ repo));
136-
// TODO: That comes from Problems.java--fix it there
137-
if (problemName.startsWith("/"))
138-
problemName = problemName.substring(1);
139-
return Util.descendantFiles(repoPath.resolve(problemName));
140-
}
134+
Map<Path, byte[]> result;
135+
byte[] zipFile = probConn.read(repo, problemName);
136+
result = Util.unzip(zipFile);
137+
return result;
141138
}
142139

143140
public Report run(String reportType, String repo,

‎app/models/ProblemConnection.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.io.FileInputStream;
67
import java.io.File;
78
import java.io.FileWriter;
89
import java.nio.file.Path;
@@ -15,6 +16,8 @@
1516
import java.util.List;
1617
import java.util.Map;
1718

19+
import com.horstmann.codecheck.Util;
20+
1821
import javax.inject.Inject;
1922
import javax.inject.Singleton;
2023

@@ -47,6 +50,7 @@
4750
import com.fasterxml.jackson.databind.ObjectMapper;
4851
import com.fasterxml.jackson.databind.node.ObjectNode;
4952
import com.typesafe.config.Config;
53+
import com.typesafe.config.ConfigException;
5054

5155
import play.Logger;
5256

@@ -210,14 +214,26 @@ public void write(String contents, String repo, String key) throws IOException {
210214
public void write(byte[] contents, String repo, String key) throws IOException {
211215
Path repoPath = Path.of(config.getString("com.horstmann.codecheck.repo." + repo));
212216
Path problemDir = repoPath.resolve(key);
213-
com.horstmann.codecheck.Util.deleteDirectory(problemDir); // Delete any prior contents so that it is replaced by new zip file
217+
Util.deleteDirectory(problemDir); // Delete any prior contents so that it is replaced by new zip file
214218
Files.createDirectories(problemDir);
215219
problemDir = problemDir.resolve("problem.zip");
216220
org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(problemDir.toString()), contents);
217221
}
218-
public void delete(String repo, String key) throws IOException {}
222+
public void delete(String repo, String key) throws IOException {
223+
224+
}
219225
public byte[] read(String repo, String problem) throws IOException {
220-
return null;
226+
Path repoPath = Path.of(config.getString("com.horstmann.codecheck.repo." + repo));
227+
if (problem.startsWith("/"))
228+
problem = problem.substring(1);
229+
230+
byte[] result = null;
231+
try {
232+
InputStream in = new FileInputStream(repoPath.resolve(problem).resolve("problem.zip").toString());
233+
result = in.readAllBytes();
234+
} catch (IOException ex) {}
235+
236+
return result;
221237
}
222238
}
223239

0 commit comments

Comments
 (0)
Failed to load comments.