aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala
blob: d50be2ce40c4d77f73022bca45fc1d26fc86f0f2 (plain) (blame)
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
package xyz.driver.pdsuidomain.storage

import com.google.cloud.storage.StorageOptions
import com.typesafe.scalalogging.StrictLogging
import xyz.driver.pdsuidomain.entities.MedicalRecord.PdfSource

import scala.concurrent.{ExecutionContext, Future, blocking}

object MedicalRecordDocumentStorage extends StrictLogging {
  private val storage = StorageOptions.getDefaultInstance.getService

  def fetchPdf(bucket: String, path: String)
              (implicit ec: ExecutionContext): Future[PdfSource] = {
    logger.trace(s"fetchPdf(bucket=$bucket, path=$path)")
    Future {
      blocking {
        Option(storage.get(bucket, path)) match {
          case Some(blob) =>
            PdfSource.Channel(() => blob.reader())
          case None =>
            logger.error(s"Failed to find the pdf file $path in bucket: $bucket")
            PdfSource.Empty
        }
      }
    }
  }
}