Skip to content

Commit 46891c3

Browse files
authored
Merge pull request #84 from AlokSP/iec_materials
Iec materials
2 parents dc139e4 + c6521d1 commit 46891c3

13 files changed

Lines changed: 622 additions & 48 deletions

src/main/java/com/health/HealthNutrition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void run(String... args) throws Exception {
5959
try {
6060

6161
taskProcessingService.createOutlineFile();
62+
taskProcessingService.addTokenInTrainingResource();
6263
taskProcessingService.addAllBrochureToQueue();
6364
taskProcessingService.addAllResearchPapertoQueue();
6465
taskProcessingService.addAllTuttorialsToQueue();

src/main/java/com/health/config/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static BCryptPasswordEncoder passwordEncoder() {
5959
"/downloadManagerforhst/**", "/downloadHealthTutorials/**", "/Training-Resource/**",
6060
"/Training-Resources/**", "/loadLanAndFileTypeByTopic/**", "/loadTopicAndFileTypeByLan/**",
6161
"/loadTopicAndLanByFileType/**", "/downloadTrainingResource/**", "/shared-Training-Resource/**",
62-
"/shared-training-resource-file/**", "/check-deployment" };
62+
"/shared-training-resource-file/**", "/check-deployment", "/training-resources/view-share/**" };
6363

6464
/**
6565
* url matcher for SUPERADMIN

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

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.annotation.Value;
3333
import org.springframework.cache.annotation.Cacheable;
3434
import org.springframework.core.env.Environment;
35+
import org.springframework.core.io.InputStreamResource;
3536
import org.springframework.core.io.Resource;
3637
import org.springframework.core.io.UrlResource;
3738
import org.springframework.http.HttpHeaders;
@@ -42,6 +43,7 @@
4243
import org.springframework.stereotype.Controller;
4344
import org.springframework.web.bind.annotation.DeleteMapping;
4445
import org.springframework.web.bind.annotation.GetMapping;
46+
import org.springframework.web.bind.annotation.PathVariable;
4547
import org.springframework.web.bind.annotation.PostMapping;
4648
import org.springframework.web.bind.annotation.RequestBody;
4749
import org.springframework.web.bind.annotation.RequestMapping;
@@ -2133,11 +2135,80 @@ public ResponseEntity<String> deleteTrainingResource(@RequestParam("trainingReso
21332135

21342136
}
21352137

2138+
/*
2139+
* @GetMapping("/downloadTrainingResource") public ResponseEntity<Resource>
2140+
* downloadTrainingResourcePost(@RequestParam(name = "filePath") String
2141+
* filePath) {
2142+
*
2143+
* try {
2144+
*
2145+
* String finalUrl = ServiceUtility.convertFilePathToUrl(filePath);
2146+
*
2147+
* Path path = Paths.get(env.getProperty("spring.applicationexternalPath.name"),
2148+
* finalUrl);
2149+
*
2150+
* if (!Files.exists(path)) { return
2151+
* ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build(); }
2152+
*
2153+
* Resource resource = new UrlResource(path.toUri());
2154+
*
2155+
* String contentType = Files.probeContentType(path); if (contentType == null) {
2156+
* contentType = "application/octet-stream"; }
2157+
*
2158+
* String originalFilename = path.getFileName().toString();
2159+
*
2160+
* String safeFilename = originalFilename.replaceAll("[\\\\/:*?\"<>|]", "_");
2161+
*
2162+
* String encodedFilename = URLEncoder.encode(safeFilename,
2163+
* "UTF-8").replace("+", "%20");
2164+
*
2165+
* return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
2166+
* .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" +
2167+
* encodedFilename) .body(resource);
2168+
*
2169+
* } catch (Exception e) { logger.error("Error in download", e); return
2170+
* ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build(); } }
2171+
*/
2172+
21362173
@GetMapping("/downloadTrainingResource")
2137-
public ResponseEntity<Resource> downloadTrainingResourcePost(@RequestParam(name = "filePath") String filePath) {
2174+
public ResponseEntity<Resource> downloadTrainingResource(@RequestParam(name = "fileType") String fileTypeString,
2175+
@RequestParam(name = "token") String token) {
21382176

21392177
try {
21402178

2179+
int fileType = ServiceUtility.getFileTypeIdByValue(fileTypeString);
2180+
TrainingResource tr = null;
2181+
2182+
if (fileType == CommonData.DOC) {
2183+
tr = trainingResourceService.findByDocToken(token);
2184+
} else if (fileType == CommonData.EXCEL) {
2185+
tr = trainingResourceService.findByExcelToken(token);
2186+
} else if (fileType == CommonData.PDF) {
2187+
tr = trainingResourceService.findByPdfToken(token);
2188+
} else if (fileType == CommonData.IMAGE) {
2189+
tr = trainingResourceService.findByImgToken(token);
2190+
}
2191+
2192+
if (tr == null) {
2193+
return ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build();
2194+
}
2195+
2196+
String filePath = null;
2197+
2198+
if (fileType == CommonData.DOC) {
2199+
filePath = tr.getDocPath();
2200+
} else if (fileType == CommonData.EXCEL) {
2201+
filePath = tr.getExcelPath();
2202+
} else if (fileType == CommonData.PDF) {
2203+
filePath = tr.getPdfPath();
2204+
} else if (fileType == CommonData.IMAGE) {
2205+
filePath = tr.getImgPath();
2206+
}
2207+
2208+
if (filePath == null || filePath.trim().isEmpty()) {
2209+
return ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build();
2210+
}
2211+
21412212
String finalUrl = ServiceUtility.convertFilePathToUrl(filePath);
21422213

21432214
Path path = Paths.get(env.getProperty("spring.applicationexternalPath.name"), finalUrl);
@@ -2164,7 +2235,80 @@ public ResponseEntity<Resource> downloadTrainingResourcePost(@RequestParam(name
21642235
.body(resource);
21652236

21662237
} catch (Exception e) {
2167-
logger.error("Error in download", e);
2238+
logger.error("Error while downloading training resource", e);
2239+
return ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
2240+
}
2241+
}
2242+
2243+
@GetMapping("/training-resources/view-share/{topic}/{lang}/{fileType}/{token}/{page}/{fileName}")
2244+
public ResponseEntity<Resource> viewOrSharePdfOrImage(@PathVariable String topic, @PathVariable String lang,
2245+
@PathVariable String fileType, @PathVariable String token, @PathVariable int page,
2246+
@PathVariable String fileName) {
2247+
2248+
try {
2249+
2250+
int fileTypeId = ServiceUtility.getFileTypeIdByValue(fileType);
2251+
2252+
TrainingResource tr = null;
2253+
if (fileTypeId == CommonData.PDF) {
2254+
tr = trainingResourceService.findByPdfToken(token);
2255+
} else if (fileTypeId == CommonData.IMAGE) {
2256+
tr = trainingResourceService.findByImgToken(token);
2257+
}
2258+
2259+
if (tr == null) {
2260+
return ResponseEntity.notFound().build();
2261+
}
2262+
2263+
String filePath = ServiceUtility.getTrainingResourceFilePath(tr, fileTypeId);
2264+
2265+
List<String> filePaths = new ArrayList<>();
2266+
if (filePath.toLowerCase().endsWith(".zip")) {
2267+
List<String> extracted = ServiceUtility.extractZipIfNeeded(filePath, env);
2268+
List<String> sorted = ServiceUtility.sortFilePathsNumericOtherwiseLexical(extracted);
2269+
2270+
if (fileType.equalsIgnoreCase("Pdf")) {
2271+
for (String str : sorted) {
2272+
if (!str.endsWith(".png")) {
2273+
filePaths.add(str);
2274+
}
2275+
}
2276+
} else {
2277+
filePaths.addAll(sorted);
2278+
}
2279+
2280+
} else {
2281+
filePaths.add(ServiceUtility.convertFilePathToUrl(filePath));
2282+
}
2283+
2284+
if (page < 0 || page >= filePaths.size()) {
2285+
return ResponseEntity.badRequest().build();
2286+
}
2287+
2288+
String finalFilePath = filePaths.get(page);
2289+
Path path = Paths.get(env.getProperty("spring.applicationexternalPath.name"), finalFilePath);
2290+
2291+
if (!Files.exists(path)) {
2292+
return ResponseEntity.notFound().build();
2293+
}
2294+
2295+
String contentType = Files.probeContentType(path);
2296+
if (contentType == null) {
2297+
contentType = "application/octet-stream";
2298+
}
2299+
2300+
String originalFilename = path.getFileName().toString();
2301+
String safeFilename = originalFilename.replaceAll("[\\\\/:*?\"<>|]", "_");
2302+
String encodedFilename = URLEncoder.encode(safeFilename, "UTF-8").replace("+", "%20");
2303+
2304+
InputStreamResource resource = new InputStreamResource(Files.newInputStream(path));
2305+
2306+
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
2307+
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename*=UTF-8''" + encodedFilename)
2308+
.body(resource);
2309+
2310+
} catch (Exception e) {
2311+
logger.error("Error viewing shared file", e);
21682312
return ResponseEntity.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
21692313
}
21702314
}

0 commit comments

Comments
 (0)