Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify and rename SipiService #2929

Merged
merged 8 commits into from
Nov 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import org.knora.webapi.store.cache.api.CacheService
import org.knora.webapi.store.cache.impl.CacheServiceInMemImpl
import org.knora.webapi.store.iiif.IIIFRequestMessageHandler
import org.knora.webapi.store.iiif.IIIFRequestMessageHandlerLive
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.iiif.impl.IIIFServiceMockImpl
import org.knora.webapi.store.iiif.impl.IIIFServiceSipiImpl
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.store.iiif.impl.SipiServiceLive
import org.knora.webapi.store.iiif.impl.SipiServiceMock
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.impl.TriplestoreServiceLive
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdater
Expand All @@ -70,7 +70,7 @@ object LayersTest {
type DefaultTestEnvironmentWithoutSipi = LayersLive.DspEnvironmentLive with FusekiTestContainer with TestClientService
type DefaultTestEnvironmentWithSipi = DefaultTestEnvironmentWithoutSipi with SipiTestContainer

type CommonR0 = ActorSystem with AppConfigurations with IIIFService with JwtService with StringFormatter
type CommonR0 = ActorSystem with AppConfigurations with SipiService with JwtService with StringFormatter
type CommonR =
ApiRoutes
with AppRouter
Expand Down Expand Up @@ -198,29 +198,30 @@ object LayersTest {
with SipiTestContainer
with AppConfigurations
with JwtService
with IIIFService
with SipiService
with StringFormatter
](
AppConfigForTestContainers.testcontainers,
FusekiTestContainer.layer,
SipiTestContainer.layer,
IIIFServiceSipiImpl.layer,
SipiServiceLive.layer,
JwtServiceLive.layer,
StringFormatter.test
)

private val fusekiTestcontainers =
ZLayer.make[FusekiTestContainer with AppConfigurations with JwtService with IIIFService with StringFormatter](
ZLayer.make[FusekiTestContainer with AppConfigurations with JwtService with SipiService with StringFormatter](
AppConfigForTestContainers.fusekiOnlyTestcontainer,
FusekiTestContainer.layer,
IIIFServiceMockImpl.layer,
SipiServiceMock.layer,
JwtServiceLive.layer,
StringFormatter.test
)

/**
* Provides a layer for integration tests which depend on Fuseki as Testcontainers.
* Sipi/IIIFService will be mocked with the [[IIIFServiceMockImpl]]
* Sipi/IIIFService will be mocked with the [[SipiServiceMock]]
*
* @param system An optional [[pekko.actor.ActorSystem]] for use with Akka's [[pekko.testkit.TestKit]]
* @return a [[ULayer]] with the [[DefaultTestEnvironmentWithoutSipi]]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.store.sipimessages._
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import org.knora.webapi.slice.admin.domain.service.Asset
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.iiif.api.FileMetadataSipiResponse
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.store.iiif.errors.SipiException

/**
* Can be used in place of [[IIIFServiceSipiImpl]] for tests without an actual Sipi server, by returning hard-coded
* Can be used in place of [[SipiServiceLive]] for tests without an actual Sipi server, by returning hard-coded
* responses simulating responses from Sipi.
*/
case class IIIFServiceMockImpl() extends IIIFService {
case class SipiServiceMock() extends SipiService {

/**
* A request with this filename will always cause a Sipi error.
*/
private val FAILURE_FILENAME: String = "failure.jp2"

def getFileMetadata(getFileMetadataRequestV2: GetFileMetadataRequest): Task[GetFileMetadataResponse] =
override def getFileMetadata(ignoredByMock: String): Task[FileMetadataSipiResponse] =
ZIO.succeed(
GetFileMetadataResponse(
FileMetadataSipiResponse(
originalFilename = Some("test2.tiff"),
originalMimeType = Some("image/tiff"),
internalMimeType = "image/jp2",
width = Some(512),
height = Some(256),
pageCount = None,
numpages = None,
duration = None,
fps = None
)
Expand Down Expand Up @@ -63,11 +64,11 @@ case class IIIFServiceMockImpl() extends IIIFService {
override def downloadAsset(asset: Asset, targetDir: Path, user: UserADM): Task[Option[Path]] = ???
}

object IIIFServiceMockImpl {
object SipiServiceMock {

val layer: ZLayer[Any, Nothing, IIIFService] =
val layer: ZLayer[Any, Nothing, SipiService] =
ZLayer
.succeed(IIIFServiceMockImpl())
.succeed(SipiServiceMock())
.tap(_ => ZIO.logInfo(">>> Mock Sipi IIIF Service Initialized <<<"))

}
10 changes: 5 additions & 5 deletions webapi/src/main/scala/org/knora/webapi/core/AppServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.knora.webapi.messages.store.sipimessages.IIIFServiceStatusOK
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
import org.knora.webapi.store.cache.api.CacheService
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.domain.TriplestoreStatus
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdater
Expand All @@ -31,7 +31,7 @@ final case class AppServer(
ru: RepositoryUpdater,
as: ActorSystem,
ontologyCache: OntologyCache,
iiifs: IIIFService,
sipiService: SipiService,
cs: CacheService,
hs: HttpServer,
appConfig: AppConfig
Expand Down Expand Up @@ -99,7 +99,7 @@ final case class AppServer(
private def checkIIIFService(requiresIIIFService: Boolean): UIO[Unit] =
for {
_ <- state.set(AppState.WaitingForIIIFService)
_ <- iiifs
_ <- sipiService
.getStatus()
.flatMap {
case IIIFServiceStatusOK =>
Expand Down Expand Up @@ -156,7 +156,7 @@ final case class AppServer(
object AppServer {

private type AppServerEnvironment =
State & TriplestoreService & RepositoryUpdater & ActorSystem & OntologyCache & IIIFService & CacheService &
State & TriplestoreService & RepositoryUpdater & ActorSystem & OntologyCache & SipiService & CacheService &
HttpServer & AppConfig

/**
Expand All @@ -169,7 +169,7 @@ object AppServer {
ru <- ZIO.service[RepositoryUpdater]
as <- ZIO.service[ActorSystem]
oc <- ZIO.service[OntologyCache]
iiifs <- ZIO.service[IIIFService]
iiifs <- ZIO.service[SipiService]
cs <- ZIO.service[CacheService]
hs <- ZIO.service[HttpServer]
c <- ZIO.service[AppConfig]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ import org.knora.webapi.store.cache.api.CacheService
import org.knora.webapi.store.cache.impl.CacheServiceInMemImpl
import org.knora.webapi.store.iiif.IIIFRequestMessageHandler
import org.knora.webapi.store.iiif.IIIFRequestMessageHandlerLive
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.iiif.impl.IIIFServiceSipiImpl
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.store.iiif.impl.SipiServiceLive
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.impl.TriplestoreServiceLive
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdater
Expand All @@ -68,7 +68,7 @@ object LayersLive {
ActorSystem & ApiRoutes & AppConfigurations & AppRouter & Authenticator & CacheService &
CacheServiceRequestMessageHandler & CardinalityHandler & CardinalityService & ConstructResponseUtilV2 &
ConstructTransformer & GravsearchTypeInspectionRunner & GroupsResponderADM & HttpServer &
IIIFRequestMessageHandler & IIIFService & InferenceOptimizationService & IriService & IriConverter & JwtService &
IIIFRequestMessageHandler & SipiService & InferenceOptimizationService & IriService & IriConverter & JwtService &
KnoraProjectRepo & ListsResponderADM & ListsResponderV2 & MessageRelay & OntologyCache & OntologyHelpers &
OntologyRepo & OntologyResponderV2 & PermissionUtilADM & PermissionsResponderADM & PredicateObjectMapper &
ProjectADMRestService & ProjectADMService & ProjectExportService & ProjectExportStorageService &
Expand Down Expand Up @@ -101,7 +101,7 @@ object LayersLive {
HandlerMapper.layer,
HttpServer.layer,
IIIFRequestMessageHandlerLive.layer,
IIIFServiceSipiImpl.layer,
SipiServiceLive.layer,
InferenceOptimizationService.layer,
IriConverter.layer,
IriService.layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1641,14 +1641,6 @@ class StringFormatter private (
}
}

/**
* Constructs a path for accessing a file that has been uploaded to Sipi's temporary storage.
*
* @param filename the filename.
* @return a URL for accessing the file.
*/
def makeSipiTempFilePath(filename: String): String = s"/tmp/$filename"

/**
* Creates a new resource IRI based on a UUID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,11 @@ import org.knora.webapi.messages.traits.RequestWithSender
import pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport

/**
* An abstract trait for messages that can be sent to the [[org.knora.webapi.store.iiif.api.IIIFService]]
* An abstract trait for messages that can be sent to the [[org.knora.webapi.store.iiif.api.SipiService]]
*/
sealed trait IIIFRequest extends StoreRequest with RelayedMessage

sealed trait SipiRequest extends IIIFRequest {
def requestingUser: UserADM
}

/**
* Requests file metadata from Sipi. A successful response is a [[GetFileMetadataResponse]].
*
* @param filePath the path at which Sipi can serve the file.
* @param requestingUser the user making the request.
*/
case class GetFileMetadataRequest(filePath: String, requestingUser: UserADM) extends SipiRequest

/**
* Represents file metadata returned by Sipi.
*
* @param originalFilename the file's original filename, if known.
* @param originalMimeType the file's original MIME type.
* @param internalMimeType the file's internal MIME type. Always defined (https://dasch.myjetbrains.com/youtrack/issue/DSP-711).
* @param width the file's width in pixels, if applicable.
* @param height the file's height in pixels, if applicable.
* @param pageCount the number of pages in the file, if applicable.
* @param duration the duration of the file in seconds, if applicable.
*/
case class GetFileMetadataResponse(
originalFilename: Option[String],
originalMimeType: Option[String],
internalMimeType: String,
width: Option[Int],
height: Option[Int],
pageCount: Option[Int],
duration: Option[BigDecimal],
fps: Option[BigDecimal]
)
sealed trait SipiRequest extends IIIFRequest

/**
* Asks Sipi to move a file from temporary to permanent storage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.knora.webapi.messages.v2.responder.*
import org.knora.webapi.messages.v2.responder.standoffmessages.MappingXMLtoStandoff
import org.knora.webapi.messages.v2.responder.valuemessages.*
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.util.*

/**
Expand Down Expand Up @@ -673,7 +674,7 @@ object CreateResourceRequestV2 {
jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: UserADM
): ZIO[IriConverter & StringFormatter & MessageRelay, Throwable, CreateResourceRequestV2] =
): ZIO[IriConverter & SipiService & StringFormatter & MessageRelay, Throwable, CreateResourceRequestV2] =
ZIO.serviceWithZIO[StringFormatter] { implicit stringFormatter =>
val validationFun: (String, => Nothing) => String =
(s, errorFun) => Iri.toSparqlEncodedString(s).getOrElse(errorFun)
Expand Down