Commit b4942f18 authored by Junling Bu's avatar Junling Bu
Browse files

update[litemall-os-api]: 对象存储服务可以在线查看图片。

parent 0bb04413
...@@ -9,6 +9,7 @@ import org.linlinjava.litemall.os.config.ObjectStorageConfig; ...@@ -9,6 +9,7 @@ import org.linlinjava.litemall.os.config.ObjectStorageConfig;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -34,15 +35,18 @@ public class StorageController { ...@@ -34,15 +35,18 @@ public class StorageController {
private ObjectStorageConfig osConfig; private ObjectStorageConfig osConfig;
private String generateUrl(String key){ private String generateUrl(String key){
return "http://" + osConfig.getAddress() + ":" + osConfig.getPort() + "/storage/storage/fetch?key=" + key; return "http://" + osConfig.getAddress() + ":" + osConfig.getPort() + "/storage/storage/fetch/" + key;
} }
private final String generateKey(){ private String generateKey(String originalFilename){
int index = originalFilename.lastIndexOf('.');
String suffix = originalFilename.substring(index);
String key = null; String key = null;
LitemallStorage storageInfo = null; LitemallStorage storageInfo = null;
do{ do{
key = CharUtil.getRandomString(20); key = CharUtil.getRandomString(20) + suffix;
storageInfo = litemallStorageService.findByKey(key); storageInfo = litemallStorageService.findByKey(key);
} }
while(storageInfo != null); while(storageInfo != null);
...@@ -74,7 +78,7 @@ public class StorageController { ...@@ -74,7 +78,7 @@ public class StorageController {
e.printStackTrace(); e.printStackTrace();
return ResponseUtil.badArgumentValue(); return ResponseUtil.badArgumentValue();
} }
String key = generateKey(); String key = generateKey(originalFilename);
storageService.store(inputStream, key); storageService.store(inputStream, key);
String url = generateUrl(key); String url = generateUrl(key);
...@@ -116,25 +120,36 @@ public class StorageController { ...@@ -116,25 +120,36 @@ public class StorageController {
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
@GetMapping("/fetch") @GetMapping("/fetch/{key:.+}")
public ResponseEntity<Resource> fetch(String key) { public ResponseEntity<Resource> fetch(@PathVariable String key) {
LitemallStorage litemallStorage = litemallStorageService.findByKey(key);
if(key == null){
ResponseEntity.notFound();
}
String type = litemallStorage.getType();
MediaType mediaType = MediaType.parseMediaType(type);
Resource file = storageService.loadAsResource(key); Resource file = storageService.loadAsResource(key);
if(file == null) { if(file == null) {
ResponseEntity.notFound(); ResponseEntity.notFound();
} }
return ResponseEntity.ok().body(file); return ResponseEntity.ok().contentType(mediaType).body(file);
} }
@GetMapping("/download") @GetMapping("/download/{key:.+}")
public ResponseEntity<Resource> download(String key) { public ResponseEntity<Resource> download(@PathVariable String key) {
LitemallStorage litemallStorage = litemallStorageService.findByKey(key);
if(key == null){
ResponseEntity.notFound();
}
String type = litemallStorage.getType();
MediaType mediaType = MediaType.parseMediaType(type);
Resource file = storageService.loadAsResource(key); Resource file = storageService.loadAsResource(key);
if(file == null) { if(file == null) {
ResponseEntity.notFound(); ResponseEntity.notFound();
} }
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, return ResponseEntity.ok().contentType(mediaType).header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file); "attachment; filename=\"" + file.getFilename() + "\"").body(file);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment