Exemple #1
0
 private void handleCombineVideos(
     HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
     throws IOException, NoSuchAlgorithmException, InvalidFileSizeException, ServiceException,
         InterruptedException {
   int projectId = getProjectIdFromSessionAttributes(session);
   String[] videos = DatabaseApi.getVideosOnTimeline(projectId);
   String format = request.getParameter("output-extension");
   log.info("Combining Videos, if necessary");
   if (videos.length == 0) {
     response.sendRedirect("editor.jsp");
   } else {
     String[] videoURLs = new String[videos.length];
     for (int i = 0; i < videos.length; i++) {
       videoURLs[i] = DatabaseApi.getVideoUrl(DatabaseApi.getVideoId(videos[i], projectId));
     }
     String combinedURL = "";
     if (videoURLs.length == 1) {
       combinedURL = S3Api.downloadVideosToTemp(videoURLs[0]);
     } else if (videoURLs.length > 1) {
       combinedURL = Combiner.combineVideos(videoURLs, videos, tempDir);
     }
     if (combinedURL == null) {
       response.sendRedirect("editor.jsp");
       return;
     }
     log.info("Now converting, if necessary");
     File file = new File(combinedURL);
     String ext = ".ogv";
     String converted = "";
     if (!format.equals("ogv")) {
       if (format.equals("avi")) {
         ext = ".avi";
       } else if (format.equals("wmv")) {
         ext = ".wmv";
       } else if (format.equals("mp4")) {
         ext = ".mp4";
       }
       converted = Combiner.convertTo(format, file, tempDir);
       file = new File(tempDir + "/" + converted);
     }
     log.info("Now Dowloading");
     final ServletOutputStream out = response.getOutputStream();
     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition", "attachment;filename=combinedVideo" + ext);
     BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
     byte[] buf = new byte[4 * 1024]; // 4K buffer
     int bytesRead;
     while ((bytesRead = is.read(buf)) != -1) {
       out.write(buf, 0, bytesRead);
     }
     is.close();
     out.flush();
     out.close();
     file.delete();
     log.info("Downloaded. End of handleCombinedVideos");
   }
 }
Exemple #2
0
 private void handleGetProjectInformation(
     HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
     throws IOException {
   int accountId = getAccountIdFromSessionAttributes(session);
   String projectString = S3Api.getProjectInformation(accountId);
   response.setContentType("application/json; charset=utf-8");
   PrintWriter out = response.getWriter();
   out.println(projectString);
   out.flush();
   out.close();
 }
Exemple #3
0
  private void handleDeleteVideo(
      HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
      throws IOException {
    DeleteVideoRequest deleteVideoRequest = new Gson().fromJson(json, DeleteVideoRequest.class);

    response.setContentType("text/plain");

    PrintWriter out = response.getWriter();

    int projectId = getProjectIdFromSessionAttributes(session);
    int videoId = DatabaseApi.getVideoId(deleteVideoRequest.arguments.video, projectId);

    S3Api.deleteFileInS3(S3Api.getNameFromUrl(DatabaseApi.getVideoUrl(videoId))); // Delete video
    S3Api.deleteFileInS3(S3Api.getNameFromUrl(DatabaseApi.getVideoIcon(videoId))); // Delete icon

    DatabaseApi.deleteVideo(videoId);

    out.println(deleteVideoRequest.arguments.video + " deleted successfully.");
    out.flush();
    out.close();
  }
Exemple #4
0
  private void addVideoEverywhere(PrintWriter out, int projectId, File video)
      throws InvalidFileSizeException, IOException, ServiceException, NoSuchAlgorithmException {
    if (Security.isSafeVideo(video)
        && Security.videoFits(DatabaseApi.getNumberOfVideos(projectId))) {
      String[] videoUrlAndIcon = S3Api.uploadFile(video);
      String videoUrl = videoUrlAndIcon[0];
      String videoIcon = videoUrlAndIcon[1];
      if (videoUrl != null) {
        // Give the video a name only at the last moment to prevent duplicates.
        String videoName = Security.convertToSafeAndUniqueVideoName(video.getName(), projectId);
        DatabaseApi.addVideo(new Video(videoName, videoUrl, videoIcon, projectId, -1, -1, false));
        // File downloadedFile = S3Api.downloadFile(videoUrl); // TODO Add to /temp/ folder so it
        // can be played in the player.
        out.println("File uploaded. Please close this window and refresh the editor page.");
        out.println();

        return;
      }
      out.println("Upload Failed. Error uploading video to the cloud.");
      log.warning("Upload Failed. Error uploading video to the cloud.");
      // response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    } else if (!Security.isSafeVideo(video)) {
      out.println("Upload Failed. Video is invalid.");
      log.warning("Upload Failed. Video is invalid.");
      return;
    } else if (!Security.videoFits(DatabaseApi.getNumberOfVideos(projectId))) {
      out.println("Upload Failed. Maximum number of videos reached.");
      log.warning("Upload Failed. Maximum number of videos reached.");
      return;
    } else {
      out.println("Upload Failed. Unknown reason.");
      log.warning("Upload Failed. Unknown reason.");
      // response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Name");
      return;
    }
  }