BinaryHandler.scala 2.26 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Record Deleter
 * Copyright (C) 2021  Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package ch.memobase

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
21
22
23
24
25
26
import ch.memobase.models.{
  ProcessedRecord,
  ProcessingFatal,
  ProcessingIgnore,
  ProcessingSuccess
}
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
28
29
30
31
32
33
34
35
36
37
import org.apache.logging.log4j.scala.Logging

import java.io.File

/**
  * Handles the deletion of binaries
  */
trait BinaryHandler {
  self: Logging with AppSettings =>

  def deleteBinaries(record: ProcessedRecord): ProcessedRecord = {
38
39
40
41
    if (record.binaries.nonEmpty) {
      val result = record.binaries
        .map(_.replaceFirst("file://", ""))
        .map(fileName => (new File(fileName), fileName))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
42
43
44
        .map(file =>
          (if (record.dryRun) file._1.exists else file._1.delete, file._2)
        )
45
46
        .foldLeft[List[String]](List())((errors, x) =>
          if (!x._1) {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
47
            logger.warn(s"Deletion of file ${x._2} failed${if (record.dryRun) " (dry-run mode)" else ""}!")
48
49
50
51
52
53
            errors :+ s"${x._2} deletion failed"
          } else {
            errors
          }
        )
      if (result.isEmpty) {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
54
        logger.debug(
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
55
          s"Deletion of binaries for record ${record.recordId} successful${if (record.dryRun) " (dry-run mode)" else ""}"
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
56
        )
57
58
59
60
61
        record.setBinariesStatus(
          ProcessingSuccess,
          "Binaries deletion successful"
        )
      } else {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
62
        logger.warn(
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
63
          s"Deletion of binaries for record ${record.recordId} failed${if (record.dryRun) " (dry-run mode)" else ""}"
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
64
        )
65
66
        record.setBinariesStatus(ProcessingFatal, result.mkString("; "))
      }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
67
    } else {
68
      record.setBinariesStatus(ProcessingIgnore, "No attached local binaries")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
69
70
71
    }
  }
}