xref: /webtrees/app/Http/RequestHandlers/ManageMediaData.php (revision f25fc0f929f69ab8124cf0cecde45e457db7574a)
18ce3bd73SGreg Roach<?php
28ce3bd73SGreg Roach
38ce3bd73SGreg Roach/**
48ce3bd73SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68ce3bd73SGreg Roach * This program is free software: you can redistribute it and/or modify
78ce3bd73SGreg Roach * it under the terms of the GNU General Public License as published by
88ce3bd73SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98ce3bd73SGreg Roach * (at your option) any later version.
108ce3bd73SGreg Roach * This program is distributed in the hope that it will be useful,
118ce3bd73SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128ce3bd73SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138ce3bd73SGreg Roach * GNU General Public License for more details.
148ce3bd73SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168ce3bd73SGreg Roach */
178ce3bd73SGreg Roach
188ce3bd73SGreg Roachdeclare(strict_types=1);
198ce3bd73SGreg Roach
208ce3bd73SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
218ce3bd73SGreg Roach
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
248ce3bd73SGreg Roachuse Fisharebest\Webtrees\I18N;
258ce3bd73SGreg Roachuse Fisharebest\Webtrees\Media;
268ce3bd73SGreg Roachuse Fisharebest\Webtrees\Mime;
278ce3bd73SGreg Roachuse Fisharebest\Webtrees\Registry;
288ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService;
294991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
308ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
318ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
32748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
338ce3bd73SGreg Roachuse Illuminate\Database\Query\Builder;
348ce3bd73SGreg Roachuse Illuminate\Database\Query\Expression;
358ce3bd73SGreg Roachuse Illuminate\Database\Query\JoinClause;
36f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
37f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
38f0448b68SGreg Roachuse League\Flysystem\UnableToCheckFileExistence;
39f32d77e6SGreg Roachuse League\Flysystem\UnableToReadFile;
40f7cf8a15SGreg Roachuse League\Flysystem\UnableToRetrieveMetadata;
418ce3bd73SGreg Roachuse Psr\Http\Message\ResponseInterface;
428ce3bd73SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
438ce3bd73SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
448ce3bd73SGreg Roachuse Throwable;
458ce3bd73SGreg Roach
468ce3bd73SGreg Roachuse function assert;
478ce3bd73SGreg Roachuse function e;
48f7cf8a15SGreg Roachuse function getimagesizefromstring;
498ce3bd73SGreg Roachuse function intdiv;
508ce3bd73SGreg Roachuse function route;
518ce3bd73SGreg Roachuse function str_starts_with;
528ce3bd73SGreg Roachuse function strlen;
538ce3bd73SGreg Roachuse function substr;
548ce3bd73SGreg Roachuse function view;
558ce3bd73SGreg Roach
568ce3bd73SGreg Roach/**
578ce3bd73SGreg Roach * Manage media from the control panel.
588ce3bd73SGreg Roach */
598ce3bd73SGreg Roachclass ManageMediaData implements RequestHandlerInterface
608ce3bd73SGreg Roach{
61c4943cffSGreg Roach    private DatatablesService $datatables_service;
628ce3bd73SGreg Roach
634991f205SGreg Roach    private LinkedRecordService $linked_record_service;
644991f205SGreg Roach
65c4943cffSGreg Roach    private MediaFileService $media_file_service;
668ce3bd73SGreg Roach
67c4943cffSGreg Roach    private TreeService $tree_service;
688ce3bd73SGreg Roach
698ce3bd73SGreg Roach    /**
708ce3bd73SGreg Roach     * @param DatatablesService   $datatables_service
714991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
728ce3bd73SGreg Roach     * @param MediaFileService    $media_file_service
738ce3bd73SGreg Roach     * @param TreeService         $tree_service
748ce3bd73SGreg Roach     */
758ce3bd73SGreg Roach    public function __construct(
768ce3bd73SGreg Roach        DatatablesService $datatables_service,
774991f205SGreg Roach        LinkedRecordService $linked_record_service,
788ce3bd73SGreg Roach        MediaFileService $media_file_service,
798ce3bd73SGreg Roach        TreeService $tree_service
808ce3bd73SGreg Roach    ) {
818ce3bd73SGreg Roach        $this->datatables_service    = $datatables_service;
824991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
838ce3bd73SGreg Roach        $this->media_file_service    = $media_file_service;
848ce3bd73SGreg Roach        $this->tree_service          = $tree_service;
858ce3bd73SGreg Roach    }
868ce3bd73SGreg Roach
878ce3bd73SGreg Roach    /**
888ce3bd73SGreg Roach     * @param ServerRequestInterface $request
898ce3bd73SGreg Roach     *
908ce3bd73SGreg Roach     * @return ResponseInterface
918ce3bd73SGreg Roach     */
928ce3bd73SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
938ce3bd73SGreg Roach    {
948ce3bd73SGreg Roach        $data_filesystem = Registry::filesystem()->data();
958ce3bd73SGreg Roach
96748dbe15SGreg Roach        $files = Validator::queryParams($request)->isInArray(['local', 'external', 'unused'])->string('files');
978ce3bd73SGreg Roach
988ce3bd73SGreg Roach        // Files within this folder
99a1bd562dSGreg Roach        $media_folders = $this->media_file_service->allMediaFolders($data_filesystem)->all();
100a1bd562dSGreg Roach        $media_folder  = Validator::queryParams($request)->isInArray($media_folders)->string('media_folder');
1018ce3bd73SGreg Roach
1028ce3bd73SGreg Roach        // Show sub-folders within $media_folder
103748dbe15SGreg Roach        $subfolders = Validator::queryParams($request)->isInArray(['include', 'exclude'])->string('subfolders');
1048ce3bd73SGreg Roach
1058ce3bd73SGreg Roach        $search_columns = ['multimedia_file_refn', 'descriptive_title'];
1068ce3bd73SGreg Roach
1078ce3bd73SGreg Roach        $sort_columns = [
1088ce3bd73SGreg Roach            0 => 'multimedia_file_refn',
10996c9e653SGreg Roach            2 => new Expression('descriptive_title || multimedia_file_refn'),
1108ce3bd73SGreg Roach        ];
1118ce3bd73SGreg Roach
1128ce3bd73SGreg Roach        // Convert a row from the database into a row for datatables
113f70bcff5SGreg Roach        $callback = function (object $row): array {
1148ce3bd73SGreg Roach            $tree  = $this->tree_service->find((int) $row->m_file);
1158ce3bd73SGreg Roach            $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom);
1168ce3bd73SGreg Roach            assert($media instanceof Media);
1178ce3bd73SGreg Roach
118f41d9852SGreg Roach            $is_http  = str_starts_with($row->multimedia_file_refn, 'http://');
119f41d9852SGreg Roach            $is_https = str_starts_with($row->multimedia_file_refn, 'https://');
120f41d9852SGreg Roach
121f41d9852SGreg Roach            if ($is_http || $is_https) {
1222fad1159SGreg Roach                return [
1232fad1159SGreg Roach                    '<a href="' . e($row->multimedia_file_refn) . '">' . e($row->multimedia_file_refn) . '</a>',
1242fad1159SGreg Roach                    view('icons/mime', ['type' => Mime::DEFAULT_TYPE]),
1252fad1159SGreg Roach                    $this->mediaObjectInfo($media),
1262fad1159SGreg Roach                ];
1272fad1159SGreg Roach            }
1282fad1159SGreg Roach
129f41d9852SGreg Roach            try {
1308ce3bd73SGreg Roach                $path = $row->media_folder . $row->multimedia_file_refn;
1318ce3bd73SGreg Roach
1328ce3bd73SGreg Roach                try {
133f7cf8a15SGreg Roach                    $mime_type = Registry::filesystem()->data()->mimeType($path);
13428d026adSGreg Roach                } catch (UnableToRetrieveMetadata) {
135f7cf8a15SGreg Roach                    $mime_type = Mime::DEFAULT_TYPE;
136f7cf8a15SGreg Roach                }
1378ce3bd73SGreg Roach
1388ce3bd73SGreg Roach                if (str_starts_with($mime_type, 'image/')) {
1398ce3bd73SGreg Roach                    $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
1408ce3bd73SGreg Roach                    $img = '<img src="' . e($url) . '">';
1418ce3bd73SGreg Roach                } else {
1428ce3bd73SGreg Roach                    $img = view('icons/mime', ['type' => $mime_type]);
1438ce3bd73SGreg Roach                }
1448ce3bd73SGreg Roach
1458ce3bd73SGreg Roach                $url = route(AdminMediaFileDownload::class, ['path' => $path]);
1468ce3bd73SGreg Roach                $img = '<a href="' . e($url) . '" type="' . $mime_type . '" class="gallery">' . $img . '</a>';
14728d026adSGreg Roach            } catch (UnableToReadFile) {
1488ce3bd73SGreg Roach                $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
1498ce3bd73SGreg Roach                $img = '<img src="' . e($url) . '">';
1508ce3bd73SGreg Roach            }
1518ce3bd73SGreg Roach
1528ce3bd73SGreg Roach            return [
1532fad1159SGreg Roach                e($row->multimedia_file_refn),
1548ce3bd73SGreg Roach                $img,
1558ce3bd73SGreg Roach                $this->mediaObjectInfo($media),
1568ce3bd73SGreg Roach            ];
1578ce3bd73SGreg Roach        };
1588ce3bd73SGreg Roach
1598ce3bd73SGreg Roach        switch ($files) {
1608ce3bd73SGreg Roach            case 'local':
1618ce3bd73SGreg Roach                $query = DB::table('media_file')
1628ce3bd73SGreg Roach                    ->join('media', static function (JoinClause $join): void {
1638ce3bd73SGreg Roach                        $join
1648ce3bd73SGreg Roach                            ->on('media.m_file', '=', 'media_file.m_file')
1658ce3bd73SGreg Roach                            ->on('media.m_id', '=', 'media_file.m_id');
1668ce3bd73SGreg Roach                    })
1679ba2a180SGreg Roach                    ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
1689ba2a180SGreg Roach                        $join
1699ba2a180SGreg Roach                            ->on('gedcom_setting.gedcom_id', '=', 'media.m_file')
1709ba2a180SGreg Roach                            ->where('setting_name', '=', 'MEDIA_DIRECTORY');
1719ba2a180SGreg Roach                    })
1728ce3bd73SGreg Roach                    ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
1738ce3bd73SGreg Roach                    ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
1748ce3bd73SGreg Roach                    ->select([
1758ce3bd73SGreg Roach                        'media.*',
1768ce3bd73SGreg Roach                        'multimedia_file_refn',
1778ce3bd73SGreg Roach                        'descriptive_title',
1789ba2a180SGreg Roach                        new Expression("COALESCE(setting_value, 'media/') AS media_folder"),
1798ce3bd73SGreg Roach                    ]);
1808ce3bd73SGreg Roach
1818ce3bd73SGreg Roach                $query->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%');
1828ce3bd73SGreg Roach
1838ce3bd73SGreg Roach                if ($subfolders === 'exclude') {
1848ce3bd73SGreg Roach                    $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%');
1858ce3bd73SGreg Roach                }
1868ce3bd73SGreg Roach
1878ce3bd73SGreg Roach                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
1888ce3bd73SGreg Roach
1898ce3bd73SGreg Roach            case 'external':
1908ce3bd73SGreg Roach                $query = DB::table('media_file')
1918ce3bd73SGreg Roach                    ->join('media', static function (JoinClause $join): void {
1928ce3bd73SGreg Roach                        $join
1938ce3bd73SGreg Roach                            ->on('media.m_file', '=', 'media_file.m_file')
1948ce3bd73SGreg Roach                            ->on('media.m_id', '=', 'media_file.m_id');
1958ce3bd73SGreg Roach                    })
1968ce3bd73SGreg Roach                    ->where(static function (Builder $query): void {
1978ce3bd73SGreg Roach                        $query
1988ce3bd73SGreg Roach                            ->where('multimedia_file_refn', 'LIKE', 'http://%')
1998ce3bd73SGreg Roach                            ->orWhere('multimedia_file_refn', 'LIKE', 'https://%');
2008ce3bd73SGreg Roach                    })
2018ce3bd73SGreg Roach                    ->select([
2028ce3bd73SGreg Roach                        'media.*',
2038ce3bd73SGreg Roach                        'multimedia_file_refn',
2048ce3bd73SGreg Roach                        'descriptive_title',
20596c9e653SGreg Roach                        new Expression("'' AS media_folder"),
2068ce3bd73SGreg Roach                    ]);
2078ce3bd73SGreg Roach
2088ce3bd73SGreg Roach                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
2098ce3bd73SGreg Roach
2108ce3bd73SGreg Roach            case 'unused':
2118ce3bd73SGreg Roach                // Which trees use which media folder?
2128ce3bd73SGreg Roach                $media_trees = DB::table('gedcom')
2138ce3bd73SGreg Roach                    ->join('gedcom_setting', 'gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
2148ce3bd73SGreg Roach                    ->where('setting_name', '=', 'MEDIA_DIRECTORY')
2158ce3bd73SGreg Roach                    ->where('gedcom.gedcom_id', '>', 0)
2168ce3bd73SGreg Roach                    ->pluck('setting_value', 'gedcom_name');
2178ce3bd73SGreg Roach
2188ce3bd73SGreg Roach                $disk_files = $this->media_file_service->allFilesOnDisk($data_filesystem, $media_folder, $subfolders === 'include');
2198ce3bd73SGreg Roach                $db_files   = $this->media_file_service->allFilesInDatabase($media_folder, $subfolders === 'include');
2208ce3bd73SGreg Roach
2218ce3bd73SGreg Roach                // All unused files
2228ce3bd73SGreg Roach                $unused_files = $disk_files->diff($db_files)
223*f25fc0f9SGreg Roach                    ->map(static fn(string $file): array => (array) $file);
2248ce3bd73SGreg Roach
2258ce3bd73SGreg Roach                $search_columns = [0];
2268ce3bd73SGreg Roach                $sort_columns   = [0 => 0];
2278ce3bd73SGreg Roach
2288ce3bd73SGreg Roach                $callback = function (array $row) use ($data_filesystem, $media_trees): array {
229f7cf8a15SGreg Roach                    try {
230f0448b68SGreg Roach                        $mime_type = $data_filesystem->mimeType($row[0]) ?: Mime::DEFAULT_TYPE;
23128d026adSGreg Roach                    } catch (FilesystemException | UnableToRetrieveMetadata) {
232f7cf8a15SGreg Roach                        $mime_type = Mime::DEFAULT_TYPE;
233f7cf8a15SGreg Roach                    }
234f7cf8a15SGreg Roach
2358ce3bd73SGreg Roach
2368ce3bd73SGreg Roach                    if (str_starts_with($mime_type, 'image/')) {
2378ce3bd73SGreg Roach                        $url = route(AdminMediaFileThumbnail::class, ['path' => $row[0]]);
2388ce3bd73SGreg Roach                        $img = '<img src="' . e($url) . '">';
2398ce3bd73SGreg Roach                    } else {
2408ce3bd73SGreg Roach                        $img = view('icons/mime', ['type' => $mime_type]);
2418ce3bd73SGreg Roach                    }
2428ce3bd73SGreg Roach
2438ce3bd73SGreg Roach                    $url = route(AdminMediaFileDownload::class, ['path' => $row[0]]);
2448ce3bd73SGreg Roach                    $img = '<a href="' . e($url) . '">' . $img . '</a>';
2458ce3bd73SGreg Roach
2468ce3bd73SGreg Roach                    // Form to create new media object in each tree
2478ce3bd73SGreg Roach                    $create_form = '';
2488ce3bd73SGreg Roach                    foreach ($media_trees as $media_tree => $media_directory) {
2498ce3bd73SGreg Roach                        if (str_starts_with($row[0], $media_directory)) {
2508ce3bd73SGreg Roach                            $tmp = substr($row[0], strlen($media_directory));
2518ce3bd73SGreg Roach                            $create_form .=
252315eb316SGreg Roach                                '<p><a href="#" data-bs-toggle="modal" data-bs-backdrop="static" data-bs-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>';
2538ce3bd73SGreg Roach                        }
2548ce3bd73SGreg Roach                    }
2558ce3bd73SGreg Roach
256d4786c66SGreg Roach                    $delete_link = '<p><a data-wt-confirm="' . I18N::translate('Are you sure you want to delete “%s”?', e($row[0])) . '" data-wt-post-url="' . e(route(DeletePath::class, [
2578ce3bd73SGreg Roach                            'path' => $row[0],
2588ce3bd73SGreg Roach                        ])) . '" href="#">' . I18N::translate('Delete') . '</a></p>';
2598ce3bd73SGreg Roach
2608ce3bd73SGreg Roach                    return [
2618ce3bd73SGreg Roach                        $this->mediaFileInfo($data_filesystem, $row[0]) . $delete_link,
2628ce3bd73SGreg Roach                        $img,
2638ce3bd73SGreg Roach                        $create_form,
2648ce3bd73SGreg Roach                    ];
2658ce3bd73SGreg Roach                };
2668ce3bd73SGreg Roach
2678ce3bd73SGreg Roach                return $this->datatables_service->handleCollection($request, $unused_files, $search_columns, $sort_columns, $callback);
2688ce3bd73SGreg Roach
2698ce3bd73SGreg Roach            default:
2708ce3bd73SGreg Roach                throw new HttpNotFoundException();
2718ce3bd73SGreg Roach        }
2728ce3bd73SGreg Roach    }
2738ce3bd73SGreg Roach
2748ce3bd73SGreg Roach    /**
2758ce3bd73SGreg Roach     * Generate some useful information and links about a media object.
2768ce3bd73SGreg Roach     *
2778ce3bd73SGreg Roach     * @param Media $media
2788ce3bd73SGreg Roach     *
2798ce3bd73SGreg Roach     * @return string HTML
2808ce3bd73SGreg Roach     */
2818ce3bd73SGreg Roach    private function mediaObjectInfo(Media $media): string
2828ce3bd73SGreg Roach    {
283d72643a1SDavid Drury        $element = Registry::elementFactory()->make('NOTE:CONC');
284c88ea636SGreg Roach        $html    = '<a href="' . e($media->url()) . '" title="' . e($media->tree()->title()) . '">' . $media->fullName() . '</a>';
285c88ea636SGreg Roach
286c88ea636SGreg Roach        if ($this->tree_service->all()->count() > 1) {
287c88ea636SGreg Roach            $html .= ' — ' . e($media->tree()->title());
288c88ea636SGreg Roach        }
289c88ea636SGreg Roach
290c88ea636SGreg Roach        $html .= $element->value($media->getNote(), $media->tree());
2918ce3bd73SGreg Roach
2928ce3bd73SGreg Roach        $linked = [];
2934991f205SGreg Roach
2944991f205SGreg Roach        foreach ($this->linked_record_service->linkedIndividuals($media) as $link) {
295c88ea636SGreg Roach            $linked[] = view('icons/individual') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2968ce3bd73SGreg Roach        }
2974991f205SGreg Roach
2984991f205SGreg Roach        foreach ($this->linked_record_service->linkedFamilies($media) as $link) {
299c88ea636SGreg Roach            $linked[] = view('icons/family') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
3008ce3bd73SGreg Roach        }
3014991f205SGreg Roach
3024991f205SGreg Roach        foreach ($this->linked_record_service->linkedSources($media) as $link) {
303c88ea636SGreg Roach            $linked[] = view('icons/source') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
3048ce3bd73SGreg Roach        }
3054991f205SGreg Roach
3064991f205SGreg Roach        foreach ($this->linked_record_service->linkedNotes($media) as $link) {
307c88ea636SGreg Roach            $linked[] = view('icons/note') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
3088ce3bd73SGreg Roach        }
3094991f205SGreg Roach
3104991f205SGreg Roach        foreach ($this->linked_record_service->linkedRepositories($media) as $link) {
311c88ea636SGreg Roach            $linked[] = view('icons/media') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
3128ce3bd73SGreg Roach        }
3134991f205SGreg Roach
3144991f205SGreg Roach        foreach ($this->linked_record_service->linkedMedia($media) as $link) {
315c88ea636SGreg Roach            $linked[] = view('icons/location') . '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
3168ce3bd73SGreg Roach        }
317c88ea636SGreg Roach
3188ce3bd73SGreg Roach        if ($linked !== []) {
319c88ea636SGreg Roach            $html .= '<ul class="list-unstyled">';
3208ce3bd73SGreg Roach            foreach ($linked as $link) {
3218ce3bd73SGreg Roach                $html .= '<li>' . $link . '</li>';
3228ce3bd73SGreg Roach            }
3238ce3bd73SGreg Roach            $html .= '</ul>';
3248ce3bd73SGreg Roach        } else {
3258ce3bd73SGreg Roach            $html .= '<div class="alert alert-danger">' . I18N::translate('There are no links to this media object.') . '</div>';
3268ce3bd73SGreg Roach        }
3278ce3bd73SGreg Roach
3288ce3bd73SGreg Roach        return $html;
3298ce3bd73SGreg Roach    }
3308ce3bd73SGreg Roach
3318ce3bd73SGreg Roach    /**
3328ce3bd73SGreg Roach     * Generate some useful information and links about a media file.
3338ce3bd73SGreg Roach     *
334f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
3358ce3bd73SGreg Roach     * @param string             $file
3368ce3bd73SGreg Roach     *
3378ce3bd73SGreg Roach     * @return string
3388ce3bd73SGreg Roach     */
339f7cf8a15SGreg Roach    private function mediaFileInfo(FilesystemOperator $data_filesystem, string $file): string
3408ce3bd73SGreg Roach    {
3418ce3bd73SGreg Roach        $html = '<dl>';
3428ce3bd73SGreg Roach        $html .= '<dt>' . I18N::translate('Filename') . '</dt>';
3438ce3bd73SGreg Roach        $html .= '<dd>' . e($file) . '</dd>';
3448ce3bd73SGreg Roach
345f0448b68SGreg Roach        try {
346f0448b68SGreg Roach            $file_exists = $data_filesystem->fileExists($file);
34728d026adSGreg Roach        } catch (FilesystemException | UnableToCheckFileExistence) {
348f0448b68SGreg Roach            $file_exists = false;
349f0448b68SGreg Roach        }
350f0448b68SGreg Roach
351f0448b68SGreg Roach        if ($file_exists) {
352f0448b68SGreg Roach            try {
353f7cf8a15SGreg Roach                $size = $data_filesystem->fileSize($file);
35428d026adSGreg Roach            } catch (FilesystemException | UnableToRetrieveMetadata) {
355f0448b68SGreg Roach                $size = 0;
356f0448b68SGreg Roach            }
3578ce3bd73SGreg Roach            $size = intdiv($size + 1023, 1024); // Round up to next KB
3588ce3bd73SGreg Roach            /* I18N: size of file in KB */
3598ce3bd73SGreg Roach            $size = I18N::translate('%s KB', I18N::number($size));
3608ce3bd73SGreg Roach            $html .= '<dt>' . I18N::translate('File size') . '</dt>';
3618ce3bd73SGreg Roach            $html .= '<dd>' . $size . '</dd>';
3628ce3bd73SGreg Roach
3638ce3bd73SGreg Roach            try {
3648ce3bd73SGreg Roach                // This will work for local filesystems.  For remote filesystems, we will
3658ce3bd73SGreg Roach                // need to copy the file locally to work out the image size.
366f7cf8a15SGreg Roach                $imgsize = getimagesizefromstring($data_filesystem->read($file));
3678ce3bd73SGreg Roach                $html .= '<dt>' . I18N::translate('Image dimensions') . '</dt>';
3688ce3bd73SGreg Roach                /* I18N: image dimensions, width × height */
3698ce3bd73SGreg Roach                $html .= '<dd>' . I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])) . '</dd>';
37028d026adSGreg Roach            } catch (FilesystemException | UnableToReadFile | Throwable) {
3718ce3bd73SGreg Roach                // Not an image, or not a valid image?
3728ce3bd73SGreg Roach            }
3738ce3bd73SGreg Roach        }
3748ce3bd73SGreg Roach
3758ce3bd73SGreg Roach        $html .= '</dl>';
3768ce3bd73SGreg Roach
3778ce3bd73SGreg Roach        return $html;
3788ce3bd73SGreg Roach    }
3798ce3bd73SGreg Roach}
380