Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supprime le code qui gérait les anciens fichiers #2131

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1276,7 +1276,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 @@ -1322,7 +1322,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 @@ -1374,9 +1374,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 @@ -1392,14 +1392,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
Loading