aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/file/FileSystemStorage.scala
blob: ce26fe43be4b4bc29cebaa0bdad2f9f8b489080d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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}

import xyz.driver.core.{Name, Revision}
import xyz.driver.core.time.Time

import scala.concurrent.{ExecutionContext, Future}
import scalaz.{ListT, OptionT}

@deprecated("Consider using xyz.driver.core.storage.FileSystemBlobStorage instead", "driver-core 1.8.14")
class FileSystemStorage(executionContext: ExecutionContext) extends FileStorage {
  implicit private val execution = executionContext

  override def upload(localSource: File, destination: Path): Future[Unit] = Future {
    checkSafeFileName(destination) {
      val destinationFile = destination.toFile

      if (destinationFile.getParentFile.exists() || destinationFile.getParentFile.mkdirs()) {
        if (localSource.renameTo(destinationFile)) ()
        else {
          throw new Exception(
            s"Failed to move file from `${localSource.getCanonicalPath}` to `${destinationFile.getCanonicalPath}`")
        }
      } else {
        throw new Exception(s"Failed to create parent directories for file `${destinationFile.getCanonicalPath}`")
      }
    }
  }

  override def download(filePath: Path): OptionT[Future, File] =
    OptionT.optionT(Future {
      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()) ()
    else {
      throw new Exception(s"Failed to delete file $file" + (if (!file.exists()) ", file does not exist." else "."))
    }
  }

  override def list(path: Path): ListT[Future, FileLink] =
    ListT.listT(Future {
      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()),
            file.length())
        }
      } else List.empty[FileLink]
    })

  override def exists(path: Path): Future[Boolean] = Future {
    Files.exists(path)
  }

}