Commit d3dfb7ac authored by Menethil's avatar Menethil
Browse files

Merge remote-tracking branch 'origin/master'

parents 1b914cc2 70c32ff9
spring:
profiles:
active: db, core, os
message:
encoding: UTF-8
server:
port: 8081
logging:
level:
root: ERROR
org.springframework: ERROR
org.mybatis: ERROR
org.linlinjava.litemall.os: DEBUG
org.linlinjava.litemall: ERROR
\ No newline at end of file
package org.linlinjava.litemall.wx.web;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.util.CharUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.LitemallStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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
@RequestMapping("/wx/storage")
public class WxStorageController {
@Autowired
private StorageService storageService;
@Autowired
private LitemallStorageService litemallStorageService;
private String generateKey(String originalFilename){
int index = originalFilename.lastIndexOf('.');
String suffix = originalFilename.substring(index);
String key = null;
LitemallStorage storageInfo = null;
do{
key = CharUtil.getRandomString(20) + suffix;
storageInfo = litemallStorageService.findByKey(key);
}
while(storageInfo != null);
return key;
}
@PostMapping("/upload")
public Object upload(@RequestParam("file") MultipartFile file) {
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, key);
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);
}
@GetMapping("/fetch/{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);
if(file == null) {
ResponseEntity.notFound();
}
return ResponseEntity.ok().contentType(mediaType).body(file);
}
@GetMapping("/download/{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);
if(file == null) {
ResponseEntity.notFound();
}
return ResponseEntity.ok().contentType(mediaType).header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
}
// 以下是业务服务器API地址
// 本机开发时使用
// var WxApiRoot = 'http://localhost:8082/wx/';
var WxApiRoot = 'http://localhost:8082/wx/';
// 局域网测试使用
// var WxApiRoot = 'http://192.168.0.101:8082/wx/';
// 云平台部署时使用
var WxApiRoot = 'http://122.152.206.172:8082/wx/';
// var WxApiRoot = 'http://122.152.206.172:8082/wx/';
// 云平台上线时使用
// var WxApiRoot = 'https://www.menethil.com.cn/wx/';
// 以下是图片存储服务器API地址
// 本机开发时使用
// var StorageApi = 'http://localhost:8081/os/storage/create';
// 局域网测试时使用
// var StorageApi = 'http://192.168.0.101:8081/os/storage/create';
// 云平台部署时使用
var StorageApi = 'http://122.152.206.172:8081/os/storage/create';
// 云平台上线时使用
// var StorageApi = 'https://www.menethil.com.cn/os/storage/create';
module.exports = {
IndexUrl: WxApiRoot + 'home/index', //首页数据接口
CatalogList: WxApiRoot + 'catalog/index', //分类目录全部分类数据接口
......@@ -89,5 +79,5 @@ module.exports = {
UserFormIdCreate: WxApiRoot + 'formid/create', //用户FromId,用于发送模版消息
StorageUpload: StorageApi, //图片上传
StorageUpload: WxApiRoot + 'storage/upload' //图片上传
};
\ No newline at end of file
......@@ -22,7 +22,6 @@
<modules>
<module>litemall-core</module>
<module>litemall-db</module>
<module>litemall-os-api</module>
<module>litemall-wx-api</module>
<module>litemall-admin-api</module>
<module>litemall-all</module>
......@@ -40,11 +39,6 @@
<artifactId>litemall-db</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.linlinjava</groupId>
<artifactId>litemall-os-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.linlinjava</groupId>
<artifactId>litemall-wx-api</artifactId>
......
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