Skip to content

Commit 9883117

Browse files
committed
dropdown and download Large file
1 parent 669819c commit 9883117

5 files changed

Lines changed: 194 additions & 30 deletions

File tree

src/main/java/com/health/controller/AjaxController.java

Lines changed: 108 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.TreeMap;
2121
import java.util.concurrent.atomic.AtomicInteger;
2222

23+
import javax.servlet.http.HttpServletRequest;
2324
import javax.servlet.http.HttpServletResponse;
2425
//import javax.servlet.http.HttpServletRequest;
2526
import 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

src/main/java/com/health/controller/HomeController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7693,7 +7693,7 @@ public String viewAndDownloadProjectReport(HttpServletRequest req,
76937693
if (state == null || district == null || inputFileType == 0) {
76947694
getModelProjectReport(model);
76957695

7696-
model.addAttribute("error_msg", "Please select all fields");
7696+
// model.addAttribute("error_msg", "Please select all fields");
76977697
return "projectReports";
76987698

76997699
}
@@ -7906,7 +7906,10 @@ private String sharedProjectreportTemplate(String stateName, String fileType, St
79067906
String fileSize = ServiceUtility.getZipSizeInKB_OR_MB(filePath, env);
79077907
model.addAttribute("fileName", fileName);
79087908
model.addAttribute("fileSizeInMb", fileSize);
7909-
if (filePath.toLowerCase().endsWith(".zip")) {
7909+
7910+
boolean maxFileSize = ServiceUtility.isFileLargerThan50Mb(filePath, env);
7911+
7912+
if (filePath.toLowerCase().endsWith(".zip") || maxFileSize) {
79107913

79117914
isZipFile = true;
79127915

src/main/java/com/health/utility/ServiceUtility.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,34 @@ public static String getZipSizeInKB_OR_MB(String zipUrl, Environment env) {
12691269

12701270
}
12711271

1272+
public static boolean isFileLargerThan100Mb(String zipUrl, Environment env) {
1273+
Path zipFilePath = Paths.get(env.getProperty("spring.applicationexternalPath.name"), zipUrl);
1274+
File zipFile = zipFilePath.toFile();
1275+
1276+
if (zipFile.exists() && zipFile.isFile()) {
1277+
long fileSizeInBytes = zipFile.length();
1278+
long sizeLimit = 100L * 1024 * 1024;
1279+
return fileSizeInBytes > sizeLimit;
1280+
1281+
}
1282+
return false;
1283+
1284+
}
1285+
1286+
public static boolean isFileLargerThan50Mb(String zipUrl, Environment env) {
1287+
Path zipFilePath = Paths.get(env.getProperty("spring.applicationexternalPath.name"), zipUrl);
1288+
File zipFile = zipFilePath.toFile();
1289+
1290+
if (zipFile.exists() && zipFile.isFile()) {
1291+
long fileSizeInBytes = zipFile.length();
1292+
long sizeLimit = 50L * 1024 * 1024;
1293+
return fileSizeInBytes > sizeLimit;
1294+
1295+
}
1296+
return false;
1297+
1298+
}
1299+
12721300
public static String getVideoResolutionPath(String VideoPath, String resolution) {
12731301
int dotIndex = VideoPath.lastIndexOf('.');
12741302

@@ -1329,6 +1357,38 @@ public static boolean hasAnyProjectReportFile(ProjectReport pr) {
13291357

13301358
}
13311359

1360+
public static String downloadFileUpTo100MB(String filePath, Environment env, HttpServletResponse response) {
1361+
1362+
Path path = Paths.get(env.getProperty("spring.applicationexternalPath.name"), filePath);
1363+
File file = path.toFile();
1364+
1365+
if (!file.exists()) {
1366+
throw new RuntimeException("File not found");
1367+
}
1368+
1369+
try (OutputStream os = response.getOutputStream();
1370+
InputStream is = new BufferedInputStream(new FileInputStream(path.toFile()));) {
1371+
1372+
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
1373+
response.setHeader("Content-Disposition", "attachment; filename=" + ServiceUtility.getZipfileName(path));
1374+
response.setContentLengthLong(Files.size(path));
1375+
1376+
byte[] buffer = new byte[16384];
1377+
int length;
1378+
1379+
while ((length = is.read(buffer)) > 0) {
1380+
1381+
os.write(buffer, 0, length);
1382+
1383+
}
1384+
os.flush();
1385+
1386+
} catch (Exception e) {
1387+
logger.info("Exception:{}", e.getMessage());
1388+
}
1389+
return null;
1390+
}
1391+
13321392
public static boolean hasAnyResourceFile(TrainingResource tr) {
13331393
boolean hasData = true;
13341394
if ((tr.getPdfPath() == null || tr.getPdfPath().isEmpty())
@@ -1470,6 +1530,11 @@ public static boolean isProjectReportFilePresent(ProjectReport pr, int fileId) {
14701530
}
14711531
}
14721532

1533+
public static boolean isProjectReportFilePresentforAnyFile(ProjectReport pr) {
1534+
return pr != null && (isNotBlank(pr.getDocPath()) || isNotBlank(pr.getExcelPath())
1535+
|| isNotBlank(pr.getImgPath()) || isNotBlank(pr.getPdfPath()));
1536+
}
1537+
14731538
public static String getTrainingResourceFilePath(TrainingResource tr, int fileId) {
14741539
if (tr == null) {
14751540
return "";

src/main/resources/templates/projectReports.html

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@
4141
<section id="p-report">
4242

4343

44-
<div class="row" id="OriginalMaodeRow">
45-
<div class="col-md-8 p-2">
46-
47-
48-
</div>
49-
50-
</div>
51-
44+
<div class="row justify-content-center" id="OriginalMaodeRow">
45+
46+
<div class="col-md-6 p-2">
47+
48+
<div class="alert alert-danger text-center" role="alert"
49+
th:if="${error_msg}">
50+
51+
<span th:text="${error_msg}"></span>
52+
53+
</div>
54+
55+
</div>
56+
57+
</div>
5258
</section>
5359

5460
</main>

src/main/resources/templates/sharedProjectReport.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ <h3 class="text-center resource-title mb-4">
9696
Download
9797
</a>
9898
</div>
99-
99+
100+
100101
</div>
101102
</div>
102103

0 commit comments

Comments
 (0)