1.需求:
之前在公司内部搭建了svn服务器,给部门存放文档、视频,做成了一个文档服务器来用,随着视频文件太大,每次下载太慢
需要把文件在线打开查看
2.解决:
kkFileView
https://kkfileview.keking.cn/zh-cn/index.html
kkFileView为文件文档在线预览解决方案,该项目使用流行的spring boot搭建,易上手和部署,基本支持主流办公文档的在线预览,如doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,rar,图片,视频,音频等等
我之前写过的svn部署文档 https://www.cnblogs.com/wang3680/p/dc92818c4cb50701dbc42eef5e8a2482.html
kkFileView无法适配svn服务,需要开发svn客户端完成svn服务器文件的checkout操作
3.改造kkFileView适配svn服务
我用的版本是4.0.0 GitHub地址 https://github.com/kekingcn/kkFileView
下载源码,导入到idea中
-
<dependency> <groupId>org.tmatesoft.svnkitgroupId> <artifactId>svnkitartifactId> <version>1.9.3version> dependency>
-
新增SVNUtils.java 在 package cn.keking.utils;
-
1 package cn.keking.utils; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.tmatesoft.svn.core.*; 6 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 7 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; 8 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory; 9 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; 10 import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions; 11 import org.tmatesoft.svn.core.io.SVNRepository; 12 import org.tmatesoft.svn.core.wc.*; 13 14 import java.io.File; 15 import java.io.FileOutputStream; 16 import java.io.IOException; 17 import java.io.OutputStream; 18 19 /** 20 * @program: filepreview 21 * @ClassName SVNUtils 22 * @description: svn下的https文件下载 23 * @author: wangshaoshuai 24 * @create: 2022/3/14 16:34 25 */ 26 public class SVNUtils { 27 private final static Logger logger = LoggerFactory.getLogger(SVNUtils.class); 28 29 private String url; 30 private String username; 31 private String password; 32 private SVNRepository repository; 33 34 public SVNUtils(String url, String username, String password) { 35 super(); 36 this.url = url; 37 this.username = username; 38 this.password = password; 39 initialize(); 40 } 41 42 /** 43 * 初始化操作 44 * 45 * @throws SVNException 46 */ 47 private void initialize() { 48 FSRepositoryFactory.setup(); 49 DAVRepositoryFactory.setup(); 50 SVNRepositoryFactoryImpl.setup(); 51 try { 52 repository = SVNRepositoryFactoryImpl.create(SVNURL.parseURIEncoded(this.url)); 53 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(this.username, this.password); 54 repository.setAuthenticationManager(authManager); 55 } catch (SVNException e) { 56 e.printStackTrace(); 57 } 58 } 59 60 /** 61 * 从SVN服务器获取最新版本的文件 62 * 63 * @param filePath 相对于仓库根目录的路径 64 * @return 返回checkout文件的版本号 65 * @throws Exception 可以自定义Exception 66 */ 67 public long getFileFromSVN(String filePath, String outFileName) { 68 return getFileFromSVN(filePath, outFileName, 0); 69 } 70 71 /** 72 * 从SVN服务器获取文件 73 * 74 * @param filePath 相对于仓库根目录的路径 75 * @param version 要checkout的版本号,当传入的版本号为0时,默认获取最新版本 76 * @return 返回checkout文件的版本号 77 * @throws Exception 可以自定义Exception 78 */ 79 public long getFileFromSVN(String filePath, String outFileName, long version) { 80 SVNNodeKind node = null; 81 try { 82 if (version == 0) { 83 version = repository.getLatestRevision(); 84 } 85 // node = repository.checkPath(filePath, version); 86 } catch (SVNException e) { 87 logger.error("SVN检测不到该文件:" + filePath, e); 88 } 89 // if (node != SVNNodeKind.FILE) { 90 // logger.error(node.toString() + "不是文件"); 91 // } 92 SVNProperties properties = new SVNProperties(); 93 try { 94 OutputStream outputStream = new FileOutputStream(outFileName); 95 repository.getFile(filePath, version, properties, outputStream); 96 outputStream.close(); 97 } catch (SVNException e) { 98 logger.error("获取SVN服务器中的" + filePath + "文件失败", e); 99 } catch (IOException e) { 100 logger.error("SVN check out file faild.", e); 101 } 102 return Long.parseLong(properties.getStringValue("svn:entry:revision")); 103 } 104 }
- 修改DownloadUtils.java 具体位置为package cn.keking.utils;
-
1 package cn.keking.utils; 2 3 import cn.keking.config.ConfigConstants; 4 import cn.keking.model.FileAttribute; 5 import cn.keking.model.ReturnResponse; 6 import io.mola.galimatias.GalimatiasParseException; 7 import org.apache.commons.io.FileUtils; 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 11 import java.io.*; 12 import java.net.*; 13 import java.util.UUID; 14 15 import static cn.keking.utils.KkFileUtils.isFtpUrl; 16 import static cn.keking.utils.KkFileUtils.isHttpUrl; 17 18 /** 19 * @author yudian-it 20 */ 21 public class DownloadUtils { 22 23 private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class); 24 private static final String fileDir = ConfigConstants.getFileDir(); 25 private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; 26 private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; 27 private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; 28 29 private static final String URL_PARAM_URL_TYPE = "url.type"; 30 private static final String URL_PARAM_SVN_USERNAME = "svn.username"; 31 private static final String URL_PARAM_SVN_PASSWORD = "svn.password"; 32 33 /** 34 * @param fileAttribute fileAttribute 35 * @param fileName 文件名 36 * @return 本地文件绝对路径 37 */ 38 public static ReturnResponse
downLoad(FileAttribute fileAttribute, String fileName) { 39 String urlStr = fileAttribute.getUrl(); 40 ReturnResponse response = new ReturnResponse<>(0, "下载成功!!!", ""); 41 String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute); 42 try { 43 URL url = WebUtils.normalizedURL(urlStr); 44 if (isHttpUrl(url)) { 45 File realFile = new File(realPath); 46 String url_type = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_URL_TYPE); 47 if(url_type.equalsIgnoreCase("svn")){//svn的服务的话下载方式不一样 48 String svn_username = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_USERNAME); 49 String svn_password = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_PASSWORD); 50 String svn_folder = urlStr.substring(0,urlStr.lastIndexOf("/")); 51 String file_name = urlStr.substring(urlStr.lastIndexOf("/")+1,urlStr.indexOf("?")); 52 SVNUtils svnUtils = new SVNUtils(svn_folder,svn_username,svn_password); 53 svnUtils.getFileFromSVN(file_name,realPath); 54 }else{ 55 FileUtils.copyURLToFile(url,realFile); 56 } 57 } else if (isFtpUrl(url)) { 58 String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); 59 String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); 60 String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); 61 FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); 62 } else { 63 response.setCode(1); 64 response.setMsg("url不能识别url" + urlStr); 65 } 66 response.setContent(realPath); 67 response.setMsg(fileName); 68 return response; 69 } catch (IOException | GalimatiasParseException e) { 70 logger.error("文件下载失败,url:{}", urlStr, e); 71 response.setCode(1); 72 response.setContent(null); 73 if (e instanceof FileNotFoundException) { 74 response.setMsg("文件不存在!!!"); 75 } else { 76 response.setMsg(e.getMessage()); 77 } 78 return response; 79 } 80 } 81 82 83 /** 84 * 获取真实文件绝对路径 85 * 86 * @param fileName 文件名 87 * @return 文件路径 88 */ 89 private static String getRelFilePath(String fileName, FileAttribute fileAttribute) { 90 String type = fileAttribute.getSuffix(); 91 if (null == fileName) { 92 UUID uuid = UUID.randomUUID(); 93 fileName = uuid + "." + type; 94 } else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】) 95 fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type); 96 } 97 String realPath = fileDir + fileName; 98 File dirFile = new File(fileDir); 99 if (!dirFile.exists() && !dirFile.mkdirs()) { 100 logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir); 101 } 102 return realPath; 103 } 104 105 } 1 public static ReturnResponse
downLoad(FileAttribute fileAttribute, String fileName) { 2 String urlStr = fileAttribute.getUrl(); 3 ReturnResponse response = new ReturnResponse<>(0, "下载成功!!!", ""); 4 String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute); 5 try { 6 URL url = WebUtils.normalizedURL(urlStr); 7 if (isHttpUrl(url)) { 8 File realFile = new File(realPath); 9 String url_type = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_URL_TYPE); 10 if(url_type.equalsIgnoreCase("svn")){//svn的服务的话下载方式不一样 11 String svn_username = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_USERNAME); 12 String svn_password = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_PASSWORD); 13 String svn_folder = urlStr.substring(0,urlStr.lastIndexOf("/")); 14 String file_name = urlStr.substring(urlStr.lastIndexOf("/")+1,urlStr.indexOf("?")); 15 SVNUtils svnUtils = new SVNUtils(svn_folder,svn_username,svn_password); 16 svnUtils.getFileFromSVN(file_name,realPath); 17 }else{ 18 FileUtils.copyURLToFile(url,realFile); 19 } 20 } else if (isFtpUrl(url)) { 21 String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); 22 String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); 23 String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); 24 FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); 25 } else { 26 response.setCode(1); 27 response.setMsg("url不能识别url" + urlStr); 28 } 29 response.setContent(realPath); 30 response.setMsg(fileName); 31 return response; 32 } catch (IOException | GalimatiasParseException e) { 33 logger.error("文件下载失败,url:{}", urlStr, e); 34 response.setCode(1); 35 response.setContent(null); 36 if (e instanceof FileNotFoundException) { 37 response.setMsg("文件不存在!!!"); 38 } else { 39 response.setMsg(e.getMessage()); 40 } 41 return response; 42 } 43 } 主要是修改这段代码,增加svn类型的判断和工具类的使用
- 使用方法如下:a.html
-
DOCTYPE html> <html lang="en"> <head> <title>kkFileView演示首页title> head> <body> <div> div> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/base64.min.js">script> <script> var url = 'https://127.0.0.1/svn/业务学习资料/资料整理文档/JAVA学习/JAVA开发工具idea的使用01-王少帅.mp4'; var pre_url = url + '?url.type=svn&svn.username=wangshaoshuai&svn.password=123456'; window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(pre_url))); script> body> html>
1 var url = 'https://127.0.0.1/svn/业务学习资料/资料整理文档/JAVA学习/JAVA开发工具idea的使用01-王少帅.mp4'; 2 var pre_url = url + '?url.type=svn&svn.username=wangshaoshuai&svn.password=123456'; 3 window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(pre_url)));
主要是这段代码,即可加入到svn列表显示的服务里面会弹窗跳转一个新的预览页面
4.测试验证
点击播放即可播放