Skip to content

Commit

Permalink
ZIO-ftp should clean up after itself
Browse files Browse the repository at this point in the history
  • Loading branch information
mberndt123 committed Aug 1, 2023
1 parent 7b7452a commit 67f3d59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
24 changes: 13 additions & 11 deletions zio-ftp/src/main/scala/zio/ftp/TestFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)))
}
}
}
}
18 changes: 10 additions & 8 deletions zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 67f3d59

Please sign in to comment.