zhizhi wu 5 years ago
parent
commit
1f0bef17cf
  1. 3
      tall/src/main/java/com/ccsens/tall/bean/dto/WpsDto.java
  2. 12
      tall/src/main/java/com/ccsens/tall/bean/vo/WpsVo.java
  3. 3
      tall/src/main/java/com/ccsens/tall/persist/dao/WpsFileDao.java
  4. 37
      tall/src/main/java/com/ccsens/tall/service/WpsService.java
  5. 32
      tall/src/main/java/com/ccsens/tall/web/FileController.java
  6. 13
      tall/src/main/java/com/ccsens/tall/web/WpsController.java
  7. 4
      tall/src/main/resources/application-dev.yml
  8. 5
      tall/src/main/resources/application-prod.yml
  9. 5
      tall/src/main/resources/application-test.yml
  10. 2
      tall/src/main/resources/mapper_dao/SysUserDao.xml
  11. 2
      tall/src/main/resources/mapper_dao/WpsFileDao.xml
  12. 54
      util/src/main/java/com/ccsens/util/UploadFileUtil_Servlet3.java

3
tall/src/main/java/com/ccsens/tall/bean/dto/WpsDto.java

@ -1,5 +1,6 @@
package com.ccsens.tall.bean.dto;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -35,7 +36,7 @@ public class WpsDto {
private String cmd;
@NotNull
@ApiModelProperty("信息内容")
private String body;
private JSONObject body;
}
@ApiModel("获取所有历史版本文件信息请求")

12
tall/src/main/java/com/ccsens/tall/bean/vo/WpsVo.java

@ -130,6 +130,18 @@ public class WpsVo {
private Long modify_time;
@JsonIgnore
private Long fileVersionId;
public FileBase(){}
public FileBase(WpsFile wpsFile) {
this.id = String.valueOf(wpsFile.getId());
this.name = wpsFile.getName();
this.currentVersion = wpsFile.getCurrentVersion();
this.size = wpsFile.getSize();
this.downloadUrl = wpsFile.getDownloadUrl();
this.creator = String.valueOf(wpsFile.getCreator());
this.modifier = String.valueOf(wpsFile.getModifier());
this.create_time = wpsFile.getCreatedAt().getTime()/1000;
this.modify_time = wpsFile.getUpdatedAt().getTime()/1000;
}
}
@Data

3
tall/src/main/java/com/ccsens/tall/persist/dao/WpsFileDao.java

