RecordProcessor.scala 3.38 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Extracts media files from Fedora repository
 * Copyright (C) 2020  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 org.memobase


import org.apache.kafka.clients.consumer.ConsumerRecord

import scala.util.{Failure, Success, Try}

trait ProcessOutcome

case class ProcessSuccess(id: String, fileType: MediaFileType, action: String) extends ProcessOutcome

case class ProcessFailure(id: String, fileType: MediaFileType, action: String, ex: Throwable) extends ProcessOutcome

case class ProcessIgnore(id: String, reason: String, warn: Boolean = false) extends ProcessOutcome

trait ProcessError

case object Process


class RecordProcessor(fh: FileHandler, ah: ApiHandler) {

  private def errorHandler(fun: Try[_], objId: String, fileType: MediaFileType, action: String): ProcessOutcome = fun match {
    case Success(_) => ProcessSuccess(objId, fileType, action)
    case Failure(ex) => ProcessFailure(objId, fileType, action, ex)
  }

  def process(record: ConsumerRecord[String, String]): ProcessOutcome = {
    val msg = KafkaMessageLoader.load(record.value())
    if (msg.isBinaryObject) {
      ah.getFile(msg.objectPath) match {
        case Success(res) =>
          res.fileType match {
            case fT: AudioFile =>
              msg.eventType match {
                case action@("Create" | "Update") =>
                  errorHandler(fh.copyAudio(res.data.array, msg.objectId), msg.objectId, fT, action)
                case action@"Delete" =>
                  errorHandler(fh.deleteAudio(msg.objectId), msg.objectId, fT, action)
              }
            case fT: ImageFile =>
              msg.eventType match {
                case action@("Create" | "Update") =>
                  errorHandler(fh.copyImage(res.data.array, msg.objectId), msg.objectId, fT, action)
                case action@"Delete" =>
                  errorHandler(fh.deleteImage(msg.objectId), msg.objectId, fT, action)
              }
            case fT: VideoFile =>
              msg.eventType match {
                case action@("Create" | "Update") =>
                  errorHandler(fh.copyVideo(res.data.array, msg.objectId), msg.objectId, fT, action)
                case action@"Delete" =>
                  errorHandler(fh.deleteVideo(msg.objectId), msg.objectId, fT, action)
              }
            case OtherFile(fT) =>
              ProcessIgnore(msg.objectId, s"files of type $fT cannot be handled", warn = true)
            case UnknownFile =>
              ProcessIgnore(msg.objectId, "no content type defined", warn = true)
          }
        case Failure(ex) => ProcessFailure(msg.objectId, UnknownFile, msg.eventType, ex)
      }
        // case et => logger.warn(s"event type $et is unknown: ignoring message")
    } else {
      ProcessIgnore(msg.objectId, "is no binary object")
    }
  }
}