Skip to content

Commit

Permalink
Merge pull request #11 from nationalarchives/Clean-the-root-directory
Browse files Browse the repository at this point in the history
Clean the root directory before downloading the file from s3 bucket
  • Loading branch information
vimleshtna authored Apr 11, 2024
2 parents 9a61d68 + c18972e commit e7a6468
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Lambda {
val pathParam = event.getPathParameters

val s3Files = S3Files(S3Utils(s3Async(s3Endpoint)))

for {
draftMetadata <- IO(DraftMetadata(UUID.fromString(pathParam.get("consignmentId"))))
_ <- s3Files.downloadFile(bucket, draftMetadata)
Expand Down Expand Up @@ -152,4 +151,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}"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand All @@ -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 {
Expand Down

0 comments on commit e7a6468

Please sign in to comment.