1 package com.dotmarketing.portlets.files.action; 2 3 import java.io.IOException ; 4 import java.net.URLDecoder ; 5 import java.util.Date ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import javax.portlet.ActionRequest; 10 import javax.portlet.ActionResponse; 11 import javax.portlet.PortletConfig; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import org.apache.struts.action.ActionForm; 15 import org.apache.struts.action.ActionMapping; 16 17 import com.dotmarketing.beans.Host; 18 import com.dotmarketing.beans.Identifier; 19 import com.dotmarketing.beans.Permission; 20 import com.dotmarketing.cache.PermissionCache; 21 import com.dotmarketing.cache.WorkingCache; 22 import com.dotmarketing.db.DotHibernate; 23 import com.dotmarketing.factories.HostFactory; 24 import com.dotmarketing.factories.IdentifierFactory; 25 import com.dotmarketing.factories.InodeFactory; 26 import com.dotmarketing.factories.PermissionFactory; 27 import com.dotmarketing.factories.PublishFactory; 28 import com.dotmarketing.factories.WebAssetFactory; 29 import com.dotmarketing.portal.struts.DotPortletAction; 30 import com.dotmarketing.portlets.files.factories.FileFactory; 31 import com.dotmarketing.portlets.files.model.File; 32 import com.dotmarketing.portlets.files.struts.FileForm; 33 import com.dotmarketing.portlets.folders.factories.FolderFactory; 34 import com.dotmarketing.portlets.folders.model.Folder; 35 import com.dotmarketing.util.Config; 36 import com.dotmarketing.util.Logger; 37 import com.dotmarketing.util.UtilMethods; 38 import com.dotmarketing.util.WebKeys; 39 import com.liferay.portal.model.User; 40 import com.liferay.portal.struts.ActionException; 41 import com.liferay.portal.util.Constants; 42 import com.liferay.portal.util.PortalUtil; 43 import com.liferay.portlet.ActionRequestImpl; 44 import com.liferay.util.FileUtil; 45 import com.liferay.util.ParamUtil; 46 import com.liferay.util.servlet.SessionMessages; 47 import com.liferay.util.servlet.UploadPortletRequest; 48 49 52 53 public class UploadMultipleFilesAction extends DotPortletAction { 54 55 public void processAction( 56 ActionMapping mapping, ActionForm form, PortletConfig config, 57 ActionRequest req, ActionResponse res) 58 throws Exception { 59 60 String cmd = req.getParameter(Constants.CMD); 61 String referer = req.getParameter("referer"); 62 63 ActionRequestImpl reqImpl = (ActionRequestImpl)req; 65 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 66 67 if ((referer!=null) && (referer.length()!=0)) { 68 referer = URLDecoder.decode(referer,"UTF-8"); 69 } 70 71 Logger.debug(this, "UploadMultipleFilesAction cmd=" + cmd); 72 73 DotHibernate.startTransaction(); 74 75 User user = _getUser(req); 76 77 try { 78 Logger.debug(this, "Calling Edit Method"); 79 _editWebAsset(req, res, config, form, user); 80 81 } 82 catch (Exception e) { 83 84 } 85 86 if ((cmd != null) && cmd.equals(Constants.ADD)) { 87 try { 88 89 Logger.debug(this, "Calling Save Method"); 90 91 String subcmd = req.getParameter("subcmd"); 92 93 _saveWebAsset(req, res, config, form, user, subcmd); 94 95 _sendToReferral(req,res,referer); 96 97 } catch (ActionException ae) { 98 _handleException(ae, req); 99 if (ae.getMessage().equals("message.file_asset.error.filename.exists")) { 100 _sendToReferral(req,res,referer); 101 } 102 else if (ae.getMessage().equals(WebKeys.USER_PERMISSIONS_EXCEPTION)) { 103 SessionMessages.add(httpReq, "message", "message.insufficient.permissions.to.save"); 104 _sendToReferral(req,res,referer); 105 } 106 } 107 108 } 109 Logger.debug(this, "Unspecified Action"); 110 DotHibernate.commitTransaction(); 111 setForward(req, "portlet.ext.files.upload_multiple"); 112 } 113 114 public void _editWebAsset(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 115 throws Exception { 116 117 Folder parentFolder = (Folder) com.dotmarketing.factories.InodeFactory.getInode(req.getParameter("parent"), Folder.class); 118 119 FileForm cf = (FileForm) form; 121 cf.setSelectedparent(parentFolder.getName()); 122 cf.setParent(parentFolder.getInode()); 123 cf.setSelectedparentPath(parentFolder.getPath()); 124 125 req.setAttribute("PARENT_FOLDER",parentFolder); 126 127 } 128 129 public void _saveWebAsset(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user, String subcmd) 130 throws Exception { 131 132 long maxsize = 50; 133 long maxwidth = 3000; 134 long maxheight = 3000; 135 long minheight = 10; 136 137 ActionRequestImpl reqImpl = (ActionRequestImpl)req; 139 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 140 141 try { 142 UploadPortletRequest uploadReq = PortalUtil.getUploadPortletRequest(req); 143 144 String parent = ParamUtil.getString(req, "parent"); 145 int countFiles = ParamUtil.getInteger(req, "countFiles"); 146 147 Folder folder = (Folder) InodeFactory.getInode(parent, Folder.class); 149 150 _checkUserPermissions(folder, user, Config.getIntProperty("PERMISSION_WRITE")); 152 153 String userId = user.getUserId(); 155 156 157 String customMessage = "Some file does not match the filters specified by the folder: "; 158 boolean filterError = false; 159 for (int k=0;k<countFiles;k++) { 160 161 File file = new File(); 162 String title = ParamUtil.getString(req, "title"+k); 163 String friendlyName = ParamUtil.getString(req, "friendlyName"+k); 164 165 166 167 Date publishDate = new Date (); 168 String fileName = ParamUtil.getString(req, "fileName"+k); 169 170 if(!FolderFactory.matchFilter(folder,fileName)) 171 { 172 customMessage += fileName + ", "; 173 filterError = true; 174 continue; 175 } 176 177 if (fileName.length()>0) { 178 179 String webInfPath = Config.CONTEXT.getRealPath("/META-INF"); 181 182 javax.activation.MimetypesFileTypeMap mimeTypeFileTypeMap = new javax.activation.MimetypesFileTypeMap (webInfPath + java.io.File.separator + "mime.types"); 183 String mimeType = mimeTypeFileTypeMap.getContentType(fileName); 184 185 String URI = folder.getPath() + fileName; 186 187 String suffix = UtilMethods.getFileExtension(fileName); 189 file.setTitle(title); 191 file.setFileName(fileName); 192 file.setFriendlyName(friendlyName); 193 file.setPublishDate(publishDate); 194 file.setModUser(userId); 196 InodeFactory.saveInode(file); 197 198 String filePath = FileFactory.getRealAssetsRootPath(); 200 new java.io.File (filePath).mkdir(); 201 202 byte[] bytes = FileUtil.getBytes(uploadReq.getFile("uploadedFile"+k)); 203 204 Logger.debug(this, "bytes" + bytes.length); 205 206 file.setSize(bytes.length-2); 208 file.setMimeType(mimeType); 209 210 Host host = HostFactory.getCurrentHost(httpReq); 212 Identifier ident = IdentifierFactory.getIdentifierByURI(URI,host); 213 214 String message = ""; 215 216 if ((FileFactory.existsFileName(folder,fileName))) { 218 InodeFactory.deleteInode(file); 219 message = "The uploaded file " + fileName + " already exists in this folder"; 220 SessionMessages.add(req, "custommessage", message); 221 } 222 else { 223 224 String fileInodePath = String.valueOf(file.getInode()); 226 if (fileInodePath.length()==1) { 227 fileInodePath = fileInodePath + "0"; 228 } 229 fileInodePath = fileInodePath.substring(0,1) + java.io.File.separator + fileInodePath.substring(1,2); 231 new java.io.File (filePath + java.io.File.separator + fileInodePath.substring(0,1)).mkdir(); 233 new java.io.File (filePath + java.io.File.separator + fileInodePath).mkdir(); 235 236 java.io.File f = new java.io.File (filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + suffix); 238 239 java.io.FileOutputStream fout = new java.io.FileOutputStream (f); 241 java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream (fout); 243 244 bout.write(bytes); 246 bout.flush(); 248 bout.close(); 249 251 Logger.debug(this, "SaveFileAction New File in =" + filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + suffix); 252 253 254 if (suffix.equals("jpg") || suffix.equals("gif")) { 256 257 com.dotmarketing.util.Thumbnail.resizeImage(filePath + java.io.File.separator + fileInodePath + java.io.File.separator,String.valueOf(file.getInode()),suffix); 258 259 int height = javax.imageio.ImageIO.read(f).getHeight(); 261 file.setHeight(height); 262 Logger.debug(this, "File height=" + height); 263 int width = javax.imageio.ImageIO.read(f).getWidth(); 265 file.setWidth(width); 266 Logger.debug(this, "File width=" + width); 267 long size = (f.length() / 1024); 269 270 if (file.getMaxSize()!=0) maxsize = file.getMaxSize(); 271 if (file.getMaxWidth()!=0) maxwidth = file.getMaxWidth(); 272 if (file.getMaxHeight()!=0) maxheight = file.getMaxHeight(); 273 if (file.getMinHeight()!=0) minheight = file.getMinHeight(); 274 275 if (width > maxwidth) { 276 message = "The uploaded file " + fileName + " is too wide: " + width + "px. Maximum width allowed is: " + maxwidth + "px"; 277 f.delete(); 278 InodeFactory.deleteInode(file); 279 Logger.debug(this, "message from File Upload=" + message); 280 SessionMessages.add(req, "custommessage", message); 281 } 282 else if (height > maxheight) { 283 message = "The uploaded file " + fileName + " is too tall: " + height + "px. Maximum height allowed is: " + maxheight + "px"; 284 f.delete(); 285 InodeFactory.deleteInode(file); 286 Logger.debug(this, "message from File Upload=" + message); 287 SessionMessages.add(req, "custommessage", message); 288 } 289 else { 290 WebAssetFactory.createAsset(file,userId,folder); 292 } 293 } 294 else { 295 WebAssetFactory.createAsset(file,userId,folder); 298 } 299 WorkingCache.addToWorkingAssetToCache(file); 300 301 _setFilePermissions (folder, file, user); 303 304 if ((subcmd != null) && subcmd.equals(com.dotmarketing.util.Constants.PUBLISH)) { 305 PublishFactory.publishAsset(file,httpReq); 306 } 307 } 308 } 309 } 310 if(filterError) 311 { 312 customMessage = customMessage.substring(0,customMessage.lastIndexOf(",")); 313 SessionMessages.add(req, "custommessage",customMessage); 314 } 315 SessionMessages.add(req, "message", "message.file_asset.save"); 316 } 317 catch (IOException e) { 318 e.printStackTrace(System.out); 319 Logger.error(this, "\n\n\nEXCEPTION IN FILE SAVING!!!"); 320 Logger.error(this, e.getMessage()); 321 throw new ActionException(e.getMessage()); 322 } 323 } 324 325 326 private void _setFilePermissions (Folder folder, File file, User user) { 327 328 329 Identifier id = IdentifierFactory.getIdentifierByInode(file); 330 331 if (id.getInode() == 0) return; 332 333 List perms = PermissionFactory.getPermissions(folder); 334 335 Iterator it = perms.iterator(); 337 338 while (it.hasNext()) { 339 Permission folderPerm = (Permission) it.next(); 340 Permission permission = new Permission(id.getInode(), folderPerm.getRoleId(), folderPerm.getPermission()); 341 PermissionFactory.savePermission(permission); 342 } 343 344 PermissionCache.addToPermissionCache(file); 346 347 } 348 349 } 350 | Popular Tags |