mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Create a preview image of all files during processing
This commit is contained in:
48
modules/common/src/main/scala/docspell/common/FileName.scala
Normal file
48
modules/common/src/main/scala/docspell/common/FileName.scala
Normal file
@ -0,0 +1,48 @@
|
||||
package docspell.common
|
||||
|
||||
case class FileName private (name: String) {
|
||||
|
||||
private[this] val (base, ext) =
|
||||
name.lastIndexOf('.') match {
|
||||
case -1 => (name, None)
|
||||
case n => (name.take(n), Some(name.drop(n + 1)))
|
||||
}
|
||||
|
||||
/** Returns the name part without the extension. If there is no
|
||||
* extension, it is the same as fullname.
|
||||
*/
|
||||
def baseName: String =
|
||||
base
|
||||
|
||||
/** Returns the extension part if available without the dot. */
|
||||
def extension: Option[String] =
|
||||
ext
|
||||
|
||||
def fullName: String =
|
||||
name
|
||||
|
||||
/** Creates a new name where part is spliced into the name before the
|
||||
* extension, separated by separator.
|
||||
*/
|
||||
def withPart(part: String, sep: Char): FileName =
|
||||
if (part.isEmpty()) this
|
||||
else
|
||||
ext
|
||||
.map(e => new FileName(s"${base}${sep}${part}.${e}"))
|
||||
.getOrElse(new FileName(s"${base}${sep}${part}"))
|
||||
|
||||
/** Create a new name using the given extension. */
|
||||
def withExtension(newExt: String): FileName =
|
||||
if (newExt.isEmpty()) new FileName(base)
|
||||
else new FileName(s"${base}.${newExt}")
|
||||
|
||||
}
|
||||
object FileName {
|
||||
|
||||
def apply(name: String): FileName =
|
||||
Option(name)
|
||||
.map(_.trim)
|
||||
.filter(_.nonEmpty)
|
||||
.map(n => new FileName(n))
|
||||
.getOrElse(new FileName("unknown-file"))
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package docspell.common
|
||||
|
||||
import minitest._
|
||||
|
||||
object FileNameTest extends SimpleTestSuite {
|
||||
|
||||
test("make filename") {
|
||||
val data = List(
|
||||
(FileName("test"), "test", None),
|
||||
(FileName("test.pdf"), "test", Some("pdf")),
|
||||
(FileName("bla.xml.gz"), "bla.xml", Some("gz")),
|
||||
(FileName(""), "unknown-file", None)
|
||||
)
|
||||
|
||||
data.foreach { case (fn, base, ext) =>
|
||||
assertEquals(fn.baseName, base)
|
||||
assertEquals(fn.extension, ext)
|
||||
}
|
||||
}
|
||||
|
||||
test("with part") {
|
||||
assertEquals(
|
||||
FileName("test.pdf").withPart("converted", '_'),
|
||||
FileName("test_converted.pdf")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("bla.xml.gz").withPart("converted", '_'),
|
||||
FileName("bla.xml_converted.gz")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("test").withPart("converted", '_'),
|
||||
FileName("test_converted")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("test").withPart("", '_'),
|
||||
FileName("test")
|
||||
)
|
||||
}
|
||||
|
||||
test("with extension") {
|
||||
assertEquals(
|
||||
FileName("test.pdf").withExtension("xml"),
|
||||
FileName("test.xml")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("test").withExtension("xml"),
|
||||
FileName("test.xml")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("test.pdf.gz").withExtension("xml"),
|
||||
FileName("test.pdf.xml")
|
||||
)
|
||||
assertEquals(
|
||||
FileName("test.pdf.gz").withExtension(""),
|
||||
FileName("test.pdf")
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user