You've already forked FrameTour-BE
修改
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.ycwl.basic.storage.adapters;
|
||||
|
||||
import com.ycwl.basic.storage.exceptions.UploadFileFailedException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
@@ -9,16 +10,17 @@ import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
public abstract class AStorageAdapter implements IStorageAdapter {
|
||||
|
||||
@Override
|
||||
public String uploadFile(File file, String ...path) {
|
||||
public String uploadFile(String contentType, File file, String ...path) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
return uploadFile(inputStream, path);
|
||||
return uploadFile(contentType, inputStream, path);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UploadFileFailedException("文件不存在");
|
||||
}
|
||||
@@ -31,8 +33,9 @@ public abstract class AStorageAdapter implements IStorageAdapter {
|
||||
}
|
||||
try {
|
||||
InputStream inputStream = file.getInputStream();
|
||||
return uploadFile(inputStream, path);
|
||||
return uploadFile(file.getContentType(), inputStream, path);
|
||||
} catch (Exception e) {
|
||||
log.warn("文件上传失败", e);
|
||||
throw new UploadFileFailedException("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
@@ -62,7 +62,7 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFile(InputStream inputStream, String ...path) {
|
||||
public String uploadFile(String contentType, InputStream inputStream, String ...path) {
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -71,6 +71,9 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(inputStream.available());
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
metadata.setContentType(contentType);
|
||||
}
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getBucketName(), fullPath, inputStream);
|
||||
ossClient.putObject(putObjectRequest);
|
||||
return getUrl(path);
|
||||
|
@@ -55,7 +55,7 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFile(InputStream inputStream, String... path) {
|
||||
public String uploadFile(String contentType, InputStream inputStream, String... path) {
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -64,6 +64,9 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(inputStream.available());
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
metadata.setContentType(contentType);
|
||||
}
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getBucketName(), fullPath, inputStream, metadata);
|
||||
putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // 设置访问权限,让所有用户都允许访问
|
||||
s3Client.putObject(putObjectRequest);
|
||||
|
@@ -14,8 +14,8 @@ import java.util.Map;
|
||||
public interface IStorageAdapter {
|
||||
void loadConfig(Map<String, String> config);
|
||||
void setConfig(StorageConfig config);
|
||||
String uploadFile(InputStream inputStream, String ...path);
|
||||
String uploadFile(File file, String ...path);
|
||||
String uploadFile(String contentType, InputStream inputStream, String ...path);
|
||||
String uploadFile(String contentType, File file, String ...path);
|
||||
String uploadFile(MultipartFile file, String ...path);
|
||||
boolean deleteFile(String ...path);
|
||||
String getUrl(String ...path);
|
||||
|
@@ -22,7 +22,7 @@ public class LocalStorageAdapter extends AStorageAdapter{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFile(InputStream inputStream, String... path) {
|
||||
public String uploadFile(String contentType, InputStream inputStream, String... path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
@@ -2,12 +2,15 @@ package com.ycwl.basic.storage.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class StorageFileObject {
|
||||
private String path;
|
||||
private String name;
|
||||
private Long size;
|
||||
private Object rawObject;
|
||||
private Date modifyTime;
|
||||
|
||||
public String getFullPath() {
|
||||
return path + "/" + name;
|
||||
|
Reference in New Issue
Block a user