Skip to content

Commit

Permalink
Supprime le code qui gérait les anciens fichiers (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
niladic authored Nov 20, 2024
1 parent c2ed5b8 commit ff4ab67
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 54 deletions.
20 changes: 6 additions & 14 deletions app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import helper.PlayFormHelpers.formErrorsLog
import helper.ScalatagsHelpers.writeableOf_Modifier
import helper.StringHelper.NonEmptyTrimmedString
import helper.TwirlImports.toHtml
import java.nio.file.{Files, Path}
import java.nio.file.Path
import java.time.{LocalDate, ZonedDateTime}
import java.util.UUID
import javax.inject.{Inject, Singleton}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ case class ApplicationController @Inject() (
IO.blocking(
eventService.log(EventType.FileNotFound, s"Le fichier $fileId n'existe pas")
).as(NotFound("Nous n'avons pas trouvé ce fichier"))
case Some((path, metadata)) =>
case Some(metadata) =>
val applicationId = metadata.attached match {
case FileMetadata.Attached.Application(id) => id
case FileMetadata.Attached.Answer(applicationId, _) => applicationId
Expand Down Expand Up @@ -1328,7 +1328,7 @@ case class ApplicationController @Inject() (
applicationId = applicationId.some
)
) >>
sendFile(path, metadata)
sendFile(metadata)
case FileMetadata.Status.Expired =>
IO.blocking(
eventService.log(
Expand Down Expand Up @@ -1380,9 +1380,9 @@ case class ApplicationController @Inject() (
.unsafeToFuture()
}

private def sendFile(localPath: Path, metadata: FileMetadata)(implicit
request: RequestWithUserData[_]
): IO[Result] = {
private def sendFile(
metadata: FileMetadata
)(implicit request: RequestWithUserData[_]): IO[Result] = {
val fileResult = (fileExists: Boolean) =>
IO(
if (fileExists)
Expand All @@ -1398,14 +1398,6 @@ case class ApplicationController @Inject() (
fileName = Some(metadata.filename)
).withHeaders(CACHE_CONTROL -> "no-store")
)
else if (Files.exists(localPath))
// TODO: this branch is legacy and should be removed
Ok.sendPath(
localPath,
`inline` = false,
fileName = (_: Path) => Some(metadata.filename)
).withHeaders(CACHE_CONTROL -> "no-store")
.asRight
else
NotFound("Nous n'avons pas trouvé ce fichier").asRight
)
Expand Down
11 changes: 0 additions & 11 deletions app/modules/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package modules

import com.typesafe.config.{Config, ConfigFactory}
import helper.{Crypto, UUIDHelper}
import java.nio.file.{Files, Path, Paths}
import java.util.UUID
import javax.inject.{Inject, Singleton}
import play.api.Configuration
Expand Down Expand Up @@ -39,16 +38,6 @@ class AppConfig @Inject() (configuration: Configuration) {
val featureAutoAddExpert: Boolean =
configuration.get[Boolean]("app.features.autoAddExpert")

val filesPath: String = configuration.get[String]("app.filesPath")

val filesDirectory: Path = {
val dir = Paths.get(filesPath)
if (!Files.isDirectory(dir)) {
val _ = Files.createDirectories(dir)
}
dir
}

val filesExpirationInDays: Int = configuration.get[Int]("app.filesExpirationInDays")

val filesOvhS3AccessKey: String = configuration.get[String]("app.filesOvhS3AccessKey")
Expand Down
32 changes: 4 additions & 28 deletions app/services/FileService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import helper.Crypto
import helper.StringHelper.normalizeNFKC
import io.laserdisc.pure.s3.tagless.{Interpreter => S3Interpreter}
import java.net.URI
import java.nio.file.{Files, Path => NioPath, Paths}
import java.nio.file.{Files, Path => NioPath}
import java.time.{Instant, ZonedDateTime}
import java.time.temporal.ChronoUnit.DAYS
import java.util.UUID
Expand Down Expand Up @@ -105,11 +105,6 @@ class FileService @Inject() (
result.map(_.map { case (_, metadata) => metadata }).value
}

def fileMetadata(fileId: UUID): IO[Either[Error, Option[(NioPath, FileMetadata)]]] =
byId(fileId).map(
_.map(_.map(metadata => (Paths.get(s"${config.filesPath}/$fileId"), metadata)))
)

/** This is the "official" way to check https://stackoverflow.com/a/56038360
*
* The AWS library throws software.amazon.awssdk.services.s3.model.NoSuchKeyException when the
Expand Down Expand Up @@ -273,7 +268,7 @@ class FileService @Inject() (

private val fieldsInSelect: String = tableFields.mkString(", ")

private def byId(fileId: UUID): IO[Either[Error, Option[FileMetadata]]] =
def fileMetadata(fileId: UUID): IO[Either[Error, Option[FileMetadata]]] =
IO.blocking(
db.withConnection { implicit connection =>
SQL(s"""SELECT $fieldsInSelect FROM file_metadata WHERE id = {fileId}::uuid""")
Expand Down Expand Up @@ -371,7 +366,7 @@ class FileService @Inject() (
s"Début de la suppression des fichiers avant $beforeDate"
)
) >>
deleteBefore(beforeDate).both(legacyDeleteExpiredFiles).map { case (result, _) => result }
deleteBefore(beforeDate)
}

private def deleteBefore(beforeDate: Instant): IO[Either[Error, Unit]] =
Expand All @@ -383,10 +378,7 @@ class FileService @Inject() (
Stream
.emits(files)
.evalMap { metadata =>
val delete = ovhS3.delete(bucket, s3fileName(metadata.id))
val deleteLegacy =
FsFiles[IO].deleteIfExists(FsPath(config.filesPath) / metadata.id.toString)
val deletion = delete.both(deleteLegacy) >>
val deletion = ovhS3.delete(bucket, s3fileName(metadata.id)) >>
updateStatus(metadata.id, FileMetadata.Status.Expired).flatMap(
_.fold(
e => IO.blocking(eventService.logErrorNoRequest(e)),
Expand All @@ -410,22 +402,6 @@ class FileService @Inject() (
)
)

private def legacyDeleteExpiredFiles = IO {
val dir = new java.io.File(config.filesPath)
if (dir.exists() && dir.isDirectory) {
val fileToDelete = dir
.listFiles()
.filter(_.isFile)
.filter { file =>
val instant = Files.getLastModifiedTime(file.toPath).toInstant
instant
.plus(config.filesExpirationInDays.toLong + 1, DAYS)
.isBefore(Instant.now())
}
fileToDelete.foreach(_.delete())
}
}

def wipeFilenames(retentionInMonths: Long): Future[Either[Error, Int]] =
Future(
Try(
Expand Down
1 change: 0 additions & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ app.passwordSessionDurationInSeconds = 2592000
app.passwordSessionDurationInSeconds = ${?APP_PASSWORD_SESSION_DURATION_IN_SECONDS}
app.passwordRecoveryTokenExpirationInMinutes = 15
app.passwordRecoveryTokenExpirationInMinutes = ${?PASSWORD_RECOVERY_TOKEN_EXPIRATION_IN_MINUTES}
app.filesPath = ${?FILES_PATH}
app.filesExpirationInDays = 7
app.filesExpirationInDays = ${?FILES_EXPIRATION_IN_DAYS}
app.filesOvhS3AccessKey = ${?FILES_OVH_S3_ACCESS_KEY}
Expand Down

0 comments on commit ff4ab67

Please sign in to comment.