예제 #1
0
  /** 删除. */
  public String remove(Long id) {
    DiskInfo diskInfo = diskInfoManager.get(id);

    if (diskInfo == null) {
      return "";
    }

    diskInfo.setStatus("trash");
    diskInfoManager.save(diskInfo);

    return diskInfo.getParentPath();
  }
예제 #2
0
  /** 重命名. */
  public String rename(String userId, Long id, String name) {
    DiskInfo diskInfo = diskInfoManager.get(id);
    String parentPath = diskInfo.getParentPath();
    String type = FileUtils.getSuffix(name);
    String hql = "select name from DiskInfo where creator=? and parentPath=? and id!=?";
    List<String> currentNames = diskInfoManager.find(hql, userId, parentPath, id);
    String targetName = FileUtils.calculateName(name, currentNames);
    diskInfo.setName(targetName);
    diskInfo.setType(type);
    diskInfoManager.save(diskInfo);

    return parentPath;
  }
예제 #3
0
  /** 上传文件,或新建文件夹. */
  public DiskInfo createDiskInfo(
      String userId,
      String name,
      long size,
      String ref,
      String type,
      int dirType,
      String parentPath) {
    if (name == null) {
      logger.info("name cannot be null");

      return null;
    }

    name = name.trim();

    if (name.length() == 0) {
      logger.info("name cannot be empty");

      return null;
    }

    if (parentPath == null) {
      parentPath = "";
    } else {
      parentPath = parentPath.trim();
    }

    if (parentPath.length() != 0) {
      if (!parentPath.startsWith("/")) {
        parentPath = "/" + parentPath;
      }

      int index = parentPath.lastIndexOf("/");
      String targetParentPath = parentPath.substring(0, index);
      String targetName = parentPath.substring(index + 1);
      String hql = "from DiskInfo where parentPath=? and name=?";
      DiskInfo parent = diskInfoManager.findUnique(hql, targetParentPath, targetName);

      if (parent == null) {
        logger.info("cannot find : {} {} {}", parentPath, targetParentPath, targetName);

        return null;
      }
    }

    String hql = "select name from DiskInfo where creator=? and parentPath=?";
    List<String> currentNames = diskInfoManager.find(hql, userId, parentPath);
    String targetName = FileUtils.calculateName(name, currentNames);

    Date now = new Date();
    DiskInfo diskInfo = new DiskInfo();
    diskInfo.setName(targetName);
    diskInfo.setType(type);
    diskInfo.setFileSize(size);
    diskInfo.setCreator(userId);
    diskInfo.setCreateTime(now);
    diskInfo.setLastModifier(userId);
    diskInfo.setLastModifiedTime(now);
    diskInfo.setDirType(dirType);
    diskInfo.setRef(ref);
    diskInfo.setStatus("active");
    diskInfo.setParentPath(parentPath);
    diskInfoManager.save(diskInfo);

    return diskInfo;
  }
예제 #4
0
  /** 移动. */
  public String move(String userId, Long id, Long parentId) {
    DiskInfo diskInfo = diskInfoManager.get(id);
    String parentPath = diskInfo.getParentPath();

    if (id == parentId) {
      logger.info("{} is equals {}", id, parentId);

      return diskInfo.getParentPath();
    }

    if (parentId != 0) {
      DiskInfo parent = diskInfoManager.get(parentId);

      if (!"dir".equals(parent.getType())) {
        logger.info(
            "{}({}) is not directory", parent.getParentPath() + "/" + parent.getName(), parentId);

        return diskInfo.getParentPath();
      }

      String currentPath = diskInfo.getParentPath() + "/" + diskInfo.getName() + "/";
      String checkedParentPath = parent.getParentPath() + "/";

      if ("dir".equals(diskInfo.getType()) && checkedParentPath.startsWith(currentPath)) {
        logger.info(
            "{}({}) is sub directory of {}({})",
            parent.getParentPath() + "/" + parent.getName(),
            parentId,
            diskInfo.getParentPath() + "/" + diskInfo.getName(),
            id);

        return diskInfo.getParentPath();
      }

      diskInfo.setParentPath(parent.getParentPath() + "/" + parent.getName());
    } else {
      diskInfo.setParentPath("");
    }

    String name = diskInfo.getName();
    String hql = "select name from DiskInfo where creator=? and parentPath=? and id!=?";
    List<String> currentNames = diskInfoManager.find(hql, userId, parentPath, id);
    String targetName = FileUtils.calculateName(name, currentNames);
    diskInfo.setName(targetName);

    diskInfoManager.save(diskInfo);

    return parentPath;
  }