From 6db5c39d78fd423bd4dc5ba9a09792b5dd9d5625 Mon Sep 17 00:00:00 2001
From: Eike Kettner <eike.kettner@posteo.de>
Date: Sun, 8 Nov 2020 01:23:07 +0100
Subject: [PATCH] Fix converted filename

Mark it by default with a string from the config file.

Issue: 397
---
 .../src/main/scala/docspell/convert/ConvertConfig.scala     | 1 +
 .../src/test/scala/docspell/convert/ConversionTest.scala    | 1 +
 modules/joex/src/main/resources/reference.conf              | 5 +++++
 .../src/main/scala/docspell/joex/process/ConvertPdf.scala   | 6 +++++-
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/modules/convert/src/main/scala/docspell/convert/ConvertConfig.scala b/modules/convert/src/main/scala/docspell/convert/ConvertConfig.scala
index f51791c0..8013f3bb 100644
--- a/modules/convert/src/main/scala/docspell/convert/ConvertConfig.scala
+++ b/modules/convert/src/main/scala/docspell/convert/ConvertConfig.scala
@@ -6,6 +6,7 @@ import docspell.convert.flexmark.MarkdownConfig
 
 case class ConvertConfig(
     chunkSize: Int,
+    convertedFilenamePart: String,
     maxImageSize: Int,
     markdown: MarkdownConfig,
     wkhtmlpdf: WkHtmlPdfConfig,
diff --git a/modules/convert/src/test/scala/docspell/convert/ConversionTest.scala b/modules/convert/src/test/scala/docspell/convert/ConversionTest.scala
index 31dadd88..4d7e80ed 100644
--- a/modules/convert/src/test/scala/docspell/convert/ConversionTest.scala
+++ b/modules/convert/src/test/scala/docspell/convert/ConversionTest.scala
@@ -23,6 +23,7 @@ object ConversionTest extends SimpleTestSuite with FileChecks {
 
   val convertConfig = ConvertConfig(
     8192,
+    "converted",
     3000 * 3000,
     MarkdownConfig("body { padding: 2em 5em; }"),
     WkHtmlPdfConfig(
diff --git a/modules/joex/src/main/resources/reference.conf b/modules/joex/src/main/resources/reference.conf
index 23ec5b47..51ad7d04 100644
--- a/modules/joex/src/main/resources/reference.conf
+++ b/modules/joex/src/main/resources/reference.conf
@@ -328,6 +328,11 @@ docspell.joex {
     # as used with the rest server.
     chunk-size = 524288
 
+    # A string used to change the filename of the converted pdf file.
+    # If empty, the original file name is used for the pdf file ( the
+    # extension is always replaced with `pdf`).
+    converted-filename-part = "converted"
+
     # When reading images, this is the maximum size. Images that are
     # larger are not processed.
     max-image-size = ${docspell.joex.extraction.ocr.max-image-size}
diff --git a/modules/joex/src/main/scala/docspell/joex/process/ConvertPdf.scala b/modules/joex/src/main/scala/docspell/joex/process/ConvertPdf.scala
index 17cca3e0..65ff0dda 100644
--- a/modules/joex/src/main/scala/docspell/joex/process/ConvertPdf.scala
+++ b/modules/joex/src/main/scala/docspell/joex/process/ConvertPdf.scala
@@ -135,7 +135,11 @@ object ConvertPdf {
   ) = {
     val hint =
       MimeTypeHint.advertised(MimeType.pdf).withName(ra.name.getOrElse("file.pdf"))
-    val newName = ra.name.map(n => s"$n.pdf")
+    val newName =
+      ra.name
+        .map(FileName.apply)
+        .map(_.withExtension("pdf").withPart(cfg.convertedFilenamePart, '.'))
+        .map(_.fullName)
     ctx.store.bitpeace
       .saveNew(pdf, cfg.chunkSize, MimetypeHint(hint.filename, hint.advertised))
       .compile