in app/controllers/ProxiesController.scala [132:178]
def getPlayable(fileId:String, proxyType:Option[String]) = IsAuthenticatedAsync { _=> _=>
val actualType = proxyType match {
case None=>"VIDEO"
case Some(t)=>t.toUpperCase
}
scanamoAlpakka
.exec(table.get("fileId"===fileId and ("proxyType"===actualType)))
.runWith(Sink.head)
.map({
case None=>
NotFound(GenericErrorResponse("not_found",s"no $proxyType proxy found for $fileId").asJson)
case Some(Right(proxyLocation))=>
implicit val s3client = s3ClientMgr.getS3Client(awsProfile, proxyLocation.region.map(Region.of))
val expiration = new java.util.Date()
expiration.setTime(expiration.getTime + (1000 * 60 * 60)) //expires in 1 hour
val result = for {
meta <- Try {
val req = HeadObjectRequest.builder().bucket(proxyLocation.bucketName).key(proxyLocation.bucketPath).build()
s3client.headObject(req)
}
presignedUrl <- S3Helper.getPresignedURL(proxyLocation, proxyLinkExpiry, defaultRegion, awsProfile)
result <- Try {
val mimeType = MimeType.fromString(meta.contentType()) match {
case Left(str) =>
logger.warn(s"Could not get MIME type for s3://${proxyLocation.bucketName}/${proxyLocation.bucketPath}: $str")
MimeType("application", "octet-stream")
case Right(t) => t
}
Ok(PlayableProxyResponse("ok", presignedUrl.toString, mimeType).asJson)
}
} yield result
result match {
case Success(result)=>result
case Failure(_:NoSuchKeyException)=>
logger.warn(s"Invalid proxy location: $proxyLocation does not point to an existing file")
NotFound(GenericErrorResponse("invalid_location",s"No proxy found for $proxyType on $fileId").asJson)
case Failure(err)=>
logger.error(s"Could not get metadata for s3://${proxyLocation.bucketName}/${proxyLocation.bucketPath}: ${err.getMessage}", err)
InternalServerError(GenericErrorResponse("error","Storage error, see logs for details").asJson)
}
case Some(Left(err))=>
InternalServerError(GenericErrorResponse("db_error", err.toString).asJson)
})
}