Esempio n. 1
0
  private Attachment prepareAttachment(
      String fileName, String contentType, User user, byte[] contents, String tags) {
    System.out.println("prepare attachment of single file");
    try {
      Attachment a = new Attachment();
      a.setName(fileName);
      a.setMimeType(contentType);
      System.out.println("tryin to new File(DEFAULT_UPLOAD_DIRECTORY, user.getLogin());");
      System.out.println("user = "******"user" + user.getId().toString());

      System.out.println("root = " + root);

      root.mkdirs();

      System.out.println("after rootmkdirs");

      File tmpFile = File.createTempFile("upload_", ".bin", root);
      FileUtils.writeToFile(tmpFile, contents);
      a.setSize((long) contents.length);
      a.setMD5(FileUtils.getMD5(tmpFile));
      a.setFileName("user" + user.getId() + "/" + tmpFile.getName());
      return a;
    } catch (Exception ex) {
      System.out.println("exception occured : exc = " + ex);
      return null;
    }
  }
Esempio n. 2
0
  /**
   * Adds a single file into the ZipOutputStream with specified entry name.
   *
   * @throws IOException
   */
  private void addFileToZip(ZipOutputStream zipOut, UploadedFile file, String name)
      throws IOException {
    if (log.isTraceEnabled()) {
      log.trace(">> addFileToZip(): " + file);
    }

    ZipEntry entry = new ZipEntry(name);
    zipOut.putNextEntry(entry);

    InputStream in = null;
    try {
      in = file.getInputstream();
      FileUtils.copyStream(file.getInputstream(), zipOut);
      zipOut.closeEntry();
    } finally {
      FileUtils.close(in);
    }

    //        try (InputStream in = file.getInputstream()) {
    //            FileUtils.copyStream(file.getInputstream(), zipOut);
    //            zipOut.closeEntry();
    //        }

    if (log.isTraceEnabled()) {
      log.trace("<< addFileToZip()");
    }
  }
Esempio n. 3
0
  private Attachment prepareAttachment(User user, List<UploadedFile> files, String tags) {
    System.out.println("prepare attachment...");
    if (files.isEmpty()) {
      System.out.println("files are empty");
      if (log.isDebugEnabled()) {
        log.debug("prepareAttachment() : List of files is empty! Nothing to compress.");
      }
      return null;
    }
    if (files.size() == 1) {
      if (log.isTraceEnabled()) {
        log.trace(
            "prepareAttachment() : Single file is being uploaded. Delegating to uploadFile()");
      }
      try {
        return prepareAttachment(
            files.get(0).getFileName(),
            files.get(0).getContentType(),
            user,
            files.get(0).getContents(),
            tags);
      } catch (IOException ex) {
        if (log.isTraceEnabled()) {
          log.trace("prepareAttachment() : I/O exception" + ex);
        }
        return null;
      }
    }

    try {
      // create zip file
      log.trace("prepareAttachment(): Creating zip-file");

      File root = new File(DEFAULT_UPLOAD_DIRECTORY, "user" + user.getId());
      root.mkdirs();
      File file = File.createTempFile("upload_", ".zip", root);

      ZipOutputStream zos = null;

      try {
        //                zos = new ZipOutputStream
        zos = new ZipOutputStream(file);
        zos.setEncoding("utf-8");
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (UploadedFile uf : files) {
          addFileToZip(zos, uf, uf.getFileName());
        }
      } finally {
        FileUtils.close(zos);
      }

      //            try (ZipOutputStream zos = new ZipOutputStream(file)) {
      //                zos.setEncoding("utf-8");
      //                zos.setMethod(ZipOutputStream.DEFLATED);
      //                zos.setLevel(Deflater.BEST_COMPRESSION);
      //
      //                for (UploadedFile uf : files) {
      //                    addFileToZip(zos, uf, uf.getFileName());
      //                }
      //            }

      if (log.isDebugEnabled()) {
        log.debug("prepareAttachment(): Files are saved at " + file);
      }

      if (file.length() > MAX_ZIP_SIZE) {
        file.delete();
        throw new IOException("File too large.");
      }

      // Create attachment
      Attachment att = new Attachment();
      att.setName(file.getName());
      att.setMimeType("application/zip");
      att.setSize(file.length());
      att.setMD5(FileUtils.getMD5(file));
      att.setFileName("user" + user.getId() + "/" + file.getName());

      if (log.isTraceEnabled()) {
        log.trace("<< prepareAttachment()");
      }
      return att;
    } catch (IOException ex) {
      log.error("prepareAttachment(): Failed to upload files. ", ex);
      return null;
    }
  }