コード例 #1
0
ファイル: FileService.java プロジェクト: lingcloud/lingcloud
 /**
  * check disk type.
  *
  * @param vafile file object
  * @throws Exception
  */
 private void checkDiskType(VAFile vafile) throws Exception {
   if (vafile == null || !vafile.getFileType().equals(VAMConstants.VAF_FILE_TYPE_DISK)) {
     throw new Exception("The file type must be + " + VAMConstants.VAF_FILE_TYPE_DISK + ".");
   }
 }
コード例 #2
0
ファイル: FileService.java プロジェクト: lingcloud/lingcloud
  /**
   * add file.
   *
   * @param vafile file object
   * @param storeLoc the file location
   * @param deleteSrcFile whether delete the source file after add file
   * @return file object
   * @throws Exception
   */
  public VAFile addFile(VAFile vafile, String storeLoc, boolean deleteSrcFile) throws Exception {
    // check whether the store file is existed
    String storePath = storeLoc;
    if (storePath != null) {
      File storeFile = new File(storePath);
      if (!storeFile.exists()) {
        throw new Exception("The file \"" + storePath + "\" is not existed!");
      }

      vafile.setSize(storeFile.length());
    }

    // check file path
    VAMUtil.checkFile(null, vafile.getLocation());

    // get file data access object
    VAFileDao fileDao = DaoFactory.getVAFileDao();
    // add the file to the database
    vafile = fileDao.add(vafile);
    if (vafile == null) {
      throw new Exception("Can't add the file to the database.");
    }

    try {
      // if the file type is disk, convert disk format
      if (vafile.getFileType().equals(VAMConstants.VAF_FILE_TYPE_DISK)) {
        vafile.setSrcPath(storeLoc);
        DiskInfo info = VAMUtil.getDiskInfo(storeLoc);
        String format = null;
        String backingFile = null;
        if (info != null) {
          format = info.getFormat();
          backingFile = info.getBackingFile();
        }
        if (format != null
            && format.equalsIgnoreCase(VAMConfig.getImageFormat())
            && backingFile == null) {
          VADisk disk = new VADisk(vafile);
          disk.setCapacity(info.getVirtualSize());
          disk.setSize(info.getDiskSize());

          FileOperation.getInstance()
              .moveFile(
                  VAMConfig.getNfsHost(),
                  VAMConfig.getNfsUser(),
                  disk,
                  storeLoc,
                  disk.getSavePath());
        } else {
          convertDiskFormat(vafile, storeLoc, VAMConfig.getImageFormat(), deleteSrcFile);
        }

      } else if (storeLoc != null) { // move file
        FileOperation.getInstance()
            .moveFile(
                VAMConfig.getNfsHost(),
                VAMConfig.getNfsUser(),
                vafile,
                storeLoc,
                vafile.getSavePath());
      }
    } catch (Exception e) {
      // remove the file in the database
      DaoFactory.getVAFileDao().remove(vafile.getGuid());
      throw new Exception(e);
    }

    return vafile;
  }