xref: /webtrees/app/Http/RequestHandlers/ManageMediaData.php (revision c4943cff72f95a28fbb9404e3c20b169ff098e5c)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Media;
25use Fisharebest\Webtrees\Mime;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\DatatablesService;
28use Fisharebest\Webtrees\Services\MediaFileService;
29use Fisharebest\Webtrees\Services\TreeService;
30use Illuminate\Database\Capsule\Manager as DB;
31use Illuminate\Database\Query\Builder;
32use Illuminate\Database\Query\Expression;
33use Illuminate\Database\Query\JoinClause;
34use League\Flysystem\FilesystemException;
35use League\Flysystem\FilesystemOperator;
36use League\Flysystem\UnableToCheckFileExistence;
37use League\Flysystem\UnableToReadFile;
38use League\Flysystem\UnableToRetrieveMetadata;
39use Psr\Http\Message\ResponseInterface;
40use Psr\Http\Message\ServerRequestInterface;
41use Psr\Http\Server\RequestHandlerInterface;
42use stdClass;
43use Throwable;
44
45use function assert;
46use function e;
47use function getimagesizefromstring;
48use function intdiv;
49use function route;
50use function str_starts_with;
51use function strlen;
52use function substr;
53use function view;
54
55/**
56 * Manage media from the control panel.
57 */
58class ManageMediaData implements RequestHandlerInterface
59{
60    private DatatablesService $datatables_service;
61
62    private MediaFileService $media_file_service;
63
64    private TreeService $tree_service;
65
66    /**
67     * MediaController constructor.
68     *
69     * @param DatatablesService $datatables_service
70     * @param MediaFileService  $media_file_service
71     * @param TreeService       $tree_service
72     */
73    public function __construct(
74        DatatablesService $datatables_service,
75        MediaFileService $media_file_service,
76        TreeService $tree_service
77    ) {
78        $this->datatables_service = $datatables_service;
79        $this->media_file_service = $media_file_service;
80        $this->tree_service       = $tree_service;
81    }
82
83    /**
84     * @param ServerRequestInterface $request
85     *
86     * @return ResponseInterface
87     */
88    public function handle(ServerRequestInterface $request): ResponseInterface
89    {
90        $data_filesystem = Registry::filesystem()->data();
91
92        $files = $request->getQueryParams()['files']; // local|external|unused
93
94        // Files within this folder
95        $media_folder = $request->getQueryParams()['media_folder'];
96
97        // Show sub-folders within $media_folder
98        $subfolders = $request->getQueryParams()['subfolders']; // include|exclude
99
100        $search_columns = ['multimedia_file_refn', 'descriptive_title'];
101
102        $sort_columns = [
103            0 => 'multimedia_file_refn',
104            2 => new Expression('descriptive_title || multimedia_file_refn'),
105        ];
106
107        // Convert a row from the database into a row for datatables
108        $callback = function (stdClass $row): array {
109            $tree  = $this->tree_service->find((int) $row->m_file);
110            $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom);
111            assert($media instanceof Media);
112
113            $is_http  = str_starts_with($row->multimedia_file_refn, 'http://');
114            $is_https = str_starts_with($row->multimedia_file_refn, 'https://');
115
116            if ($is_http || $is_https) {
117                return [
118                    '<a href="' . e($row->multimedia_file_refn) . '">' . e($row->multimedia_file_refn) . '</a>',
119                    view('icons/mime', ['type' => Mime::DEFAULT_TYPE]),
120                    $this->mediaObjectInfo($media),
121                ];
122            }
123
124            try {
125                $path = $row->media_folder . $row->multimedia_file_refn;
126
127                try {
128                    $mime_type = Registry::filesystem()->data()->mimeType($path);
129                } catch (UnableToRetrieveMetadata $ex) {
130                    $mime_type = Mime::DEFAULT_TYPE;
131                }
132
133                if (str_starts_with($mime_type, 'image/')) {
134                    $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
135                    $img = '<img src="' . e($url) . '">';
136                } else {
137                    $img = view('icons/mime', ['type' => $mime_type]);
138                }
139
140                $url = route(AdminMediaFileDownload::class, ['path' => $path]);
141                $img = '<a href="' . e($url) . '" type="' . $mime_type . '" class="gallery">' . $img . '</a>';
142            } catch (UnableToReadFile $ex) {
143                $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
144                $img = '<img src="' . e($url) . '">';
145            }
146
147            return [
148                e($row->multimedia_file_refn),
149                $img,
150                $this->mediaObjectInfo($media),
151            ];
152        };
153
154        switch ($files) {
155            case 'local':
156                $query = DB::table('media_file')
157                    ->join('media', static function (JoinClause $join): void {
158                        $join
159                            ->on('media.m_file', '=', 'media_file.m_file')
160                            ->on('media.m_id', '=', 'media_file.m_id');
161                    })
162                    ->join('gedcom_setting', 'gedcom_id', '=', 'media.m_file')
163                    ->where('setting_name', '=', 'MEDIA_DIRECTORY')
164                    ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
165                    ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
166                    ->select([
167                        'media.*',
168                        'multimedia_file_refn',
169                        'descriptive_title',
170                        'setting_value AS media_folder',
171                    ]);
172
173                $query->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%');
174
175                if ($subfolders === 'exclude') {
176                    $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%');
177                }
178
179                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
180
181            case 'external':
182                $query = DB::table('media_file')
183                    ->join('media', static function (JoinClause $join): void {
184                        $join
185                            ->on('media.m_file', '=', 'media_file.m_file')
186                            ->on('media.m_id', '=', 'media_file.m_id');
187                    })
188                    ->where(static function (Builder $query): void {
189                        $query
190                            ->where('multimedia_file_refn', 'LIKE', 'http://%')
191                            ->orWhere('multimedia_file_refn', 'LIKE', 'https://%');
192                    })
193                    ->select([
194                        'media.*',
195                        'multimedia_file_refn',
196                        'descriptive_title',
197                        new Expression("'' AS media_folder"),
198                    ]);
199
200                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
201
202            case 'unused':
203                // Which trees use which media folder?
204                $media_trees = DB::table('gedcom')
205                    ->join('gedcom_setting', 'gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
206                    ->where('setting_name', '=', 'MEDIA_DIRECTORY')
207                    ->where('gedcom.gedcom_id', '>', 0)
208                    ->pluck('setting_value', 'gedcom_name');
209
210                $disk_files = $this->media_file_service->allFilesOnDisk($data_filesystem, $media_folder, $subfolders === 'include');
211                $db_files   = $this->media_file_service->allFilesInDatabase($media_folder, $subfolders === 'include');
212
213                // All unused files
214                $unused_files = $disk_files->diff($db_files)
215                    ->map(static function (string $file): array {
216                        return (array) $file;
217                    });
218
219                $search_columns = [0];
220                $sort_columns   = [0 => 0];
221
222                $callback = function (array $row) use ($data_filesystem, $media_trees): array {
223                    try {
224                        $mime_type = $data_filesystem->mimeType($row[0]) ?: Mime::DEFAULT_TYPE;
225                    } catch (FileSystemException | UnableToRetrieveMetadata $ex) {
226                        $mime_type = Mime::DEFAULT_TYPE;
227                    }
228
229
230                    if (str_starts_with($mime_type, 'image/')) {
231                        $url = route(AdminMediaFileThumbnail::class, ['path' => $row[0]]);
232                        $img = '<img src="' . e($url) . '">';
233                    } else {
234                        $img = view('icons/mime', ['type' => $mime_type]);
235                    }
236
237                    $url = route(AdminMediaFileDownload::class, ['path' => $row[0]]);
238                    $img = '<a href="' . e($url) . '">' . $img . '</a>';
239
240                    // Form to create new media object in each tree
241                    $create_form = '';
242                    foreach ($media_trees as $media_tree => $media_directory) {
243                        if (str_starts_with($row[0], $media_directory)) {
244                            $tmp         = substr($row[0], strlen($media_directory));
245                            $create_form .=
246                                '<p><a href="#" data-toggle="modal" data-backdrop="static" data-target="#modal-create-media-from-file" data-file="' . e($tmp) . '" data-url="' . e(route(CreateMediaObjectFromFile::class, ['tree' => $media_tree])) . '" onclick="document.getElementById(\'modal-create-media-from-file-form\').action=this.dataset.url; document.getElementById(\'file\').value=this.dataset.file;">' . I18N::translate('Create') . '</a> — ' . e($media_tree) . '<p>';
247                        }
248                    }
249
250                    $delete_link = '<p><a data-confirm="' . I18N::translate('Are you sure you want to delete “%s”?', e($row[0])) . '" data-post-url="' . e(route(DeletePath::class, [
251                            'path' => $row[0],
252                        ])) . '" href="#">' . I18N::translate('Delete') . '</a></p>';
253
254                    return [
255                        $this->mediaFileInfo($data_filesystem, $row[0]) . $delete_link,
256                        $img,
257                        $create_form,
258                    ];
259                };
260
261                return $this->datatables_service->handleCollection($request, $unused_files, $search_columns, $sort_columns, $callback);
262
263            default:
264                throw new HttpNotFoundException();
265        }
266    }
267
268    /**
269     * Generate some useful information and links about a media object.
270     *
271     * @param Media $media
272     *
273     * @return string HTML
274     */
275    private function mediaObjectInfo(Media $media): string
276    {
277        $html = '<b><a href="' . e($media->url()) . '">' . $media->fullName() . '</a></b>' . '<br><i>' . e($media->getNote()) . '</i></br><br>';
278
279        $linked = [];
280        foreach ($media->linkedIndividuals('OBJE') as $link) {
281            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
282        }
283        foreach ($media->linkedFamilies('OBJE') as $link) {
284            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
285        }
286        foreach ($media->linkedSources('OBJE') as $link) {
287            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
288        }
289        foreach ($media->linkedNotes('OBJE') as $link) {
290            // Invalid GEDCOM - you cannot link a NOTE to an OBJE
291            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
292        }
293        foreach ($media->linkedRepositories('OBJE') as $link) {
294            // Invalid GEDCOM - you cannot link a REPO to an OBJE
295            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
296        }
297        foreach ($media->linkedLocations('OBJE') as $link) {
298            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
299        }
300        if ($linked !== []) {
301            $html .= '<ul>';
302            foreach ($linked as $link) {
303                $html .= '<li>' . $link . '</li>';
304            }
305            $html .= '</ul>';
306        } else {
307            $html .= '<div class="alert alert-danger">' . I18N::translate('There are no links to this media object.') . '</div>';
308        }
309
310        return $html;
311    }
312
313    /**
314     * Generate some useful information and links about a media file.
315     *
316     * @param FilesystemOperator $data_filesystem
317     * @param string             $file
318     *
319     * @return string
320     */
321    private function mediaFileInfo(FilesystemOperator $data_filesystem, string $file): string
322    {
323        $html = '<dl>';
324        $html .= '<dt>' . I18N::translate('Filename') . '</dt>';
325        $html .= '<dd>' . e($file) . '</dd>';
326
327        try {
328            $file_exists = $data_filesystem->fileExists($file);
329        } catch (FilesystemException | UnableToCheckFileExistence $ex) {
330            $file_exists = false;
331        }
332
333        if ($file_exists) {
334            try {
335                $size = $data_filesystem->fileSize($file);
336            } catch (FilesystemException | UnableToRetrieveMetadata $ex) {
337                $size = 0;
338            }
339            $size = intdiv($size + 1023, 1024); // Round up to next KB
340            /* I18N: size of file in KB */
341            $size = I18N::translate('%s KB', I18N::number($size));
342            $html .= '<dt>' . I18N::translate('File size') . '</dt>';
343            $html .= '<dd>' . $size . '</dd>';
344
345            try {
346                // This will work for local filesystems.  For remote filesystems, we will
347                // need to copy the file locally to work out the image size.
348                $imgsize = getimagesizefromstring($data_filesystem->read($file));
349                $html    .= '<dt>' . I18N::translate('Image dimensions') . '</dt>';
350                /* I18N: image dimensions, width × height */
351                $html .= '<dd>' . I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])) . '</dd>';
352            } catch (FilesystemException | UnableToReadFile | Throwable $ex) {
353                // Not an image, or not a valid image?
354            }
355        }
356
357        $html .= '</dl>';
358
359        return $html;
360    }
361}
362