001 /** 002 * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portlet.documentlibrary.store; 016 017 import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil; 018 import com.liferay.portal.kernel.exception.PortalException; 019 import com.liferay.portal.kernel.util.ReferenceRegistry; 020 021 import java.io.File; 022 import java.io.InputStream; 023 024 /** 025 * Provides methods for storing files in the portal. The file storage 026 * implementation can be selected in <code>portal.properties</code> under the 027 * property <code>dl.store.impl</code>. Virus checking can also be enabled under 028 * the property <code>dl.store.antivirus.impl</code>. 029 * 030 * <p> 031 * The main client for this class is the Document Library portlet. It is also 032 * used by other portlets like Wiki and Message Boards to store file 033 * attachments. For the Document Library portlet, the <code>repositoryId</code> 034 * can be obtained by calling {@link 035 * com.liferay.portlet.documentlibrary.model.DLFolderConstants#getDataRepositoryId( 036 * long,long)}. For all other portlets, the <code>repositoryId</code> should be 037 * set to {@link com.liferay.portal.model.CompanyConstants#SYSTEM}. These 038 * methods can be used in plugins and other portlets, as shown below. 039 * </p> 040 * 041 * <p> 042 * <pre> 043 * <code> 044 * long repositoryId = CompanyConstants.SYSTEM; 045 * String dirName = "portlet_name/1234"; 046 * 047 * try { 048 * DLStoreUtil.addDirectory(companyId, repositoryId, dirName); 049 * } 050 * catch (DuplicateDirectoryException dde) { 051 * } 052 * 053 * DLStoreUtil.addFile( 054 * companyId, repositoryId, dirName + "/" + fileName, file); 055 * </code> 056 * </pre> 057 * </p> 058 * 059 * @author Brian Wing Shun Chan 060 * @author Alexander Chow 061 * @author Edward Han 062 * @see DLStoreImpl 063 */ 064 public class DLStoreUtil { 065 066 /** 067 * Adds a directory. 068 * 069 * @param companyId the primary key of the company 070 * @param repositoryId the primary key of the data repository (optionally 071 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 072 * @param dirName the directory's name 073 */ 074 public static void addDirectory( 075 long companyId, long repositoryId, String dirName) 076 throws PortalException { 077 078 getStore().addDirectory(companyId, repositoryId, dirName); 079 } 080 081 /** 082 * Adds a file based on a byte array. 083 * 084 * @param companyId the primary key of the company 085 * @param repositoryId the primary key of the data repository (optionally 086 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 087 * @param fileName the file name 088 * @param validateFileExtension whether to validate the file's extension 089 * @param bytes the files's data 090 */ 091 public static void addFile( 092 long companyId, long repositoryId, String fileName, 093 boolean validateFileExtension, byte[] bytes) 094 throws PortalException { 095 096 getStore().addFile( 097 companyId, repositoryId, fileName, validateFileExtension, bytes); 098 } 099 100 /** 101 * Adds a file based on a {@link File} object. 102 * 103 * @param companyId the primary key of the company 104 * @param repositoryId the primary key of the data repository (optionally 105 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 106 * @param fileName the file name 107 * @param validateFileExtension whether to validate the file's extension 108 * @param file Name the file name 109 */ 110 public static void addFile( 111 long companyId, long repositoryId, String fileName, 112 boolean validateFileExtension, File file) 113 throws PortalException { 114 115 getStore().addFile( 116 companyId, repositoryId, fileName, validateFileExtension, file); 117 } 118 119 /** 120 * Adds a file based on a {@link InputStream} object. 121 * 122 * @param companyId the primary key of the company 123 * @param repositoryId the primary key of the data repository (optionally 124 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 125 * @param fileName the file name 126 * @param validateFileExtension whether to validate the file's extension 127 * @param is the files's data 128 */ 129 public static void addFile( 130 long companyId, long repositoryId, String fileName, 131 boolean validateFileExtension, InputStream is) 132 throws PortalException { 133 134 getStore().addFile( 135 companyId, repositoryId, fileName, validateFileExtension, is); 136 } 137 138 /** 139 * Adds a file based on a byte array. Enforces validation of file's 140 * extension. 141 * 142 * @param companyId the primary key of the company 143 * @param repositoryId the primary key of the data repository (optionally 144 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 145 * @param fileName the file name 146 * @param bytes the files's data 147 */ 148 public static void addFile( 149 long companyId, long repositoryId, String fileName, byte[] bytes) 150 throws PortalException { 151 152 getStore().addFile(companyId, repositoryId, fileName, bytes); 153 } 154 155 /** 156 * Adds a file based on a {@link File} object. Enforces validation of file's 157 * extension. 158 * 159 * @param companyId the primary key of the company 160 * @param repositoryId the primary key of the data repository (optionally 161 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 162 * @param fileName the file name 163 * @param file Name the file name 164 */ 165 public static void addFile( 166 long companyId, long repositoryId, String fileName, File file) 167 throws PortalException { 168 169 getStore().addFile(companyId, repositoryId, fileName, file); 170 } 171 172 /** 173 * Adds a file based on an {@link InputStream} object. Enforces validation 174 * of file's extension. 175 * 176 * @param companyId the primary key of the company 177 * @param repositoryId the primary key of the data repository (optionally 178 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 179 * @param fileName the file name 180 * @param is the files's data 181 */ 182 public static void addFile( 183 long companyId, long repositoryId, String fileName, InputStream is) 184 throws PortalException { 185 186 getStore().addFile(companyId, repositoryId, fileName, is); 187 } 188 189 /** 190 * Ensures company's root directory exists. Only implemented by {@link 191 * JCRStore#checkRoot(long)}. 192 * 193 * @param companyId the primary key of the company 194 */ 195 public static void checkRoot(long companyId) { 196 getStore().checkRoot(companyId); 197 } 198 199 /** 200 * Creates a new copy of the file version. 201 * 202 * @param companyId the primary key of the company 203 * @param repositoryId the primary key of the data repository (optionally 204 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 205 * @param fileName the original's file name 206 * @param fromVersionLabel the original file's version label 207 * @param toVersionLabel the new version label 208 */ 209 public static void copyFileVersion( 210 long companyId, long repositoryId, String fileName, 211 String fromVersionLabel, String toVersionLabel) 212 throws PortalException { 213 214 getStore().copyFileVersion( 215 companyId, repositoryId, fileName, fromVersionLabel, 216 toVersionLabel); 217 } 218 219 /** 220 * Deletes a directory. 221 * 222 * @param companyId the primary key of the company 223 * @param repositoryId the primary key of the data repository (optionally 224 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 225 * @param dirName the directory's name 226 */ 227 public static void deleteDirectory( 228 long companyId, long repositoryId, String dirName) { 229 230 getStore().deleteDirectory(companyId, repositoryId, dirName); 231 } 232 233 /** 234 * Deletes a file. If a file has multiple versions, all versions will be 235 * deleted. 236 * 237 * @param companyId the primary key of the company 238 * @param repositoryId the primary key of the data repository (optionally 239 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 240 * @param fileName the file's name 241 */ 242 public static void deleteFile( 243 long companyId, long repositoryId, String fileName) 244 throws PortalException { 245 246 getStore().deleteFile(companyId, repositoryId, fileName); 247 } 248 249 /** 250 * Deletes a file at a particular version. 251 * 252 * @param companyId the primary key of the company 253 * @param repositoryId the primary key of the data repository (optionally 254 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 255 * @param fileName the file's name 256 * @param versionLabel the file's version label 257 */ 258 public static void deleteFile( 259 long companyId, long repositoryId, String fileName, 260 String versionLabel) 261 throws PortalException { 262 263 getStore().deleteFile(companyId, repositoryId, fileName, versionLabel); 264 } 265 266 /** 267 * Returns the file as a {@link File} object. 268 * 269 * <p> 270 * This method is useful when optimizing low-level file operations like 271 * copy. The client must not delete or change the returned {@link File} 272 * object in any way. This method is only supported in certain stores. If 273 * not supported, this method will throw an {@link 274 * UnsupportedOperationException}. 275 * </p> 276 * 277 * <p> 278 * If using an S3 store, it is preferable for performance reasons to use 279 * {@link #getFileAsStream(long, long, String)} instead of this method 280 * wherever possible. 281 * </p> 282 * 283 * @param companyId the primary key of the company 284 * @param repositoryId the primary key of the data repository (optionally 285 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 286 * @param fileName the file's name 287 * @return Returns the {@link File} object with the file's name 288 */ 289 public static File getFile( 290 long companyId, long repositoryId, String fileName) 291 throws PortalException { 292 293 return getStore().getFile(companyId, repositoryId, fileName); 294 } 295 296 /** 297 * Returns the file as a {@link File} object. 298 * 299 * <p> 300 * This method is useful when optimizing low-level file operations like 301 * copy. The client must not delete or change the returned {@link File} 302 * object in any way. This method is only supported in certain stores. If 303 * not supported, this method will throw an {@link 304 * UnsupportedOperationException}. 305 * </p> 306 * 307 * <p> 308 * If using an S3 store, it is preferable for performance reasons to use 309 * {@link #getFileAsStream(long, long, String, String)} instead of this 310 * method wherever possible. 311 * </p> 312 * 313 * @param companyId the primary key of the company 314 * @param repositoryId the primary key of the data repository (optionally 315 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 316 * @param fileName the file's name 317 * @param versionLabel the file's version label 318 * @return Returns the {@link File} object with the file's name 319 */ 320 public static File getFile( 321 long companyId, long repositoryId, String fileName, 322 String versionLabel) 323 throws PortalException { 324 325 return getStore().getFile( 326 companyId, repositoryId, fileName, versionLabel); 327 } 328 329 /** 330 * Returns the file as a byte array. 331 * 332 * @param companyId the primary key of the company 333 * @param repositoryId the primary key of the data repository (optionally 334 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 335 * @param fileName the file's name 336 * @return Returns the byte array with the file's name 337 */ 338 public static byte[] getFileAsBytes( 339 long companyId, long repositoryId, String fileName) 340 throws PortalException { 341 342 return getStore().getFileAsBytes(companyId, repositoryId, fileName); 343 } 344 345 /** 346 * Returns the file as a byte array. 347 * 348 * @param companyId the primary key of the company 349 * @param repositoryId the primary key of the data repository (optionally 350 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 351 * @param fileName the file's name 352 * @param versionLabel the file's version label 353 * @return Returns the byte array with the file's name 354 */ 355 public static byte[] getFileAsBytes( 356 long companyId, long repositoryId, String fileName, 357 String versionLabel) 358 throws PortalException { 359 360 return getStore().getFileAsBytes( 361 companyId, repositoryId, fileName, versionLabel); 362 } 363 364 /** 365 * Returns the file as an {@link InputStream} object. 366 * 367 * <p> 368 * If using an S3 store, it is preferable for performance reasons to use 369 * this method to get the file as an {@link InputStream} instead of using 370 * other methods to get the file as a {@link File}. 371 * </p> 372 * 373 * @param companyId the primary key of the company 374 * @param repositoryId the primary key of the data repository (optionally 375 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 376 * @param fileName the file's name 377 * @return Returns the {@link InputStream} object with the file's name 378 */ 379 public static InputStream getFileAsStream( 380 long companyId, long repositoryId, String fileName) 381 throws PortalException { 382 383 return getStore().getFileAsStream(companyId, repositoryId, fileName); 384 } 385 386 /** 387 * Returns the file as an {@link InputStream} object. 388 * 389 * <p> 390 * If using an S3 store, it is preferable for performance reasons to use 391 * this method to get the file as an {@link InputStream} instead of using 392 * other methods to get the file as a {@link File}. 393 * </p> 394 * 395 * @param companyId the primary key of the company 396 * @param repositoryId the primary key of the data repository (optionally 397 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 398 * @param fileName the file's name 399 * @param versionLabel the file's version label 400 * @return Returns the {@link InputStream} object with the file's name 401 */ 402 public static InputStream getFileAsStream( 403 long companyId, long repositoryId, String fileName, 404 String versionLabel) 405 throws PortalException { 406 407 return getStore().getFileAsStream( 408 companyId, repositoryId, fileName, versionLabel); 409 } 410 411 /** 412 * Returns all files of the directory. 413 * 414 * @param companyId the primary key of the company 415 * @param repositoryId the primary key of the data repository (optionally 416 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 417 * @param dirName the directory's name 418 * @return Returns all files of the directory 419 */ 420 public static String[] getFileNames( 421 long companyId, long repositoryId, String dirName) 422 throws PortalException { 423 424 return getStore().getFileNames(companyId, repositoryId, dirName); 425 } 426 427 /** 428 * Returns the size of the file. 429 * 430 * @param companyId the primary key of the company 431 * @param repositoryId the primary key of the data repository (optionally 432 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 433 * @param fileName the file's name 434 * @return Returns the size of the file 435 */ 436 public static long getFileSize( 437 long companyId, long repositoryId, String fileName) 438 throws PortalException { 439 440 return getStore().getFileSize(companyId, repositoryId, fileName); 441 } 442 443 /** 444 * Returns the {@link DLStore} object. Used primarily by Spring and should 445 * not be used by the client. 446 * 447 * @return Returns the {@link DLStore} object 448 */ 449 public static DLStore getStore() { 450 if (_store == null) { 451 _store = (DLStore)PortalBeanLocatorUtil.locate( 452 DLStore.class.getName()); 453 454 ReferenceRegistry.registerReference(DLStoreUtil.class, "_store"); 455 } 456 457 return _store; 458 } 459 460 /** 461 * Returns <code>true</code> if the directory exists. 462 * 463 * @param companyId the primary key of the company 464 * @param repositoryId the primary key of the data repository (optionally 465 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 466 * @param dirName the directory's name 467 * @return <code>true</code> if the directory exists; <code>false</code> 468 * otherwise 469 */ 470 public static boolean hasDirectory( 471 long companyId, long repositoryId, String dirName) 472 throws PortalException { 473 474 return getStore().hasDirectory(companyId, repositoryId, dirName); 475 } 476 477 /** 478 * Returns <code>true</code> if the file exists. 479 * 480 * @param companyId the primary key of the company 481 * @param repositoryId the primary key of the data repository (optionally 482 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 483 * @param fileName the file's name 484 * @return <code>true</code> if the file exists; <code>false</code> 485 * otherwise 486 */ 487 public static boolean hasFile( 488 long companyId, long repositoryId, String fileName) 489 throws PortalException { 490 491 return getStore().hasFile(companyId, repositoryId, fileName); 492 } 493 494 /** 495 * Returns <code>true</code> if the file exists. 496 * 497 * @param companyId the primary key of the company 498 * @param repositoryId the primary key of the data repository (optionally 499 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 500 * @param fileName the file's name 501 * @param versionLabel the file's version label 502 * @return <code>true</code> if the file exists; <code>false</code> 503 * otherwise 504 */ 505 public static boolean hasFile( 506 long companyId, long repositoryId, String fileName, 507 String versionLabel) 508 throws PortalException { 509 510 return getStore().hasFile( 511 companyId, repositoryId, fileName, versionLabel); 512 } 513 514 public static boolean isValidName(String name) { 515 return getStore().isValidName(name); 516 } 517 518 /** 519 * Moves an existing directory. Only implemented by {@link 520 * JCRStore#move(String, String)}. 521 * 522 * @param srcDir the original directory's name 523 * @param destDir the new directory's name 524 */ 525 public static void move(String srcDir, String destDir) { 526 getStore().move(srcDir, destDir); 527 } 528 529 /** 530 * Moves a file to a new data repository. 531 * 532 * @param companyId the primary key of the company 533 * @param repositoryId the primary key of the data repository 534 * @param newRepositoryId the primary key of the new data repository 535 * @param fileName the file's name 536 */ 537 public static void updateFile( 538 long companyId, long repositoryId, long newRepositoryId, 539 String fileName) 540 throws PortalException { 541 542 getStore().updateFile( 543 companyId, repositoryId, newRepositoryId, fileName); 544 } 545 546 /** 547 * Update's the file's name 548 * 549 * @param companyId the primary key of the company 550 * @param repositoryId the primary key of the data repository (optionally 551 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 552 * @param fileName the file's name 553 * @param newFileName the file's new name 554 */ 555 public static void updateFile( 556 long companyId, long repositoryId, String fileName, 557 String newFileName) 558 throws PortalException { 559 560 getStore().updateFile(companyId, repositoryId, fileName, newFileName); 561 } 562 563 /** 564 * Updates a file based on a {@link File} object. 565 * 566 * @param companyId the primary key of the company 567 * @param repositoryId the primary key of the data repository (optionally 568 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 569 * @param fileName the file name 570 * @param fileExtension the file's extension 571 * @param validateFileExtension whether to validate the file's extension 572 * @param versionLabel the file's new version label 573 * @param sourceFileName the new file's original name 574 * @param file Name the file name 575 */ 576 public static void updateFile( 577 long companyId, long repositoryId, String fileName, 578 String fileExtension, boolean validateFileExtension, 579 String versionLabel, String sourceFileName, File file) 580 throws PortalException { 581 582 getStore().updateFile( 583 companyId, repositoryId, fileName, fileExtension, 584 validateFileExtension, versionLabel, sourceFileName, file); 585 } 586 587 /** 588 * Updates a file based on a {@link InputStream} object. 589 * 590 * @param companyId the primary key of the company 591 * @param repositoryId the primary key of the data repository (optionally 592 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 593 * @param fileName the file name 594 * @param fileExtension the file's extension 595 * @param validateFileExtension whether to validate the file's extension 596 * @param versionLabel the file's new version label 597 * @param sourceFileName the new file's original name 598 * @param is the new file's data 599 */ 600 public static void updateFile( 601 long companyId, long repositoryId, String fileName, 602 String fileExtension, boolean validateFileExtension, 603 String versionLabel, String sourceFileName, InputStream is) 604 throws PortalException { 605 606 getStore().updateFile( 607 companyId, repositoryId, fileName, fileExtension, 608 validateFileExtension, versionLabel, sourceFileName, is); 609 } 610 611 /** 612 * Update's a file version label. Similar to {@link #copyFileVersion(long, 613 * long, String, String, String)} except that the old file version is 614 * deleted. 615 * 616 * @param companyId the primary key of the company 617 * @param repositoryId the primary key of the data repository (optionally 618 * {@link com.liferay.portal.model.CompanyConstants#SYSTEM}) 619 * @param fileName the file's name 620 * @param fromVersionLabel the file's version label 621 * @param toVersionLabel the file's new version label 622 */ 623 public static void updateFileVersion( 624 long companyId, long repositoryId, String fileName, 625 String fromVersionLabel, String toVersionLabel) 626 throws PortalException { 627 628 getStore().updateFileVersion( 629 companyId, repositoryId, fileName, fromVersionLabel, 630 toVersionLabel); 631 } 632 633 /** 634 * Validates a file's name. 635 * 636 * @param fileName the file's name 637 * @param validateFileExtension whether to validate the file's extension 638 */ 639 public static void validate(String fileName, boolean validateFileExtension) 640 throws PortalException { 641 642 getStore().validate(fileName, validateFileExtension); 643 } 644 645 /** 646 * Validates a file's name and data. 647 * 648 * @param fileName the file's name 649 * @param validateFileExtension whether to validate the file's extension 650 * @param bytes the file's data (optionally <code>null</code>) 651 */ 652 public static void validate( 653 String fileName, boolean validateFileExtension, byte[] bytes) 654 throws PortalException { 655 656 getStore().validate(fileName, validateFileExtension, bytes); 657 } 658 659 /** 660 * Validates a file's name and data. 661 * 662 * @param fileName the file's name 663 * @param validateFileExtension whether to validate the file's extension 664 * @param file Name the file's name 665 */ 666 public static void validate( 667 String fileName, boolean validateFileExtension, File file) 668 throws PortalException { 669 670 getStore().validate(fileName, validateFileExtension, file); 671 } 672 673 /** 674 * Validates a file's name and data. 675 * 676 * @param fileName the file's name 677 * @param validateFileExtension whether to validate the file's extension 678 * @param is the file's data (optionally <code>null</code>) 679 */ 680 public static void validate( 681 String fileName, boolean validateFileExtension, InputStream is) 682 throws PortalException { 683 684 getStore().validate(fileName, validateFileExtension, is); 685 } 686 687 public static void validate( 688 String fileName, String fileExtension, String sourceFileName, 689 boolean validateFileExtension) 690 throws PortalException { 691 692 getStore().validate( 693 fileName, fileExtension, sourceFileName, validateFileExtension); 694 } 695 696 /** 697 * Validates a file's name and data. 698 * 699 * @param fileName the file's name 700 * @param fileExtension the file's extension 701 * @param sourceFileName the file's original name 702 * @param validateFileExtension whether to validate the file's extension 703 * @param file Name the file's name 704 */ 705 public static void validate( 706 String fileName, String fileExtension, String sourceFileName, 707 boolean validateFileExtension, File file) 708 throws PortalException { 709 710 getStore().validate( 711 fileName, fileExtension, sourceFileName, validateFileExtension, 712 file); 713 } 714 715 /** 716 * Validates a file's name and data. 717 * 718 * @param fileName the file's name 719 * @param fileExtension the file's extension 720 * @param sourceFileName the file's original name 721 * @param validateFileExtension whether to validate the file's extension 722 * @param is the file's data (optionally <code>null</code>) 723 */ 724 public static void validate( 725 String fileName, String fileExtension, String sourceFileName, 726 boolean validateFileExtension, InputStream is) 727 throws PortalException { 728 729 getStore().validate( 730 fileName, fileExtension, sourceFileName, validateFileExtension, is); 731 } 732 733 public static void validateDirectoryName(String directoryName) 734 throws PortalException { 735 736 getStore().validateDirectoryName(directoryName); 737 } 738 739 /** 740 * Set's the {@link DLStore} object. Used primarily by Spring and should not 741 * be used by the client. 742 * 743 * @param store the {@link DLStore} object 744 */ 745 public void setStore(DLStore store) { 746 _store = store; 747 748 ReferenceRegistry.registerReference(DLStoreUtil.class, "_store"); 749 } 750 751 private static DLStore _store; 752 753 }