2020import java .util .TreeMap ;
2121import java .util .concurrent .atomic .AtomicInteger ;
2222
23+ import javax .servlet .http .HttpServletRequest ;
2324import javax .servlet .http .HttpServletResponse ;
2425//import javax.servlet.http.HttpServletRequest;
2526import javax .validation .Valid ;
@@ -2521,6 +2522,14 @@ public ResponseEntity<String> deleteProjectReport(@RequestParam("projectReportId
25212522 }
25222523 }
25232524 }
2525+
2526+ else {
2527+ for (ProjectReport temp : prList ) {
2528+ if (ServiceUtility .isProjectReportFilePresentforAnyFile (temp )) {
2529+ newprList1 .add (temp );
2530+ }
2531+ }
2532+ }
25242533 if (!newprList1 .isEmpty ())
25252534 prList = newprList1 ;
25262535
@@ -2570,6 +2579,14 @@ public ResponseEntity<String> deleteProjectReport(@RequestParam("projectReportId
25702579 }
25712580 }
25722581
2582+ else {
2583+ for (ProjectReport temp : prList ) {
2584+ if (ServiceUtility .isProjectReportFilePresentforAnyFile (temp )) {
2585+ newprList .add (temp );
2586+ }
2587+ }
2588+ }
2589+
25732590 if (!newprList .isEmpty ())
25742591 prList = newprList ;
25752592
@@ -2678,9 +2695,75 @@ public ResponseEntity<String> deleteProjectReport(@RequestParam("projectReportId
26782695
26792696 }
26802697
2698+ /*
2699+ * @GetMapping("/downloadProjectReport") public ResponseEntity<Resource>
2700+ * downloadProjectReport(@RequestParam(name = "fileType") String fileTypeString,
2701+ *
2702+ * @RequestParam(name = "token") String token) {
2703+ *
2704+ * try {
2705+ *
2706+ * int fileType = ServiceUtility.getFileTypeIdByValue(fileTypeString);
2707+ * ProjectReport pr = null;
2708+ *
2709+ * if (fileType == CommonData.DOC) { pr =
2710+ * projectReportService.findByDocToken(token); } else if (fileType ==
2711+ * CommonData.EXCEL) { pr = projectReportService.findByExcelToken(token); } else
2712+ * if (fileType == CommonData.PDF) { pr =
2713+ * projectReportService.findByPdfToken(token); } else if (fileType ==
2714+ * CommonData.IMAGE) { pr = projectReportService.findByImgToken(token); }
2715+ *
2716+ * if (pr == null) { return
2717+ * ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build(); }
2718+ *
2719+ * String filePath = null;
2720+ *
2721+ * if (fileType == CommonData.DOC) { filePath = pr.getDocPath(); } else if
2722+ * (fileType == CommonData.EXCEL) { filePath = pr.getExcelPath(); } else if
2723+ * (fileType == CommonData.PDF) { filePath = pr.getPdfPath(); } else if
2724+ * (fileType == CommonData.IMAGE) { filePath = pr.getImgPath(); }
2725+ *
2726+ * if (filePath == null || filePath.trim().isEmpty()) { return
2727+ * ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build(); }
2728+ *
2729+ * String finalUrl = ServiceUtility.convertFilePathToUrl(filePath);
2730+ *
2731+ * Path path = Paths.get(env.getProperty("spring.applicationexternalPath.name"),
2732+ * finalUrl);
2733+ *
2734+ * if (!Files.exists(path)) { return
2735+ * ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build(); }
2736+ *
2737+ * Resource resource = new UrlResource(path.toUri());
2738+ *
2739+ * String contentType = Files.probeContentType(path); if (contentType == null) {
2740+ * contentType = "application/octet-stream"; }
2741+ *
2742+ * String originalFilename = path.getFileName().toString();
2743+ *
2744+ * String safeFilename = originalFilename.replaceAll("[\\\\/:*?\"<>|]", "_");
2745+ *
2746+ * String encodedFilename = URLEncoder.encode(safeFilename,
2747+ * "UTF-8").replace("+", "%20");
2748+ *
2749+ * return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
2750+ * .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" +
2751+ * encodedFilename) .body(resource);
2752+ *
2753+ * } catch (Exception e) {
2754+ * logger.error("Error while downloading training resource", e); return
2755+ * ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build(); } }
2756+ */
2757+
26812758 @ GetMapping ("/downloadProjectReport" )
2682- public ResponseEntity <Resource > downloadProjectReport (@ RequestParam (name = "fileType" ) String fileTypeString ,
2683- @ RequestParam (name = "token" ) String token ) {
2759+ public String downloadProjectReport (@ RequestParam (name = "fileType" ) String fileTypeString ,
2760+ @ RequestParam (name = "token" ) String token , HttpServletResponse response ,
2761+ RedirectAttributes redirectAttributes , HttpServletRequest req ) {
2762+
2763+ String referer = req .getHeader ("Referer" );
2764+ if (referer == null || referer .trim ().isEmpty ()) {
2765+ referer = "/Project-Reports" ;
2766+ }
26842767
26852768 try {
26862769
@@ -2698,7 +2781,8 @@ public ResponseEntity<Resource> downloadProjectReport(@RequestParam(name = "file
26982781 }
26992782
27002783 if (pr == null ) {
2701- return ResponseEntity .status (HttpStatus .SC_NOT_FOUND ).build ();
2784+ redirectAttributes .addFlashAttribute ("error_msg" , "No Project Report Found" );
2785+ return "redirect:" + referer ;
27022786 }
27032787
27042788 String filePath = null ;
@@ -2714,37 +2798,42 @@ public ResponseEntity<Resource> downloadProjectReport(@RequestParam(name = "file
27142798 }
27152799
27162800 if (filePath == null || filePath .trim ().isEmpty ()) {
2717- return ResponseEntity .status (HttpStatus .SC_NOT_FOUND ).build ();
2801+ redirectAttributes .addFlashAttribute ("error_msg" , "No Project Report File Found" );
2802+ return "redirect:" + referer ;
27182803 }
27192804
27202805 String finalUrl = ServiceUtility .convertFilePathToUrl (filePath );
27212806
27222807 Path path = Paths .get (env .getProperty ("spring.applicationexternalPath.name" ), finalUrl );
27232808
27242809 if (!Files .exists (path )) {
2725- return ResponseEntity .status (HttpStatus .SC_NOT_FOUND ).build ();
2726- }
2727-
2728- Resource resource = new UrlResource (path .toUri ());
2729-
2730- String contentType = Files .probeContentType (path );
2731- if (contentType == null ) {
2732- contentType = "application/octet-stream" ;
2810+ redirectAttributes .addFlashAttribute ("error_msg" , "No Project Report File Found" );
2811+ return "redirect:" + referer ;
27332812 }
27342813
2735- String originalFilename = path .getFileName ().toString ();
2814+ if (!ServiceUtility .isFileLargerThan100Mb (finalUrl , env )) {
2815+ String msg = ServiceUtility .downloadFileUpTo100MB (finalUrl , env , response );
2816+ if (msg != null ) {
2817+ redirectAttributes .addFlashAttribute ("error_msg" , "Please Try Again" );
2818+ return "redirect:" + referer ;
2819+ }
27362820
2737- String safeFilename = originalFilename . replaceAll ( "[ \\ \\ /:*? \" <>|]" , "_" );
2821+ } else {
27382822
2739- String encodedFilename = URLEncoder .encode (safeFilename , "UTF-8" ).replace ("+" , "%20" );
2823+ String message = ServiceUtility .downloadManager (finalUrl , downloadCount , downloadLimit , downloadTimeOut ,
2824+ env , response );
27402825
2741- return ResponseEntity .ok ().contentType (MediaType .parseMediaType (contentType ))
2742- .header (HttpHeaders .CONTENT_DISPOSITION , "attachment; filename*=UTF-8''" + encodedFilename )
2743- .body (resource );
2826+ if (message != null ) {
2827+ redirectAttributes .addFlashAttribute ("error_msg" , message );
2828+ return "redirect:" + referer ;
2829+ }
2830+ }
2831+ return null ;
27442832
27452833 } catch (Exception e ) {
27462834 logger .error ("Error while downloading training resource" , e );
2747- return ResponseEntity .status (HttpStatus .SC_INTERNAL_SERVER_ERROR ).build ();
2835+ redirectAttributes .addFlashAttribute ("error_msg" , "Something went wrong" );
2836+ return "redirect:" + referer ;
27482837 }
27492838 }
27502839
0 commit comments