Esempio n. 1
0
  private void handleSplitVideo(
      HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
      throws IOException {
    SplitVideoRequest splitVideoRequest = new Gson().fromJson(json, SplitVideoRequest.class);

    response.setContentType("text/plain");

    PrintWriter out = response.getWriter();

    if (!Security.isSafeVideoName(splitVideoRequest.arguments.video)) {
      out.println("Name of video to split is invalid.");
      log.warning("Name of video to split is invalid.");
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    if (!Security.isSafeSplitTimeInSeconds(splitVideoRequest.arguments.splitTimeInSeconds)) {
      out.println("Split time (in seconds) is invalid.");
      log.warning("Split time (in seconds) is invalid.");
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    int projectId = getProjectIdFromSessionAttributes(session);
    int videoId = DatabaseApi.getVideoId(splitVideoRequest.arguments.video, projectId);
    Video[] videoFragments =
        ZencoderApi.split(videoId, splitVideoRequest.arguments.splitTimeInSeconds);

    for (Video videoFragment : videoFragments) { // foreach-loop
      // Give the video a name only at the last moment to prevent duplicates.
      String newVideoName =
          Security.convertToSafeAndUniqueVideoName(
              videoFragment.getName(),
              projectId); // .getName() returns the original video name at this point.
      videoFragment.setName(newVideoName); // Now, change .getName() to a unique name.

      DatabaseApi.addVideo(
          new Video(
              videoFragment.getName(),
              videoFragment.getUrl(),
              videoFragment.getIcon(),
              projectId,
              -1,
              -1,
              false)); // projectId not computed by Zencoder
    }

    out.println(
        splitVideoRequest.arguments.video
            + " split at "
            + splitVideoRequest.arguments.splitTimeInSeconds
            + " seconds successfully.");
    out.flush();
    out.close();
  }