From 7fb69448e14ede30a372045210efe078469328d1 Mon Sep 17 00:00:00 2001 From: VimleshGupta Date: Tue, 9 Apr 2024 18:14:52 +0100 Subject: [PATCH 1/3] Clean the root directory before downloading the file from s3 bucket --- .../nationalarchives/draftmetadatavalidator/Lambda.scala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala index df6cfcd..f9fe858 100644 --- a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala +++ b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala @@ -26,12 +26,14 @@ import uk.gov.nationalarchives.tdr.GraphQLClient import uk.gov.nationalarchives.tdr.keycloak.{KeycloakUtils, TdrKeycloakDeployment} import uk.gov.nationalarchives.tdr.validation.Metadata +import java.io.File import java.net.URI import java.sql.Timestamp import java.time.LocalDate import java.time.format.DateTimeFormatter import java.util.UUID import scala.concurrent.ExecutionContext.Implicits.global +import scala.reflect.io.Directory class Lambda { @@ -49,8 +51,8 @@ class Lambda { def handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = { val pathParam = event.getPathParameters + cleanupRootDirectory() val s3Files = S3Files(S3Utils(s3Async(s3Endpoint))) - for { draftMetadata <- IO(DraftMetadata(UUID.fromString(pathParam.get("consignmentId")))) _ <- s3Files.downloadFile(bucket, draftMetadata) @@ -138,6 +140,11 @@ class Lambda { filteredMetadata.map(_.name) } + private def cleanupRootDirectory() = { + val directory = new Directory(new File(rootDirectory)) + directory.deleteRecursively() + } + implicit class AttributeHelper(attribute: Option[DisplayProperties.Attributes]) { def getBoolean: Boolean = { From 0e99d4aa6fafac437b46da6edbaa51e09bbd3081 Mon Sep 17 00:00:00 2001 From: VimleshGupta Date: Thu, 11 Apr 2024 13:12:52 +0100 Subject: [PATCH 2/3] PR feedback --- .../draftmetadatavalidator/Lambda.scala | 8 ++------ .../draftmetadatavalidator/S3Files.scala | 12 +++++++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala index f9fe858..9e8aaf5 100644 --- a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala +++ b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala @@ -51,7 +51,6 @@ class Lambda { def handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = { val pathParam = event.getPathParameters - cleanupRootDirectory() val s3Files = S3Files(S3Utils(s3Async(s3Endpoint))) for { draftMetadata <- IO(DraftMetadata(UUID.fromString(pathParam.get("consignmentId")))) @@ -140,11 +139,6 @@ class Lambda { filteredMetadata.map(_.name) } - private def cleanupRootDirectory() = { - val directory = new Directory(new File(rootDirectory)) - directory.deleteRecursively() - } - implicit class AttributeHelper(attribute: Option[DisplayProperties.Attributes]) { def getBoolean: Boolean = { @@ -159,4 +153,6 @@ class Lambda { object Lambda { case class DraftMetadata(consignmentId: UUID) def getFilePath(draftMetadata: DraftMetadata) = s"""${rootDirectory}/${draftMetadata.consignmentId}/$fileName""" + + def getFolderPath(draftMetadata: DraftMetadata) = s"""${rootDirectory}/${draftMetadata.consignmentId}""" } diff --git a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/S3Files.scala b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/S3Files.scala index 41c8acd..3822987 100644 --- a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/S3Files.scala +++ b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/S3Files.scala @@ -5,16 +5,19 @@ import cats.implicits.catsSyntaxOptionId import org.typelevel.log4cats.SelfAwareStructuredLogger import uk.gov.nationalarchives.aws.utils.s3.S3Utils import uk.gov.nationalarchives.draftmetadatavalidator.ApplicationConfig.fileName -import uk.gov.nationalarchives.draftmetadatavalidator.Lambda.{DraftMetadata, getFilePath} +import uk.gov.nationalarchives.draftmetadatavalidator.Lambda.{DraftMetadata, getFilePath, getFolderPath} import java.io.File import java.nio.file.Paths +import scala.reflect.io.Directory class S3Files(s3Utils: S3Utils)(implicit val logger: SelfAwareStructuredLogger[IO]) { def key(draftMetadata: DraftMetadata) = s"${draftMetadata.consignmentId}/$fileName" def downloadFile(bucket: String, draftMetadata: DraftMetadata): IO[Any] = { + + cleanup(getFolderPath(draftMetadata)) val filePath = getFilePath(draftMetadata) if (new File(filePath).exists()) { logger.info("") @@ -29,6 +32,13 @@ class S3Files(s3Utils: S3Utils)(implicit val logger: SelfAwareStructuredLogger[I def uploadFile(bucket: String, draftMetadata: DraftMetadata): IO[Unit] = for { _ <- s3Utils.upload(bucket, key(draftMetadata), Paths.get(getFilePath(draftMetadata))) } yield () + + private def cleanup(path: String): Unit = { + val directory = new Directory(new File(path)) + if (directory.exists) { + directory.deleteRecursively() + } + } } object S3Files { From c18972ed73a34ca726b63bff141131cef8f7fc69 Mon Sep 17 00:00:00 2001 From: VimleshGupta Date: Thu, 11 Apr 2024 14:08:26 +0100 Subject: [PATCH 3/3] Removed unused comments --- .../uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala index 9e8aaf5..2743f22 100644 --- a/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala +++ b/src/main/scala/uk/gov/nationalarchives/draftmetadatavalidator/Lambda.scala @@ -26,14 +26,12 @@ import uk.gov.nationalarchives.tdr.GraphQLClient import uk.gov.nationalarchives.tdr.keycloak.{KeycloakUtils, TdrKeycloakDeployment} import uk.gov.nationalarchives.tdr.validation.Metadata -import java.io.File import java.net.URI import java.sql.Timestamp import java.time.LocalDate import java.time.format.DateTimeFormatter import java.util.UUID import scala.concurrent.ExecutionContext.Implicits.global -import scala.reflect.io.Directory class Lambda {