From 794844646f98a92607f9da605a5fb3059196b67e Mon Sep 17 00:00:00 2001 From: Raitis Veinbahs Date: Mon, 25 Nov 2024 09:56:10 +0100 Subject: [PATCH] remove DeleteTemporaryFileRequest & delete_temp_file.lua --- .../knora/sipi/SipiClientTestDelegator.scala | 10 -- sipi/scripts/delete_temp_file.lua | 122 ------------------ .../store/sipimessages/SipiMessages.scala | 8 -- .../iiif/IIIFRequestMessageHandler.scala | 2 - .../webapi/store/iiif/api/SipiService.scala | 8 -- .../store/iiif/impl/SipiServiceLive.scala | 27 ---- .../store/iiif/impl/SipiServiceMock.scala | 7 - .../valuemessages/ValueContentV2Spec.scala | 5 - 8 files changed, 189 deletions(-) delete mode 100644 sipi/scripts/delete_temp_file.lua diff --git a/integration/src/test/scala/org/knora/sipi/SipiClientTestDelegator.scala b/integration/src/test/scala/org/knora/sipi/SipiClientTestDelegator.scala index 7c8ef102e9..75222b37c2 100644 --- a/integration/src/test/scala/org/knora/sipi/SipiClientTestDelegator.scala +++ b/integration/src/test/scala/org/knora/sipi/SipiClientTestDelegator.scala @@ -10,7 +10,6 @@ import zio.ULayer import zio.ZLayer import zio.nio.file.Path -import org.knora.webapi.messages.store.sipimessages.DeleteTemporaryFileRequest import org.knora.webapi.messages.store.sipimessages.MoveTemporaryFileToPermanentStorageRequest import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileRequest import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileResponse @@ -69,15 +68,6 @@ case class SipiServiceTestDelegator( ): Task[SuccessResponseV2] = sipiService.moveTemporaryFileToPermanentStorage(moveTemporaryFileToPermanentStorageRequestV2) - /** - * Asks Sipi to delete a temporary file. - * - * @param deleteTemporaryFileRequestV2 the request. - * @return a [[SuccessResponseV2]]. - */ - override def deleteTemporaryFile(deleteTemporaryFileRequestV2: DeleteTemporaryFileRequest): Task[SuccessResponseV2] = - sipiService.deleteTemporaryFile(deleteTemporaryFileRequestV2) - /** * Asks Sipi for a text file used internally by Knora. * diff --git a/sipi/scripts/delete_temp_file.lua b/sipi/scripts/delete_temp_file.lua deleted file mode 100644 index 28f17050da..0000000000 --- a/sipi/scripts/delete_temp_file.lua +++ /dev/null @@ -1,122 +0,0 @@ --- * Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. --- * SPDX-License-Identifier: Apache-2.0 - --- --- Deletes a file from temporary storage. --- - -require "send_response" -require "authentication" - --- Buffer the response (helps with error handling). - -local success, error_msg = server.setBuffer() - -if not success then - send_error(500, "server.setBuffer() failed: " .. error_msg) - return -end - --- Check that this request is really from Knora and that the user has permission --- to delete the file. - -local token = auth_get_jwt_decoded() - -if token == nil then - return -end - -local knora_data = token["knora-data"] - -if knora_data == nil then - send_error(403, "No knora-data in token") - return -end - -if knora_data["permission"] ~= "DeleteTempFile" then - send_error(403, "Token does not grant permission to delete tmp file") - return -end - -local token_filename = knora_data["filename"] - -if token_filename == nil then - send_error(401, "Token does not specify a filename") - return -end - --- Parse the URL to get the filename to be deleted. - -local last_slash_pos = server.uri:match(".*/()") - -if last_slash_pos == nil then - send_error(400, "Invalid path: " .. server.uri) - return -end - -local filename = server.uri:sub(last_slash_pos) - -if filename:len() == 0 or filename == "." or filename == ".." then - send_error(400, "Invalid filename: " .. filename) - return -end - --- Check that the filename matches the one in the token. - -if filename ~= token_filename then - send_error(401, "Incorrect filename in token") - return -end - --- Check that the file exists in the temp directory. - -local hashed_filename -success, hashed_filename = helper.filename_hash(filename) - -if not success then - send_error(500, "helper.filename_hash() failed: " .. hashed_filename) - return -end - -local temp_file_path = config.imgroot .. '/tmp/' .. hashed_filename - -local exists -success, exists = server.fs.exists(temp_file_path) -if not success then - send_error(500, "server.fs.exists() failed: " .. exists) - return -end - -if not exists then - send_error(404, filename .. " not found") - return -end - -local filetype -success, filetype = server.fs.ftype(temp_file_path) -if not success then - send_error(500, "server.fs.ftype() failed: " .. filetype) - return -end - -if filetype ~= "FILE" then - send_error(400, filename .. " is not a file") - return -end - --- Delete the file. - -local errmsg -success, errmsg = server.fs.unlink(temp_file_path) -if not success then - send_error(500, "server.fs.unlink() failed: " .. errmsg) - return -end - -server.log("delete_temp_file.lua: deleted " .. temp_file_path, server.loglevel.LOG_DEBUG) - -local result = { - status = 0 -} - -send_success(result) diff --git a/webapi/src/main/scala/org/knora/webapi/messages/store/sipimessages/SipiMessages.scala b/webapi/src/main/scala/org/knora/webapi/messages/store/sipimessages/SipiMessages.scala index 2c3b620117..97589ad82b 100644 --- a/webapi/src/main/scala/org/knora/webapi/messages/store/sipimessages/SipiMessages.scala +++ b/webapi/src/main/scala/org/knora/webapi/messages/store/sipimessages/SipiMessages.scala @@ -30,14 +30,6 @@ sealed trait SipiRequest extends IIIFRequest case class MoveTemporaryFileToPermanentStorageRequest(internalFilename: String, prefix: String, requestingUser: User) extends SipiRequest -/** - * Asks Sipi to delete a temporary file. - * - * @param internalFilename the name of the file. - * @param requestingUser the user making the request. - */ -case class DeleteTemporaryFileRequest(internalFilename: String, requestingUser: User) extends SipiRequest - /** * Asks Sipi for a text file. Currently only for UTF8 encoded text files. * diff --git a/webapi/src/main/scala/org/knora/webapi/store/iiif/IIIFRequestMessageHandler.scala b/webapi/src/main/scala/org/knora/webapi/store/iiif/IIIFRequestMessageHandler.scala index 3364ea90ca..919ef1c6ca 100644 --- a/webapi/src/main/scala/org/knora/webapi/store/iiif/IIIFRequestMessageHandler.scala +++ b/webapi/src/main/scala/org/knora/webapi/store/iiif/IIIFRequestMessageHandler.scala @@ -10,7 +10,6 @@ import zio.* import org.knora.webapi.core.MessageHandler import org.knora.webapi.core.MessageRelay import org.knora.webapi.messages.ResponderRequest -import org.knora.webapi.messages.store.sipimessages.DeleteTemporaryFileRequest import org.knora.webapi.messages.store.sipimessages.IIIFRequest import org.knora.webapi.messages.store.sipimessages.MoveTemporaryFileToPermanentStorageRequest import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileRequest @@ -25,7 +24,6 @@ final case class IIIFRequestMessageHandlerLive(iiifService: SipiService) extends override def handle(message: ResponderRequest): Task[Any] = message match { case req: MoveTemporaryFileToPermanentStorageRequest => iiifService.moveTemporaryFileToPermanentStorage(req) - case req: DeleteTemporaryFileRequest => iiifService.deleteTemporaryFile(req) case req: SipiGetTextFileRequest => iiifService.getTextFileRequest(req) case other => ZIO.logError(s"IIIFServiceManager received an unexpected message: $other") } diff --git a/webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala b/webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala index 00c00f35e6..b7230669db 100644 --- a/webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala +++ b/webapi/src/main/scala/org/knora/webapi/store/iiif/api/SipiService.scala @@ -80,14 +80,6 @@ trait SipiService { moveTemporaryFileToPermanentStorageRequestV2: MoveTemporaryFileToPermanentStorageRequest, ): Task[SuccessResponseV2] - /** - * Asks Sipi to delete a temporary file. - * - * @param deleteTemporaryFileRequestV2 the request. - * @return a [[SuccessResponseV2]]. - */ - def deleteTemporaryFile(deleteTemporaryFileRequestV2: DeleteTemporaryFileRequest): Task[SuccessResponseV2] - /** * Asks Sipi for a text file used internally by Knora. * diff --git a/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala b/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala index ab2d966bb9..5b5797fe0d 100644 --- a/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala +++ b/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceLive.scala @@ -10,7 +10,6 @@ import sttp.client3 import sttp.client3.* import sttp.client3.SttpBackend import sttp.client3.httpclient.zio.HttpClientZioBackend -import sttp.model.Uri import zio.* import zio.json.ast.Json import zio.nio.file.Path @@ -97,32 +96,6 @@ final case class SipiServiceLive( } yield SuccessResponseV2("Moved file to permanent storage.") } - /** - * Asks Sipi to delete a temporary file. - * - * @param deleteTemporaryFileRequestV2 the request. - * @return a [[SuccessResponseV2]]. - */ - def deleteTemporaryFile(deleteTemporaryFileRequestV2: DeleteTemporaryFileRequest): Task[SuccessResponseV2] = { - val deleteRequestContent = - Map( - "knora-data" -> Json.Obj( - "permission" -> Json.Str("DeleteTempFile"), - "filename" -> Json.Str(deleteTemporaryFileRequestV2.internalFilename), - ), - ) - - val url: String => Uri = s => - uri"${sipiConfig.internalBaseUrl}/${sipiConfig.deleteTempFileRoute}/${deleteTemporaryFileRequestV2.internalFilename}?token=${s}" - - val user = deleteTemporaryFileRequestV2.requestingUser - for { - scope <- scopeResolver.resolve(user) - token <- jwtService.createJwt(user.userIri, scope, deleteRequestContent) - _ <- doSipiRequest(quickRequest.delete(url(token.jwtString))) - } yield SuccessResponseV2("Deleted temporary file.") - } - /** * Asks Sipi for a text file used internally by Knora. * diff --git a/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceMock.scala b/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceMock.scala index 010ee3d6c3..5dd3a47717 100644 --- a/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceMock.scala +++ b/webapi/src/main/scala/org/knora/webapi/store/iiif/impl/SipiServiceMock.scala @@ -57,13 +57,6 @@ case class SipiServiceMock(ref: Ref[Map[SipiMockMethodName, Task[Object]]]) exte ZIO.succeed(SuccessResponseV2("Moved file to permanent storage")) } - def deleteTemporaryFile(deleteTemporaryFileRequestV2: DeleteTemporaryFileRequest): Task[SuccessResponseV2] = - if (deleteTemporaryFileRequestV2.internalFilename == FAILURE_FILENAME) { - ZIO.fail(SipiException("Sipi failed to delete temporary file")) - } else { - ZIO.succeed(SuccessResponseV2("Deleted temporary file")) - } - override def getTextFileRequest(textFileRequest: SipiGetTextFileRequest): Task[SipiGetTextFileResponse] = getReturnValue(GetTextFileRequest) diff --git a/webapi/src/test/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueContentV2Spec.scala b/webapi/src/test/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueContentV2Spec.scala index 9622deccaa..a2f1543a9d 100644 --- a/webapi/src/test/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueContentV2Spec.scala +++ b/webapi/src/test/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueContentV2Spec.scala @@ -13,7 +13,6 @@ import zio.test.Spec import zio.test.ZIOSpecDefault import zio.test.assertTrue -import org.knora.webapi.messages.store.sipimessages.DeleteTemporaryFileRequest import org.knora.webapi.messages.store.sipimessages.MoveTemporaryFileToPermanentStorageRequest import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileRequest import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileResponse @@ -60,10 +59,6 @@ object ValueContentV2Spec extends ZIOSpecDefault { moveTemporaryFileToPermanentStorageRequestV2: MoveTemporaryFileToPermanentStorageRequest, ): Task[SuccessResponseV2] = ZIO.dieMessage("unsupported operation") - def deleteTemporaryFile( - deleteTemporaryFileRequestV2: DeleteTemporaryFileRequest, - ): Task[SuccessResponseV2] = - ZIO.dieMessage("unsupported operation") def getTextFileRequest(textFileRequest: SipiGetTextFileRequest): Task[SipiGetTextFileResponse] = ZIO.dieMessage("unsupported operation") def downloadAsset(asset: Asset, targetDir: Path, user: User): Task[Option[Path]] =