コード例 #1
0
  /**
   * Copy a file into the given directory OR host
   *
   * @param file File to be copied
   * @param parent Destination Folder
   * @param host Destination host
   * @return true if copy success, false otherwise
   * @throws IOException
   * @throws DotHibernateException
   */
  private File copyFile(File file, Folder parent, Host host) throws DotDataException, IOException {

    File newFile = new File();

    newFile.copy(file);

    // gets filename before extension
    String fileName = com.dotmarketing.util.UtilMethods.getFileName(file.getFileName());
    // gets file extension
    String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName());

    Boolean fileNameExists;
    if (parent != null) {
      fileNameExists = fileNameExists(parent, file.getFileName());
    } else {
      fileNameExists =
          fileNameExists(APILocator.getFolderAPI().findSystemFolder(), file.getFileName());
    }

    // Setting file name
    if (fileNameExists) {
      // adds "copy" word to the filename
      newFile.setFileName(fileName + "_copy." + fileExtension);
      newFile.setFriendlyName(file.getFriendlyName() + " (COPY) ");
    } else {
      newFile.setFileName(fileName + "." + fileExtension);
    }

    Identifier identifier;
    if (parent != null) {
      identifier = APILocator.getIdentifierAPI().createNew(newFile, parent);
    } else {
      identifier = APILocator.getIdentifierAPI().createNew(newFile, host);
    }
    newFile.setIdentifier(identifier.getInode());

    // persists the webasset
    HibernateUtil.saveOrUpdate(newFile);

    saveFileData(file, newFile, null);

    Logger.debug(FileFactory.class, "identifier=" + identifier.getURI());

    WorkingCache.removeAssetFromCache(newFile);
    WorkingCache.addToWorkingAssetToCache(newFile);
    PermissionAPI permissionAPI = APILocator.getPermissionAPI();

    try {
      APILocator.getVersionableAPI().setWorking(newFile);
      if (file.isLive()) APILocator.getVersionableAPI().setLive(newFile);
    } catch (DotStateException e) {
      Logger.error(this, e.getMessage());
    } catch (DotSecurityException e) {
      Logger.error(this, e.getMessage());
    }
    // Copy permissions
    permissionAPI.copyPermissions(file, newFile);

    return newFile;
  }