Example #1
0
  /**
   * Helper method creates a temp file from the multipart form data and persists the upload file
   * metadata to the database
   *
   * @param filePart data from the form submission
   * @return an instance of UploadFile that has been persisted to the db
   */
  private static UserUpload uploadFile(Http.MultipartFormData.FilePart filePart) {
    File src = (File) filePart.getFile();
    File file = null;
    try {
      file = File.createTempFile(filePart.getFilename() + "-", ".csv");
      FileUtils.copyFile(src, file);
    } catch (IOException e) {
      throw new RuntimeException("Could not create temp file for upload", e);
    }

    UserUpload uploadFile = new UserUpload();
    uploadFile.absolutePath = file.getAbsolutePath();
    uploadFile.fileName = filePart.getFilename();
    uploadFile.user = Application.getCurrentUser();
    uploadFile.save();

    return uploadFile;
  }