diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index 6b55d119..223121b5 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -18,7 +18,7 @@ jobs: if: ${{ github.event_name == 'pull_request' }} steps: - name: Git Checkout - uses: actions/checkout@v3.5.1 + uses: actions/checkout@v3.3.0 with: fetch-depth: '0' - name: Setup Scala @@ -41,7 +41,7 @@ jobs: if: ${{ ((github.event_name == 'release') && (github.event.action == 'published')) || (github.event_name == 'workflow_dispatch') }} steps: - name: Git Checkout - uses: actions/checkout@v3.5.1 + uses: actions/checkout@v3.3.0 with: fetch-depth: '0' - name: Setup Scala @@ -65,7 +65,7 @@ jobs: if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event_name == 'published')) }} steps: - name: Git Checkout - uses: actions/checkout@v3.5.1 + uses: actions/checkout@v3.3.0 with: ref: ${{ github.head_ref }} fetch-depth: '0' diff --git a/README.md b/README.md index d6aa7587..6fa27072 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ In order to use this library, we need to add the following line in our `build.sbt` file: ```scala -libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.0" +libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.1" ``` ## How to use it? @@ -78,7 +78,7 @@ object ZIOFTPExample extends ZIOAppDefault { private val settings = UnsecureFtpSettings("127.0.0.1", 21, FtpCredentials("one", "1234")) - private val myApp: ZIO[Scope with Ftp, IOException, Unit] = + private val myApp: ZIO[Ftp, IOException, Unit] = for { _ <- Console.printLine("List of files at root directory:") resource <- ls("/").runCollect @@ -94,7 +94,7 @@ object ZIOFTPExample extends ZIOAppDefault { .via(ZPipeline.utf8Decode) .runCollect _ <- Console.printLine(s"Content of $path file:") - _ <- Console.printLine(file.fold("")(_ + _)) + _ <- Console.printLine(file.mkString) } yield () override def run = myApp.provideSomeLayer(unsecure(settings)) diff --git a/docs/index.md b/docs/index.md index 6ec767b2..11cc53ad 100644 --- a/docs/index.md +++ b/docs/index.md @@ -77,7 +77,7 @@ object ZIOFTPExample extends ZIOAppDefault { private val settings = UnsecureFtpSettings("127.0.0.1", 21, FtpCredentials("one", "1234")) - private val myApp: ZIO[Scope with Ftp, IOException, Unit] = + private val myApp: ZIO[Ftp, IOException, Unit] = for { _ <- Console.printLine("List of files at root directory:") resource <- ls("/").runCollect @@ -93,7 +93,7 @@ object ZIOFTPExample extends ZIOAppDefault { .via(ZPipeline.utf8Decode) .runCollect _ <- Console.printLine(s"Content of $path file:") - _ <- Console.printLine(file.fold("")(_ + _)) + _ <- Console.printLine(file.mkString) } yield () override def run = myApp.provideSomeLayer(unsecure(settings)) diff --git a/zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala b/zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala index 3d1b9736..03720b39 100644 --- a/zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala +++ b/zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala @@ -1,7 +1,7 @@ package zio.ftp import java.io.IOException -import zio.{ Scope, ZIO } +import zio.ZIO import zio.stream.ZStream trait FtpAccessors[+A] { @@ -73,6 +73,6 @@ trait FtpAccessors[+A] { * @param source data stream to store * @tparam R environment of the specified stream source, required to extend Blocking */ - def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R with Scope, IOException, Unit] + def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R, IOException, Unit] } diff --git a/zio-ftp/src/main/scala/zio/ftp/TestFtp.scala b/zio-ftp/src/main/scala/zio/ftp/TestFtp.scala index 4aac8ca3..abbd45f4 100644 --- a/zio-ftp/src/main/scala/zio/ftp/TestFtp.scala +++ b/zio-ftp/src/main/scala/zio/ftp/TestFtp.scala @@ -23,7 +23,7 @@ import java.nio.file.attribute.PosixFilePermission import zio.nio.file.{ Path => ZPath } import zio.nio.file.Files import zio.stream.{ ZSink, ZStream } -import zio.{ Cause, Scope, ZIO } +import zio.{ Cause, ZIO } object TestFtp { @@ -98,18 +98,20 @@ object TestFtp { override def upload[R]( path: String, source: ZStream[R, Throwable, Byte] - ): ZIO[R with Scope, IOException, Unit] = { + ): ZIO[R, IOException, Unit] = { val file = (root / ZPath(path).elements.mkString("/")).toFile - ZIO - .fromAutoCloseable(ZIO.attempt(new FileOutputStream(file))) - .flatMap { out => - source - .run(ZSink.fromOutputStream(out)) - .unit - } - .refineToOrDie[IOException] - .catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot upload data to : $path", err))) + ZIO.scoped[R] { + ZIO + .fromAutoCloseable(ZIO.attempt(new FileOutputStream(file))) + .flatMap { out => + source + .run(ZSink.fromOutputStream(out)) + .unit + } + .refineToOrDie[IOException] + .catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot upload data to : $path", err))) + } } } } diff --git a/zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala b/zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala index bbdf2068..0cec9d7a 100644 --- a/zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala +++ b/zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala @@ -79,14 +79,16 @@ final private class UnsecureFtp(unsafeClient: Client) extends FtpAccessors[Clien ZStream(FtpResource.fromFtpFile(f, Some(path))) } - def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R with Scope, IOException, Unit] = - source.toInputStream - .mapError(new IOException(_)) - .flatMap(is => - execute(_.storeFile(path, is)) - .filterOrFail(identity)(InvalidPathError(s"Path is invalid. Cannot upload data to : $path")) - .unit - ) + def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R, IOException, Unit] = + ZIO.scoped[R] { + source.toInputStream + .mapError(new IOException(_)) + .flatMap(is => + execute(_.storeFile(path, is)) + .filterOrFail(identity)(InvalidPathError(s"Path is invalid. Cannot upload data to : $path")) + .unit + ) + } override def execute[T](f: Client => T): ZIO[Any, IOException, T] = attemptBlockingIO(f(unsafeClient)) diff --git a/zio-ftp/src/main/scala/zio/ftp/package.scala b/zio-ftp/src/main/scala/zio/ftp/package.scala index 0e258ce8..6176f184 100644 --- a/zio-ftp/src/main/scala/zio/ftp/package.scala +++ b/zio-ftp/src/main/scala/zio/ftp/package.scala @@ -55,7 +55,7 @@ package object ftp { def upload[R]( path: String, source: ZStream[R, Throwable, Byte] - ): ZIO[R & Scope with Ftp, IOException, Unit] = + ): ZIO[R with Ftp, IOException, Unit] = for { ftp <- ZIO.service[Ftp] _ <- ftp.upload(path, source) @@ -91,7 +91,7 @@ package object ftp { def upload[R]( path: String, source: ZStream[R, Throwable, Byte] - ): ZIO[SFtp with R with Scope, IOException, Unit] = + ): ZIO[SFtp with R, IOException, Unit] = for { ftp <- ZIO.service[SFtp] _ <- ftp.upload(path, source) @@ -127,7 +127,7 @@ package object ftp { def upload[R]( path: String, source: ZStream[R, Throwable, Byte] - ): ZIO[StubFtp with R with Scope, IOException, Unit] = + ): ZIO[StubFtp with R, IOException, Unit] = for { ftp <- ZIO.service[StubFtp] _ <- ftp.upload(path, source) @@ -137,11 +137,11 @@ package object ftp { ZStream.serviceWithStream(_.readFile(path, chunkSize)) } - def unsecure(settings: UnsecureFtpSettings): ZLayer[Scope, ConnectionError, Ftp] = - ZLayer.fromZIO(UnsecureFtp.connect(settings)) + def unsecure(settings: UnsecureFtpSettings): ZLayer[Any, ConnectionError, Ftp] = + ZLayer.scoped(UnsecureFtp.connect(settings)) - def secure(settings: SecureFtpSettings): ZLayer[Scope, ConnectionError, SFtp] = - ZLayer.fromZIO(SecureFtp.connect(settings)) + def secure(settings: SecureFtpSettings): ZLayer[Any, ConnectionError, SFtp] = + ZLayer.scoped(SecureFtp.connect(settings)) def stub(path: ZPath): Layer[Any, StubFtp] = ZLayer.succeed(TestFtp.create(path))