| 1 package com.dotmarketing.portlets.files.factories; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import javax.servlet.http.HttpServletRequest ; 8 9 import com.dotmarketing.beans.Host; 10 import com.dotmarketing.beans.Identifier; 11 import com.dotmarketing.beans.Inode; 12 import com.dotmarketing.beans.PermissionAsset; 13 import com.dotmarketing.beans.Tree; 14 import com.dotmarketing.cache.LiveCache; 15 import com.dotmarketing.cache.PermissionCache; 16 import com.dotmarketing.cache.WorkingCache; 17 import com.dotmarketing.db.DotHibernate; 18 import com.dotmarketing.exception.DotRuntimeException; 19 import com.dotmarketing.factories.HostFactory; 20 import com.dotmarketing.factories.IdentifierFactory; 21 import com.dotmarketing.factories.InodeFactory; 22 import com.dotmarketing.factories.PermissionFactory; 23 import com.dotmarketing.factories.TreeFactory; 24 import com.dotmarketing.factories.WebAssetFactory; 25 import com.dotmarketing.menubuilders.RefreshMenus; 26 import com.dotmarketing.portlets.files.model.File; 27 import com.dotmarketing.portlets.folders.model.Folder; 28 import com.dotmarketing.util.Config; 29 import com.dotmarketing.util.Logger; 30 import com.dotmarketing.util.UtilMethods; 31 import com.dotmarketing.util.WebKeys; 32 import com.liferay.portal.model.Role; 33 import com.liferay.portal.model.User; 34 import com.liferay.portal.struts.ActionException; 35 import com.liferay.portlet.ActionRequestImpl; 36 import com.liferay.util.servlet.SessionMessages; 37 38 42 public class FileFactory { 43 44 public static File getChildImage(Inode i) { 45 46 return (File) InodeFactory.getChildOfClassbyCondition(i, File.class, 47 "(file_name like '%.jpg' or file_name like '%.gif')"); 48 49 } 50 51 public static java.util.List getLiveFiles() { 52 DotHibernate dh = new DotHibernate(File.class); 53 dh.setQuery("from inode in class com.dotmarketing.portlets.files.model.File where live = " 54 + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted = " 55 + com.dotmarketing.db.DbConnectionFactory.getDBFalse()); 56 return dh.list(); 57 } 58 59 public static java.util.List getWorkingFiles() { 60 DotHibernate dh = new DotHibernate(File.class); 61 dh.setQuery("from inode in class com.dotmarketing.portlets.files.model.File where working = " 62 + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted = " 63 + com.dotmarketing.db.DbConnectionFactory.getDBFalse()); 64 return dh.list(); 65 } 66 67 public static File getChildMP3(Inode i) { 68 return (File) InodeFactory.getChildOfClassbyCondition(i, File.class, "file_name like '%.mp3'"); 69 70 } 71 72 public static java.util.List getChildrenFilesByOrder(Inode i) { 73 74 return InodeFactory.getChildrenClassByOrder(i, File.class, "sort_order"); 75 76 } 77 78 public static java.util.List getFileChildrenByCondition(Inode i, String condition) { 79 80 return InodeFactory.getChildrenClassByConditionAndOrderBy(i, File.class, condition, "file_name, sort_order"); 81 82 } 83 84 public static java.util.List getFileChildrenByConditionAndOrder(Inode i, String condition,String order) { 85 86 return InodeFactory.getChildrenClassByConditionAndOrderBy(i, File.class, condition, order); 87 88 } 89 90 public static java.util.List getFileChildren(Inode i) { 91 92 return InodeFactory.getChildrenClassByOrder(i, File.class, "inode, sort_order"); 93 94 } 95 96 public static boolean existsFileName(Inode parent, String fileName) { 97 98 Logger.debug(FileFactory.class, "UtilMethods.sqlify(fileName)" + UtilMethods.sqlify(fileName)); 99 File f = (File) InodeFactory.getChildOfClassbyCondition(parent, File.class, "file_name = '" 100 + UtilMethods.sqlify(fileName) + "'"); 101 102 return (f.getInode() > 0); 103 } 104 105 public static File getFileByURI(String uri, Host host, boolean live) { 106 return getFileByURI(uri, host.getInode(), live); 107 } 108 109 public static File getFileByURI(String uri, long hostId, boolean live) { 110 111 uri = uri.replaceAll(Config.getStringProperty("VIRTUAL_FILE_PREFIX"), ""); 112 Logger.debug(FileFactory.class, "getFileByURI=" + uri); 113 Identifier i = IdentifierFactory.getIdentifierByURI(uri, hostId); 114 return getFileByIdentifier(i, live); 115 116 } 117 118 public static File getFileByIdentifier(Identifier i, boolean live) { 119 120 if (live) { 121 return (File) InodeFactory.getChildOfClassbyCondition(i, File.class, "live = " 122 + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted = " 123 + com.dotmarketing.db.DbConnectionFactory.getDBFalse()); 124 125 } else { 126 return (File) InodeFactory.getChildOfClassbyCondition(i, File.class, "working = " 127 + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted = " 128 + com.dotmarketing.db.DbConnectionFactory.getDBFalse()); 129 130 } 131 132 } 133 134 public static String getVirtualFileURI(File file) { 135 136 Identifier identifier = (Identifier) InodeFactory.getParentOfClass(file, Identifier.class); 137 if (identifier.getInode() > 0) { 138 return (Config.getStringProperty("VIRTUAL_FILE_PREFIX") + identifier.getURI()).intern(); 139 } 140 return null; 141 } 142 143 public static String getVersionFileURI(File file) { 144 145 return Config.getStringProperty("VERSION_FILE_PREFIX") + file.getInode() + "." 146 + UtilMethods.getFileExtension(file.getFileName()).intern(); 147 148 } 149 150 public static String getRelativeAssetPath(Inode inode) { 151 String _inode = Long.toString(inode.getInode()); 152 return getRelativeAssetPath(_inode, UtilMethods.getFileExtension(((com.dotmarketing.portlets.files.model.File) inode).getFileName()) 153 .intern()); 154 } 155 156 public static String getRelativeAssetPath(String inode, String ext) { 157 String _inode = inode; 158 String path = ""; 159 160 path = Config.getStringProperty("ASSET_PATH") + java.io.File.separator + _inode.charAt(0) 161 + java.io.File.separator + _inode.charAt(1) + java.io.File.separator + _inode + "." + ext; 162 163 return path; 164 } 165 166 public static String getRealAssetPath(Inode inode) { 167 String _inode = Long.toString(inode.getInode()); 168 return getRealAssetPath (_inode, UtilMethods.getFileExtension(((com.dotmarketing.portlets.files.model.File) inode).getFileName()) 169 .intern()); 170 } 171 172 public static String getRealAssetPath(String inode, String ext) { 173 String _inode = inode; 174 String path = ""; 175 176 String realPath = Config.getStringProperty("ASSET_REAL_PATH"); 177 if (UtilMethods.isSet(realPath) && !realPath.endsWith(java.io.File.separator)) 178 realPath = realPath + java.io.File.separator; 179 180 String assetPath = Config.getStringProperty("ASSET_PATH"); 181 if (UtilMethods.isSet(assetPath) && !assetPath.endsWith(java.io.File.separator)) 182 assetPath = assetPath + java.io.File.separator; 183 184 path = ((!UtilMethods.isSet(realPath)) ? assetPath : realPath) 185 + _inode.charAt(0) + java.io.File.separator + _inode.charAt(1) 186 + java.io.File.separator + _inode + "." + ext; 187 188 if (!UtilMethods.isSet(realPath)) 189 return Config.CONTEXT.getRealPath(path); 190 else 191 return path; 192 } 193 194 public static String getRelativeAssetsRootPath() { 195 String path = ""; 196 197 path = Config.getStringProperty("ASSET_PATH"); 198 199 return path; 200 } 201 202 public static String getRealAssetsRootPath() { 203 String realPath = Config.getStringProperty("ASSET_REAL_PATH"); 204 if (UtilMethods.isSet(realPath) && !realPath.endsWith(java.io.File.separator)) 205 realPath = realPath + java.io.File.separator; 206 if (!UtilMethods.isSet(realPath)) 207 return Config.CONTEXT.getRealPath(getRelativeAssetsRootPath()); 208 else 209 return realPath; 210 } 211 212 227 228 public static java.util.List getFilesPerRoleAndCondition(Host host, Role[] roles, String condition) { 229 return getFilesPerRoleAndCondition(host.getInode(), roles, condition); 230 } 231 232 public static java.util.List getFilesPerRoleAndCondition(long hostId, Role[] roles, String condition) { 233 java.util.List entries = new java.util.ArrayList (); 234 java.util.List folders = com.dotmarketing.portlets.folders.factories.FolderFactory.getFoldersByParent(hostId); 235 return com.dotmarketing.portlets.folders.factories.FolderFactory.getFoldersAndEntriesByRoles(folders, entries, 236 roles, File.class, condition); 237 } 238 239 public static java.util.List getFilesPerRole(long hostId, Role[] roles) { 240 java.util.List entries = new java.util.ArrayList (); 241 java.util.List folders = com.dotmarketing.portlets.folders.factories.FolderFactory.getFoldersByParent(hostId); 242 return com.dotmarketing.portlets.folders.factories.FolderFactory.getFoldersAndEntriesByRoles(folders, entries, 243 roles, File.class); 244 } 245 246 public static java.util.List getFilesByCondition(String condition) { 247 248 DotHibernate dh = new DotHibernate(File.class); 249 dh.setQuery("from inode in class class com.dotmarketing.portlets.files.model.File where " + condition 250 + " order by file_name, sort_order"); 251 return dh.list(); 252 } 253 254 public static java.util.List <PermissionAsset> getFilesAndPermissionsPerRoleAndCondition(Role[] roles, 255 String condition, int limit, int offset, String orderby) { 256 257 java.util.List <PermissionAsset> entries = new java.util.ArrayList <PermissionAsset>(); 258 orderby = "file_asset." + orderby; 259 java.util.List elements = WebAssetFactory.getAssetsPerConditionWithPermission(condition, File.class, roles, 260 limit, offset, orderby, 0); 261 java.util.Iterator elementsIter = elements.iterator(); 262 263 while (elementsIter.hasNext()) { 264 265 File file = (File) elementsIter.next(); 266 Folder folderParent = (Folder) InodeFactory.getParentOfClass(file, Folder.class); 267 268 java.util.List permissions = PermissionCache.getPermissionIdsByRolesFromCache(file, roles); 269 270 PermissionAsset permAsset = new PermissionAsset(); 271 permAsset.setPathToMe(folderParent.getPath()); 272 permAsset.setPermissions(permissions); 273 permAsset.setAsset(file); 274 entries.add(permAsset); 275 } 276 277 return entries; 278 279 } 280 281 public static java.util.List getFilesPerRoleAndParent(Role[] roles, 282 com.dotmarketing.portlets.folders.model.Folder folderParent) { 283 284 java.util.List entries = new java.util.ArrayList (); 285 java.util.List permissions = PermissionCache.getPermissionIdsByRolesFromCache(folderParent, roles); 286 if (permissions.contains(Config.getStringProperty("PERMISSION_READ"))) { 287 entries = InodeFactory.getChildrenClass(folderParent, File.class); 289 } 290 return entries; 291 } 292 293 public static java.util.List getFilesPerRoleParentAndCondition(Role[] roles, 294 com.dotmarketing.portlets.folders.model.Folder folderParent, String condition) { 295 296 java.util.List entries = new java.util.ArrayList (); 297 java.util.List permissions = PermissionCache.getPermissionIdsByRolesFromCache(folderParent, roles); 298 if (permissions.contains(Config.getStringProperty("PERMISSION_READ"))) { 299 entries = InodeFactory.getChildrenClassByCondition(folderParent, File.class, condition); 301 } 302 return entries; 303 } 304 305 public static java.util.List getFilesByParentFolderPerRoleAndCondition(Role[] roles, 306 Folder folderParent, String condition) { 307 308 List <File> entries = new ArrayList (); 309 310 List <File> elements = InodeFactory.getChildrenClassByCondition(folderParent, File.class, condition);; 311 for(File file : elements){ 312 List permissions = PermissionCache 313 .getPermissionIdsByRolesFromCache(file, roles); 314 315 if (permissions.contains(Config 316 .getStringProperty("PERMISSION_READ"))) { 317 319 entries.add(file); 320 } 321 } 322 323 return entries; 324 } 325 326 335 public static boolean moveFile(File file, Folder parent) { 336 337 Identifier identifier = com.dotmarketing.factories.IdentifierFactory.getParentIdentifier(file); 338 339 File workingWebAsset = (File) IdentifierFactory.getWorkingChildOfClass(identifier, File.class); 341 File liveWebAsset = (File) IdentifierFactory.getLiveChildOfClass(identifier, File.class); 343 344 if (FileFactory.existsFileName(parent, file.getFileName())) { 347 return false; 348 } 349 350 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 352 LiveCache.removeAssetFromCache(liveWebAsset); 353 } 354 WorkingCache.removeAssetFromCache(workingWebAsset); 355 356 Folder oldParent = (Folder) InodeFactory.getParentOfClass(workingWebAsset, Folder.class); 358 359 oldParent.deleteChild(workingWebAsset); 360 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 361 oldParent.deleteChild(liveWebAsset); 362 } 363 parent.addChild(workingWebAsset); 365 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 366 parent.addChild(liveWebAsset); 367 } 368 369 Host newHost = HostFactory.getParentHost(parent); 372 identifier.setHostInode(newHost.getInode()); 373 identifier.setURI(workingWebAsset.getURI(parent)); 374 InodeFactory.saveInode(identifier); 375 376 if ((liveWebAsset != null) && (liveWebAsset.getInode() > 0)) { 378 LiveCache.addToLiveAssetToCache(liveWebAsset); 379 } 380 WorkingCache.addToWorkingAssetToCache(workingWebAsset); 381 382 if (file.isShowOnMenu()) { 383 RefreshMenus.deleteMenus(); 385 } 386 387 return true; 388 389 } 390 391 400 public static File copyFile(File file, Folder parent) { 401 402 File newFile = new File(); 403 try { 404 405 newFile.copy(file); 406 newFile.setLocked(false); 407 newFile.setLive(false); 408 newFile.setFriendlyName(file.getFriendlyName() + " (COPY) "); 409 410 String fileName = com.dotmarketing.util.UtilMethods.getFileName(file.getFileName()); 412 String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName()); 414 415 if (FileFactory.existsFileName(parent, file.getFileName())) { 417 newFile.setFileName(fileName + "_copy." + fileExtension); 419 } else { 420 newFile.setFileName(fileName + "." + fileExtension); 421 } 422 423 InodeFactory.saveInode(newFile); 425 426 String filePath = getRealAssetsRootPath(); 429 String fileInodePath = String.valueOf(newFile.getInode()); 431 if (fileInodePath.length() == 1) { 432 fileInodePath = fileInodePath + "0"; 433 } 434 fileInodePath = fileInodePath.substring(0, 1) + java.io.File.separator + fileInodePath.substring(1, 2); 436 new java.io.File (filePath + java.io.File.separator + fileInodePath.substring(0, 1)).mkdir(); 439 new java.io.File (filePath + java.io.File.separator + fileInodePath).mkdir(); 442 443 java.io.File newF = new java.io.File (filePath + java.io.File.separator + fileInodePath 445 + java.io.File.separator + newFile.getInode() + "." + fileExtension); 446 447 fileInodePath = String.valueOf(file.getInode()); 448 if (fileInodePath.length() == 1) { 449 fileInodePath = fileInodePath + "0"; 450 } 451 fileInodePath = fileInodePath.substring(0, 1) + java.io.File.separator + fileInodePath.substring(1, 2); 453 java.io.File currF = new java.io.File (filePath + java.io.File.separator + fileInodePath 456 + java.io.File.separator + file.getInode() + "." + fileExtension); 457 458 java.io.FileOutputStream fout = new java.io.FileOutputStream (newF); 460 java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream (fout); 462 java.io.FileInputStream is = new java.io.FileInputStream (currF); 464 java.io.BufferedInputStream bin = new java.io.BufferedInputStream (is); 466 467 int bytedata; 468 while ((bytedata = bin.read()) != -1) { 470 bout.write(bytedata); 471 } 472 bout.flush(); 474 bout.close(); 475 bin.close(); 476 477 479 parent.addChild(newFile); 481 482 Identifier newIdentifier = IdentifierFactory.createNewIdentifier(newFile, parent); 484 485 Logger.debug(FileFactory.class, "identifier=" + newIdentifier.getURI()); 486 487 WorkingCache.addToWorkingAssetToCache(newFile); 488 LiveCache.addToLiveAssetToCache(newFile); 489 490 PermissionFactory.copyPermissions(file, newFile); 492 } catch (IOException e) { 493 throw new DotRuntimeException("An error ocurred trying to copy the file.", e); 494 } 495 return newFile; 496 497 498 } 499 500 public static boolean renameFile (File file, String newName, User user) throws Exception { 501 502 if (!PermissionFactory.doesUserHavePermission(file, Config.getIntProperty("PERMISSION_WRITE"), user)) 504 throw new ActionException(WebKeys.USER_PERMISSIONS_EXCEPTION); 505 506 String oldFileName = file.getFileName(); 508 String ext = UtilMethods.getFileExtension(oldFileName); 509 Folder folder = (Folder)InodeFactory.getParentOfClass(file, Folder.class); 510 511 Identifier ident = IdentifierFactory.getIdentifierByInode(file); 512 513 String newFileName = newName + "." + ext; 514 515 if(FileFactory.existsFileName(folder, newFileName) || file.isLocked()) 516 return false; 517 518 List <File> versions = IdentifierFactory.getVersionsandLiveandWorkingChildrenOfClass(ident, File.class); 519 520 for (File f : versions) { 521 522 f.setFileName(newFileName); 524 f.setFriendlyName(newFileName); 525 526 if (f.isLive()) 527 LiveCache.addToLiveAssetToCache(f); 528 if (f.isWorking()) 529 WorkingCache.addToWorkingAssetToCache(f); 530 531 InodeFactory.saveInode(f); 532 533 } 534 535 ident.setURI(file.getURI(folder)); 536 InodeFactory.saveInode(ident); 537 538 RefreshMenus.deleteMenus(); 539 540 return true; 541 542 } 543 } 544 | Popular Tags |