in sessions/fall24/books-genai-vertex-springai/src/main/java/services/web/UploadController.java [41:86]
public ResponseEntity<String> receiveMessage(
@RequestParam("file") MultipartFile file,
@RequestParam("bucketName") String bucketName) {
// create a book summary and persist it in the database
long start = System.currentTimeMillis();
logger.info("Book summarization flow : start");
logger.info("Upload image to Cloud Storage bucket {}", bucketName);
try {
// Read the file content
byte[] fileContent = file.getBytes();
// Create a unique file name
String fileName = file.getOriginalFilename();
// Create a BlobId
BlobId blobId = BlobId.of(bucketName, fileName);
// Create BlobInfo
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
.setContentType(file.getContentType())
.build();
// Create a Storage client.
Storage storage = StorageOptions.getDefaultInstance().getService();
// Upload the file to Google Cloud Storage
storage.create(blobInfo, fileContent);
logger.info("File uploaded successfully: {} to bucket: {}", fileName, bucketName);
// If it's an image, you might want to process it or create a summary
if (file.getContentType() != null && file.getContentType().startsWith("image/")) {
// Here you could call a method to process the image or create a summary
// For example: String summary = booksService.createImageSummary(bucketName, fileName);
// return new ResponseEntity<>(summary, HttpStatus.OK);
}
logger.info("Upload file: end {}ms", System.currentTimeMillis() - start);
// return the response to the caller
return new ResponseEntity<>("File uploaded successfully: " + fileName, HttpStatus.OK);
} catch (IOException e) {
logger.error("Failed to upload file", e);
return new ResponseEntity<>("Failed to upload file: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}