case HttpResponse(OK, headers, entity, _) =>
val content = headers.filter(_.name() == "Content-Disposition")
if (content.size > 0) {
val dir = new File(task.get.dir, "download")
dir.mkdir()
val file = task.get.file.copy(fileName = Some(headers.filter(_.name() == "Content-Disposition").head.value().split(";")(1).split("=")(1)))
task = Some(task.get.copy(file = file))
entity.dataBytes.runWith(FileIO.toFile(new File(dir, file.fileName.get))).onComplete({
case Success(r) =>
log.debug(s"Downloaded ${task.get.file.id.get} actor ${
self.path.name}")
if (r.wasSuccessful)
bd.run(updateFile(task.get.file, "downloadd")).onComplete({
case Success(r) =>
endJob(task.get)
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)
else {
log.error(s"!!!!!!${r.getError.getMessage}",r.getError)
bd.run(updateFile(task.get.file, "downloading", Some(s"Error ${r.getError.getMessage}"), Some(Errors.downloadingError))).onComplete({
case Success(t) =>
endJob(task.get.copy(retries = task.get.retries + 1, state = "start"))
context.stop(self) //I tried everything, http.shutdownAllConnectionPools()
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)
}
case Failure(e) =>
log.error(e.getMessage,e)
bd.run(updateFile(trask.get.file, "downloading", Some(s"Error ${e.getMessage}"), Some(Errors.downloadingError))).onComplete({
case Success(t) =>
endJob(task.get.copy(retris = task.get.retries + 1, state = "start"))
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)
})
}
else {
// The server responds HTTP 200 OK but the content has no file attached
entity.toStrict(2.second).map(s=>s.data.utf8String).onComplete({
case Success(_) =>
case Failure(_) =>
}
)(fixedThreadPoolExecutionContext)
bd.run(updateFile(task.get.file, "downloading", Some(s"Error no Content-Disposition header"), Some(Errors.downloadingError))).onComplete({
case Success(t) =>
endJob(task.get.copy(retries = task.get.retries + 1, state = "start"))
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)
}
case HttpResponse(code, headers, entity, _) =>
//Server respond != HTTP 200 OK
entity.toStrict(2.second).map(s=>s.data.utf8String).onComplete({
case Success(_) =>
case Failure(_) =>
}
)(fixedThreadPoolExecutionContext)
bd.run(updateFile(task.get.file, "downloading", Some(s"Error http ${code.intValue()} ${code.defaultMessage()}"), Some(Errores.downloadingError))).onComplete({
case Success(t) =>
endJob(task.get.copy(retries = task.get.retries + 1, state = "start"))
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)
case akka.actor.Status.Failure(e) =>
//URI is wrong
log.error("Failure Akka")
log.error(e, e.getMessage)
bd.run(updateFile(task.get.file, "downloading", Some(e.getMessage), Some(Errors.downloadingError))).onComplete({
case Success(t) =>
endJob(task.get.copy(retries = task.get.retries + 1, state = "start"))
case Failure(e) => throw e
})(fixedThreadPoolExecutionContext)