aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon')
-rw-r--r--src/main/scala/xyz/driver/pdsuicommon/db/PostgresContext.scala72
-rw-r--r--src/main/scala/xyz/driver/pdsuicommon/pdf/PdfRenderer.scala12
-rw-r--r--src/main/scala/xyz/driver/pdsuicommon/pdf/WkHtmlToPdfRenderer.scala105
3 files changed, 0 insertions, 189 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/db/PostgresContext.scala b/src/main/scala/xyz/driver/pdsuicommon/db/PostgresContext.scala
deleted file mode 100644
index 0098a64..0000000
--- a/src/main/scala/xyz/driver/pdsuicommon/db/PostgresContext.scala
+++ /dev/null
@@ -1,72 +0,0 @@
-package xyz.driver.pdsuicommon.db
-
-import java.io.Closeable
-import java.time.{LocalDateTime, ZoneOffset}
-import java.util.UUID
-import java.util.concurrent.Executors
-import javax.sql.DataSource
-
-import io.getquill._
-import xyz.driver.pdsuicommon.concurrent.MdcExecutionContext
-import xyz.driver.pdsuicommon.db.PostgresContext.Settings
-import xyz.driver.pdsuicommon.domain.UuidId
-import xyz.driver.pdsuicommon.logging._
-
-import scala.concurrent.ExecutionContext
-import scala.util.control.NonFatal
-import scala.util.{Failure, Success, Try}
-
-object PostgresContext extends PhiLogging {
-
- final case class Settings(connection: com.typesafe.config.Config,
- connectionAttemptsOnStartup: Int,
- threadPoolSize: Int)
-
- def apply(settings: Settings): PostgresContext = {
- // Prevent leaking credentials to a log
- Try(JdbcContextConfig(settings.connection).dataSource) match {
- case Success(dataSource) => new PostgresContext(dataSource, settings)
- case Failure(NonFatal(e)) =>
- logger.error(phi"Can not load dataSource, error: ${Unsafe(e.getClass.getName)}")
- throw new IllegalArgumentException("Can not load dataSource from config. Check your database and config", e)
- }
- }
-
-}
-
-class PostgresContext(val dataSource: DataSource with Closeable, settings: Settings)
- extends PostgresJdbcContext[SnakeCase](dataSource) with TransactionalContext {
-
- private val tpe = Executors.newFixedThreadPool(settings.threadPoolSize)
-
- implicit val executionContext: ExecutionContext = {
- val orig = ExecutionContext.fromExecutor(tpe)
- MdcExecutionContext.from(orig)
- }
-
- override def close(): Unit = {
- super.close()
- tpe.shutdownNow()
- }
-
- /**
- * Usable for QueryBuilder's extractors
- */
- def timestampToLocalDateTime(timestamp: java.sql.Timestamp): LocalDateTime = {
- LocalDateTime.ofInstant(timestamp.toInstant, ZoneOffset.UTC)
- }
-
- implicit def encodeUuidId[T] = MappedEncoding[UuidId[T], String](_.toString)
- implicit def decodeUuidId[T] = MappedEncoding[String, UuidId[T]] { uuid =>
- UuidId[T](UUID.fromString(uuid))
- }
-
- def decodeOptUuidId[T] = MappedEncoding[Option[String], Option[UuidId[T]]] {
- case Some(x) => Option(x).map(y => UuidId[T](UUID.fromString(y)))
- case None => None
- }
-
- implicit def decodeUuid[T] = MappedEncoding[String, UUID] { uuid =>
- UUID.fromString(uuid)
- }
-}
diff --git a/src/main/scala/xyz/driver/pdsuicommon/pdf/PdfRenderer.scala b/src/main/scala/xyz/driver/pdsuicommon/pdf/PdfRenderer.scala
deleted file mode 100644
index 93be2cc..0000000
--- a/src/main/scala/xyz/driver/pdsuicommon/pdf/PdfRenderer.scala
+++ /dev/null
@@ -1,12 +0,0 @@
-package xyz.driver.pdsuicommon.pdf
-
-import java.nio.file.Path
-
-trait PdfRenderer {
-
- def render(html: String, documentName: String, force: Boolean = false): Path
-
- def delete(documentName: String): Unit
-
- def getPath(fileName: String): Path
-}
diff --git a/src/main/scala/xyz/driver/pdsuicommon/pdf/WkHtmlToPdfRenderer.scala b/src/main/scala/xyz/driver/pdsuicommon/pdf/WkHtmlToPdfRenderer.scala
deleted file mode 100644
index 8141b7f..0000000
--- a/src/main/scala/xyz/driver/pdsuicommon/pdf/WkHtmlToPdfRenderer.scala
+++ /dev/null
@@ -1,105 +0,0 @@
-package xyz.driver.pdsuicommon.pdf
-
-import java.io.IOException
-import java.nio.file._
-
-import io.github.cloudify.scala.spdf._
-import xyz.driver.pdsuicommon.logging._
-import xyz.driver.pdsuicommon.pdf.WkHtmlToPdfRenderer.Settings
-
-object WkHtmlToPdfRenderer {
-
- final case class Settings(downloadsDir: String) {
-
- lazy val downloadsPath: Path = getPathFrom(downloadsDir)
-
- private def getPathFrom(x: String): Path = {
- val dirPath =
- if (x.startsWith("/")) Paths.get(x)
- else {
- val workingDir = Paths.get(".")
- workingDir.resolve(x)
- }
-
- dirPath.toAbsolutePath.normalize()
- }
- }
-}
-
-class WkHtmlToPdfRenderer(settings: Settings) extends PdfRenderer with PhiLogging {
-
- private val pdf = Pdf(new PdfConfig {
- disableJavascript := true
- disableExternalLinks := true
- disableInternalLinks := true
- printMediaType := Some(true)
- orientation := Portrait
- pageSize := "A4"
- lowQuality := true
- })
-
- override def render(html: String, documentName: String, force: Boolean = false): Path = {
- checkedCreate(html, documentName, force)
- }
-
- override def delete(documentName: String): Unit = {
- logger.trace(phi"delete(${Unsafe(documentName)})")
-
- val file = getPath(documentName)
- logger.debug(phi"File: $file")
- if (Files.deleteIfExists(file)) {
- logger.info(phi"Deleted")
- } else {
- logger.warn(phi"Doesn't exist")
- }
- }
-
- override def getPath(documentName: String): Path = {
- settings.downloadsPath.resolve(s"$documentName.pdf").toAbsolutePath
- }
-
- protected def checkedCreate[SourceT: SourceDocumentLike](src: SourceT, fileName: String, force: Boolean): Path = {
- logger.trace(phi"checkedCreate(fileName=${Unsafe(fileName)}, force=$force)")
-
- val dest = getPath(fileName)
- logger.debug(phi"Destination file: $dest")
-
- if (force || !dest.toFile.exists()) {
- logger.trace(phi"Force refresh the file")
- val newDocPath = forceCreate(src, dest)
- logger.info(phi"Updated")
- newDocPath
- } else if (dest.toFile.exists()) {
- logger.trace(phi"Already exists")
- dest
- } else {
- logger.trace(phi"The file does not exist")
- val newDocPath = forceCreate(src, dest)
- logger.info(phi"Created")
- newDocPath
- }
- }
-
- protected def forceCreate[SourceT: SourceDocumentLike](src: SourceT, dest: Path): Path = {
- logger.trace(phi"forceCreate[${Unsafe(src.getClass.getName)}](dest=$dest)")
-
- val destTemp = Files.createTempFile("driver", ".pdf")
- val destTempFile = destTemp.toFile
-
- Files.createDirectories(dest.getParent)
-
- val retCode = pdf.run(src, destTempFile)
- lazy val pdfSize = destTempFile.length()
- if (retCode != 0) {
- // Try to google "wkhtmltopdf returns {retCode}"
- throw new IOException(s"Can create the document, the return code is $retCode")
- } else if (pdfSize == 0) {
- // Anything could happen, e.g. https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2540
- throw new IOException("The pdf is empty")
- } else {
- logger.debug(phi"Size: ${Unsafe(pdfSize)}B")
- Files.move(destTemp, dest, StandardCopyOption.REPLACE_EXISTING)
- dest
- }
- }
-}