|
|
@@ -1,9 +1,7 @@
|
|
|
package cn.reghao.dfs.store.controller;
|
|
|
|
|
|
import cn.reghao.dfs.store.service.FileUrlService;
|
|
|
-import cn.reghao.dfs.store.service.media.MediaQuality;
|
|
|
-import cn.reghao.dfs.store.service.media.MediaResolution;
|
|
|
-import cn.reghao.dfs.store.util.media.ImageOps;
|
|
|
+import cn.reghao.dfs.store.service.media.ImageFileService;
|
|
|
import org.springframework.core.io.InputStreamResource;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -11,8 +9,6 @@ import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
|
|
|
/**
|
|
|
@@ -23,9 +19,11 @@ import java.io.*;
|
|
|
@RequestMapping
|
|
|
public class ImageFileController {
|
|
|
private final FileUrlService fileUrlService;
|
|
|
+ private final ImageFileService imageFileService;
|
|
|
|
|
|
- public ImageFileController(FileUrlService fileUrlService) {
|
|
|
+ public ImageFileController(FileUrlService fileUrlService, ImageFileService imageFileService) {
|
|
|
this.fileUrlService = fileUrlService;
|
|
|
+ this.imageFileService = imageFileService;
|
|
|
}
|
|
|
|
|
|
@GetMapping("/image/{filename}")
|
|
|
@@ -49,60 +47,27 @@ public class ImageFileController {
|
|
|
@GetMapping("/image1/{filename:.+}")
|
|
|
public ResponseEntity<InputStreamResource> image1(@PathVariable("filename") String filename) throws Exception {
|
|
|
String[] strs = filename.split("@");
|
|
|
- /*String uploadId = strs[0].split("\\.")[0];
|
|
|
+ String uploadId = strs[0].split("\\.")[0];
|
|
|
String filePath = fileUrlService.getFilePath(uploadId);
|
|
|
if (filePath == null) {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
|
|
- }*/
|
|
|
+ }
|
|
|
|
|
|
+ InputStream inputStream;
|
|
|
if (strs.length == 1) {
|
|
|
- String filePath = "/home/reghao/Downloads/0.jpg";
|
|
|
- InputStream inputStream = new FileInputStream(filePath);
|
|
|
- InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
|
|
|
-
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.set("Pragma", "No-cache");
|
|
|
- httpHeaders.set("Cache-Control", "no-cache");
|
|
|
- return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).contentType(MediaType.IMAGE_JPEG)
|
|
|
- .body(inputStreamResource);
|
|
|
+ inputStream = new FileInputStream(filePath);
|
|
|
+ } else {
|
|
|
+ String[] params = strs[1].split("_");
|
|
|
+ int width = Integer.parseInt(params[0].replace("w", ""));
|
|
|
+ int height = Integer.parseInt(params[1].replace("h", ""));
|
|
|
+ inputStream = imageFileService.getImageStream(filePath, width, height);
|
|
|
}
|
|
|
|
|
|
- String[] params = strs[1].split("_");
|
|
|
- int width = Integer.parseInt(params[0].replace("w", ""));
|
|
|
- int height = Integer.parseInt(params[1].replace("h", ""));
|
|
|
-
|
|
|
- String filePath = "/home/reghao/Downloads/0.jpg";
|
|
|
- InputStream inputStream = getImageStream(filePath, width, height);
|
|
|
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
|
|
|
-
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
httpHeaders.set("Pragma", "No-cache");
|
|
|
httpHeaders.set("Cache-Control", "no-cache");
|
|
|
return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).contentType(MediaType.IMAGE_JPEG)
|
|
|
.body(inputStreamResource);
|
|
|
}
|
|
|
-
|
|
|
- private InputStream getImageStream(String filePath, int reqWidth, int reqHeight) throws IOException {
|
|
|
- // TODO 添加一层缓存
|
|
|
- File file = new File(filePath);
|
|
|
- ImageOps.Size imgSize = ImageOps.info(file);
|
|
|
- int width = imgSize.getWidth();
|
|
|
- int height = imgSize.getHeight();
|
|
|
-
|
|
|
- MediaResolution quality = MediaQuality.getQuality1(width, height);
|
|
|
- int height1 = quality.getHeight();
|
|
|
-
|
|
|
- MediaResolution reqQuality = MediaQuality.getQuality1(reqWidth, reqHeight);
|
|
|
- int hight2 = reqQuality.getHeight();
|
|
|
-
|
|
|
- if (height1 < hight2) {
|
|
|
- return new FileInputStream(filePath);
|
|
|
- } else {
|
|
|
- int ratio = height1/hight2;
|
|
|
- BufferedImage bufferedImage = ImageOps.resize(filePath, ratio);
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(bufferedImage, "jpg", baos);
|
|
|
- return new ByteArrayInputStream(baos.toByteArray());
|
|
|
- }
|
|
|
- }
|
|
|
}
|