From 67f3d590c14dbaca0dbee45cd72d7e8e2d2dbe79 Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Tue, 1 Aug 2023 12:21:13 +0200 Subject: [PATCH] ZIO-ftp should clean up after itself --- zio-ftp/src/main/scala/zio/ftp/TestFtp.scala | 24 ++++++++++--------- .../src/main/scala/zio/ftp/UnsecureFtp.scala | 18 +++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) 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))