Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed logic for file extension.
  • Loading branch information
Avinash Yachamaneni committed Oct 5, 2018
commit 6f167e432eda5090aee8d58579faecfd629694ea
69 changes: 32 additions & 37 deletions src/main/java/com/example/filedemo/controller/FileController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,46 @@
@RestController
public class FileController {

private static final Logger logger = LoggerFactory.getLogger(FileController.class);
private static final Logger logger = LoggerFactory.getLogger(FileController.class);

@Autowired
private FileStorageService fileStorageService;
@Autowired
private FileStorageService fileStorageService;

@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = fileStorageService.storeFile(file);
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = fileStorageService.storeFile(file);

String fileDownloadUri = Paths.get("/Users", "TestApk").toString();
String fileDownloadUri = Paths.get("/Users", "TestApk", fileName).toString();

return new UploadFileResponse(fileName, fileDownloadUri,
file.getContentType(), file.getSize());
}
return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize());
}

@PostMapping("/uploadMultipleFiles")
public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
return Arrays.asList(files)
.stream()
.map(file -> uploadFile(file))
.collect(Collectors.toList());
}
@PostMapping("/uploadMultipleFiles")
public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
return Arrays.asList(files).stream().map(file -> uploadFile(file)).collect(Collectors.toList());
}

@GetMapping("/downloadFile/{fileName:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// Load file as Resource
Resource resource = fileStorageService.loadFileAsResource(fileName);
@GetMapping("/downloadFile/{fileName:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// Load file as Resource
Resource resource = fileStorageService.loadFileAsResource(fileName);

// Try to determine file's content type
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
logger.info("Could not determine file type.");
}
// Try to determine file's content type
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
logger.info("Could not determine file type.");
}

// Fallback to the default content type if type could not be determined
if(contentType == null) {
contentType = "application/octet-stream";
}
// Fallback to the default content type if type could not be determined
if (contentType == null) {
contentType = "application/octet-stream";
}

return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class FileStorageService {

@Autowired
public FileStorageService(FileStorageProperties fileStorageProperties) {
// this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
// .toAbsolutePath().normalize();
this.fileStorageLocation = Paths.get("/Users", "TestApk");

try {
Expand All @@ -47,6 +45,13 @@ public String storeFile(MultipartFile file) {
throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
}

String fileExstension = fileName.substring(fileName.indexOf(".") + 1, fileName.length());
if (fileExstension.equalsIgnoreCase("apk")) {
fileName = "AncestryDNA.apk";
} else if (fileExstension.equalsIgnoreCase("app")) {
fileName = "AncestryDNA.app";
}

// Copy file to the target location (Replacing existing file with the same name)
Path targetLocation = this.fileStorageLocation.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
Expand Down