Skip to content

Commit 9556753

Browse files
committed
image manipulation WIP
1 parent b6ffa02 commit 9556753

File tree

5 files changed

+124
-23
lines changed

5 files changed

+124
-23
lines changed
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
package com.benodeveloper.medialibrary.controllers;
22

3+
import java.nio.file.Path;
34
import java.util.Arrays;
45

6+
import com.benodeveloper.medialibrary.entities.Media;
7+
import com.benodeveloper.medialibrary.services.ImageService;
58
import org.springframework.http.ResponseEntity;
69
import org.springframework.web.bind.annotation.PostMapping;
710
import org.springframework.web.bind.annotation.RequestMapping;
811
import org.springframework.web.bind.annotation.RequestParam;
912
import org.springframework.web.bind.annotation.RestController;
1013
import org.springframework.web.multipart.MultipartFile;
1114

15+
import java.nio.file.Paths;
16+
import java.util.UUID;
17+
18+
import com.benodeveloper.medialibrary.services.StorageService;
19+
1220
@RestController
1321
@RequestMapping("/api/media/images")
1422
public class ImageController {
1523

16-
public static final String[] ALLOWED_CONTENT_TYPE = new String[]{
17-
"image/gif",
18-
"image/jpeg",
19-
"image/png",
20-
"image/svg+xml",
21-
"image/tiff",
22-
"image/x-tiff",
23-
"image/apng",
24-
"image/avif",
25-
"image/webp",};
24+
private final ImageService imageService;
25+
26+
public ImageController(ImageService imageService) {
27+
this.imageService = imageService;
28+
}
29+
2630

2731
@PostMapping("/upload")
28-
public ResponseEntity<?> uploadImage(@RequestParam("image") MultipartFile file) {
29-
// TODO: handle upload in a separate directory + convert image to jpeg or webp + crop images, ...
30-
try {
31-
if (!Arrays.asList(ALLOWED_CONTENT_TYPE).contains(file.getContentType())) {
32-
throw new Exception("Unsupported image content type");
33-
}
34-
return ResponseEntity.ok(file.getOriginalFilename());
35-
} catch (Exception e) {
36-
return ResponseEntity.internalServerError().body(e.getMessage());
37-
}
32+
public ResponseEntity<?> uploadImage(@RequestParam("image") MultipartFile file) throws Exception {
33+
// UUID mediaUUID = UUID.randomUUID();
34+
// Path path = imageService.saveImage(file);
35+
//// storageService.saveFile(file, mediaUUID.toString());
36+
//
37+
// Media media = Media.builder()
38+
// .UUID(mediaUUID)
39+
// .fileName(path.getFileName().toString())
40+
// .name(file.getOriginalFilename())
41+
// .fileType(file.getContentType())
42+
// .size(file.getSize())
43+
// .path(path.toString())
44+
// .build();
45+
return ResponseEntity.ok("");
3846
}
3947
}

src/main/java/com/benodeveloper/medialibrary/controllers/MediaController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public ResponseEntity<?> getResource(@PathVariable("filename") String filename)
6969

7070
/**
7171
* Get media by uuid.
72+
*
7273
* @param uuid
7374
* @return
7475
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.benodeveloper.medialibrary.services;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.web.multipart.MultipartFile;
6+
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.Arrays;
10+
import java.util.Objects;
11+
12+
import static com.benodeveloper.medialibrary.utils.ImageUtils.*;
13+
14+
@Service
15+
public class ImageService {
16+
17+
private final StorageService storageService;
18+
19+
public ImageService(StorageService storageService) {
20+
this.storageService = storageService;
21+
}
22+
23+
public Path saveImage(MultipartFile file, String filename) throws Exception {
24+
// TODO: handle upload in a separate directory + convert image to jpeg or webp +
25+
// crop images, ...
26+
if (!Arrays.asList(ALLOWED_CONTENT_TYPE).contains(file.getContentType()))
27+
throw new Exception("Unsupported image content type");
28+
if(!Objects.equals(file.getContentType(), JPG_CONTENT_TYPE)) {
29+
// ConvertImageToJPG(file.getInputStream());
30+
}
31+
32+
return storageService.saveFile(file, filename);
33+
}
34+
}

src/main/java/com/benodeveloper/medialibrary/services/StorageService.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public void init() {
4949
*/
5050
public Optional<Resource> loadResource(String filename) throws MalformedURLException {
5151
var file = uploadPath.resolve(filename);
52-
if(Files.exists(file) && Files.isReadable(file)) {
52+
if (Files.exists(file) && Files.isReadable(file)) {
5353
var resource = new UrlResource(file.toUri());
5454
return Optional.of(resource);
5555
}
56-
return Optional.empty();
56+
return Optional.empty();
5757
}
5858

5959
public Stream<Path> loadAll() {
@@ -78,7 +78,19 @@ public Path saveFile(MultipartFile file, String filename) throws Exception {
7878
// get file extension
7979
String fileExtension = FilenameUtils.getExtension(file.getOriginalFilename());
8080
filename = filename + "." + fileExtension;
81-
Path path = this.uploadPath.resolve(filename);
81+
82+
return SaveMultipartFile(file, this.uploadPath.resolve(filename));
83+
}
84+
85+
/**
86+
* Save Multipart file in a given path.
87+
*
88+
* @param file
89+
* @param path
90+
* @return
91+
* @throws Exception
92+
*/
93+
public static Path SaveMultipartFile(MultipartFile file, Path path) throws Exception {
8294

8395
if (Files.exists(path)) {
8496
throw new Exception("The file is already existed");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.benodeveloper.medialibrary.utils;
2+
3+
import javax.imageio.ImageIO;
4+
import java.awt.*;
5+
import java.awt.image.BufferedImage;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
public class ImageUtils {
12+
13+
public static String GIF_CONTENT_TYPE = "image/gif";
14+
public static String JPG_CONTENT_TYPE = "image/jpeg";
15+
public static String PNG_CONTENT_TYPE = "image/png";
16+
public static String TIFF_CONTENT_TYPE = "image/tiff";
17+
public static String X_TIFF_CONTENT_TYPE = "image/x-tiff";
18+
public static String APNG_CONTENT_TYPE = "image/apng";
19+
public static String AVIF_CONTENT_TYPE = "image/avif";
20+
public static String WEBP_CONTENT_TYPE = "image/webp";
21+
22+
public static String[] ALLOWED_CONTENT_TYPE = new String[]{
23+
GIF_CONTENT_TYPE,
24+
JPG_CONTENT_TYPE,
25+
PNG_CONTENT_TYPE,
26+
TIFF_CONTENT_TYPE,
27+
X_TIFF_CONTENT_TYPE,
28+
APNG_CONTENT_TYPE,
29+
AVIF_CONTENT_TYPE,
30+
WEBP_CONTENT_TYPE,
31+
};
32+
33+
public static void ConvertImageToJPG(InputStream inputStream,String filename) throws IOException {
34+
final BufferedImage image = ImageIO.read(inputStream);
35+
inputStream.close();
36+
37+
final BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
38+
convertedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
39+
final FileOutputStream outputStream = new FileOutputStream(filename);
40+
final boolean canWrite = ImageIO.write(convertedImage,"jpg", outputStream);
41+
outputStream.close();
42+
43+
if(!canWrite) throw new IllegalArgumentException("Failed to write image.");
44+
45+
}
46+
}

0 commit comments

Comments
 (0)