From a39ab2cb0e19f84176513d7b1e145351c3ceef8b Mon Sep 17 00:00:00 2001 From: vlad Date: Thu, 25 Aug 2016 12:32:30 -0700 Subject: Id generation within limits + File error logging fix --- src/main/scala/com/drivergrp/core/file.scala | 53 +++++++++++----------- src/main/scala/com/drivergrp/core/generators.scala | 2 + 2 files changed, 28 insertions(+), 27 deletions(-) (limited to 'src/main/scala') diff --git a/src/main/scala/com/drivergrp/core/file.scala b/src/main/scala/com/drivergrp/core/file.scala index da46107..2c6a990 100644 --- a/src/main/scala/com/drivergrp/core/file.scala +++ b/src/main/scala/com/drivergrp/core/file.scala @@ -62,19 +62,20 @@ object file { } } - 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) - - if (!tempDestinationFile.getParentFile.mkdirs()) { - throw new Exception(s"Failed to create temp directory to download file `$file`") - } else { - Option(s3.getObject(new GetObjectRequest(bucket, filePath.toString), tempDestinationFile)).map { _ => - tempDestinationFile + 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) + + if (!tempDestinationFile.getParentFile.mkdirs()) { + throw new Exception(s"Failed to create temp directory to download file `$tempDestinationFile`") + } else { + Option(s3.getObject(new GetObjectRequest(bucket, filePath.toString), tempDestinationFile)).map { _ => + tempDestinationFile + } } - } - }) + }) def delete(filePath: Path): Future[Unit] = Future { s3.deleteObject(bucket, filePath.toString) @@ -94,11 +95,10 @@ object file { } flatMap { result => result.getObjectSummaries.asScala.toList.map { summary => val file = new File(summary.getKey) - FileLink( - Name[File](file.getName), - Paths.get(file.getPath), - Revision[File](summary.getETag), - Time(summary.getLastModified.getTime)) + FileLink(Name[File](file.getName), + Paths.get(file.getPath), + Revision[File](summary.getETag), + Time(summary.getLastModified.getTime)) } filterNot isInSubFolder(path) } toList }) @@ -123,9 +123,10 @@ object file { } } - def download(filePath: Path): OptionT[Future, File] = OptionT.optionT(Future { - Option(new File(filePath.toString)).filter(file => 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) @@ -140,14 +141,12 @@ object file { val file = new File(path.toString) if (file.isDirectory) { file.listFiles().toList.filter(_.isFile).map { file => - FileLink( - Name[File](file.getName), - Paths.get(file.getPath), - Revision[File](file.hashCode.toString), - Time(file.lastModified())) + FileLink(Name[File](file.getName), + Paths.get(file.getPath), + Revision[File](file.hashCode.toString), + Time(file.lastModified())) } - } - else List.empty[FileLink] + } else List.empty[FileLink] }) } } diff --git a/src/main/scala/com/drivergrp/core/generators.scala b/src/main/scala/com/drivergrp/core/generators.scala index 86183a1..10df7db 100644 --- a/src/main/scala/com/drivergrp/core/generators.scala +++ b/src/main/scala/com/drivergrp/core/generators.scala @@ -18,6 +18,8 @@ object generators { def nextId[T](): Id[T] = Id[T](scala.math.abs(nextLong())) + def nextId[T](maxValue: Int): Id[T] = Id[T](scala.math.abs(nextInt(maxValue).toLong)) + def nextName[T](maxLength: Int = DefaultMaxLength): Name[T] = Name[T](nextString(maxLength)) def nextUuid() = java.util.UUID.randomUUID -- cgit v1.2.3