From 124fd35527601c61c20f7f9e4a3d099c5e0b883b Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 25 Jul 2017 00:16:26 -0700 Subject: Add streaming download to file storage. This enables downloading of content without saving it to a file. --- src/main/scala/xyz/driver/core/file/FileSystemStorage.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/main/scala/xyz/driver/core/file/FileSystemStorage.scala') diff --git a/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala b/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala index fab1307..0d49f2d 100644 --- a/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala +++ b/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala @@ -1,5 +1,8 @@ package xyz.driver.core.file +import akka.NotUsed +import akka.stream.scaladsl.{FileIO, Source} +import akka.util.ByteString import java.io.File import java.nio.file.{Files, Path, Paths} @@ -33,6 +36,15 @@ class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage Option(new File(filePath.toString)).filter(file => file.exists() && file.isFile) }) + override def stream(filePath: Path): OptionT[Future, Source[ByteString, NotUsed]] = + OptionT.optionT(Future { + if (Files.exists(filePath)) { + Some(FileIO.fromPath(filePath).mapMaterializedValue(_ => NotUsed)) + } else { + None + } + }) + override def delete(filePath: Path): Future[Unit] = Future { val file = new File(filePath.toString) if (file.delete()) () -- cgit v1.2.3