-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOnlyOfficeUtils.java
217 lines (178 loc) · 8.3 KB
/
OnlyOfficeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
*
* (c) Copyright Ascensio System SIA 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package onlyoffice.integration;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import com.liferay.document.library.constants.DLPortletKeys;
import com.liferay.document.library.kernel.model.DLFolderConstants;
import com.liferay.document.library.kernel.service.DLFileEntryLocalService;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.repository.model.FileVersion;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionCheckerFactory;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import java.util.Arrays;
import java.util.List;
import onlyoffice.integration.config.OnlyOfficeConfigManager;
@Component(
service = OnlyOfficeUtils.class
)
public class OnlyOfficeUtils {
public String getDocServerUrl() {
return fixUrl(_config.getDocUrl());
}
public String getDocServerInnnerUrl() {
return fixUrl(_config.getDocInnerUrl());
}
public String getLiferayUrl(HttpServletRequest req) {
return fixUrl(_config.getLiferayUrlOrDefault(PortalUtil.getPortalURL(req)));
}
public String getSaveAsUrl(HttpServletRequest req) {
return getLiferayUrl(req) + "o/onlyoffice/api?type=save-as";
}
public String replaceDocServerURLToInternal(String url) {
String innerDocEditorUrl = getDocServerInnnerUrl();
String publicDocEditorUrl = getDocServerUrl();
if (!publicDocEditorUrl.equals(innerDocEditorUrl)) {
url = url.replace(publicDocEditorUrl, innerDocEditorUrl);
}
return url;
}
private List<String> editableExtensions = Arrays.asList("docx", "xlsx", "pptx", "docxf");
public boolean isEditable(String ext) {
return editableExtensions.contains(trimDot(ext));
}
private List<String> fillFormExtensions = Arrays.asList("oform");
public boolean isFillForm(String ext) {
return fillFormExtensions.contains(trimDot(ext));
}
private List<String> viewableExtensions = Arrays.asList("odt", "doc", "ods", "xls", "odp", "ppt", "csv", "rtf", "txt", "pdf");
public boolean isViewable(String ext) {
return viewableExtensions.contains(trimDot(ext)) || isEditable(ext) || isFillForm(ext);
}
private String getGoBackUrl(RenderRequest request, FileEntry fileEntry) {
Group group = GroupLocalServiceUtil.fetchGroup(fileEntry.getGroupId());
PortletURL portletURL = PortalUtil.getControlPanelPortletURL(
request, group, DLPortletKeys.DOCUMENT_LIBRARY_ADMIN,
0, 0, PortletRequest.RENDER_PHASE);
long folderId = fileEntry.getFolderId();
if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
portletURL.setParameter("mvcRenderCommandName", "/document_library/view");
} else {
portletURL.setParameter("mvcRenderCommandName", "/document_library/view_folder");
portletURL.setParameter("folderId", String.valueOf(folderId));
}
return portletURL.toString();
}
public String getDocumentConfig(FileVersion file, RenderRequest req) {
JSONObject responseJson = new JSONObject();
JSONObject documentObject = new JSONObject();
JSONObject editorConfigObject = new JSONObject();
JSONObject customizationObject = new JSONObject();
JSONObject goBackObject = new JSONObject();
JSONObject userObject = new JSONObject();
JSONObject permObject = new JSONObject();
try {
String ext = file.getExtension();
User user = PortalUtil.getUser(req);
Long fileVersionId = file.getFileVersionId();
PermissionChecker checker = _permissionFactory.create(PortalUtil.getUser(req));
FileEntry fe = file.getFileEntry();
boolean editPerm = fe.containsPermission(checker, ActionKeys.UPDATE);
if (!fe.containsPermission(checker, ActionKeys.VIEW)) {
throw new Exception("User don't have read rights");
}
boolean edit = (isEditable(ext) || isFillForm(ext)) && editPerm;
String url = getFileUrl(PortalUtil.getHttpServletRequest(req), fileVersionId);
responseJson.put("type", "desktop");
responseJson.put("width", "100%");
responseJson.put("height", "100%");
responseJson.put("documentType", getDocType(ext));
responseJson.put("document", documentObject);
documentObject.put("title", file.getFileName());
documentObject.put("url", url);
documentObject.put("fileType", ext);
documentObject.put("key", file.getUuid() + "_" + file.getVersion().hashCode());
documentObject.put("permissions", permObject);
permObject.put("edit", edit);
responseJson.put("editorConfig", editorConfigObject);
editorConfigObject.put("lang", LocaleUtil.fromLanguageId(LanguageUtil.getLanguageId(req)).toLanguageTag());
editorConfigObject.put("mode", edit ? "edit" : "view");
editorConfigObject.put("customization", customizationObject);
customizationObject.put("goback", goBackObject);
goBackObject.put("url", getGoBackUrl(req, fe));
if (edit) {
editorConfigObject.put("callbackUrl", url);
}
editorConfigObject.put("user", userObject);
userObject.put("id", Long.toString(user.getUserId()));
userObject.put("firstname", user.getFirstName());
userObject.put("lastname", user.getLastName());
userObject.put("name", user.getFullName());
if (_jwt.isEnabled()) {
responseJson.put("token", _jwt.createToken(responseJson));
}
} catch (Exception e) {
_log.error(e.getMessage(), e);
}
return responseJson.toString().replace("'", "\\'");
}
public String getFileUrl(HttpServletRequest request, Long id) {
return getLiferayUrl(request) + "o/onlyoffice/doc?key=" + _hasher.getHash(id);
}
private String trimDot(String ext) {
if (ext.startsWith(".")) {
return ext.substring(1);
}
return ext;
}
private String fixUrl(String url) {
return url.endsWith("/") ? url : url + "/";
}
private String getDocType(String ext) {
if (".doc.docx.docm.dot.dotx.dotm.odt.fodt.ott.rtf.txt.html.htm.mht.pdf.djvu.fb2.epub.xps.docxf.oform".indexOf(ext) != -1) return "text";
if (".xls.xlsx.xlsm.xlt.xltx.xltm.ods.fods.ots.csv".indexOf(ext) != -1) return "spreadsheet";
if (".pps.ppsx.ppsm.ppt.pptx.pptm.pot.potx.potm.odp.fodp.otp".indexOf(ext) != -1) return "presentation";
return null;
}
@Reference
private OnlyOfficeJWT _jwt;
@Reference
private OnlyOfficeHasher _hasher;
@Reference
private OnlyOfficeConfigManager _config;
@Reference
private DLFileEntryLocalService _dlFile;
@Reference
private PermissionCheckerFactory _permissionFactory;
private static final Log _log = LogFactoryUtil.getLog(
OnlyOfficeUtils.class);
}