|
|
@@ -15,10 +15,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@RestController
|
|
|
@@ -31,18 +28,18 @@ public class WxStorageController {
|
|
|
@Autowired
|
|
|
private LitemallStorageService litemallStorageService;
|
|
|
|
|
|
- private String generateKey(String originalFilename){
|
|
|
+ private String generateKey(String originalFilename) {
|
|
|
int index = originalFilename.lastIndexOf('.');
|
|
|
String suffix = originalFilename.substring(index);
|
|
|
|
|
|
String key = null;
|
|
|
LitemallStorage storageInfo = null;
|
|
|
|
|
|
- do{
|
|
|
+ do {
|
|
|
key = CharUtil.getRandomString(20) + suffix;
|
|
|
storageInfo = litemallStorageService.findByKey(key);
|
|
|
}
|
|
|
- while(storageInfo != null);
|
|
|
+ while (storageInfo != null);
|
|
|
|
|
|
return key;
|
|
|
}
|
|
|
@@ -50,40 +47,24 @@ public class WxStorageController {
|
|
|
@PostMapping("/upload")
|
|
|
public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
|
- InputStream inputStream = null;
|
|
|
- try {
|
|
|
- inputStream = file.getInputStream();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return ResponseUtil.badArgumentValue();
|
|
|
- }
|
|
|
- String key = generateKey(originalFilename);
|
|
|
- storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), key);
|
|
|
+ String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
|
|
|
|
|
|
- String url = storageService.generateUrl(key);
|
|
|
- LitemallStorage storageInfo = new LitemallStorage();
|
|
|
- storageInfo.setName(originalFilename);
|
|
|
- storageInfo.setSize((int)file.getSize());
|
|
|
- storageInfo.setType(file.getContentType());
|
|
|
- storageInfo.setAddTime(LocalDateTime.now());
|
|
|
- storageInfo.setModified(LocalDateTime.now());
|
|
|
- storageInfo.setKey(key);
|
|
|
- storageInfo.setUrl(url);
|
|
|
- litemallStorageService.add(storageInfo);
|
|
|
- return ResponseUtil.ok(storageInfo);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("url", url);
|
|
|
+ return ResponseUtil.ok(data);
|
|
|
}
|
|
|
|
|
|
@GetMapping("/fetch/{key:.+}")
|
|
|
public ResponseEntity<Resource> fetch(@PathVariable String key) {
|
|
|
LitemallStorage litemallStorage = litemallStorageService.findByKey(key);
|
|
|
- if(key == null){
|
|
|
+ if (key == null) {
|
|
|
ResponseEntity.notFound();
|
|
|
}
|
|
|
String type = litemallStorage.getType();
|
|
|
MediaType mediaType = MediaType.parseMediaType(type);
|
|
|
|
|
|
Resource file = storageService.loadAsResource(key);
|
|
|
- if(file == null) {
|
|
|
+ if (file == null) {
|
|
|
ResponseEntity.notFound();
|
|
|
}
|
|
|
return ResponseEntity.ok().contentType(mediaType).body(file);
|
|
|
@@ -92,14 +73,14 @@ public class WxStorageController {
|
|
|
@GetMapping("/download/{key:.+}")
|
|
|
public ResponseEntity<Resource> download(@PathVariable String key) {
|
|
|
LitemallStorage litemallStorage = litemallStorageService.findByKey(key);
|
|
|
- if(key == null){
|
|
|
+ if (key == null) {
|
|
|
ResponseEntity.notFound();
|
|
|
}
|
|
|
String type = litemallStorage.getType();
|
|
|
MediaType mediaType = MediaType.parseMediaType(type);
|
|
|
|
|
|
Resource file = storageService.loadAsResource(key);
|
|
|
- if(file == null) {
|
|
|
+ if (file == null) {
|
|
|
ResponseEntity.notFound();
|
|
|
}
|
|
|
return ResponseEntity.ok().contentType(mediaType).header(HttpHeaders.CONTENT_DISPOSITION,
|