diff options
Diffstat (limited to 'src/main/scala/xyz/driver/core/file')
4 files changed, 46 insertions, 18 deletions
diff --git a/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala b/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala index bfe6995..fab1307 100644 --- a/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala +++ b/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala @@ -1,7 +1,7 @@ package xyz.driver.core.file import java.io.File -import java.nio.file.{Path, Paths} +import java.nio.file.{Files, Path, Paths} import xyz.driver.core.{Name, Revision} import xyz.driver.core.time.Time @@ -12,7 +12,7 @@ import scalaz.{ListT, OptionT} class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage { implicit private val execution = executionContext - def upload(localSource: File, destination: Path): Future[Unit] = Future { + override def upload(localSource: File, destination: Path): Future[Unit] = Future { checkSafeFileName(destination) { val destinationFile = destination.toFile @@ -28,12 +28,12 @@ class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage } } - def download(filePath: Path): OptionT[Future, File] = + override 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 { + override def delete(filePath: Path): Future[Unit] = Future { val file = new File(filePath.toString) if (file.delete()) () else { @@ -41,7 +41,7 @@ class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage } } - def list(path: Path): ListT[Future, FileLink] = + override def list(path: Path): ListT[Future, FileLink] = ListT.listT(Future { val file = new File(path.toString) if (file.isDirectory) { @@ -54,4 +54,9 @@ class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage } } else List.empty[FileLink] }) + + override def exists(path: Path): Future[Boolean] = Future { + Files.exists(path) + } + } diff --git a/src/main/scala/xyz/driver/core/file/GcsStorage.scala b/src/main/scala/xyz/driver/core/file/GcsStorage.scala index 6c2746e..deb8a0e 100644 --- a/src/main/scala/xyz/driver/core/file/GcsStorage.scala +++ b/src/main/scala/xyz/driver/core/file/GcsStorage.scala @@ -52,27 +52,42 @@ class GcsStorage(storageClient: Storage, bucketName: Name[Bucket], executionCont storageClient.delete(BlobId.of(bucketName.value, filePath.toString)) } - override def list(path: Path): ListT[Future, FileLink] = + override def list(directoryPath: Path): ListT[Future, FileLink] = ListT.listT(Future { val page = storageClient.list( bucketName.value, BlobListOption.currentDirectory(), - BlobListOption.prefix(path.toString) + BlobListOption.prefix(s"$directoryPath/") ) - page.iterateAll().asScala.map(blobToFileLink(path, _)).toList + page.iterateAll().asScala.map(blobToFileLink(directoryPath, _)).toList }) protected def blobToFileLink(path: Path, blob: Blob): FileLink = { + def nullError(property: String) = throw new IllegalStateException(s"Blob $blob at $path does not have $property") + val name = Option(blob.getName).getOrElse(nullError("a name")) + val generation = Option(blob.getGeneration).getOrElse(nullError("a generation")) + val updateTime = Option(blob.getUpdateTime).getOrElse(nullError("an update time")) + val size = Option(blob.getSize).getOrElse(nullError("a size")) + FileLink( - Name(blob.getName), - Paths.get(path.toString, blob.getName), - Revision(blob.getGeneration.toString), - Time(blob.getUpdateTime), - blob.getSize + Name(name), + Paths.get(path.toString, name), + Revision(generation.toString), + Time(updateTime), + size ) } + override def exists(path: Path): Future[Boolean] = Future { + val blob = Option( + storageClient.get( + bucketName.value, + path.toString + )) + blob.isDefined + } + override def signedFileUrl(filePath: Path, duration: Duration): OptionT[Future, URL] = OptionT.optionT(Future { Option(storageClient.get(bucketName.value, filePath.toString)).filterNot(_.getSize == 0).map { blob => diff --git a/src/main/scala/xyz/driver/core/file/S3Storage.scala b/src/main/scala/xyz/driver/core/file/S3Storage.scala index 933b01a..7df3db2 100644 --- a/src/main/scala/xyz/driver/core/file/S3Storage.scala +++ b/src/main/scala/xyz/driver/core/file/S3Storage.scala @@ -15,13 +15,13 @@ import scalaz.{ListT, OptionT} class S3Storage(s3: AmazonS3, bucket: Name[Bucket], executionContext: ExecutionContext) extends FileStorage { implicit private val execution = executionContext - def upload(localSource: File, destination: Path): Future[Unit] = Future { + override def upload(localSource: File, destination: Path): Future[Unit] = Future { checkSafeFileName(destination) { val _ = s3.putObject(bucket.value, destination.toString, localSource).getETag } } - def download(filePath: Path): OptionT[Future, File] = + override def download(filePath: Path): OptionT[Future, File] = OptionT.optionT(Future { val tempDir = System.getProperty("java.io.tmpdir") val randomFolderName = randomUUID().toString @@ -36,11 +36,11 @@ class S3Storage(s3: AmazonS3, bucket: Name[Bucket], executionContext: ExecutionC } }) - def delete(filePath: Path): Future[Unit] = Future { + override def delete(filePath: Path): Future[Unit] = Future { s3.deleteObject(bucket.value, filePath.toString) } - def list(path: Path): ListT[Future, FileLink] = + override def list(path: Path): ListT[Future, FileLink] = ListT.listT(Future { import scala.collection.JavaConverters._ val req = new ListObjectsV2Request().withBucketName(bucket.value).withPrefix(path.toString).withMaxKeys(2) @@ -63,4 +63,9 @@ class S3Storage(s3: AmazonS3, bucket: Name[Bucket], executionContext: ExecutionC } filterNot isInSubFolder(path) } toList }) + + override def exists(path: Path): Future[Boolean] = Future { + s3.doesObjectExist(bucket.value, path.toString) + } + } diff --git a/src/main/scala/xyz/driver/core/file/package.scala b/src/main/scala/xyz/driver/core/file/package.scala index 9000894..b2c679e 100644 --- a/src/main/scala/xyz/driver/core/file/package.scala +++ b/src/main/scala/xyz/driver/core/file/package.scala @@ -37,7 +37,10 @@ package file { def delete(filePath: Path): Future[Unit] - def list(path: Path): ListT[Future, FileLink] + /** List contents of a directory */ + def list(directoryPath: Path): ListT[Future, FileLink] + + def exists(path: Path): Future[Boolean] /** List of characters to avoid in S3 (I would say file names in general) * |