Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/scala/io/apibuilder/validation/MultiService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.apibuilder.validation.util.{FileOrder, StandardErrors}
import io.apibuilder.validation.zip.ZipFileReader
import play.api.libs.json._

import scala.util.Using

/**
* Wrapper to work with multiple API Builder services.
* Takes an ordered list of services. If multiple
Expand Down Expand Up @@ -169,7 +171,8 @@ object MultiService {

/**
* Allow a client to read from for example s3 by providing a reader already constructed from the
* stream.
* stream. Note that the caller is responsible for ensuring that close() is called on the reader
* when done.
*/
def fromZipFileReader(reader: ZipFileReader): ValidatedNec[String, Seq[ApiBuilderService]] = {
val fileSorter = FileOrder(reader.entries.find(_.name.toLowerCase() == OrderByFileName).map(_.file))
Expand All @@ -181,6 +184,10 @@ object MultiService {
}

private def servicesFromZip(url: String): ValidatedNec[String, Seq[ApiBuilderService]] = {
ZipFileReader.fromUrl(url).andThen(fromZipFileReader)
ZipFileReader
.fromUrl(url)
.andThen { zfr =>
Using.resource(zfr)(fromZipFileReader)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.nio.file.Files
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import io.apibuilder.validation.util.UrlDownloader
import org.apache.commons.io.FileUtils

object ZipFileReader {

Expand Down Expand Up @@ -36,10 +37,14 @@ object ZipFileReader {
}
}

case class ZipFileReader(inputStream: InputStream) {
case class ZipFileReader(inputStream: InputStream) extends AutoCloseable {

private val destDir: File = Files.createTempDirectory("zipfilereader").toFile

override def close(): Unit = {
FileUtils.deleteQuietly(destDir)
}

/**
* Returns a list of the entries of the zip file (all files ending with .json)
*/
Expand Down