Skip to content

Commit

Permalink
Refactor ArchiveRoutes, inline vals, remove zip extension val
Browse files Browse the repository at this point in the history
  • Loading branch information
dantb committed Sep 20, 2023
1 parent 925d7a5 commit 5863418
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ import ch.epfl.bluebrain.nexus.delta.sdk.utils.HeadersUtils
object Zip {
type WriteFlow[Metadata] = Flow[(Metadata, Source[ByteString, _]), ByteString, NotUsed]

def apply(req: HttpRequest): Option[Zip.type] =
if (HeadersUtils.matches(req.headers, Zip.contentType.mediaType)) Some(Zip) else None
lazy val contentType: ContentType = MediaTypes.`application/zip`

def contentType: ContentType = MediaTypes.`application/zip`
lazy val writeFlow: WriteFlow[ArchiveMetadata] = Archive.zip()

def fileExtension: String = "zip"
lazy val ordering: Ordering[ArchiveMetadata] = Ordering.by(md => md.filePath)

def metadata(filename: String): ArchiveMetadata =
ArchiveMetadata.create(filename)
def metadata(filename: String): ArchiveMetadata = ArchiveMetadata.create(filename)

def writeFlow: WriteFlow[ArchiveMetadata] = Archive.zip()

def ordering: Ordering[ArchiveMetadata] = Ordering.by(md => md.filePath)
def checkHeader(req: HttpRequest): Boolean = HeadersUtils.matches(req.headers, Zip.contentType.mediaType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.epfl.bluebrain.nexus.delta.plugins.archive.routes

import akka.http.scaladsl.model.StatusCodes.{Created, SeeOther}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive1, Route}
import akka.http.scaladsl.server.Route
import ch.epfl.bluebrain.nexus.delta.plugins.archive.Archives
import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.permissions
import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.Zip
Expand Down Expand Up @@ -54,10 +54,10 @@ class ArchiveRoutes(
(post & entity(as[Json]) & pathEndOrSingleSlash) { json =>
operationName(s"$prefix/archives/{org}/{project}") {
authorizeFor(ref, permissions.write).apply {
archiveResponse {
case Some(_) => emitRedirect(SeeOther, archives.create(ref, json).map(_.uris.accessUri))
case None => emit(Created, archives.create(ref, json).mapValue(_.metadata))
}
archiveResponse(
emitRedirect(SeeOther, archives.create(ref, json).map(_.uris.accessUri)),
emit(Created, archives.create(ref, json).mapValue(_.metadata))
)
}
}
},
Expand All @@ -67,25 +67,24 @@ class ArchiveRoutes(
// create an archive with an id
(put & entity(as[Json]) & pathEndOrSingleSlash) { json =>
authorizeFor(ref, permissions.write).apply {
archiveResponse {
case Some(_) => emitRedirect(SeeOther, archives.create(id, ref, json).map(_.uris.accessUri))
case None => emit(Created, archives.create(id, ref, json).mapValue(_.metadata))
}
archiveResponse(
emitRedirect(SeeOther, archives.create(id, ref, json).map(_.uris.accessUri)),
emit(Created, archives.create(id, ref, json).mapValue(_.metadata))
)
}
},
// fetch or download an archive
(get & pathEndOrSingleSlash) {
authorizeFor(ref, permissions.read).apply {
archiveResponse {
case Some(_) =>
parameter("ignoreNotFound".as[Boolean] ? false) { ignoreNotFound =>
val response = archives.download(id, ref, ignoreNotFound).map { source =>
sourceToFileResponse(source)
}
emit(response)
archiveResponse(
parameter("ignoreNotFound".as[Boolean] ? false) { ignoreNotFound =>
val response = archives.download(id, ref, ignoreNotFound).map { source =>
sourceToFileResponse(source)
}
case None => emit(archives.fetch(id, ref))
}
emit(response)
},
emit(archives.fetch(id, ref))
)
}
}
)
Expand All @@ -97,9 +96,9 @@ class ArchiveRoutes(
}
}

private def sourceToFileResponse(source: AkkaSource): FileResponse =
FileResponse(s"archive.${Zip.fileExtension}", Zip.contentType, 0L, source)
private def archiveResponse(validResp: Route, invalidResp: Route): Route =
extractRequest.map(Zip.checkHeader(_)).apply(valid => if (valid) validResp else invalidResp)

private def archiveResponse: Directive1[Option[Zip.type]] =
extractRequest.map(Zip(_))
private def sourceToFileResponse(source: AkkaSource): FileResponse =
FileResponse(s"archive.zip", Zip.contentType, 0L, source)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.ArchiveReference.{Fil
import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.ArchiveRejection.{AuthorizationFailed, InvalidFileSelf, ResourceNotFound}
import ch.epfl.bluebrain.nexus.delta.sdk.model.ResourceRepresentation.{CompactedJsonLd, Dot, ExpandedJsonLd, NQuads, NTriples, SourceJson}
import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.{ArchiveRejection, ArchiveValue}
import ch.epfl.bluebrain.nexus.delta.plugins.archive.model.Zip
import ch.epfl.bluebrain.nexus.delta.plugins.storage.RemoteContextResolutionFixture
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileAttributes.FileAttributesOrigin.Client
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileRejection.FileNotFound
Expand Down Expand Up @@ -159,7 +158,7 @@ class ArchiveDownloadSpec
.rejectedWith[AuthorizationFailed]
}

s"provide a ${Zip.fileExtension} for both resources and files" in {
s"provide a zip for both resources and files" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(
ResourceReference(Latest(id1), None, None, None),
Expand All @@ -174,7 +173,7 @@ class ArchiveDownloadSpec
result shouldEqual expected
}

s"provide a ${Zip.fileExtension} for file selfs" in {
s"provide a zip for file selfs" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(
FileSelfReference(file1Self, None)
Expand All @@ -187,12 +186,12 @@ class ArchiveDownloadSpec
result shouldEqual expected
}

s"fail to provide a ${Zip.fileExtension} for file selfs which do not resolve" in {
s"fail to provide a zip for file selfs which do not resolve" in {
val value = ArchiveValue.unsafe(NonEmptySet.of(FileSelfReference("http://wrong.file/self", None)))
failToDownload[InvalidFileSelf](value, ignoreNotFound = false)
}

s"provide a ${Zip.fileExtension} for both resources and files with different paths and formats" in {
s"provide a zip for both resources and files with different paths and formats" in {
val list = List(
SourceJson -> file1.value.asJson.sort.spaces2,
CompactedJsonLd -> file1.toCompactedJsonLd.accepted.json.sort.spaces2,
Expand Down Expand Up @@ -235,7 +234,7 @@ class ArchiveDownloadSpec
downloadAndExtract(value, ignoreNotFound = false) should contain key file2Path
}

s"fail to provide a ${Zip.fileExtension} when a resource is not found" in {
s"fail to provide a zip when a resource is not found" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(
ResourceReference(Latest(iri"http://localhost/${genString()}"), None, None, None),
Expand All @@ -245,7 +244,7 @@ class ArchiveDownloadSpec
failToDownload[ResourceNotFound](value, ignoreNotFound = false)
}

s"fail to provide a ${Zip.fileExtension} when a file is not found" in {
s"fail to provide a zip when a file is not found" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(
ResourceReference(Latest(id1), None, None, None),
Expand Down Expand Up @@ -283,14 +282,14 @@ class ArchiveDownloadSpec
result shouldEqual expected
}

s"fail to provide a ${Zip.fileExtension} when access to a resource is not found" in {
s"fail to provide a zip when access to a resource is not found" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(ResourceReference(Latest(id1), None, None, None))
)
rejectedAccess(value)
}

s"fail to provide a ${Zip.fileExtension} when access to a file is not found" in {
s"fail to provide a zip when access to a file is not found" in {
val value = ArchiveValue.unsafe(
NonEmptySet.of(FileReference(Latest(id1), None, None))
)
Expand Down

0 comments on commit 5863418

Please sign in to comment.