diff options
author | vlad <vlad@drivergrp.com> | 2016-08-17 10:02:48 -0700 |
---|---|---|
committer | vlad <vlad@drivergrp.com> | 2016-08-17 10:02:48 -0700 |
commit | 447dcfcc10de2fa21094f626ad06c583a05c4a2b (patch) | |
tree | a3d604dd3faef183670bca05666ef1b59897e907 | |
parent | 143f5f37166ea5d41c23e3dbf7893a728234e0b4 (diff) | |
download | driver-core-447dcfcc10de2fa21094f626ad06c583a05c4a2b.tar.gz driver-core-447dcfcc10de2fa21094f626ad06c583a05c4a2b.tar.bz2 driver-core-447dcfcc10de2fa21094f626ad06c583a05c4a2b.zip |
More functional file store api
-rw-r--r-- | src/main/scala/com/drivergrp/core/file.scala | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/main/scala/com/drivergrp/core/file.scala b/src/main/scala/com/drivergrp/core/file.scala index b4ec94c..8f365bf 100644 --- a/src/main/scala/com/drivergrp/core/file.scala +++ b/src/main/scala/com/drivergrp/core/file.scala @@ -9,7 +9,7 @@ import com.amazonaws.services.s3.model.{Bucket, GetObjectRequest, ListObjectsV2R import com.drivergrp.core.time.Time import scala.concurrent.{ExecutionContext, Future} -import scalaz.ListT +import scalaz.{ListT, OptionT} object file { @@ -31,7 +31,7 @@ object file { def upload(localSource: File, destination: Path): Future[Unit] - def download(filePath: Path): Future[File] + def download(filePath: Path): OptionT[Future, File] def delete(filePath: Path): Future[Unit] @@ -61,7 +61,7 @@ object file { } } - def download(filePath: Path): Future[File] = Future { + def download(filePath: Path): OptionT[Future, File] = OptionT.optionT(Future { val tempDir = System.getProperty("java.io.tmpdir") val randomFolderName = randomUUID().toString val tempDestinationFile = new File(Paths.get(tempDir, randomFolderName, filePath.toString).toString) @@ -69,10 +69,11 @@ object file { if (!tempDestinationFile.getParentFile.mkdirs()) { throw new Exception(s"Failed to create temp directory to download file `$file`") } else { - val _ = s3.getObject(new GetObjectRequest(bucket, filePath.toString), tempDestinationFile) - tempDestinationFile + Option(s3.getObject(new GetObjectRequest(bucket, filePath.toString), tempDestinationFile)).map { _ => + tempDestinationFile + } } - } + }) def delete(filePath: Path): Future[Unit] = Future { s3.deleteObject(bucket, filePath.toString) @@ -115,11 +116,9 @@ object file { } } - def download(filePath: Path): Future[File] = Future { - make(new File(filePath.toString)) { file => - assert(file.exists() && file.isFile) - } - } + def download(filePath: Path): OptionT[Future, File] = OptionT.optionT(Future { + Option(new File(filePath.toString)).filter(file => file.exists() && file.isFile) + }) def delete(filePath: Path): Future[Unit] = Future { val file = new File(filePath.toString) |