提交 3bdc51ea 编写于 作者: sq3536's avatar sq3536

在线编辑

上级 db88bf31
......@@ -18,11 +18,16 @@ import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
......@@ -151,6 +156,18 @@ public class DiskCoreService {
throw new InternalServerErrorException("文件未找到");
}
public File getFileById(String fileId) {
SDFile sdFile=sdFileService.getById(fileId);
if(sdFile!=null&&(!StringUtils.isEmpty(sdFile.getFilePath()))){
String fileFullPath = this.fileRoot.concat(sdFile.getFilePath());
fileFullPath = fileFullPath.replace("\\",File.separator).replace("/",File.separator);
File file = new File(fileFullPath);
if(file.exists())
return file;
}
throw new InternalServerErrorException("文件未找到");
}
public List<FileItem> getFileList(String folder,String ownerType,String ownerId)
{
List<FileItem> fileItems = new ArrayList<>();
......@@ -189,4 +206,45 @@ public class DiskCoreService {
return fileName;
}
public static String getExtensionName(File file) {
return getExtensionName(file.getName());
}
public static String getHash256(File file) {
String value = "";
// 获取hash值
InputStream fis = null;
try {
byte[] buffer = new byte[1024];
int numRead;
fis = new FileInputStream(file);
//如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256
MessageDigest complete = MessageDigest.getInstance("SHA-256");
do {
//从文件读到buffer
numRead = fis.read(buffer);
if (numRead > 0) {
//用读到的字节进行MD5的计算,第二个参数是偏移量
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
value = new String(Base64.getEncoder().encode(complete.digest()));
} catch (NoSuchAlgorithmException e) {
}catch (IOException e) {
}
finally {
if(fis!=null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
return value;
}
}
\ No newline at end of file
......@@ -121,7 +121,8 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter {
// 文件操作
.antMatchers("/"+downloadpath+"/**").permitAll()
.antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll();
.antMatchers("/"+previewpath+"/**").permitAll()
.antMatchers("/net-disk/wopi/**").permitAll();
for (String excludePattern : excludesPattern) {
authenticationTokenFilter.addExcludePattern(excludePattern);
......
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.disk.domain.SDFile;
import cn.ibizlab.core.disk.extensions.service.DiskCoreService;
import cn.ibizlab.core.disk.extensions.service.FileCoreService;
import cn.ibizlab.core.disk.extensions.vo.FileItem;
import cn.ibizlab.core.disk.service.ISDFileService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -19,6 +22,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Hashtable;
import java.util.List;
......@@ -34,6 +40,9 @@ public class DiskCoreResource
@Autowired
private ISDFileService fileService;
@Autowired
private FileCoreService fileCoreService;
private Hashtable<String, String> type = null;
private String getType(String ext)
{
......@@ -56,8 +65,12 @@ public class DiskCoreResource
type.put(".txt","text/plain");
}
if(type.containsKey(ext.toLowerCase()))
return type.get(ext.toLowerCase());
String key=ext.toLowerCase();
key=ext.toLowerCase();
if(!key.startsWith("."))
key="."+key;
if(type.containsKey(key))
return type.get(key);
else
return "application/octet-stream";
......@@ -95,7 +108,7 @@ public class DiskCoreResource
@RequestHeader(value = "authcode",required = false) String authcode,
@RequestParam(value = "authcode",required = false) String checkcode,HttpServletResponse response){
File file = diskCoreService.getFile(folder,id,StringUtils.isEmpty(authcode)?checkcode:authcode);
String type = getType(ext);
String type = getType(DiskCoreService.getExtensionName(file));
response.setContentType(type);
if(type.toLowerCase().equals("application/octet-stream"))
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
......@@ -108,8 +121,18 @@ public class DiskCoreResource
return ResponseEntity.status(HttpStatus.OK).body(fileService.remove(sdfile_id));
}
@GetMapping(value = "net-disk/files/{folder}")
public ResponseEntity<List<FileItem>> getFiles(@PathVariable("folder") String folder,@RequestParam("ownertype") String ownertype, @RequestParam("ownerid") String ownerid){
return ResponseEntity.ok().body(diskCoreService.getFileList(folder,ownertype,ownerid));
}
@PostMapping(value = "net-disk/files/{folder}")
public ResponseEntity<Boolean> saveFiles(@PathVariable("folder") String folder,@RequestParam("ownertype") String ownertype, @RequestParam("ownerid") String ownerid, @RequestBody List<FileItem> fileItems){
diskCoreService.saveFileList(folder,ownertype,ownerid,fileItems);
return ResponseEntity.ok().body(true);
}
@Value("ibiz.file.proxy.previewpath")
@Value("ibiz.file.proxy.previewpath:http://172.16.100.243:8012/onlinePreview?url=")
private String previewPath;
@GetMapping(value = "net-disk/preview/{folder}/{id}/{name}.{ext}")
......@@ -119,16 +142,19 @@ public class DiskCoreResource
@RequestParam(value = "authcode",required = false) String checkcode, HttpServletRequest request){
if(StringUtils.isEmpty(previewPath))
throw new BadRequestAlertException("未配置预览系统地址","SDFile","");
authcode=StringUtils.isEmpty(authcode)?checkcode:authcode;
if ((!folder.toLowerCase().startsWith("ibizutil"))&&(StringUtils.isEmpty(authcode)||(!authcode.equals(fileCoreService.getAuthCode(id)))))
throw new BadRequestAlertException("没有权限预览","SDFile","");
String redirectUrl = request.getScheme().concat("://").concat(request.getServerName());
if(request.getServerPort()!=80&&request.getServerPort()!=443)
redirectUrl=redirectUrl.concat(":").concat(request.getServerPort()+"");
redirectUrl=redirectUrl.concat("/net-disk/download/")
.concat(folder).concat("/").concat(folder).concat("/").concat(name).concat(".").concat(ext).concat("?authcode=").concat(StringUtils.isEmpty(authcode)?checkcode:authcode);
redirectUrl=previewPath.concat("?url=").concat(encodeURIComponent(redirectUrl));
.concat(folder).concat("/").concat(id).concat("/").concat(name).concat(".").concat(ext).concat("?authcode=").concat(StringUtils.isEmpty(authcode)?checkcode:authcode);
redirectUrl=previewPath.concat("").concat(encodeURIComponent(redirectUrl));
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).header(HttpHeaders.LOCATION, redirectUrl).build();
}
@Value("ibiz.file.proxy.ocrpath")
@Value("ibiz.file.proxy.ocrpath:http://101.132.236.47:58114/ocr/view?url=")
private String ocrPath;
@GetMapping(value = "net-disk/ocrview/{folder}/{id}/{name}.{ext}")
......@@ -136,28 +162,129 @@ public class DiskCoreResource
@PathVariable("name") String name, @PathVariable("ext") String ext,
@RequestHeader(value = "authcode",required = false) String authcode,
@RequestParam(value = "authcode",required = false) String checkcode, HttpServletRequest request){
if(StringUtils.isEmpty(previewPath))
if(StringUtils.isEmpty(ocrPath))
throw new BadRequestAlertException("未配置预览系统地址","SDFile","");
if(!this.checkOcr(ext))
throw new BadRequestAlertException("不支持识别","SDFile",ext);
authcode=StringUtils.isEmpty(authcode)?checkcode:authcode;
if ((!folder.toLowerCase().startsWith("ibizutil"))&&(StringUtils.isEmpty(authcode)||(!authcode.equals(fileCoreService.getAuthCode(id)))))
throw new BadRequestAlertException("没有权限识别","SDFile","");
String redirectUrl = request.getScheme().concat("://").concat(request.getServerName());
if(request.getServerPort()!=80&&request.getServerPort()!=443)
redirectUrl=redirectUrl.concat(":").concat(request.getServerPort()+"");
redirectUrl=redirectUrl.concat("/net-disk/download/")
.concat(folder).concat("/").concat(folder).concat("/").concat(name).concat(".").concat(ext).concat("?authcode=").concat(StringUtils.isEmpty(authcode)?checkcode:authcode);
redirectUrl=ocrPath.concat("?url=").concat(encodeURIComponent(redirectUrl));
.concat(folder).concat("/").concat(id).concat("/").concat(name).concat(".").concat(ext).concat("?authcode=").concat(StringUtils.isEmpty(authcode)?checkcode:authcode);
redirectUrl=ocrPath.concat("").concat(encodeURIComponent(redirectUrl));
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).header(HttpHeaders.LOCATION, redirectUrl).build();
}
@GetMapping(value = "net-disk/files/{folder}")
public ResponseEntity<List<FileItem>> getFiles(@PathVariable("folder") String folder,@RequestParam("ownertype") String ownertype, @RequestParam("ownerid") String ownerid){
return ResponseEntity.ok().body(diskCoreService.getFileList(folder,ownertype,ownerid));
@Value("ibiz.file.proxy.editpath:http://172.16.180.233:9980/loleaflet/dist/loleaflet.html?file_path=")
private String editPath;
@GetMapping(value = "net-disk/edit/{folder}/{id}/{name}.{ext}")
public ResponseEntity editview(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable("name") String name, @PathVariable("ext") String ext,
@RequestHeader(value = "authcode",required = false) String authcode,
@RequestParam(value = "authcode",required = false) String checkcode, HttpServletRequest request){
if(StringUtils.isEmpty(editPath))
throw new BadRequestAlertException("未配置预览系统地址","SDFile","");
if(!this.checkEdit(ext))
throw new BadRequestAlertException("不支持编辑","SDFile",ext);
authcode=StringUtils.isEmpty(authcode)?checkcode:authcode;
if ((!folder.toLowerCase().startsWith("ibizutil"))&&(StringUtils.isEmpty(authcode)||(!authcode.equals(fileCoreService.getAuthCode(id)))))
throw new BadRequestAlertException("没有权限编辑","SDFile","");
String redirectUrl = request.getScheme().concat("://").concat(request.getServerName());
if(request.getServerPort()!=80&&request.getServerPort()!=443)
redirectUrl=redirectUrl.concat(":").concat(request.getServerPort()+"");
redirectUrl=redirectUrl.concat("/net-disk/wopi/")
.concat(folder).concat("/").concat(id).concat("/").concat(encodeURIComponent(name)).concat(".").concat(ext);
redirectUrl=editPath.concat("").concat(redirectUrl);
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).header(HttpHeaders.LOCATION, redirectUrl).build();
}
@PostMapping(value = "net-disk/files/{folder}")
public ResponseEntity<Boolean> saveFiles(@PathVariable("folder") String folder,@RequestParam("ownertype") String ownertype, @RequestParam("ownerid") String ownerid, @RequestBody List<FileItem> fileItems){
diskCoreService.saveFileList(folder,ownertype,ownerid,fileItems);
return ResponseEntity.ok().body(true);
@GetMapping(value = "net-disk/wopi/{folder}/{id}/{name}.{ext}")
public ResponseEntity<JSONObject> getWoAPI(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable(value = "name",required = false) String name, @PathVariable(value = "ext", required = false) String ext){
SDFile sdFile = fileService.getById(id);
if(sdFile==null)
throw new BadRequestAlertException("文件未找到","SDFile",id);
File file = diskCoreService.getFile(sdFile);
JSONObject json = new JSONObject();
// 取得文件名
json.put("BaseFileName", file.getName());
json.put("Size", file.length());
json.put("OwnerId", StringUtils.isEmpty(sdFile.getUpdateman())?"admin":sdFile.getUpdateman());
json.put("Version", file.lastModified());
json.put("Sha256", DiskCoreService.getHash256(file));
json.put("AllowExternalMarketplace", true);
json.put("UserCanWrite", true);
json.put("SupportsUpdate", true);
json.put("SupportsLocks", true);
return ResponseEntity.ok().body(json);
}
@GetMapping(value = "net-disk/wopi/{folder}/{id}/{name}.{ext}/contents")
@ResponseStatus(HttpStatus.OK)
public void getWoAPIContents(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable(value = "name",required = false) String name, @PathVariable(value = "ext", required = false) String ext, HttpServletResponse response) {
File file = diskCoreService.getFile(folder,id, fileCoreService.getAuthCode(id));
InputStream fis = null;
OutputStream toClient = null;
try {
if (file != null) {
// 取得文件名
String filename = file.getName();
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" +
getFileName(filename));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
}
} catch (IOException ex) {
} finally {
if (fis != null) {
try {
fis.close();
}
catch (IOException e) {
}
}
if (toClient != null) {
try {
toClient.close();
}
catch (IOException e) {
}
}
}
}
@PostMapping("net-disk/wopi/{folder}/{id}/{name}.{ext}/contents")
@ResponseStatus(HttpStatus.OK)
public void postWoAPIFile(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable(value = "name",required = false) String name, @PathVariable(value = "ext", required = false) String ext, @RequestBody byte[] content) {
SDFile sdFile = fileService.getById(id);
if(sdFile==null)
throw new BadRequestAlertException("文件未找到","SDFile",id);
diskCoreService.saveFile(sdFile,content);
}
protected void sendRespose(HttpServletResponse response, File file){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
......@@ -195,7 +322,7 @@ public class DiskCoreResource
protected String getFileName(String fileName){
try {
return new String(fileName.getBytes("utf-8"),"iso8859-1");//防止中文乱码
return new String(fileName.getBytes("utf-8"),"ISO-8859-1");//防止中文乱码
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
......@@ -223,4 +350,23 @@ public class DiskCoreResource
return result;
}
protected boolean checkOcr(String ext)
{
ext = ext.toLowerCase();
if(ext.equals("bmp")||ext.equals("gif")||ext.equals("tif")||ext.equals("tiff")||ext.equals("jpg")||ext.equals("jpeg")||ext.equals("png")||ext.equals("pdf"))
return true;
return false;
}
protected boolean checkEdit(String ext)
{
ext = ext.toLowerCase();
if(ext.equals("wps")||ext.equals("et")||ext.equals("doc")||ext.equals("docx")||ext.equals("xls")||ext.equals("xlsx")||ext.equals("ppt")||ext.equals("pptx")
||ext.equals("dps")||ext.equals("rtf")||ext.equals("cvs")||ext.equals("txt")||ext.equals("odt"))
return true;
return false;
}
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册