refact: 图片存储服务的API调整
This commit is contained in:
@@ -68,7 +68,7 @@ public class AdminStorageController {
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public Object create(@LoginAdmin Integer adminId, @RequestParam("file") MultipartFile file) {
|
||||
public Object create(@LoginAdmin Integer adminId, @RequestParam("file") MultipartFile file) throws IOException {
|
||||
if(adminId == null){
|
||||
return ResponseUtil.unlogin();
|
||||
}
|
||||
@@ -81,7 +81,7 @@ public class AdminStorageController {
|
||||
return ResponseUtil.badArgumentValue();
|
||||
}
|
||||
String key = generateKey(originalFilename);
|
||||
storageService.store(file, key);
|
||||
storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), key);
|
||||
|
||||
String url = storageService.generateUrl(key);
|
||||
LitemallStorage storageInfo = new LitemallStorage();
|
||||
|
||||
@@ -81,12 +81,6 @@
|
||||
<artifactId>weixin-java-pay</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.0.7.RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -6,9 +6,7 @@ import org.linlinjava.litemall.core.storage.StorageService;
|
||||
import org.linlinjava.litemall.core.system.SystemConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
@@ -41,9 +39,9 @@ public class QCodeService {
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
//将商品图片,商品名字,商城名字画到模版图中
|
||||
byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName());
|
||||
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), "image/jpeg", imageData);
|
||||
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
|
||||
//存储分享图
|
||||
storageService.store(multipartFile, getKeyName(goodId));
|
||||
storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId));
|
||||
} catch (WxErrorException e) {
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
|
||||
@@ -6,8 +6,8 @@ import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
@@ -74,14 +74,14 @@ public class AliyunStorage implements Storage {
|
||||
* 阿里云OSS对象存储简单上传实现
|
||||
*/
|
||||
@Override
|
||||
public void store(MultipartFile file, String keyName) {
|
||||
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
|
||||
try {
|
||||
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(file.getSize());
|
||||
objectMetadata.setContentType(file.getContentType());
|
||||
objectMetadata.setContentLength(contentLength);
|
||||
objectMetadata.setContentType(contentType);
|
||||
// 对象键(Key)是对象在存储桶中的唯一标识。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, file.getInputStream(), objectMetadata);
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
|
||||
PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
@@ -3,9 +3,9 @@ package org.linlinjava.litemall.core.storage;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -47,9 +47,9 @@ public class LocalStorage implements Storage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(MultipartFile file, String keyName) {
|
||||
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
|
||||
try {
|
||||
Files.copy(file.getInputStream(), rootLocation.resolve(keyName), StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(inputStream, rootLocation.resolve(keyName), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to store file " + keyName, e);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.linlinjava.litemall.core.storage;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -13,10 +13,12 @@ public interface Storage {
|
||||
|
||||
/**
|
||||
* 存储一个文件对象
|
||||
* @param file SpringBoot MultipartFile文件对象
|
||||
* @param inputStream 文件输入流
|
||||
* @param contentLength 文件长度
|
||||
* @param contentType 文件类型
|
||||
* @param keyName 文件索引名
|
||||
*/
|
||||
void store(MultipartFile file, String keyName);
|
||||
void store(InputStream inputStream, long contentLength, String contentType, String keyName);
|
||||
|
||||
Stream<Path> loadAll();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.linlinjava.litemall.core.storage;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -31,12 +32,13 @@ public class StorageService {
|
||||
|
||||
/**
|
||||
* 存储一个文件对象
|
||||
*
|
||||
* @param file SpringBoot MultipartFile文件对象
|
||||
* @param keyName 文件索引名
|
||||
* @param inputStream 文件输入流
|
||||
* @param contentLength 文件长度
|
||||
* @param contentType 文件类型
|
||||
* @param keyName 文件索引名
|
||||
*/
|
||||
public void store(MultipartFile file, String keyName) {
|
||||
storage.store(file, keyName);
|
||||
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
|
||||
storage.store(inputStream, contentLength, contentType, keyName);
|
||||
}
|
||||
|
||||
public Stream<Path> loadAll() {
|
||||
|
||||
@@ -8,13 +8,10 @@ import com.qcloud.cos.model.ObjectMetadata;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.PutObjectResult;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
@@ -81,14 +78,14 @@ public class TencentStorage implements Storage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(MultipartFile file, String keyName) {
|
||||
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
|
||||
try {
|
||||
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(file.getSize());
|
||||
objectMetadata.setContentType(file.getContentType());
|
||||
objectMetadata.setContentLength(contentLength);
|
||||
objectMetadata.setContentType(contentType);
|
||||
// 对象键(Key)是对象在存储桶中的唯一标识。例如,在对象的访问域名 `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg` 中,对象键为 doc1/pic1.jpg, 详情参考 [对象键](https://cloud.tencent.com/document/product/436/13324)
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, file.getInputStream(), objectMetadata);
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
|
||||
PutObjectResult putObjectResult = getCOSClient().putObject(putObjectRequest);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -24,9 +25,8 @@ public class AliyunStorageTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String test = getClass().getClassLoader().getResource("litemall.png").getFile();
|
||||
byte[] content = (byte[])FileCopyUtils.copyToByteArray(new FileInputStream(test));
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("litemall.png", "litemall.png", "image/png", content);
|
||||
aliyunStorage.store(mockMultipartFile, "litemall.png");
|
||||
File testFile = new File(test);
|
||||
aliyunStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png");
|
||||
Resource resource = aliyunStorage.loadAsResource("litemall.png");
|
||||
String url = aliyunStorage.generateUrl("litemall.png");
|
||||
System.out.println("test file " + test);
|
||||
|
||||
@@ -6,11 +6,9 @@ import org.linlinjava.litemall.core.storage.LocalStorage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -24,9 +22,8 @@ public class LocalStorageTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String test = getClass().getClassLoader().getResource("litemall.png").getFile();
|
||||
byte[] content = (byte[])FileCopyUtils.copyToByteArray(new FileInputStream(test));
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("litemall.png", "litemall.png", "image/jpeg", content);
|
||||
localStorage.store(mockMultipartFile, "litemall.png");
|
||||
File testFile = new File(test);
|
||||
localStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png");
|
||||
Resource resource = localStorage.loadAsResource("litemall.png");
|
||||
String url = localStorage.generateUrl("litemall.png");
|
||||
System.out.println("test file " + test);
|
||||
|
||||
@@ -6,11 +6,10 @@ import org.linlinjava.litemall.core.storage.TencentStorage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -24,9 +23,8 @@ public class TencentStorageTest {
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String test = getClass().getClassLoader().getResource("litemall.png").getFile();
|
||||
byte[] content = (byte[])FileCopyUtils.copyToByteArray(new FileInputStream(test));
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("litemall.png", "litemall.png", "image/png", content);
|
||||
tencentStorage.store(mockMultipartFile, "litemall.png");
|
||||
File testFile = new File(test);
|
||||
tencentStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png");
|
||||
Resource resource = tencentStorage.loadAsResource("litemall.png");
|
||||
String url = tencentStorage.generateUrl("litemall.png");
|
||||
System.out.println("test file " + test);
|
||||
|
||||
@@ -48,7 +48,7 @@ public class WxStorageController {
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public Object upload(@RequestParam("file") MultipartFile file) {
|
||||
public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
@@ -58,7 +58,7 @@ public class WxStorageController {
|
||||
return ResponseUtil.badArgumentValue();
|
||||
}
|
||||
String key = generateKey(originalFilename);
|
||||
storageService.store(file, key);
|
||||
storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), key);
|
||||
|
||||
String url = storageService.generateUrl(key);
|
||||
LitemallStorage storageInfo = new LitemallStorage();
|
||||
|
||||
Reference in New Issue
Block a user