Example #1
0
  public static FileAsset saveTempFile(
      User user, Host host, java.io.File uploadedFile, String folderPath, String title)
      throws Exception {

    Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host, user, false);

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes != null) {

      String name = UtilMethods.getFileName(title);
      int counter = 1;
      while (APILocator.getFileAPI().fileNameExists(folder, name)) {
        name = name + counter;
        counter++;
      }

      Contentlet cont = new Contentlet();
      cont.setStructureInode(folder.getDefaultFileType());
      cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(name));
      cont.setFolder(folder.getInode());
      cont.setHost(host.getIdentifier());
      cont.setBinary(FileAssetAPI.BINARY_FIELD, uploadedFile);
      APILocator.getContentletAPI().checkin(cont, user, false);
      APILocator.getVersionableAPI().setLive(cont);
      return APILocator.getFileAssetAPI().fromContentlet(cont);
    }

    return null;
  }
Example #2
0
  /**
   * Save the file uploaded
   *
   * @param user the user that save the file
   * @param host Current host
   * @param uploadedFile
   * @param folder The folder where the file is going to be save
   * @param title The filename
   * @return File
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  private static FileAsset saveFile(
      User user, Host host, java.io.File uploadedFile, String folderPath, String title)
      throws Exception {

    Folder folder = APILocator.getFolderAPI().findFolderByPath(folderPath, host, user, false);
    if (!UtilMethods.isSet(folder.getInode())) {
      User systemUser = APILocator.getUserAPI().getSystemUser();
      folder =
          APILocator.getFolderAPI()
              .createFolders(folderPath, host, APILocator.getUserAPI().getSystemUser(), false);
    }

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes != null) {
      String newFileName = "";
      String name = UtilMethods.getFileName(title);
      int counter = 1;
      String fileName = name + "." + UtilMethods.getFileExtension(title);
      while (APILocator.getFileAPI().fileNameExists(folder, fileName)) {
        newFileName = name + "(" + counter + ")";
        fileName = newFileName + "." + UtilMethods.getFileExtension(title);
        counter++;
      }
      while (APILocator.getFileAssetAPI().fileNameExists(host, folder, name, "")) {
        newFileName = name + "(" + counter + ")";
        fileName = newFileName + "." + UtilMethods.getFileExtension(title);
        counter++;
      }
      if (UtilMethods.isSet(newFileName)) {
        name = newFileName;
      }
      Contentlet cont = new Contentlet();
      cont.setStructureInode(folder.getDefaultFileType());
      cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(name));
      cont.setFolder(folder.getInode());
      cont.setHost(host.getIdentifier());
      cont.setBinary(FileAssetAPI.BINARY_FIELD, uploadedFile);
      if (StructureCache.getStructureByInode(cont.getStructureInode()).getStructureType()
          == Structure.STRUCTURE_TYPE_FILEASSET) cont.setStringProperty("fileName", title);
      cont = APILocator.getContentletAPI().checkin(cont, user, false);
      if (APILocator.getPermissionAPI()
          .doesUserHavePermission(cont, PermissionAPI.PERMISSION_PUBLISH, user))
        APILocator.getVersionableAPI().setLive(cont);
      return APILocator.getFileAssetAPI().fromContentlet(cont);
    }

    return null;
  }
Example #3
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;
  }