|
| 1 | +package com.devd.spring.bookstorecatalogservice.controller; |
| 2 | + |
| 3 | +import com.devd.spring.bookstorecatalogservice.repository.ImageRepository; |
| 4 | +import com.devd.spring.bookstorecatalogservice.repository.dao.ImageModel; |
| 5 | +import com.devd.spring.bookstorecommons.exception.RunTimeExceptionPlaceHolder; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.core.io.ByteArrayResource; |
| 8 | +import org.springframework.core.io.Resource; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | +import org.springframework.web.multipart.MultipartFile; |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Base64; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.zip.DataFormatException; |
| 27 | +import java.util.zip.Deflater; |
| 28 | +import java.util.zip.Inflater; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Devaraj Reddy - 21-Dec-2020 |
| 32 | + */ |
| 33 | +@RestController |
| 34 | +@RequestMapping(path = "image") |
| 35 | +public class ImageUploadController { |
| 36 | + @Autowired |
| 37 | + ImageRepository imageRepository; |
| 38 | + |
| 39 | + @PostMapping("/upload") |
| 40 | + public ResponseEntity<?> uploadImage(@RequestParam("imageFile") MultipartFile file) throws IOException { |
| 41 | + System.out.println("Original Image Byte Size - " + file.getBytes().length); |
| 42 | + ImageModel img = new ImageModel(file.getOriginalFilename(), file.getContentType(), |
| 43 | + compressBytes(file.getBytes())); |
| 44 | + ImageModel savedImage = imageRepository.save(img); |
| 45 | + Map<String, String> response = new HashMap<>(); |
| 46 | + response.put("imageId", savedImage.getImageId()); |
| 47 | + return ResponseEntity.status(HttpStatus.OK).body(response); |
| 48 | + } |
| 49 | + |
| 50 | + @GetMapping(path = "/{imageId}") |
| 51 | + public ResponseEntity<?> getImage(@PathVariable String imageId) { |
| 52 | + try { |
| 53 | + final Optional<ImageModel> retrievedImage = imageRepository.findByImageId(imageId); |
| 54 | + if (!retrievedImage.isPresent()) { |
| 55 | + throw new RunTimeExceptionPlaceHolder("Image doesn't exist!"); |
| 56 | + } |
| 57 | + ByteArrayResource resource = new ByteArrayResource(decompressBytes(retrievedImage.get().getPicByte())); |
| 58 | + String encodedString = Base64.getEncoder().encodeToString(resource.getByteArray()); |
| 59 | + |
| 60 | + Map<String, String> response = new HashMap<>(); |
| 61 | + response.put("imageBase64", encodedString); |
| 62 | + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(encodedString); |
| 63 | + } catch (Exception e) { |
| 64 | + throw new RunTimeExceptionPlaceHolder("Unable to get image"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // compress the image bytes before storing it in the database |
| 69 | + public static byte[] compressBytes(byte[] data) { |
| 70 | + Deflater deflater = new Deflater(); |
| 71 | + deflater.setInput(data); |
| 72 | + deflater.finish(); |
| 73 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); |
| 74 | + byte[] buffer = new byte[1024]; |
| 75 | + while (!deflater.finished()) { |
| 76 | + int count = deflater.deflate(buffer); |
| 77 | + outputStream.write(buffer, 0, count); |
| 78 | + } |
| 79 | + try { |
| 80 | + outputStream.close(); |
| 81 | + } catch (IOException e) { |
| 82 | + } |
| 83 | + System.out.println("Compressed Image Byte Size - " + outputStream.toByteArray().length); |
| 84 | + return outputStream.toByteArray(); |
| 85 | + } |
| 86 | + |
| 87 | + // uncompress the image bytes before returning it to the angular application |
| 88 | + public static byte[] decompressBytes(byte[] data) { |
| 89 | + Inflater inflater = new Inflater(); |
| 90 | + inflater.setInput(data); |
| 91 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); |
| 92 | + byte[] buffer = new byte[1024]; |
| 93 | + try { |
| 94 | + while (!inflater.finished()) { |
| 95 | + int count = inflater.inflate(buffer); |
| 96 | + outputStream.write(buffer, 0, count); |
| 97 | + } |
| 98 | + outputStream.close(); |
| 99 | + } catch (IOException ioe) { |
| 100 | + } catch (DataFormatException e) { |
| 101 | + } |
| 102 | + return outputStream.toByteArray(); |
| 103 | + } |
| 104 | +} |
0 commit comments