1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2021 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Http\RequestHandlers; |
21
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Mime; |
23
|
|
|
use Fisharebest\Webtrees\Registry; |
24
|
|
|
use Fisharebest\Webtrees\Services\PendingChangesService; |
25
|
|
|
use Fisharebest\Webtrees\Services\TreeService; |
26
|
|
|
use League\Flysystem\FilesystemException; |
27
|
|
|
use League\Flysystem\UnableToDeleteFile; |
28
|
|
|
use League\Flysystem\UnableToMoveFile; |
29
|
|
|
use League\Flysystem\UnableToReadFile; |
30
|
|
|
use League\Flysystem\UnableToRetrieveMetadata; |
31
|
|
|
use Psr\Http\Message\ResponseInterface; |
32
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
33
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
34
|
|
|
|
35
|
|
|
use function dirname; |
36
|
|
|
use function explode; |
37
|
|
|
use function response; |
38
|
|
|
use function strlen; |
39
|
|
|
use function substr; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Import custom thumbnails from webtrees 1.x. |
43
|
|
|
*/ |
44
|
|
|
class ImportThumbnailsAction implements RequestHandlerInterface |
45
|
|
|
{ |
46
|
|
|
/** @var PendingChangesService */ |
47
|
|
|
private $pending_changes_service; |
48
|
|
|
|
49
|
|
|
/** @var TreeService */ |
50
|
|
|
private $tree_service; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* ImportThumbnailsController constructor. |
54
|
|
|
* |
55
|
|
|
* @param PendingChangesService $pending_changes_service |
56
|
|
|
* @param TreeService $tree_service |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
PendingChangesService $pending_changes_service, |
60
|
|
|
TreeService $tree_service |
61
|
|
|
) { |
62
|
|
|
$this->pending_changes_service = $pending_changes_service; |
63
|
|
|
$this->tree_service = $tree_service; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Import custom thumbnails from webtrees 1.x. |
68
|
|
|
* |
69
|
|
|
* @param ServerRequestInterface $request |
70
|
|
|
* |
71
|
|
|
* @return ResponseInterface |
72
|
|
|
*/ |
73
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
74
|
|
|
{ |
75
|
|
|
$data_filesystem = Registry::filesystem()->data(); |
76
|
|
|
|
77
|
|
|
$params = (array) $request->getParsedBody(); |
78
|
|
|
|
79
|
|
|
$thumbnail = $params['thumbnail']; |
80
|
|
|
$action = $params['action']; |
81
|
|
|
$xrefs = $params['xref']; |
82
|
|
|
$geds = $params['ged']; |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$file_exists = $data_filesystem->fileExists($thumbnail); |
86
|
|
|
} catch (FilesystemException | UnableToRetrieveMetadata $ex) { |
87
|
|
|
$file_exists = false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$file_exists) { |
91
|
|
|
return response([]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$media_objects = []; |
95
|
|
|
|
96
|
|
|
foreach ($xrefs as $key => $xref) { |
97
|
|
|
$tree = $this->tree_service->all()->get($geds[$key]); |
98
|
|
|
$media_objects[] = Registry::mediaFactory()->make($xref, $tree); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
switch ($action) { |
102
|
|
|
case 'delete': |
103
|
|
|
try { |
104
|
|
|
$data_filesystem->delete($thumbnail); |
105
|
|
|
} catch (FilesystemException | UnableToDeleteFile $ex) { |
106
|
|
|
// Cannot delete the file. Leave it there. |
107
|
|
|
} |
108
|
|
|
break; |
109
|
|
|
|
110
|
|
|
case 'add': |
111
|
|
|
try { |
112
|
|
|
$mime_type = $data_filesystem->mimeType($thumbnail) ?: Mime::DEFAULT_TYPE; |
113
|
|
|
} catch (FilesystemException | UnableToRetrieveMetadata $ex) { |
114
|
|
|
$mime_type = Mime::DEFAULT_TYPE; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$directory = dirname($thumbnail, 2); |
119
|
|
|
$sha1 = sha1($data_filesystem->read($thumbnail)); |
120
|
|
|
$extension = explode('/', $mime_type)[1]; |
121
|
|
|
$move_to = $directory . '/' . $sha1 . '.' . $extension; |
122
|
|
|
|
123
|
|
|
$data_filesystem->move($thumbnail, $move_to); |
124
|
|
|
|
125
|
|
|
foreach ($media_objects as $media_object) { |
126
|
|
|
$prefix = $media_object->tree()->getPreference('MEDIA_DIRECTORY'); |
127
|
|
|
$gedcom = '1 FILE ' . substr($move_to, strlen($prefix)) . "\n2 FORM " . $extension; |
128
|
|
|
|
129
|
|
|
if ($media_object->firstImageFile() === null) { |
130
|
|
|
// The media object doesn't have an image. Add this as a secondary file. |
131
|
|
|
$media_object->createFact($gedcom, true); |
132
|
|
|
} else { |
133
|
|
|
// The media object already has an image. Show this custom one in preference. |
134
|
|
|
$gedcom = '0 @' . $media_object->xref() . "@ OBJE\n" . $gedcom; |
135
|
|
|
foreach ($media_object->facts() as $fact) { |
136
|
|
|
$gedcom .= "\n" . $fact->gedcom(); |
137
|
|
|
} |
138
|
|
|
$media_object->updateRecord($gedcom, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Accept the changes, to keep the filesystem in sync with the GEDCOM data. |
142
|
|
|
$this->pending_changes_service->acceptRecord($media_object); |
143
|
|
|
} |
144
|
|
|
} catch (FilesystemException | UnableToReadFile | UnableToMoveFile $ex) { |
145
|
|
|
// Cannot import the file? |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return response([]); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|