001
014
015 package com.liferay.portlet.documentlibrary.action;
016
017 import com.liferay.portal.kernel.util.FileUtil;
018 import com.liferay.portal.kernel.util.MimeTypesUtil;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.struts.ActionConstants;
023 import com.liferay.portal.struts.PortletAction;
024 import com.liferay.portal.theme.ThemeDisplay;
025 import com.liferay.portal.util.PortalUtil;
026 import com.liferay.portal.util.WebKeys;
027 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
028 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
030 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
031 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
032 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
033 import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
034 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
035 import com.liferay.portlet.documentlibrary.util.DLUtil;
036 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
037 import com.liferay.util.servlet.ServletResponseUtil;
038
039 import java.io.InputStream;
040
041 import javax.portlet.ActionRequest;
042 import javax.portlet.ActionResponse;
043 import javax.portlet.PortletConfig;
044
045 import javax.servlet.http.HttpServletRequest;
046 import javax.servlet.http.HttpServletResponse;
047
048 import org.apache.struts.action.ActionForm;
049 import org.apache.struts.action.ActionForward;
050 import org.apache.struts.action.ActionMapping;
051
052
058 public class GetFileAction extends PortletAction {
059
060 public ActionForward strutsExecute(
061 ActionMapping mapping, ActionForm form, HttpServletRequest request,
062 HttpServletResponse response)
063 throws Exception {
064
065 try {
066 long folderId = ParamUtil.getLong(request, "folderId");
067 String name = ParamUtil.getString(request, "name");
068 String title = ParamUtil.getString(request, "title");
069 String version = ParamUtil.getString(request, "version");
070
071 long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
072
073 String uuid = ParamUtil.getString(request, "uuid");
074
075 String targetExtension = ParamUtil.getString(
076 request, "targetExtension");
077
078 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
079 WebKeys.THEME_DISPLAY);
080
081 long groupId = ParamUtil.getLong(
082 request, "groupId", themeDisplay.getScopeGroupId());
083
084 getFile(
085 folderId, name, title, version, fileShortcutId, uuid, groupId,
086 targetExtension, themeDisplay, request, response);
087
088 return null;
089 }
090 catch (Exception e) {
091 PortalUtil.sendError(e, request, response);
092
093 return null;
094 }
095 }
096
097 public void processAction(
098 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
099 ActionRequest actionRequest, ActionResponse actionResponse)
100 throws Exception {
101
102 try {
103 long folderId = ParamUtil.getLong(actionRequest, "folderId");
104 String name = ParamUtil.getString(actionRequest, "name");
105 String title = ParamUtil.getString(actionRequest, "title");
106 String version = ParamUtil.getString(actionRequest, "version");
107
108 long fileShortcutId = ParamUtil.getLong(
109 actionRequest, "fileShortcutId");
110
111 String uuid = ParamUtil.getString(actionRequest, "uuid");
112
113 String targetExtension = ParamUtil.getString(
114 actionRequest, "targetExtension");
115
116 ThemeDisplay themeDisplay =
117 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
118
119 long groupId = ParamUtil.getLong(
120 actionRequest, "groupId", themeDisplay.getScopeGroupId());
121
122 HttpServletRequest request = PortalUtil.getHttpServletRequest(
123 actionRequest);
124 HttpServletResponse response = PortalUtil.getHttpServletResponse(
125 actionResponse);
126
127 getFile(
128 folderId, name, title, version, fileShortcutId, uuid, groupId,
129 targetExtension, themeDisplay, request, response);
130
131 setForward(actionRequest, ActionConstants.COMMON_NULL);
132 }
133 catch (NoSuchFileEntryException nsfee) {
134 PortalUtil.sendError(
135 HttpServletResponse.SC_NOT_FOUND, nsfee, actionRequest,
136 actionResponse);
137 }
138 catch (Exception e) {
139 PortalUtil.sendError(e, actionRequest, actionResponse);
140 }
141 }
142
143 protected void getFile(
144 long folderId, String name, String title, String version,
145 long fileShortcutId, String uuid, long groupId,
146 String targetExtension, ThemeDisplay themeDisplay,
147 HttpServletRequest request, HttpServletResponse response)
148 throws Exception {
149
150 long companyId = themeDisplay.getCompanyId();
151 long userId = themeDisplay.getUserId();
152
153 if (name.startsWith("DLFE-")) {
154 name = name.substring("DLFE-".length());
155 }
156
157 name = FileUtil.stripExtension(name);
158
159 DLFileEntry fileEntry = null;
160
161 if (Validator.isNotNull(uuid) && (groupId > 0)) {
162 try {
163 fileEntry = DLFileEntryServiceUtil.getFileEntryByUuidAndGroupId(
164 uuid, groupId);
165
166 folderId = fileEntry.getFolderId();
167 name = fileEntry.getName();
168 }
169 catch (Exception e) {
170 }
171 }
172
173 if (fileShortcutId <= 0) {
174 if (Validator.isNotNull(name)) {
175 fileEntry = DLFileEntryServiceUtil.getFileEntry(
176 groupId, folderId, name);
177
178 title = fileEntry.getTitle();
179 }
180 else if (Validator.isNotNull(title)) {
181 fileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
182 groupId, folderId, title);
183
184 name = fileEntry.getName();
185 }
186 }
187 else {
188 DLFileShortcut fileShortcut =
189 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
190
191 folderId = fileShortcut.getToFolderId();
192 name = fileShortcut.getToName();
193
194 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
195 groupId, folderId, name);
196 }
197
198 if (Validator.isNull(version)) {
199 if (Validator.isNotNull(fileEntry.getVersion())) {
200 version = fileEntry.getVersion();
201 }
202 else {
203 throw new NoSuchFileEntryException();
204 }
205 }
206
207 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
208 companyId, userId, groupId, folderId, name, version);
209
210 boolean converted = false;
211
212 String fileName = fileEntry.getTitle();
213
214 if (Validator.isNotNull(targetExtension)) {
215 String id = DocumentConversionUtil.getTempFileId(
216 fileEntry.getFileEntryId(), version);
217
218 String sourceExtension = FileUtil.getExtension(fileName);
219
220 InputStream convertedIS = DocumentConversionUtil.convert(
221 id, is, sourceExtension, targetExtension);
222
223 if ((convertedIS != null) && (convertedIS != is)) {
224 fileName = FileUtil.stripExtension(
225 fileEntry.getTitle()).concat(StringPool.PERIOD).concat(
226 targetExtension);
227
228 is = convertedIS;
229
230 converted = true;
231 }
232 }
233
234 int contentLength = 0;
235
236 if (!converted) {
237 if (DLUtil.compareVersions(version, fileEntry.getVersion()) >= 0) {
238 contentLength = (int)fileEntry.getSize();
239 }
240 else {
241 DLFileVersion fileVersion =
242 DLFileVersionLocalServiceUtil.getFileVersion(
243 groupId, folderId, name, version);
244
245 contentLength = (int)fileVersion.getSize();
246 }
247 }
248
249 String contentType = MimeTypesUtil.getContentType(fileName);
250
251 ServletResponseUtil.sendFile(
252 request, response, fileName, is, contentLength, contentType);
253 }
254
255 protected boolean isCheckMethodOnProcessAction() {
256 return _CHECK_METHOD_ON_PROCESS_ACTION;
257 }
258
259 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
260
261 }