summaryrefslogtreecommitdiff
path: root/app/Services/MediaFileService.php
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2020-04-09 21:47:28 +0100
committerGreg Roach <greg@subaqua.co.uk>2020-04-09 21:47:28 +0100
commit45fc2659f70ca8cab67da5532777c60a94946d72 (patch)
tree528fd18e624eb76bce9bc2b2c12f7f55cb927950 /app/Services/MediaFileService.php
parent5de4ab516780967b88b62e6f7feb7594c5760063 (diff)
downloadwebtrees-45fc2659f70ca8cab67da5532777c60a94946d72.tar.gz
webtrees-45fc2659f70ca8cab67da5532777c60a94946d72.tar.bz2
webtrees-45fc2659f70ca8cab67da5532777c60a94946d72.zip
Fix: #3160 - missing FORM field when creating media files
Diffstat (limited to 'app/Services/MediaFileService.php')
-rw-r--r--app/Services/MediaFileService.php39
1 files changed, 29 insertions, 10 deletions
diff --git a/app/Services/MediaFileService.php b/app/Services/MediaFileService.php
index e0667a97fa..f70598ea34 100644
--- a/app/Services/MediaFileService.php
+++ b/app/Services/MediaFileService.php
@@ -42,12 +42,13 @@ use function ini_get;
use function intdiv;
use function min;
use function pathinfo;
-use function preg_match;
+use function preg_replace;
use function sha1;
use function sort;
use function str_replace;
use function strpos;
use function strtolower;
+use function strtr;
use function substr;
use function trim;
@@ -69,6 +70,11 @@ class MediaFileService
'confidential',
];
+ public const EXTENSION_TO_FORM = [
+ 'jpg' => 'jpeg',
+ 'tif' => 'tiff',
+ ];
+
/**
* What is the largest file a user may upload?
*/
@@ -256,27 +262,40 @@ class MediaFileService
* @param string $file
* @param string $type
* @param string $title
+ * @param string $note
*
* @return string
*/
- public function createMediaFileGedcom(string $file, string $type, string $title): string
+ public function createMediaFileGedcom(string $file, string $type, string $title, string $note): string
{
- if (preg_match('/\.([a-z0-9]+)/i', $file, $match)) {
- $extension = strtolower($match[1]);
- $extension = str_replace('jpg', 'jpeg', $extension);
- $extension = ' ' . $extension;
- } else {
- $extension = '';
- }
+ // Tidy whitespace
+ $type = trim(preg_replace('/\s+/', ' ', $type));
+ $title = trim(preg_replace('/\s+/', ' ', $title));
$gedcom = '1 FILE ' . $file;
+
+ $format = strtolower(pathinfo($file, PATHINFO_EXTENSION));
+ $format = self::EXTENSION_TO_FORM[$format] ?? $format;
+
+ if ($format !== '') {
+ $gedcom .= "\n2 FORM " . $format;
+ } elseif ($type !== '') {
+ $gedcom .= "\n2 FORM";
+ }
+
if ($type !== '') {
- $gedcom .= "\n2 FORM" . $extension . "\n3 TYPE " . $type;
+ $gedcom .= "\n3 TYPE " . $type;
}
+
if ($title !== '') {
$gedcom .= "\n2 TITL " . $title;
}
+ if ($note !== '') {
+ // Convert HTML line endings to GEDCOM continuations
+ $gedcom .= "\n1 NOTE " . strtr($note, ["\r\n" => "\n2 CONT "]);
+ }
+
return $gedcom;
}