Skip to content

Commit

Permalink
add new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ssedoudbgouv committed Jul 13, 2024
1 parent 70f00b8 commit 5ddce38
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Build, format & test workflow

on: [push]
env:
USER: signalconso
USE_TEXT_LOGS: true

jobs:
# This workflow contains a single job called "greet"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
#name: Build, format & test workflow
#
#on: [push]
#env:
# USER: signalconso
# USE_TEXT_LOGS: true
#
#jobs:
# # This workflow contains a single job called "greet"
# build:
# # The type of runner that the job will run on
# runs-on: ubuntu-latest
# services:
# postgres:
# image: postgres:14
Expand All @@ -25,21 +25,21 @@ jobs:
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5

# Steps represent a sequence of tasks that will be
# executed as part of the job
steps:
- uses: actions/checkout@v3
- name: Setup JDK
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17
cache: 'sbt'

- run: sudo apt-get update
#
# # Steps represent a sequence of tasks that will be
# # executed as part of the job
# steps:
# - uses: actions/checkout@v3
# - name: Setup JDK
# uses: actions/setup-java@v3
# with:
# distribution: temurin
# java-version: 17
# cache: 'sbt'
#
# - run: sudo apt-get update
# - run: sudo apt-get install postgresql-client
# - run: whoami
# - run: psql -d postgresql://signalconso@localhost/test_signalconso -p 5432 -c "CREATE EXTENSION pg_trgm;"
- name: Build and Test
run: sbt scalafmtCheckAll scalafmtSbtCheck -v +test
# - name: Build and Test
# run: sbt scalafmtCheckAll scalafmtSbtCheck -v +test
13 changes: 12 additions & 1 deletion app/controllers/AntivirusController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import controllers.error.ApiError.FileNameTooLong
import controllers.error.ApiError.MalformedFileKey
import controllers.error.ApiError.MissingExternalId
import controllers.error.AppErrorTransformer.handleError
import models.ScanCommand
import play.api.Logger
import play.api.libs.Files
import play.api.libs.json.JsError
Expand Down Expand Up @@ -47,12 +48,22 @@ class AntivirusController(
.liftTo[Future](MissingExternalId)
fileName = filePart.filename
file = pathFromFilePart(filePart)
_ <- antivirusService.scanAndSave(externalId, fileName, file)
_ <- antivirusService.scanFromFile(externalId, fileName, file)
} yield NoContent

app.recover { case err => handleError(request, err) }
}

def scan() =
SecuredAction.async(parse.json) { request =>
request.body
.validate[ScanCommand]
.fold(
errors => Future.successful(BadRequest(JsError.toJson(errors))),
results => antivirusService.scan(results).map(_ => NoContent)
)
}

def rescan() =
SecuredAction.async(parse.json) { request =>
request.body
Expand Down
13 changes: 13 additions & 0 deletions app/models/ScanCommand.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

import play.api.libs.json._

import java.time.OffsetDateTime

case class ScanCommand(
externalId: String,
filename: String
)
object ScanCommand {
implicit val fileFormat: OFormat[ScanCommand] = Json.format[ScanCommand]
}
46 changes: 31 additions & 15 deletions app/service/AntivirusService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,45 @@ import actors.AntivirusScanActor
import actors.antivirus.AntivirusScanExitCode
import cats.implicits.catsSyntaxOption
import controllers.error.ApiError.UnknownExternalId
import models.FileData
import models.FileId
import models.{FileData, FileId, ScanCommand}
import org.apache.pekko.actor.typed.ActorRef
import play.api.Logger
import repositories.FileDataRepositoryInterface

import java.time.OffsetDateTime
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.{ExecutionContext, Future}

class AntivirusService(
antivirusScanActor: ActorRef[AntivirusScanActor.ScanCommand],
fileDataRepository: FileDataRepositoryInterface
)(implicit val executionContext: ExecutionContext) {
val logger = Logger(this.getClass)

def scanAndSave(externalId: String, filename: String, file: java.io.File): Future[FileData] =
def scanFromFile(externalId: String, filename: String, file: java.io.File): Future[FileData] =
for {
fileData <- fileDataRepository.create(
FileData(
FileId.generateId(),
externalId = externalId,
creationDate = OffsetDateTime.now(),
filename = filename,
scanResult = None,
avOutput = None
)
fileData <- createFileData(
externalId = externalId,
filename = filename
)
_ = logger.debug(s"Uploaded file ${fileData.id} to S3")
_ = logger.debug(s"Scheduling scan for ${fileData.filename}")
} yield {
antivirusScanActor ! AntivirusScanActor.ScanFromFile(fileData, file)
fileData
}

def scan(scanCommand: ScanCommand): Future[FileData] =
for {
fileData <-
createFileData(
externalId = scanCommand.externalId,
filename = scanCommand.filename
)
_ = logger.debug(s"Scheduling scan for ${fileData.filename}")
} yield {
antivirusScanActor ! AntivirusScanActor.ScanFromBucket(fileData)
fileData
}

def reScanFile(fileExternalIds: List[String]) = fileDataRepository
.getByExternalId(fileExternalIds)
.map { files =>
Expand All @@ -46,6 +51,17 @@ class AntivirusService(
.map(file => antivirusScanActor ! AntivirusScanActor.ScanFromBucket(file))
}

private def createFileData(externalId: String, filename: String): Future[FileData] = fileDataRepository.create(
FileData(
FileId.generateId(),
externalId = externalId,
creationDate = OffsetDateTime.now(),
filename = filename,
scanResult = None,
avOutput = None
)
)

def fileStatus(externalFileId: String): Future[FileData] =
for {
fileDataOpt <- fileDataRepository.getByExternalId(List(externalFileId)).map(_.headOption)
Expand Down
3 changes: 2 additions & 1 deletion conf/routes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

POST /api/file/scan controllers.AntivirusController.scanAndUpload()
POST /api/file/scanFile controllers.AntivirusController.scanAndUpload()
POST /api/file/scan controllers.AntivirusController.scan()
POST /api/file/rescan controllers.AntivirusController.rescan()
GET /api/file/:externalFileId controllers.AntivirusController.fileStatus(externalFileId :String)

0 comments on commit 5ddce38

Please sign in to comment.