背景
- 本人刚入行不到1年的程序猿。老板说下个项目要集成onlyoffice,让我研究一下。(最新版本)
开始
- 我在网上找了一圈,发现挺简单。使用docker很容易就安装好了。然后让老板看一下效果说不错,不过不能使用docker,要散装。下面就是我遇到的各种问题:
- 下载好的rpm安装后不能用,在线安装正常。这就很离谱,请懂的大哥指点一下
$ sudo yum install onlyoffice-documentserver --downloadonly --downloaddir /opt/temp/onlyoffice-documentserver
$ sudo yum install onlyoffice-documentserver
- onlyoffice连接mysql
$ cd /usr/bin/
$ vim documentserver-configure.sh
修改 DB_TYPE
DB_TYPE=${DB_TYPE:-mysql}
- onlyoffice去掉token验证
$ cd /etc/onlyoffice/documentserver
$ vim local.json
修改为
"token": {
"enable": {
"request": {
"inbox": false,
"outbox": false
},
"browser": false
},
"secret": {
"inbox": {
"string": ""
},
"outbox": {
"string": ""
},
"session": {
"string": ""
}
}
安装阶段到此结束
- 具体的安装过程网都有,我这就不细说了。只写一下我没找到的问题。
需求
-
控制编辑器的菜单,(不是保存、下载按钮,是菜单栏中的一级菜单)
网上、技术群里问了一遍,都没有办法。最后不了了之了。
-
把各种功能按钮提出来。(保存、下载、打印...)
2.1. 保存
* https://documentserver/coauthoring/CommandService.ashx
* {
* "c": "forcesave",
* "key": "Khirz6zTPdfd7",
* "userdata": "sample userdata"
* }
public String save(String key,String userId){
Gson gson = new Gson();
Map<String,String> map = new HashMap<>();
map.put("c","forcesave");
map.put("key",key);
map.put("userdata",userId);
String bodyString = gson.toJson(map);
byte[] bodyByte = bodyString.getBytes(StandardCharsets.UTF_8);
try {
URL url = new URL(onlyoffic服务器的地址+/coauthoring/CommandService.ashx);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setFixedLengthStreamingMode(bodyByte.length);
connection.setRequestProperty("Accept", "application/json");
connection.setConnectTimeout(onlyProperties.getTimeout());
connection.connect();
OutputStream os = connection.getOutputStream();
os.write(bodyByte);
InputStream stream = connection.getInputStream();
if (stream == null){
throw new Exception("Could not get an answer");
}
/**将流转为字符串*/
String jsonString = FileUtil.ConvertStreamToString(stream);
connection.disconnect();
JSONObject jsonObj = JSONObject.parseObject(jsonString);
log.debug(jsonObj.toJSONString());
Object error = jsonObj.get("error");
String msg = "";
if ( error != null) {
switch ((Integer) error){
case 1:msg = "缺少文档密钥或找不到具有此类密钥的文档";break;
case 2:msg="回调网址不正确";break;
case 3:msg="内部服务器错误";break;
case 4:msg="在收到强制保存命令之前,未对文档应用任何更改";break;
case 5:msg="命令不正确";break;
case 6:msg="令牌无效";break;
default:msg="保存成功";
}
}
return msg;
}catch (Exception e){
e.printStackTrace();
}
return "";
}
2.2 转化
/**
* 文件转化
* @param filetype 文件类型
* @param key 文件key
* @param outputtype 输出类型
* @param fileUrl 文件路径
* @param title 输出文件名称
* @return 文件路径
*/
@Override
public String converted(String filetype, String key, String outputtype, String fileUrl, String title) {
log.debug("文件开始转化{}->{}",filetype,outputtype);
ConvertBody body = new ConvertBody(filetype, key, outputtype, fileUrl, title);
Gson gson = new Gson();
String bodyString = gson.toJson(body);
log.debug(bodyString);
byte[] bodyByte = bodyString.getBytes(StandardCharsets.UTF_8);
try {
URL url = new URL(onlyoffic服务器的地址+/ConvertService.ashx);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setFixedLengthStreamingMode(bodyByte.length);
connection.setRequestProperty("Accept", "application/json");
connection.setConnectTimeout(onlyProperties.getTimeout());
connection.connect();
OutputStream os = connection.getOutputStream();
os.write(bodyByte);
InputStream stream = connection.getInputStream();
if (stream == null){
throw new Exception("Could not get an answer");
}
/**将流转为字符串*/
String jsonString = FileUtil.ConvertStreamToString(stream);
connection.disconnect();
JSONObject jsonObj = JSONObject.parseObject(jsonString);
log.debug(jsonObj.toJSONString());
/**
* {
* "endConvert":true,//转换是否完成
* "fileUrl":“ https://documentserver/ResourceService.ashx?filename=output.doc”,//转换后的文件地址
* "percent":100//转换完成百分比 仅参数设置为异步时
* }
*/
Object error = jsonObj.get("error");
if (error != null) {
ProcessConvertServiceResponceError((Integer) error);
}
/**检查转换是否完成,并将结果保存到一个变量中*/
Boolean isEndConvert = (Boolean) jsonObj.get("endConvert");
Long resultPercent = 0l;
String responseUri = null;
if (isEndConvert) {
resultPercent = 100l;
responseUri = (String) jsonObj.get("fileUrl");
log.debug("文件转化完成{}->{}",filetype,outputtype);
} else {
resultPercent = (Long) jsonObj.get("percent");
resultPercent = resultPercent >= 100l ? 99l : resultPercent;
}
return resultPercent >= 100l ? responseUri : "";
}catch (Exception e){
e.printStackTrace();
}
return null;
}
2.3 打印
我是把源码中的打印功能提取出来了
function onPrintUrl(t) {
if (this.iframePrint && (this.iframePrint.parentNode.removeChild(this.iframePrint),
this.iframePrint = null),
!this.iframePrint) {
var e = this;
this.iframePrint = document.createElement("iframe"),
this.iframePrint.id = "id-print-frame",
this.iframePrint.style.display = "none",
this.iframePrint.style.visibility = "hidden",
this.iframePrint.style.position = "fixed",
this.iframePrint.style.right = "0",
this.iframePrint.style.bottom = "0",
document.body.appendChild(this.iframePrint),
this.iframePrint.onload = function() {
try {
layer.closeAll();
e.iframePrint.contentWindow.focus(),
e.iframePrint.contentWindow.print(),
e.iframePrint.contentWindow.blur(),
window.focus()
} catch (t) {
console.log("打印出错")
}
}
}
t && (this.iframePrint.src = t)
}
结束
本文整理了遇到的一些问题。
又学习了一些知识