<commit_before>/*
 * Part of GDL book_api.
 * Copyright (C) 2017 Global Digital Library
 *
 * See LICENSE
 */

package io.digitallibrary.bookapi.service

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStream}

import com.amazonaws.services.s3.model.{GetObjectRequest, ObjectMetadata, PutObjectRequest, S3Object}
import com.openhtmltopdf.extend.FSSupplier
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder
import com.typesafe.scalalogging.LazyLogging
import io.digitallibrary.bookapi.BookApiProperties
import io.digitallibrary.bookapi.integration.AmazonClient
import io.digitallibrary.bookapi.model.api.{Chapter, NotFoundException, PdfStream}
import io.digitallibrary.bookapi.model.domain.{BookFormat, PdfCss}
import io.digitallibrary.bookapi.repository.{BookRepository, TranslationRepository}
import io.digitallibrary.language.model.LanguageTag
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Entities.EscapeMode

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


trait PdfService {
  this: TranslationRepository with BookRepository with TranslationRepository with ReadService with AmazonClient =>
  val pdfService: PdfService

  class PdfService extends LazyLogging {

    case class S3Pdf(s3Object: S3Object, fileName: String) extends PdfStream {
      override def stream: InputStream = {
        s3Object.getObjectContent
      }
    }

    case class RBPdf(pdfRendererBuilder: PdfRendererBuilder, fileName: String) extends PdfStream {
      override def stream: InputStream = {
        val out = new ByteArrayOutputStream()
        pdfRendererBuilder.toStream(out).run()
        new ByteArrayInputStream(out.toByteArray)
      }
    }

    case class FontDefinition(fontFile: String, fontName: String)

    class NotoFontSupplier(stream: InputStream) extends FSSupplier[InputStream] {
      override def supply(): InputStream = stream
    }

    val DefaultFont = FontDefinition("/NotoSans-Regular.ttf", "Noto Sans")
    val fonts = Map(
      "ethiopic" -> FontDefinition("/NotoSansEthiopic.ttf", "Noto Sans Ethiopic"),
      "devangari" -> FontDefinition("/NotoSansDevanagari-Regular.ttf", "Noto Sans Devanagari"),
      "bengali" -> FontDefinition("/NotoSansBengali-Regular.ttf", "Noto Sans Bengali"),
      "khmer" -> FontDefinition("/NotoSansKhmer-Regular.ttf", "Noto Sans Khmer"),
      "tamil" -> FontDefinition("/NotoSansTamil-Regular.ttf", "Noto Sans Tamil"),
      "arabic" -> FontDefinition("/NotoKufiArabic-Regular.ttf", "Noto Kufi Arabic"),
    )
    val fontDefinitions = Map(
      LanguageTag("tir-et") -> fonts("ethiopic"),
      LanguageTag("tir") -> fonts("ethiopic"),
      LanguageTag("amh") -> fonts("ethiopic"),
      LanguageTag("mar") -> fonts("devangari"),
      LanguageTag("hin") -> fonts("devangari"),
      LanguageTag("ben") -> fonts("bengali"),
      LanguageTag("nep") -> fonts("devangari"),
      LanguageTag("khm") -> fonts("khmer"),
      LanguageTag("tam") -> fonts("tamil"),
      LanguageTag("ar-ae") -> fonts("arabic"),
      LanguageTag("ar") -> fonts("arabic")
    )

