3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Media\Photo;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Core\System;
30 use Friendica\Model\Photo;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException\UnauthorizedException;
33 use Friendica\Util\Images;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Strings;
36 use Psr\Log\LoggerInterface;
41 class Browser extends BaseModule
43 /** @var IHandleUserSessions */
48 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, App $app, array $server, array $parameters = [])
50 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52 $this->session = $session;
56 protected function content(array $request = []): string
58 if (!$this->session->getLocalUserId()) {
59 throw new UnauthorizedException($this->t('You need to be logged in to access this page.'));
62 // Needed to match the correct template in a module that uses a different theme than the user/site/default
63 $theme = Strings::sanitizeFilePathItem($request['theme'] ?? '');
64 if ($theme && is_file("view/theme/$theme/config.php")) {
65 $this->app->setCurrentTheme($theme);
68 $album = $this->parameters['album'] ?? null;
70 $photos = Photo::getBrowsablePhotosForUser($this->session->getLocalUserId(), $album);
71 $albums = $album ? false : Photo::getBrowsableAlbumsForUser($this->session->getLocalUserId());
74 '' => $this->t('Photos'),
77 $path[$album] = $album;
80 $photosArray = array_map([$this, 'map_files'], $photos);
82 $tpl = Renderer::getMarkupTemplate('media/browser.tpl');
83 $output = Renderer::replaceMacros($tpl, [
86 '$folders' => $albums,
87 '$files' => $photosArray,
88 '$cancel' => $this->t('Cancel'),
89 '$nickname' => $this->app->getLoggedInUserNickname(),
90 '$upload' => $this->t('Upload'),
93 if (empty($request['mode'])) {
94 System::httpExit($output);
100 protected function map_files(array $record): array
102 $types = Images::supportedTypes();
103 $ext = $types[$record['type']];
104 $filename_e = $record['filename'];
106 // Take the largest picture that is smaller or equal 640 pixels
107 $photo = Photo::selectFirst(
110 "`resource-id` = ? AND `height` <= ? AND `width` <= ?",
111 $record['resource-id'],
115 ['order' => ['scale']]);
116 $scale = $photo['scale'] ?? $record['loq'];
119 sprintf('%s/photos/%s/image/%s', $this->baseUrl, $this->app->getLoggedInUserNickname(), $record['resource-id']),
121 sprintf('%s/photo/%s-%s.%s', $this->baseUrl, $record['resource-id'], $scale, $ext),