コード例 #1
0
  @GET
  @Path("download")
  public Response downloadVideo(@Context final MessageContext cxt, @Context final UriInfo uri) {
    // validate the session
    final Session session = this.getSession(cxt, uri);
    if (session == null || session.isExpired()) {
      return this.unauthorized(cxt, uri).build();
    }

    // generate URL for the requested video
    try {
      // find the output to be returned
      final AwsOutput output =
          this.txService.inReadOnlyTransaction(
              new Callable<AwsOutput>() {
                @Override
                public AwsOutput call() throws Exception {
                  // find the video
                  final Video video = getVideo(uri, session.getGuid());
                  if (video == null) {
                    return null;
                  }

                  // find the highest quality video available to download
                  return findOutput(video, getType(uri, Type.MP4));
                }
              });

      if (output != null && output.isDownloadable()) {
        final URL url = this.awsController.getSignedUrl(output.getFile());
        if (url != null) {
          return ResponseUtils.temporaryRedirect(url.toURI()).build();
        }
      }
    } catch (final Exception e) {
      LOG.debug("Error generating download url", e);
      return Response.status(Status.NOT_FOUND).build();
    }

    return Response.status(Status.NOT_FOUND).build();
  }