    def getPdf(language: LanguageTag, uuid: String): Option[PdfStream] = {
      unFlaggedTranslationsRepository.withUuId(uuid) match {
        case Some(translation) => translation.bookFormat match {
          case BookFormat.HTML =>
            Some(RBPdf(createPdf(language, uuid).get, s"${translation.title}.pdf"))
          case BookFormat.PDF => getFromS3(uuid) match {<commit_msg>#459: Removed incorrect comma GlobalDigitalLibraryio/issues#459<commit_after>/*
 * Part of GDL book_api.
 * Copyright (C) 2017 Global Digital Library
 *
 * See LICENSE
 */

package io.digitallibrary.bookapi.service

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStream}

import com.amazonaws.services.s3.model.{GetObjectRequest, ObjectMetadata, PutObjectRequest, S3Object}
import com.openhtmltopdf.extend.FSSupplier
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder
import com.typesafe.scalalogging.LazyLogging
import io.digitallibrary.bookapi.BookApiProperties
import io.digitallibrary.bookapi.integration.AmazonClient
import io.digitallibrary.bookapi.model.api.{Chapter, NotFoundException, PdfStream}
import io.digitallibrary.bookapi.model.domain.{BookFormat, PdfCss}
import io.digitallibrary.bookapi.repository.{BookRepository, TranslationRepository}
import io.digitallibrary.language.model.LanguageTag
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Entities.EscapeMode

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


trait PdfService {
  this: TranslationRepository with BookRepository with TranslationRepository with ReadService with AmazonClient =>
  val pdfService: PdfService

  class PdfService extends LazyLogging {

    case class S3Pdf(s3Object: S3Object, fileName: String) extends PdfStream {
      override def stream: InputStream = {
        s3Object.getObjectContent
      }
    }

    case class RBPdf(pdfRendererBuilder: PdfRendererBuilder, fileName: String) extends PdfStream {
      override def stream: InputStream = {
        val out = new ByteArrayOutputStream()
        pdfRendererBuilder.toStream(out).run()
        new ByteArrayInputStream(out.toByteArray)
      }
    }

    case class FontDefinition(fontFile: String, fontName: String)

    class NotoFontSupplier(stream: InputStream) extends FSSupplier[InputStream] {
      override def supply(): InputStream = stream
    }

    val DefaultFont = FontDefinition("/NotoSans-Regular.ttf", "Noto Sans")
    val fonts = Map(
      "ethiopic" -> FontDefinition("/NotoSansEthiopic.ttf", "Noto Sans Ethiopic"),
      "devangari" -> FontDefinition("/NotoSansDevanagari-Regular.ttf", "Noto Sans Devanagari"),
      "bengali" -> FontDefinition("/NotoSansBengali-Regular.ttf", "Noto Sans Bengali"),
      "khmer" -> FontDefinition("/NotoSansKhmer-Regular.ttf", "Noto Sans Khmer"),
      "tamil" -> FontDefinition("/NotoSansTamil-Regular.ttf", "Noto Sans Tamil"),
      "arabic" -> FontDefinition("/NotoKufiArabic-Regular.ttf", "Noto Kufi Arabic")
    )
    val fontDefinitions = Map(
      LanguageTag("tir-et") -> fonts("ethiopic"),
      LanguageTag("tir") -> fonts("ethiopic"),
      LanguageTag("amh") -> fonts("ethiopic"),
      LanguageTag("mar") -> fonts("devangari"),
      LanguageTag("hin") -> fonts("devangari"),
      LanguageTag("ben") -> fonts("bengali"),
      LanguageTag("nep") -> fonts("devangari"),
      LanguageTag("khm") -> fonts("khmer"),
      LanguageTag("tam") -> fonts("tamil"),
      LanguageTag("ar-ae") -> fonts("arabic"),
      LanguageTag("ar") -> fonts("arabic")
    )

    def getPdf(language: LanguageTag, uuid: String): Option[PdfStream] = {
      unFlaggedTranslationsRepository.withUuId(uuid) match {
        case Some(translation) => translation.bookFormat match {
          case BookFormat.HTML =>
            Some(RBPdf(createPdf(language, uuid).get, s"${translation.title}.pdf"))
          case BookFormat.PDF => getFromS3(uuid) match {
{'fraction_non_alphanumeric': 0.12192313228731827, 'fraction_numerical': 0.004030516769828703, 'mean_word_length': 4.30381679389313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890137', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2090, 'n_words': 418}