Example #1
0
  /**
   * 업로드된 {@code file}을 주어진 {@code name}으로 {@code container}에 첨부한다.
   *
   * <p>when: 업로드된 파일이 사용자에게 첨부될 때. 혹은 사용자를 거치지 않고 바로 다른 리소스로 첨부될 때.
   *
   * <p>업로드된 파일을 업로드 디렉토리로 옮긴다. 이 때 파일이름을 그 파일의 해시값으로 변경한다. 그 후 이 파일에 대한 메타정보 및 첨부될 대상에 대한 정보를 이
   * 엔터티에 담는다. 만약 이 엔터티와 같은 내용을 갖고 있는 엔터티가 이미 존재한다면, 이미 {@code container}에 같은 첨부가 존재하고 있으므로 첨부하지 않고
   * {@code false}를 반환한다. 그렇지 않다면 첨부 후 {@code true}를 반환한다.
   *
   * @param file 첨부할 파일
   * @param name 파일 이름
   * @param container 파일이 첨부될 리소스
   * @return 파일이 새로 첨부되었다면 {@code true}, 이미 같은 첨부가 존재하여 첨부되지 않았다면 {@code false}
   * @throws IOException
   * @throws NoSuchAlgorithmException
   */
  @Transient
  public boolean store(File file, String name, Resource container)
      throws IOException, NoSuchAlgorithmException {
    // Store the file as its SHA1 hash in filesystem, and record its
    // metadata - containerType, containerId, size and hash - in Database.
    this.containerType = container.getType();
    this.containerId = container.getId();
    this.createdDate = JodaDateUtil.now();

    if (name == null) {
      this.name = file.getName();
    } else {
      this.name = name;
    }

    if (this.mimeType == null) {
      this.mimeType = FileUtil.detectMediaType(file, name);
    }

    // the size must be set before it is moved.
    this.size = file.length();
    this.hash = Attachment.moveFileIntoUploadDirectory(file);

    // Add the attachment into the Database only if there is no same record.
    Attachment sameAttach = Attachment.findBy(this);
    if (sameAttach == null) {
      super.save();
      return true;
    } else {
      this.id = sameAttach.id;
      return false;
    }
  }
Example #2
0
  private ObjectNode fileAsJson(String path, org.tmatesoft.svn.core.io.SVNRepository repository)
      throws SVNException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SVNProperties prop = new SVNProperties();
    repository.getFile(path, -1l, prop, baos);
    SVNDirEntry entry = repository.info(path, -1l);
    long size = entry.getSize();
    boolean isBinary;
    String mimeType;
    String data = null;

    if (size > MAX_FILE_SIZE_CAN_BE_VIEWED) {
      isBinary = true;
      mimeType = "application/octet-stream";
    } else {
      byte[] bytes = baos.toByteArray();
      isBinary = RawText.isBinary(bytes);
      if (!isBinary) {
        data = new String(bytes, FileUtil.detectCharset(bytes));
      }
      mimeType = new Tika().detect(bytes, path);
    }

    String author = prop.getStringValue(SVNProperty.LAST_AUTHOR);
    User user = User.findByLoginId(author);

    String commitDate = prop.getStringValue(SVNProperty.COMMITTED_DATE);
    DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime();
    Long commitTime = dateFormatter.parseMillis(commitDate);

    ObjectNode result = Json.newObject();
    result.put("type", "file");
    result.put("revisionNo", prop.getStringValue(SVNProperty.COMMITTED_REVISION));
    result.put("author", author);
    result.put("avatar", getAvatar(user));
    result.put("userName", user.name);
    result.put("userLoginId", user.loginId);
    result.put("createdDate", commitTime);
    result.put("commitMessage", entry.getCommitMessage());
    result.put("commiter", author);
    result.put("size", size);
    result.put("isBinary", isBinary);
    result.put("mimeType", mimeType);
    result.put("data", data);

    return result;
  }
Example #3
0
  /**
   * 备份数据库
   *
   * @param isVisual 是否在数据库操作记录表中可见
   * @param error
   * @return
   */
  public static String backup(boolean isVisual, ErrorInfo error) {
    error.clear();

    String fileName = Constants.SQL_PATH + UUID.randomUUID().toString();
    FileUtil.getStore(Constants.SQL_PATH);

    if (0 != MySQLTool.dumpSqlFile(username, password, host, port, database, fileName, error)) {
      return null;
    }

    if (!FileEncrypt.encrypt(fileName, Constants.ENCRYPTION_KEY)) {
      error.code = -1;
      error.msg = "备份数据库失败";

      return null;
    }

    if (isVisual) {
      if (0 != createOperation(DBOperationType.BACKUP, fileName, error)) {
        return null;
      }

      DealDetail.supervisorEvent(
          Supervisor.currSupervisor().id, SupervisorEvent.DB_BACKUP, "备份数据", error);

      if (error.code < 0) {
        JPA.setRollbackOnly();

        return null;
      }
    }

    error.code = 0;
    error.msg = "备份数据库成功";

    return fileName;
  }
Example #4
0
 @Override
 public void delete() {
   FileUtil.rm_rf(new File(getRepoPrefix() + ownerName + "/" + projectName));
 }
Example #5
0
 @Override
 public void delete() throws Exception {
   FileUtil.rm_rf(getDirectory());
 }