Commit dbabffda by 1539136324@qq.com

优化素材上传添加本地上传与云上传自由切换的开关,目前针对云上传优化

1 parent 98472295
...@@ -8,6 +8,8 @@ import com.linkwechat.common.core.domain.AjaxResult; ...@@ -8,6 +8,8 @@ import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.file.FileUploadUtils; import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.common.utils.file.FileUtils; import com.linkwechat.common.utils.file.FileUtils;
import com.linkwechat.framework.web.domain.server.SysFile;
import com.linkwechat.framework.web.service.FileService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -34,6 +36,10 @@ public class CommonController { ...@@ -34,6 +36,10 @@ public class CommonController {
@Autowired @Autowired
private CosConfig cosConfig; private CosConfig cosConfig;
@Autowired
private FileService fileService;
/** /**
* 通用下载请求 * 通用下载请求
* *
...@@ -117,11 +123,14 @@ public class CommonController { ...@@ -117,11 +123,14 @@ public class CommonController {
public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception { public AjaxResult uploadFile2Cos(MultipartFile file) throws Exception {
try { try {
String fileName = FileUploadUtils.upload2Cos(file, cosConfig); SysFile sysFile
= fileService.upload(file);
// String fileName = FileUploadUtils.upload2Cos(file, cosConfig);
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName); ajax.put("fileName", sysFile.getFileName());
ajax.put("url", url(fileName)); ajax.put("url", sysFile.getImgUrlPrefix()+sysFile.getFileName());
return ajax; return ajax;
} catch (Exception e) { } catch (Exception e) {
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
...@@ -129,8 +138,8 @@ public class CommonController { ...@@ -129,8 +138,8 @@ public class CommonController {
} }
public String url(String fileName){ // public String url(String fileName){
return "https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/"+fileName; // return "https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/"+fileName;
} // }
} }
...@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; ...@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
* @author Kewen * @author Kewen
*/ */
@Component @Component
@ConfigurationProperties(prefix = "cos") //@ConfigurationProperties(prefix = "cos")
@Data @Data
public class CosConfig { public class CosConfig {
......
package com.linkwechat.common.config;
import lombok.Data;
@Data
public class FileConfig {
/**是否开启云存储*/
private boolean startCosUpload=false;
/**腾讯云存储相关配置*/
private CosConfig cos;
/**文件前缀*/
private String imgUrlPrefix;
}
package com.linkwechat.common.config; package com.linkwechat.common.config;
import com.linkwechat.common.utils.OsUtils;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -34,6 +35,9 @@ public class RuoYiConfig ...@@ -34,6 +35,9 @@ public class RuoYiConfig
private static String weUserDefaultPwd="123456"; private static String weUserDefaultPwd="123456";
private FileConfig file;
/**匿名访问的URL*/ /**匿名访问的URL*/
private String[] anonUrl; private String[] anonUrl;
...@@ -80,7 +84,11 @@ public class RuoYiConfig ...@@ -80,7 +84,11 @@ public class RuoYiConfig
public static String getProfile() public static String getProfile()
{ {
return profile; if(OsUtils.isWindows()){
return "D:/linkWeChat/uploadPath";
}
return "/linkWeChat/uploadPath";
} }
public void setProfile(String profile) public void setProfile(String profile)
...@@ -145,4 +153,12 @@ public class RuoYiConfig ...@@ -145,4 +153,12 @@ public class RuoYiConfig
public void setAnonUrl(String[] anonUrl) { public void setAnonUrl(String[] anonUrl) {
this.anonUrl = anonUrl; this.anonUrl = anonUrl;
} }
public FileConfig getFile() {
return file;
}
public void setFile(FileConfig file) {
this.file = file;
}
} }
package com.linkwechat.common.utils;
/**
* 操作系统相关判断
*/
public class OsUtils {
public static boolean isWindows() {
String osName = System.getProperties().getProperty("os.name");
System.out.println("current system is " + osName);
return osName.toUpperCase().indexOf("WINDOWS") != -1;
}
}
...@@ -56,6 +56,8 @@ public class FileUploadUtils { ...@@ -56,6 +56,8 @@ public class FileUploadUtils {
return defaultBaseDir; return defaultBaseDir;
} }
/** /**
* 以默认配置进行文件上传 * 以默认配置进行文件上传
* *
......
package com.tencent.wework; package com.tencent.wework;
import com.linkwechat.common.utils.OsUtils;
public class Finance { public class Finance {
public native static long NewSdk(); public native static long NewSdk();
...@@ -111,7 +113,7 @@ public class Finance { ...@@ -111,7 +113,7 @@ public class Finance {
public native static int IsMediaDataFinish(long mediaData); public native static int IsMediaDataFinish(long mediaData);
static { static {
if (isWindows()) { if (OsUtils.isWindows()) {
System.loadLibrary("libeay32"); System.loadLibrary("libeay32");
System.loadLibrary("libprotobuf"); System.loadLibrary("libprotobuf");
System.loadLibrary("ssleay32"); System.loadLibrary("ssleay32");
...@@ -123,9 +125,5 @@ public class Finance { ...@@ -123,9 +125,5 @@ public class Finance {
} }
public static boolean isWindows() {
String osName = System.getProperties().getProperty("os.name");
System.out.println("current system is " + osName);
return osName.toUpperCase().indexOf("WINDOWS") != -1;
}
} }
\ No newline at end of file \ No newline at end of file
package com.linkwechat.framework.web.domain.server; package com.linkwechat.framework.web.domain.server;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* 系统文件相关信息 * 系统文件相关信息
* *
* @author ruoyi * @author ruoyi
*/ */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SysFile public class SysFile
{ {
/** /**
...@@ -42,73 +51,17 @@ public class SysFile ...@@ -42,73 +51,17 @@ public class SysFile
*/ */
private double usage; private double usage;
public String getDirName()
{
return dirName;
}
public void setDirName(String dirName)
{
this.dirName = dirName;
}
public String getSysTypeName()
{
return sysTypeName;
}
public void setSysTypeName(String sysTypeName)
{
this.sysTypeName = sysTypeName;
}
public String getTypeName()
{
return typeName;
}
public void setTypeName(String typeName)
{
this.typeName = typeName;
}
public String getTotal()
{
return total;
}
public void setTotal(String total) /**
{ * 文件名
this.total = total; */
} private String fileName;
public String getFree()
{
return free;
}
public void setFree(String free)
{
this.free = free;
}
public String getUsed()
{
return used;
}
public void setUsed(String used) /**
{ * 文件前缀
this.used = used; */
} private String imgUrlPrefix;
public double getUsage()
{
return usage;
}
public void setUsage(double usage)
{
this.usage = usage;
}
} }
package com.linkwechat.framework.web.service;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.utils.OsUtils;
import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.framework.web.domain.server.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* 文件服务
*/
@Component
public class FileService {
@Autowired
private RuoYiConfig ruoYiConfig;
private static final String WINDOWSFILEPATH="D:/linkWeChat/uploadPath";
private static final String LINUXFILEPATH="/linkWeChat/uploadPath";
/**
* 文件上传
* @param file
* @return
* @throws IOException
*/
public SysFile upload(MultipartFile file) throws IOException {
String fileName="";
if(ruoYiConfig.getFile().isStartCosUpload()){//开启云上传
//开启云上传开关则云上传,不然上传本地
fileName = FileUploadUtils.upload2Cos(file, ruoYiConfig.getFile().getCos());
}else {//本地上传
File osFile=OsUtils.isWindows()?new File(WINDOWSFILEPATH):new File(LINUXFILEPATH);
if(!osFile.exists()){
osFile.mkdirs();
}
fileName = FileUploadUtils.upload(osFile.getPath(), file);
}
return SysFile.builder()
.fileName(fileName)
.imgUrlPrefix(ruoYiConfig.getFile().getImgUrlPrefix())
.build();
}
}
...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl; ...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig; import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.config.ServerConfig; import com.linkwechat.common.config.ServerConfig;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
...@@ -40,38 +41,45 @@ public class WeMaterialServiceImpl implements IWeMaterialService { ...@@ -40,38 +41,45 @@ public class WeMaterialServiceImpl implements IWeMaterialService {
@Autowired @Autowired
private WeMaterialMapper weMaterialMapper; private WeMaterialMapper weMaterialMapper;
@Autowired
private ServerConfig serverConfig;
@Autowired @Autowired
private WeMediaClient weMediaClient; private WeMediaClient weMediaClient;
@Autowired
private CosConfig cosConfig;
@Override @Override
public WeMaterialFileVO uploadWeMaterialFile(MultipartFile file, String type) { public WeMaterialFileVO uploadWeMaterialFile(MultipartFile file, String type) {
if (null == file) { if (null == file) {
throw new WeComException("文件为空!"); throw new WeComException("文件为空!");
} }
try { try {
//上传文件到文件服务器
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath(); // /**
String fileName = ""; // * 当前上传腾讯云
if (com.linkwechat.common.enums.MediaType .FILE.getType().equals(type)) { // */
fileName = FileUploadUtils.uploadFile(file); // String s = FileUploadUtils.upload2Cos(file, cosConfig);
} else { // //上传文件到文件服务器
// 上传并返回新文件名称 // // 上传文件路径
fileName = FileUploadUtils.upload(filePath, file); // String filePath = RuoYiConfig.getUploadPath();
} // String fileName = "";
String url = fileName; // if (com.linkwechat.common.enums.MediaType .FILE.getType().equals(type)) {
//上传临时素材 // fileName = FileUploadUtils.uploadFile(file);
// } else {
// // 上传并返回新文件名称
// fileName = FileUploadUtils.upload(filePath, file);
// }
// String url = fileName;
// //上传临时素材
Optional<com.linkwechat.common.enums.MediaType > mediaType = com.linkwechat.common.enums.MediaType .of(type); Optional<com.linkwechat.common.enums.MediaType > mediaType = com.linkwechat.common.enums.MediaType .of(type);
if (!mediaType.isPresent()) { if (!mediaType.isPresent()) {
throw new WeComException("媒体类型出错!"); throw new WeComException("媒体类型出错!");
} }
//构造返回结果 //构造返回结果
return WeMaterialFileVO.builder().materialUrl(url).materialName(file.getOriginalFilename()).build(); // return WeMaterialFileVO.builder().materialUrl(url).materialName(file.getOriginalFilename()).build();
return WeMaterialFileVO.builder().materialUrl(cosConfig.getImgUrlPrefix()).materialName(FileUploadUtils.upload2Cos(file, cosConfig)).build();
} catch (Exception e) { } catch (Exception e) {
throw new WeComException(e.getMessage()); throw new WeComException(e.getMessage());
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!