mirror of https://github.com/halo-dev/halo
🐛 修复OSS上传的bug
parent
82ce86311f
commit
2998da98e4
|
@ -148,7 +148,12 @@ public enum BlogPropertiesEnum {
|
||||||
/**
|
/**
|
||||||
* API Token
|
* API Token
|
||||||
*/
|
*/
|
||||||
API_TOKEN("api_token");
|
API_TOKEN("api_token"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 附件存储位置
|
||||||
|
*/
|
||||||
|
ATTACH_LOC("attach_loc");
|
||||||
|
|
||||||
private String prop;
|
private String prop;
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.domain.Attachment;
|
import cc.ryanc.halo.model.domain.Attachment;
|
||||||
import cc.ryanc.halo.model.domain.Options;
|
import cc.ryanc.halo.model.dto.HaloConst;
|
||||||
import cc.ryanc.halo.model.dto.QiNiuPutSet;
|
import cc.ryanc.halo.model.dto.QiNiuPutSet;
|
||||||
import cc.ryanc.halo.model.enums.AttachLocationEnum;
|
import cc.ryanc.halo.model.enums.AttachLocationEnum;
|
||||||
|
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.repository.AttachmentRepository;
|
import cc.ryanc.halo.repository.AttachmentRepository;
|
||||||
import cc.ryanc.halo.repository.OptionsRepository;
|
|
||||||
import cc.ryanc.halo.service.AttachmentService;
|
import cc.ryanc.halo.service.AttachmentService;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cc.ryanc.halo.utils.Md5Util;
|
import cc.ryanc.halo.utils.Md5Util;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.UpYun;
|
import com.UpYun;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
@ -57,9 +58,6 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private AttachmentRepository attachmentRepository;
|
private AttachmentRepository attachmentRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private OptionsRepository optionsRepository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增附件信息
|
* 新增附件信息
|
||||||
*
|
*
|
||||||
|
@ -129,11 +127,11 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> upload(MultipartFile file, HttpServletRequest request) {
|
public Map<String, String> upload(MultipartFile file, HttpServletRequest request) {
|
||||||
Map<String, String> resultMap;
|
Map<String, String> resultMap;
|
||||||
Options options = optionsRepository.findOptionsByOptionName("attach_loc");
|
String attachLoc = HaloConst.OPTIONS.get(BlogPropertiesEnum.ATTACH_LOC.getProp());
|
||||||
if (options == null) {
|
if (StrUtil.isEmpty(attachLoc)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
switch (options.getOptionValue()) {
|
switch (attachLoc) {
|
||||||
case "server":
|
case "server":
|
||||||
resultMap = this.attachUpload(file, request);
|
resultMap = this.attachUpload(file, request);
|
||||||
break;
|
break;
|
||||||
|
@ -255,19 +253,19 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
try {
|
try {
|
||||||
Configuration cfg = new Configuration(Zone.zone0());
|
Configuration cfg = new Configuration(Zone.zone0());
|
||||||
String key = Md5Util.getMD5Checksum(file);
|
String key = Md5Util.getMD5Checksum(file);
|
||||||
Options accessKey = optionsRepository.findOptionsByOptionName("qiniu_access_key");
|
String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
|
||||||
Options secretKey = optionsRepository.findOptionsByOptionName("qiniu_secret_key");
|
String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
|
||||||
Options domain = optionsRepository.findOptionsByOptionName("qiniu_domain");
|
String domain = HaloConst.OPTIONS.get("qiniu_domain");
|
||||||
Options bucket = optionsRepository.findOptionsByOptionName("qiniu_bucket");
|
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
||||||
Options smallUrl = optionsRepository.findOptionsByOptionName("qiniu_small_url");
|
String smallUrl = HaloConst.OPTIONS.get("qiniu_small_url");
|
||||||
if (accessKey == null || secretKey == null || domain == null || bucket == null) {
|
if (accessKey == null || secretKey == null || domain == null || bucket == null) {
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
Auth auth = Auth.create(accessKey.getOptionValue(), secretKey.getOptionValue());
|
Auth auth = Auth.create(accessKey, secretKey);
|
||||||
StringMap putPolicy = new StringMap();
|
StringMap putPolicy = new StringMap();
|
||||||
putPolicy.put("returnBody", "{\"size\":$(fsize),\"w\":$(imageInfo.width),\"h\":$(imageInfo.height)}");
|
putPolicy.put("returnBody", "{\"size\":$(fsize),\"w\":$(imageInfo.width),\"h\":$(imageInfo.height)}");
|
||||||
String upToken = auth.uploadToken(bucket.getOptionValue(), null, 3600, putPolicy);
|
String upToken = auth.uploadToken(bucket, null, 3600, putPolicy);
|
||||||
String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket.getOptionValue()).toString();
|
String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
|
||||||
QiNiuPutSet putSet = new QiNiuPutSet();
|
QiNiuPutSet putSet = new QiNiuPutSet();
|
||||||
try {
|
try {
|
||||||
FileRecorder fileRecorder = new FileRecorder(localTempDir);
|
FileRecorder fileRecorder = new FileRecorder(localTempDir);
|
||||||
|
@ -288,10 +286,10 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
String filePath = domain.getOptionValue().contains("http://") ? domain.getOptionValue().trim() : ("http://" + domain.getOptionValue().trim()) + "/" + key;
|
String filePath = domain.trim() + "/" + key;
|
||||||
resultMap.put("fileName", file.getOriginalFilename());
|
resultMap.put("fileName", file.getOriginalFilename());
|
||||||
resultMap.put("filePath", filePath.trim());
|
resultMap.put("filePath", filePath.trim());
|
||||||
resultMap.put("smallPath", smallUrl == null ? filePath.trim() : (filePath + smallUrl.getOptionValue()).trim());
|
resultMap.put("smallPath", smallUrl == null ? filePath.trim() : (filePath + smallUrl).trim());
|
||||||
resultMap.put("suffix", file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.')));
|
resultMap.put("suffix", file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.')));
|
||||||
resultMap.put("size", HaloUtils.parseSize(file.getSize()));
|
resultMap.put("size", HaloUtils.parseSize(file.getSize()));
|
||||||
resultMap.put("wh", putSet.getW() + "x" + putSet.getH());
|
resultMap.put("wh", putSet.getW() + "x" + putSet.getH());
|
||||||
|
@ -314,26 +312,26 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
Map<String, String> resultMap = new HashMap<>(6);
|
Map<String, String> resultMap = new HashMap<>(6);
|
||||||
try {
|
try {
|
||||||
String key = Md5Util.getMD5Checksum(file);
|
String key = Md5Util.getMD5Checksum(file);
|
||||||
Options ossSrc = optionsRepository.findOptionsByOptionName("upyun_oss_src");
|
String ossSrc = HaloConst.OPTIONS.get("upyun_oss_src");
|
||||||
Options ossPwd = optionsRepository.findOptionsByOptionName("upyun_oss_pwd");
|
String ossPwd = HaloConst.OPTIONS.get("upyun_oss_pwd");
|
||||||
Options bucket = optionsRepository.findOptionsByOptionName("upyun_oss_bucket");
|
String bucket = HaloConst.OPTIONS.get("upyun_oss_bucket");
|
||||||
Options domain = optionsRepository.findOptionsByOptionName("upyun_oss_domain");
|
String domain = HaloConst.OPTIONS.get("upyun_oss_domain");
|
||||||
Options operator = optionsRepository.findOptionsByOptionName("upyun_oss_operator");
|
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
||||||
Options smallUrl = optionsRepository.findOptionsByOptionName("upyun_oss_small");
|
String smallUrl = HaloConst.OPTIONS.get("upyun_oss_small");
|
||||||
if (ossSrc == null || ossPwd == null || domain == null || bucket == null || operator == null) {
|
if (ossSrc == null || ossPwd == null || domain == null || bucket == null || operator == null) {
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
|
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
|
||||||
UpYun upYun = new UpYun(bucket.getOptionValue(), operator.getOptionValue(), ossPwd.getOptionValue());
|
UpYun upYun = new UpYun(bucket, operator, ossPwd);
|
||||||
upYun.setTimeout(60);
|
upYun.setTimeout(60);
|
||||||
upYun.setApiDomain(UpYun.ED_AUTO);
|
upYun.setApiDomain(UpYun.ED_AUTO);
|
||||||
upYun.setDebug(true);
|
upYun.setDebug(true);
|
||||||
upYun.writeFile(ossSrc.getOptionValue() + key + "." + fileSuffix, file.getBytes(), true, null);
|
upYun.writeFile(ossSrc + key + fileSuffix, file.getBytes(), true, null);
|
||||||
String filePath = domain.getOptionValue().contains("http://") ? domain.getOptionValue().trim() : ("http://" + domain.getOptionValue().trim() + ossSrc.getOptionValue() + key + "." + fileSuffix);
|
String filePath = domain.trim() + ossSrc + key + fileSuffix;
|
||||||
String smallPath = filePath;
|
String smallPath = filePath;
|
||||||
if (smallUrl != null) {
|
if (smallUrl != null) {
|
||||||
smallPath += smallUrl.getOptionValue();
|
smallPath += smallUrl;
|
||||||
}
|
}
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
|
@ -362,16 +360,16 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
public boolean deleteQiNiuAttachment(String key) {
|
public boolean deleteQiNiuAttachment(String key) {
|
||||||
boolean flag = true;
|
boolean flag = true;
|
||||||
Configuration cfg = new Configuration(Zone.zone0());
|
Configuration cfg = new Configuration(Zone.zone0());
|
||||||
Options accessKey = optionsRepository.findOptionsByOptionName("qiniu_access_key");
|
String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
|
||||||
Options secretKey = optionsRepository.findOptionsByOptionName("qiniu_secret_key");
|
String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
|
||||||
Options bucket = optionsRepository.findOptionsByOptionName("qiniu_bucket");
|
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
|
||||||
if (accessKey == null || secretKey == null || bucket == null) {
|
if (accessKey == null || secretKey == null || bucket == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Auth auth = Auth.create(accessKey.getOptionValue(), secretKey.getOptionValue());
|
Auth auth = Auth.create(accessKey, secretKey);
|
||||||
BucketManager bucketManager = new BucketManager(auth, cfg);
|
BucketManager bucketManager = new BucketManager(auth, cfg);
|
||||||
try {
|
try {
|
||||||
bucketManager.delete(bucket.getOptionValue(), key);
|
bucketManager.delete(bucket, key);
|
||||||
} catch (QiniuException ex) {
|
} catch (QiniuException ex) {
|
||||||
System.err.println(ex.code());
|
System.err.println(ex.code());
|
||||||
System.err.println(ex.response.toString());
|
System.err.println(ex.response.toString());
|
||||||
|
@ -389,17 +387,17 @@ public class AttachmentServiceImpl implements AttachmentService {
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteUpYunAttachment(String fileName) {
|
public boolean deleteUpYunAttachment(String fileName) {
|
||||||
boolean flag = true;
|
boolean flag = true;
|
||||||
Options ossSrc = optionsRepository.findOptionsByOptionName("upyun_oss_src");
|
String ossSrc = HaloConst.OPTIONS.get("upyun_oss_src");
|
||||||
Options ossPwd = optionsRepository.findOptionsByOptionName("upyun_oss_pwd");
|
String ossPwd = HaloConst.OPTIONS.get("upyun_oss_pwd");
|
||||||
Options bucket = optionsRepository.findOptionsByOptionName("upyun_oss_bucket");
|
String bucket = HaloConst.OPTIONS.get("upyun_oss_bucket");
|
||||||
Options operator = optionsRepository.findOptionsByOptionName("upyun_oss_operator");
|
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
|
||||||
if (ossSrc == null || ossPwd == null || bucket == null || operator == null) {
|
if (ossSrc == null || ossPwd == null || bucket == null || operator == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
UpYun upYun = new UpYun(bucket.getOptionValue(), operator.getOptionValue(), ossPwd.getOptionValue());
|
UpYun upYun = new UpYun(bucket, operator, ossPwd);
|
||||||
upYun.setApiDomain(UpYun.ED_AUTO);
|
upYun.setApiDomain(UpYun.ED_AUTO);
|
||||||
try {
|
try {
|
||||||
flag = upYun.deleteFile(ossSrc.getOptionValue() + fileName);
|
flag = upYun.deleteFile(ossSrc + fileName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (UpException e) {
|
} catch (UpException e) {
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class AttachmentController {
|
||||||
private LocaleMessageUtil localeMessageUtil;
|
private LocaleMessageUtil localeMessageUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取upload的所有图片资源并渲染页面
|
* 复印件列表
|
||||||
*
|
*
|
||||||
* @param model model
|
* @param model model
|
||||||
* @return 模板路径admin/admin_attachment
|
* @return 模板路径admin/admin_attachment
|
||||||
|
|
|
@ -64,7 +64,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="box-footer">
|
<div class="box-footer">
|
||||||
<button type="button" class="btn btn-danger btn-sm pull-left" onclick="btn_delete()"><@spring.message code="common.btn.delete" /></button>
|
<button type="button" class="btn btn-danger btn-sm pull-left" onclick="btn_delete()"><@spring.message code="common.btn.delete" /></button>
|
||||||
<button type="button" class="btn btn-info btn-sm pull-right btn-copy" data-clipboard-text="<#if attachment.attachLocation?? || attachment.attachLocation == 'SERVER'>${options.blog_url!}</#if>${attachment.attachPath}"><@spring.message code='admin.attachments.modal.form.btn.copy-path' /></button>
|
<button type="button" class="btn btn-info btn-sm pull-right btn-copy" data-clipboard-text="<#if !attachment.attachLocation?? || attachment.attachLocation! == 'SERVER'>${options.blog_url!}</#if>${attachment.attachPath}"><@spring.message code='admin.attachments.modal.form.btn.copy-path' /></button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue