Skip to content

Commit

Permalink
remove doSipiPostUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
siers committed Nov 21, 2024
1 parent 83eaf08 commit 155e430
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.apache.pekko.http.scaladsl.model.headers.BasicHttpCredentials
import org.apache.pekko.http.scaladsl.unmarshalling.Unmarshal
import zio.ZIO

import java.nio.file.Files
import java.nio.file.Paths
import scala.concurrent.Await
import scala.concurrent.duration.*
Expand All @@ -36,7 +35,6 @@ class KnoraSipiAuthenticationITSpec
private val password = SharedTestDataADM.testPass

private val marblesOriginalFilename = "marbles.tif"
private val pathToMarbles = Paths.get("..", s"test_data/test_route/images/$marblesOriginalFilename")

override lazy val rdfDataObjects: List[RdfDataObject] = List(
RdfDataObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ import org.knora.webapi.IRI
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.store.sipimessages.DeleteTemporaryFileRequest
import org.knora.webapi.messages.store.sipimessages.MoveTemporaryFileToPermanentStorageRequest
import org.knora.webapi.messages.twirl.queries.sparql
import org.knora.webapi.messages.util.PermissionUtilADM
import org.knora.webapi.messages.v2.responder.UpdateResultInProject
import org.knora.webapi.messages.v2.responder.resourcemessages.ReadResourceV2
import org.knora.webapi.messages.v2.responder.valuemessages.FileValueContentV2
import org.knora.webapi.messages.v2.responder.valuemessages.ReadValueV2
import org.knora.webapi.messages.v2.responder.valuemessages.StillImageExternalFileValueContentV2
import org.knora.webapi.slice.admin.domain.model.Permission
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.store.iiif.api.SipiService
Expand Down Expand Up @@ -71,24 +66,6 @@ trait ResourceUtilV2 {
* @return Task of Either None for nonexistent, true for root and false for child node.
*/
def checkListNodeExistsAndIsRootNode(nodeIri: IRI): Task[Either[Option[Nothing], Boolean]]

/**
* Given an update task which changes [[FileValueContentV2]] values the related files need to be finalized.
* If the update was successful the temporary files are moved to permanent storage.
* If the update failed the temporary files are deleted silently.
*
* @param updateTask the [[Task]] that updates the triplestore.
* @param fileValues the values which the task updates.
* @param requestingUser the user making the request.
*
* @return The result of the updateTask, unless this task was successful and the subsequent move to permanent storage failed.
* In the latter case the failure from Sipi is returned.
*/
def doSipiPostUpdate[T <: UpdateResultInProject](
updateTask: Task[T],
fileValues: Seq[FileValueContentV2],
requestingUser: User,
): Task[T]
}

final case class ResourceUtilV2Live(triplestore: TriplestoreService, sipiService: SipiService)
Expand Down Expand Up @@ -182,48 +159,6 @@ final case class ResourceUtilV2Live(triplestore: TriplestoreService, sipiService

} yield maybeList
}

/**
* Given a future representing an operation that was supposed to update a value in a triplestore, checks whether
* the updated value was a file value. If not, this method returns the same future. If it was a file value, this
* method checks whether the update was successful. If so, it asks Sipi to move the file to permanent storage.
* If not, it asks Sipi to delete the temporary file.
*
* @param updateTask the Task that should have updated the triplestore.
* @param valueContents: Seq[FileValueContentV2], the value that should have been created or updated.
* @param requestingUser the user making the request.
*/
override def doSipiPostUpdate[T <: UpdateResultInProject](
updateTask: Task[T],
valueContents: Seq[FileValueContentV2],
requestingUser: User,
): Task[T] = {
val temporaryFiles = valueContents.filterNot(_.is[StillImageExternalFileValueContentV2])
updateTask.foldZIO(
(e: Throwable) => {
ZIO
.foreachDiscard(temporaryFiles) { file =>
sipiService
.deleteTemporaryFile(DeleteTemporaryFileRequest(file.fileValue.internalFilename, requestingUser))
.logError
}
.ignore *> ZIO.fail(e)
},
(updateInProject: T) => {
ZIO
.foreachDiscard(temporaryFiles) { file =>
sipiService.moveTemporaryFileToPermanentStorage(
MoveTemporaryFileToPermanentStorageRequest(
file.fileValue.internalFilename,
updateInProject.projectADM.shortcode,
requestingUser,
),
)
}
.as(updateInProject)
},
)
}
}

object ResourceUtilV2Live {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,14 @@ final case class ValuesResponderV2(
)
}

val triplestoreUpdateFuture: Task[CreateValueResponseV2] = for {
for {
// Don't allow anonymous users to create values.
_ <- ZIO.when(requestingUser.isAnonymousUser)(
ZIO.fail(ForbiddenException("Anonymous users aren't allowed to create values")),
)
// Do the remaining pre-update checks and the update while holding an update lock on the resource.
taskResult <- IriLocker.runWithIriLock(apiRequestID, valueToCreate.resourceIri, taskZio)
} yield taskResult

// If we were creating a file value, have Sipi move the file to permanent storage if the update
// was successful, or delete the temporary file if the update failed.
resourceUtilV2.doSipiPostUpdate(
triplestoreUpdateFuture,
valueToCreate.valueContent.asOpt[FileValueContentV2].toSeq,
requestingUser,
)
}

private def ifIsListValueThenCheckItPointsToListNodeWhichIsNotARootNode(valueContent: ValueContentV2) =
Expand Down Expand Up @@ -949,18 +941,12 @@ final case class ValuesResponderV2(
updateValue match {
case updateValueContentV2: UpdateValueContentV2 =>
// This is a request to update the content of a value.
val triplestoreUpdate = IriLocker.runWithIriLock(
IriLocker.runWithIriLock(
apiRequestId,
updateValueContentV2.resourceIri,
makeTaskFutureToUpdateValueContent(updateValueContentV2),
)

resourceUtilV2.doSipiPostUpdate(
triplestoreUpdate,
updateValueContentV2.valueContent.asOpt[FileValueContentV2].toSeq,
requestingUser,
)

case updateValuePermissionsV2: UpdateValuePermissionsV2 =>
// This is a request to update the permissions attached to a value.
IriLocker.runWithIriLock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ final case class CreateResourceV2Handler(
* @return a [[ReadResourcesSequenceV2]] containing a preview of the resource.
*/
def apply(createResourceRequestV2: CreateResourceRequestV2): Task[ReadResourcesSequenceV2] =
resourceUtilV2.doSipiPostUpdate(
triplestoreUpdate(createResourceRequestV2),
createResourceRequestV2.createResource.flatValues.flatMap(_.valueContent.asOpt[FileValueContentV2]).toSeq,
createResourceRequestV2.requestingUser,
)
triplestoreUpdate(createResourceRequestV2)

private def triplestoreUpdate(
createResourceRequestV2: CreateResourceRequestV2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ object PermissionEndpointsRequests {
forProperty.isDefined
}
object ChangeDoapRequest {
import org.knora.webapi.slice.admin.api.Codecs.ZioJsonCodec.projectIri
given JsonCodec[ChangeDoapRequest] = DeriveJsonCodec.gen[ChangeDoapRequest]
}
}
Expand Down

0 comments on commit 155e430

Please sign in to comment.