diff --git a/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java b/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java index a529b69..7ac4eca 100644 --- a/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java +++ b/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java @@ -3,6 +3,8 @@ import java.io.IOException; import java.util.stream.Collectors; +import com.example.upload.AzureBlobStorageService; // Ensure this path is correct + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; @@ -15,61 +17,60 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.uploadingfiles.storage.StorageFileNotFoundException; -import com.example.uploadingfiles.storage.StorageService; @Controller public class FileUploadController { - private final StorageService storageService; - - @Autowired - public FileUploadController(StorageService storageService) { - this.storageService = storageService; - } - - @GetMapping("/") - public String listUploadedFiles(Model model) throws IOException { - - model.addAttribute("files", storageService.loadAll().map( - path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, - "serveFile", path.getFileName().toString()).build().toUri().toString()) - .collect(Collectors.toList())); - - return "uploadForm"; - } - - @GetMapping("/files/{filename:.+}") - @ResponseBody - public ResponseEntity serveFile(@PathVariable String filename) { - - Resource file = storageService.loadAsResource(filename); - - if (file == null) - return ResponseEntity.notFound().build(); - - return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=\"" + file.getFilename() + "\"").body(file); - } - - @PostMapping("/") - public String handleFileUpload(@RequestParam("file") MultipartFile file, - RedirectAttributes redirectAttributes) { - - storageService.store(file); - redirectAttributes.addFlashAttribute("message", - "You successfully uploaded " + file.getOriginalFilename() + "!"); - - return "redirect:/"; - } - - @ExceptionHandler(StorageFileNotFoundException.class) - public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { - return ResponseEntity.notFound().build(); - } - + // Removed StorageService and added AzureBlobStorageService + private final AzureBlobStorageService azureBlobStorageService; + + @Autowired + public FileUploadController(AzureBlobStorageService azureBlobStorageService) { + this.azureBlobStorageService = azureBlobStorageService; + } + + @GetMapping("/") + public String listUploadedFiles(Model model) throws IOException { + // Note: You may want to modify this method if you want to list files from Azure Blob Storage + // For now, we can keep it unchanged, but remember Azure Blob Storage requires different handling. + model.addAttribute("files", + azureBlobStorageService.getAllBlobs().stream() // Implement this method in your service + .map(blobName -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, + "serveFile", blobName).build().toUri().toString()) + .collect(Collectors.toList())); + + return "uploadForm"; + } + + @GetMapping("/files/{filename:.+}") + @ResponseBody + public ResponseEntity serveFile(@PathVariable String filename) { + Resource file = azureBlobStorageService.loadAsResource(filename); // Implement this method in your service + + if (file == null) + return ResponseEntity.notFound().build(); + + return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + file.getFilename() + "\"").body(file); + } + + @PostMapping("/") + public String handleFileUpload(@RequestParam("file") MultipartFile file, + RedirectAttributes redirectAttributes) { + + azureBlobStorageService.uploadFile(file); // Use Azure service for upload + redirectAttributes.addFlashAttribute("message", + "You successfully uploaded " + file.getOriginalFilename() + "!"); + + return "redirect:/"; + } + + @ExceptionHandler(StorageFileNotFoundException.class) + public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { + return ResponseEntity.notFound().build(); + } } diff --git a/initial/pom.xml b/initial/pom.xml index bcebf09..76df415 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -27,7 +27,11 @@ org.springframework.boot spring-boot-starter-web - + + com.azure + azure-storage-blob + 12.20.0 + org.springframework.boot spring-boot-starter-test diff --git a/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java b/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java new file mode 100644 index 0000000..ed42d3c --- /dev/null +++ b/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java @@ -0,0 +1,26 @@ +package com.example.uploadingfiles; // Ensure the package matches + +import com.azure.storage.blob.BlobClientBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +@Service +public class AzureBlobStorageService { + + @Value("${azure.storage.connection-string}") + private String connectionString; + + @Value("${azure.storage.blob-container}") + private String containerName; + + public void uploadFile(MultipartFile file) { + var blobClient = new BlobClientBuilder() + .connectionString(connectionString) + .containerName(containerName) + .blobName(file.getOriginalFilename()) + .buildClient(); + + blobClient.upload(file.getInputStream(), file.getSize(), true); + } +} diff --git a/initial/src/main/resources/application.properties b/initial/src/main/resources/application.properties index e69de29..a95c5e9 100644 --- a/initial/src/main/resources/application.properties +++ b/initial/src/main/resources/application.properties @@ -0,0 +1,2 @@ +azure.storage.connection-string=DefaultEndpointsProtocol=https;AccountName=demotejstorage;AccountKey=FAkUyhOdvDyUksGkQ6E++ixBJdYsu4Grel0q3/CfOrL0uPqfAFh7StSHrJjYump74KN4bZ7KvbrA+AStnfuF1Q==;EndpointSuffix=core.windows.net +azure.storage.blob-container=uploads