@ -6,6 +6,7 @@ import com.ccsens.tall.persist.mapper.WpsFileMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* @author whj
@ -19,7 +20,7 @@ public interface WpsFileDao extends WpsFileMapper {
* @param version 版本
* @return 文件信息
*/
WpsVo.FileBase getVersion(@Param("fileId") Long fileId, @Param("version") Integer version);
Map<String, Object> getVersion(@Param("fileId") Long fileId, @Param("version") Integer version);
/**
* 文件重命名

37
tall/src/main/java/com/ccsens/tall/service/WpsService.java

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ccsens.tall.bean.dto.WpsDto;
import com.ccsens.tall.bean.po.*;
import com.ccsens.tall.bean.vo.UserVo;
@ -26,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.Part;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -59,7 +61,7 @@ public class WpsService implements IWpsService {
@Resource
private Snowflake snowflake;
private String fileUrl = "http://wwo.wps.cn/office/{}/{}?_w_fname={}&_w_userid={}&_w_appid="+WebConstant.Wps.APPID+"&_w_signature={}";
private String fileUrl = "http://wwo.wps.cn/office/{}/{}?_w_appid="+WebConstant.Wps.APPID+"&_w_signature={}";
@Override
public void notify(String token, WpsDto.NotifyBody body) throws Exception {
@ -67,7 +69,7 @@ public class WpsService implements IWpsService {
WpsNotify wpsNotify = new WpsNotify();
wpsNotify.setId(snowflake.nextId());
wpsNotify.setCmd(body.getCmd());
wpsNotify.setBody(body.getBody());
wpsNotify.setBody(body.getBody().toJSONString());
wpsNotifyMapper.insertSelective(wpsNotify);
}
@ -88,13 +90,14 @@ public class WpsService implements IWpsService {
// 删除原来的文件版本记录
WpsFileVersionExample wpsFileVersionExample = new WpsFileVersionExample();
wpsFileVersionExample.createCriteria().andFileIdEqualTo(fileId)
.andVersionEqualTo(wpsFile.getCurrentVersion())
.andVersionEqualTo(file.getCurrentVersion())
.andRecStatusEqualTo(WebConstant.REC_STATUS.Normal.value);
WpsFileVersion updateVersion = new WpsFileVersion();
updateVersion.setRecStatus(WebConstant.REC_STATUS.Deleted.value);
wpsFileVersionMapper.updateByExampleSelective(updateVersion, wpsFileVersionExample);
// 保存版本信息
WpsFileVersion version = saveVersion(wpsFile);
WpsFile newWpsFile = wpsFileDao.selectByPrimaryKey(fileId);
WpsFileVersion version = saveVersion(newWpsFile);
// 保存操作记录
WpsFileUser fileUser = initFileUser(userId, version.getId());
fileUser.setOperation(WebConstant.Wps.USER_OPERATION_RENAME);
@ -112,7 +115,8 @@ public class WpsService implements IWpsService {
public WpsVo.FileBase fileVersion(String token, Long fileId, Integer version) throws Exception {
Long userId = getUserId(token);
// 查找指定版本version
WpsVo.FileBase base = wpsFileDao.getVersion(fileId, version);
Map<String, Object> map = wpsFileDao.getVersion(fileId, version);
WpsVo.FileBase base = JSONObject.parseObject(JSONObject.toJSONString(map), WpsVo.FileBase.class);
// 保存用户打开文件旧版本记录
WpsFileUser wpsFileUser = initFileUser(userId, base.getFileVersionId());
wpsFileUser.setOperation(WebConstant.Wps.USER_OPERATION_OLD);
@ -128,13 +132,13 @@ public class WpsService implements IWpsService {
String fileType = WebConstant.Wps.getFileType(ext);
Map<String, String> paramMap= new HashMap<>();
paramMap.put("_w_appid", WebConstant.Wps.APPID);
paramMap.put("_w_fname", fileName);
paramMap.put("_w_userid", String.valueOf(wpsFile.getCreator()));
// paramMap.put("_w_fname", fileName);
// paramMap.put("_w_userid", String.valueOf(wpsFile.getCreator()));
String newSignature = WpsSignature.getSignature(paramMap, WebConstant.Wps.APPKEY);
String url = StrUtil.format(fileUrl, fileType, wpsFile.getId(), fileName, newSignature);
String url = StrUtil.format(fileUrl, fileType, wpsFile.getId(), newSignature);
WpsVo.FileNew fileNew = new WpsVo.FileNew();
fileNew.setRedirect_url(url);
fileNew.setUser_id(fileNew.getUser_id());
fileNew.setUser_id(String.valueOf(wpsFile.getCreator()));
return fileNew;
}
@ -217,10 +221,7 @@ public class WpsService implements IWpsService {
WpsFile wpsFile = saveWpsFile(token, fileId, file,WebConstant.Wps.USER_OPERATION_SAVE);
// 返回参数
WpsVo.FileBase fileBase = new WpsVo.FileBase();
BeanUtils.copyProperties(wpsFile, fileBase);
fileBase.setCreate_time(wpsFile.getCreatedAt().getTime()/1000);
fileBase.setModify_time(wpsFile.getUpdatedAt().getTime()/1000);
WpsVo.FileBase fileBase = new WpsVo.FileBase(wpsFile);
return new WpsVo.FileSave(fileBase);
}
@ -282,10 +283,18 @@ public class WpsService implements IWpsService {
}
wpsFile.setCurrentVersion(wpsFile.getCurrentVersion() + 1);
}
wpsFile.setCreatedAt(new Date());
wpsFile.setUpdatedAt(new Date());
wpsFile.setName(name);
wpsFile.setSize(file.getSize());
wpsFile.setDownloadUrl(PropUtil.domain + filePath);
wpsFile.setDownloadUrl(PropUtil.domain + "file/download/" + name + "?path=" + filePath);
wpsFile.setModifier(userId);
if (StrUtil.isEmpty(fileId)) {
wpsFileDao.insertSelective(wpsFile);
} else {
wpsFileDao.updateByPrimaryKeySelective(wpsFile);
}
return wpsFile;
}

32
tall/src/main/java/com/ccsens/tall/web/FileController.java

@ -0,0 +1,32 @@
package com.ccsens.tall.web;
import com.ccsens.util.UploadFileUtil_Servlet3;
import com.ccsens.util.WebConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
* 对文件操作
* @author: whj
* @date : 2020/6/28 10:51
*/
@Slf4j
@Api(tags = "文件操作")
@RestController
@RequestMapping("/file")
public class FileController {
@ApiOperation(value = "文件下载", notes = "")
@GetMapping(value = "download/{fileName}")
public void download(HttpServletResponse response, @PathVariable("fileName")String fileName, String path) throws Exception {
String parentPath = WebConstant.UPLOAD_PATH_BASE + File.separator + path;
UploadFileUtil_Servlet3.download(response, parentPath, fileName);
}
}

13
tall/src/main/java/com/ccsens/tall/web/WpsController.java

@ -1,5 +1,6 @@
package com.ccsens.tall.web;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.ccsens.tall.bean.dto.WpsDto;
import com.ccsens.tall.bean.vo.UserVo;
@ -24,6 +25,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -45,7 +47,7 @@ public class WpsController {
private IUserService userService;
@InitBinder
protected void initBinder(WebDataBinder binder) {
public void initBinder(WebDataBinder binder) {
// 解决下划线开头参数无法映射到实体的问题
binder.setFieldMarkerPrefix(null);
}
@ -71,7 +73,7 @@ public class WpsController {
})
@PostMapping(value = "user/info", produces = {"application/json;charset=UTF-8"})
public WpsVo.UserInfo userInfo(HttpServletRequest request, WpsDto.UserInfo userInfo,
@RequestBody WpsDto.UserInfoBody body){
@RequestBody WpsDto.UserInfoBody body) throws Exception {
log.info("获取用户信息请求参数:{}, ids:{}", userInfo, body);
if (body.getIds() == null || body.getIds().length <= 0) {
log.info("请求参数中没有用户ID");
@ -170,6 +172,7 @@ public class WpsController {
return JsonResponse.newInstance().ok();
}
@Data
@ApiModel("wps通用请求头参数")
private static class WpsHeader{
@ -210,7 +213,7 @@ public class WpsController {
* @param request
* @param signature
*/
private WpsHeader checkSignature(HttpServletRequest request, String signature) {
private WpsHeader checkSignature(HttpServletRequest request, String signature) throws Exception {
WpsHeader header = WpsHeader.getHeader(request);
Map<String, String> paramMap = initMap();
if (StrUtil.isNotEmpty(request.getParameter("_w_url"))) {
@ -218,7 +221,9 @@ public class WpsController {
}
String newSignature = WpsSignature.getSignature(paramMap, WebConstant.Wps.APPKEY);
if (!signature.equals(newSignature)) {
log.info("newSignature:{}", newSignature);
log.info("参数签名:{}---{}", signature, URLEncoder.encode(signature, CharsetUtil.UTF_8));
if (!newSignature.equals(URLEncoder.encode(signature, CharsetUtil.UTF_8))) {
log.info("签名验证失败");
throw new BaseException(CodeEnum.SIGNATURE_FAIL);
}

4
tall/src/main/resources/application-dev.yml

@ -33,4 +33,6 @@ gatewayUrl: https://test.tall.wiki/gateway/
notGatewayUrl: /home/staticrec/logo.png
wx:
prefixUrl: https://test.tall.wiki/wxconfigurer-api/
file:
domain: http://localhost:7030/v1.0/
imgDomain: http://localhost:7030/v1.0/uploads

5
tall/src/main/resources/application-prod.yml

@ -38,4 +38,7 @@ eureka:
gatewayUrl: https://www.tall.wiki/gateway/
notGatewayUrl: https://www.tall.wiki/
wx:
prefixUrl: https://test.tall.wiki/wxconfigurer-api/
prefixUrl: https://test.tall.wiki/wxconfigurer-api/
file:
domain: https://www.tall.wiki/gateway/tall/v1.0/
imgDomain: https://www.tall.wiki/gateway/tall/v1.0/uploads

5
tall/src/main/resources/application-test.yml

@ -39,4 +39,7 @@ eureka:
gatewayUrl: https://test.tall.wiki/gateway/
notGatewayUrl: https://test.tall.wiki/
wx:
prefixUrl: https://test.tall.wiki/wxconfigurer-api/
prefixUrl: https://test.tall.wiki/wxconfigurer-api/
file:
domain: https://test.tall.wiki/gateway/tall/v1.0/
imgDomain: https://test.tall.wiki/gateway/tall/v1.0/uploads

2
tall/src/main/resources/mapper_dao/SysUserDao.xml

@ -124,7 +124,7 @@
a.rec_status = 0
and
u.id in
<foreach collection="Array" item="item" separator="," open="(" close=")">
<foreach collection="array" item="item" separator="," open="(" close=")">
#{item}
</foreach>
</select>

2
tall/src/main/resources/mapper_dao/WpsFileDao.xml

@ -34,7 +34,7 @@
AND v.rec_status = 0
</update>
<select id="getVersion" resultType="com.ccsens.tall.bean.vo.WpsVo$FileBase">
<select id="getVersion" resultType="HashMap">
SELECT
f.id,
v.id as fileVersionId,

54
util/src/main/java/com/ccsens/util/UploadFileUtil_Servlet3.java

@ -2,16 +2,19 @@ package com.ccsens.util;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
@Slf4j
public class UploadFileUtil_Servlet3 {
/**
* Servlet3.0文件上传 (生成uuid.txt文件)
@ -92,4 +95,49 @@ public class UploadFileUtil_Servlet3 {
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
return fileName;
}
public static void download(HttpServletResponse response, String path, String fileName) throws Exception{
File file = new File(path);
// 如果文件存在,则进行下载
if (!file.exists()) {
return;
}
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, CharsetUtil.UTF_8));
// 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("关闭流异常", e);
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
log.error("关闭流异常", e);
}
}
}
}
}

Loading…
Cancel
Save