Skip to content

Commit

Permalink
Merge pull request #110 from correctexam/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
barais authored Dec 20, 2023
2 parents b5f9d84 + 3be3a9e commit 86f31bf
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 22 deletions.
38 changes: 38 additions & 0 deletions src/main/java/fr/istic/service/FichierS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.UploadObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
Expand Down Expand Up @@ -104,7 +105,44 @@ public InputStream getObject(String name)
}
}
}
public void uploadObject(String name, String filename, String contenttype) throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException{
if (!saveasfile) {
try {

this.createBucketifNotExist();

minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.object(name).filename(
filename)
.contentType(contenttype)
.build());
} catch (MinioException e) {
e.printStackTrace();
throw new IllegalStateException(e);

}}
else{
Path patht = Paths.get("template");
if (!Files.exists(patht)){
Files.createDirectory(patht);
}
Path paths = Paths.get("scan");
if (!Files.exists(paths)){
Files.createDirectory(paths);
}
Path pathc = Paths.get("cache");
if (!Files.exists(pathc)){
Files.createDirectory(pathc);
}
Path path = Paths.get(name);
OutputStream outputStream = Files.newOutputStream(path);
outputStream.write(Files.readAllBytes(Paths.get(filename)));
outputStream.close();
}

}
public void putObject(String name, byte[] bytes, String contenttype)
throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
if (saveasfile) {
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/fr/istic/service/ImportExportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand Down Expand Up @@ -76,7 +79,7 @@ public class ImportExportService {
@Inject
CourseMapper courseMapper;

public JsonObject export(long courseId, boolean includeStudentData) {
public JsonObject export(long courseId, boolean includeStudentData, long eid) {
Map<UUID, Long> uuidMap = new HashMap<UUID, Long>();
JsonObject root = new JsonObject();

Expand Down Expand Up @@ -140,7 +143,12 @@ public JsonObject export(long courseId, boolean includeStudentData) {
JsonArray exams = new JsonArray();
Map<Long, UUID> examsUID = new HashMap<>();
root.add("exams", exams);
course.exams.stream().forEach(exam -> {
Set<Exam> _exams = course.exams;
if (eid != -1){
_exams= _exams.stream().filter(ex-> ex.id == eid).collect(Collectors.toSet());
}

_exams.stream().forEach(exam -> {
JsonObject examJ = new JsonObject();
UUID examU = UUID.randomUUID();
uuidMap.put(examU, exam.id);
Expand Down Expand Up @@ -458,7 +466,7 @@ public JsonObject export(long courseId, boolean includeStudentData) {
}
JsonArray courseExamR = new JsonArray();
root.add("courseExamR", courseExamR);
course.exams.forEach(ex -> {
_exams.forEach(ex -> {
JsonObject ob = new JsonObject();
ob.addProperty("left", courseU.toString());
ob.addProperty("right", examsUID.get(ex.id).toString());
Expand Down Expand Up @@ -723,10 +731,12 @@ public JsonObject export(long courseId, boolean includeStudentData) {
studentsUID.keySet().forEach(sid -> {
Student s = Student.findById(sid);
s.examSheets.forEach(gc -> {
JsonObject ob = new JsonObject();
ob.addProperty("left", studentsUID.get(sid).toString());
ob.addProperty("right", examSheetsUID.get(gc.id).toString());
studentExamSheetR.add(ob);
if (examSheetsUID.containsKey(gc.id)){
JsonObject ob = new JsonObject();
ob.addProperty("left", studentsUID.get(sid).toString());
ob.addProperty("right", examSheetsUID.get(gc.id).toString());
studentExamSheetR.add(ob);
}
});
});

Expand Down
27 changes: 14 additions & 13 deletions src/main/java/fr/istic/service/ScanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.io.RandomAccessStreamCache;
import org.apache.pdfbox.io.ScratchFile;
import org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.multipdf.PDFMergerUtility.DocumentMergeMode;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import org.simpleframework.xml.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -317,22 +310,25 @@ protected void mergeFile(InputStream inputStream, String contenttype, long scanI
src.save(fo);
fo.close();

byte[] bytes = IOUtils.toByteArray(new FileInputStream(res.toFile()));
scan.toFile().delete();
toadd.toFile().delete();


// byte[] bytes = IOUtils.toByteArray(new FileInputStream(res.toFile()));
if (this.uses3) {
String fileName = "scan/" + scanId + ".pdf";
try {
this.uploadObject(fileName, res.toFile().getAbsoluteFile().getAbsolutePath(), contenttype);
res.toFile().delete();

this.putObject(fileName, bytes, contenttype);
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
}
} else {
byte[] bytes = IOUtils.toByteArray(new FileInputStream(res.toFile()));
this.updateContent(scanId, bytes);

res.toFile().delete();
}
scan.toFile().delete();
toadd.toFile().delete();
res.toFile().delete();

} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalArgumentException e) {
e.printStackTrace();
Expand Down Expand Up @@ -374,5 +370,10 @@ protected void putObject(String name, byte[] bytes, String contenttype)
throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
this.fichierS3Service.putObject(name, bytes, contenttype);
}
protected void uploadObject(String name,String origfilename, String contenttype)
throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
this.fichierS3Service.uploadObject(name, origfilename, contenttype);
}


}
80 changes: 78 additions & 2 deletions src/main/java/fr/istic/web/rest/ExtendedAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,45 @@ public void write(OutputStream outputStream) throws IOException, WebApplicationE
InputStream source = null;
try {
source = new ByteArrayInputStream(
new Gson().toJson(importExportService.export(courseId, true)).getBytes());
new Gson().toJson(importExportService.export(courseId, true,-1)).getBytes());

} catch (Exception e) {
e.printStackTrace();
return;
}
byte[] buf = new byte[8192];
int length;
while ((length = source.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
}
}, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment;filename=" + courseId + ".json")
.build();
} catch (Exception e) {

e.printStackTrace();
return Response.noContent().build();
}
}

@GET
@Path("/exportExam/{courseId}/{examId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN })
public Response exportExam(@PathParam("courseId") long courseId, @PathParam("examId") long examId, @Context SecurityContext ctx) {
if (!securityService.canAccess(ctx, courseId, Course.class)) {
return Response.status(403, "Current user cannot access to this ressource").build();
}
try {
return Response.ok(
new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
InputStream source = null;
try {
source = new ByteArrayInputStream(
new Gson().toJson(importExportService.export(courseId, true,examId)).getBytes());

} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -1397,7 +1435,45 @@ public void write(OutputStream outputStream) throws IOException, WebApplicationE
InputStream source = null;
try {
source = new ByteArrayInputStream(
new Gson().toJson(importExportService.export(courseId, false)).getBytes());
new Gson().toJson(importExportService.export(courseId, false,-1)).getBytes());

} catch (Exception e) {
e.printStackTrace();
return;
}
byte[] buf = new byte[8192];
int length;
while ((length = source.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
}
}, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment;filename=" + courseId + ".json")
.build();
} catch (Exception e) {

e.printStackTrace();
return Response.noContent().build();
}
}

@GET
@Path("/exportExamWithoutStudentData/{courseId}/{examId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN })
public Response exportExamWithoutStudentData(@PathParam("courseId") long courseId, @PathParam("examId") long examId, @Context SecurityContext ctx) {
if (!securityService.canAccess(ctx, courseId, Course.class)) {
return Response.status(403, "Current user cannot access to this ressource").build();
}
try {
return Response.ok(
new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
InputStream source = null;
try {
source = new ByteArrayInputStream(
new Gson().toJson(importExportService.export(courseId, false,examId)).getBytes());

} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 86f31bf

Please sign in to comment.