/** * REST endpoint returns a json array containing metadata about the currently logged in user's * uploaded files. * * @return json containing file upload ids and filenames */ public Result listUploads() { List<UserUpload> uploadList = UserUpload.findUploadsByUserId(Application.getCurrentUserId()); ObjectNode response = Json.newObject(); ArrayNode uploads = response.putArray("uploads"); for (UserUpload userUpload : uploadList) { ObjectNode upload = Json.newObject(); upload.put("id", userUpload.id); upload.put("filename", userUpload.fileName); uploads.add(upload); } return ok(uploads); }
/** * 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; }