diff --git a/src/main/java/fr/istic/service/FichierS3Service.java b/src/main/java/fr/istic/service/FichierS3Service.java index 26c94ca..99d48dd 100644 --- a/src/main/java/fr/istic/service/FichierS3Service.java +++ b/src/main/java/fr/istic/service/FichierS3Service.java @@ -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; @@ -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) { diff --git a/src/main/java/fr/istic/service/ImportExportService.java b/src/main/java/fr/istic/service/ImportExportService.java index 409a462..8ca4efc 100644 --- a/src/main/java/fr/istic/service/ImportExportService.java +++ b/src/main/java/fr/istic/service/ImportExportService.java @@ -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; @@ -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 uuidMap = new HashMap(); JsonObject root = new JsonObject(); @@ -140,7 +143,12 @@ public JsonObject export(long courseId, boolean includeStudentData) { JsonArray exams = new JsonArray(); Map examsUID = new HashMap<>(); root.add("exams", exams); - course.exams.stream().forEach(exam -> { + Set _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); @@ -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()); @@ -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); + } }); }); diff --git a/src/main/java/fr/istic/service/ScanService.java b/src/main/java/fr/istic/service/ScanService.java index 169853e..54654b8 100644 --- a/src/main/java/fr/istic/service/ScanService.java +++ b/src/main/java/fr/istic/service/ScanService.java @@ -15,18 +15,12 @@ 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; @@ -34,7 +28,6 @@ import javax.inject.Inject; import javax.transaction.Transactional; -import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -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(); @@ -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); + } + } diff --git a/src/main/java/fr/istic/web/rest/ExtendedAPI.java b/src/main/java/fr/istic/web/rest/ExtendedAPI.java index b1c16a9..68493bb 100644 --- a/src/main/java/fr/istic/web/rest/ExtendedAPI.java +++ b/src/main/java/fr/istic/web/rest/ExtendedAPI.java @@ -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(); @@ -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();