2023-01-18 03:02:00 +00:00
|
|
|
<?php
|
|
|
|
|
2023-10-15 02:20:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-10-15 01:51:17 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2023-03-23 23:58:49 +00:00
|
|
|
use OCA\Memories\Db\AlbumsQuery;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\AppFramework\Controller;
|
2023-03-10 02:13:29 +00:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2023-10-14 23:06:25 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2023-05-23 04:21:22 +00:00
|
|
|
use OCP\AppFramework\Http\Template\LinkMenuAction;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2023-03-14 20:42:55 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\IConfig;
|
2023-05-23 04:21:22 +00:00
|
|
|
use OCP\IL10N;
|
2023-10-15 01:51:17 +00:00
|
|
|
use OCP\IRequest;
|
2023-03-10 02:13:29 +00:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserSession;
|
2023-01-18 03:02:00 +00:00
|
|
|
use OCP\Util;
|
|
|
|
|
|
|
|
class PublicAlbumController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct(
|
2023-10-15 01:51:17 +00:00
|
|
|
IRequest $request,
|
|
|
|
protected IEventDispatcher $eventDispatcher,
|
|
|
|
protected IInitialState $initialState,
|
|
|
|
protected IAppManager $appManager,
|
|
|
|
protected IConfig $config,
|
|
|
|
protected IUserSession $userSession,
|
|
|
|
protected IRootFolder $rootFolder,
|
|
|
|
protected IURLGenerator $urlGenerator,
|
|
|
|
protected AlbumsQuery $albumsQuery,
|
2023-10-15 01:59:00 +00:00
|
|
|
protected IL10N $l10n,
|
2023-01-18 03:02:00 +00:00
|
|
|
) {
|
2023-10-15 01:51:17 +00:00
|
|
|
parent::__construct(Application::APPNAME, $request);
|
2023-01-18 03:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function showShare(string $token): Response
|
2023-01-18 03:02:00 +00:00
|
|
|
{
|
|
|
|
// Validate token exists
|
2023-03-23 23:58:49 +00:00
|
|
|
$album = $this->albumsQuery->getAlbumByLink($token);
|
2023-01-18 03:02:00 +00:00
|
|
|
if (!$album) {
|
|
|
|
return new TemplateResponse('core', '404', [], 'guest');
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:13:29 +00:00
|
|
|
// Check if the current user has access to the album
|
|
|
|
// Just redirect to the user's page if the user is the owner or a collaborator
|
|
|
|
if ($user = $this->userSession->getUser()) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$albumId = (int) $album['album_id'];
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($uid === $album['user'] || $this->albumsQuery->userIsCollaborator($uid, $albumId)) {
|
2023-03-10 02:13:29 +00:00
|
|
|
$idStr = $album['user'].'/'.$album['name'];
|
2023-06-26 21:53:32 +00:00
|
|
|
$url = $this->urlGenerator->linkToRoute('memories.Page.albums', [
|
|
|
|
'id' => $idStr, // id of album
|
|
|
|
'noinit' => 1, // prevent showing first-start page
|
|
|
|
]);
|
2023-03-10 02:13:29 +00:00
|
|
|
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browse anonymously if the album is accessed as a link
|
|
|
|
\OC_User::setIncognitoMode(true);
|
|
|
|
|
2023-11-14 09:09:59 +00:00
|
|
|
// Get page title
|
|
|
|
$title = $album['name'];
|
|
|
|
if (str_starts_with($title, '.link-')) {
|
|
|
|
$title = $this->l10n->t('Shared Link');
|
|
|
|
}
|
|
|
|
|
2023-03-14 20:42:55 +00:00
|
|
|
// Add OG metadata
|
2023-11-14 09:09:59 +00:00
|
|
|
$this->addOgMetadata($album, $title, $token);
|
2023-03-14 20:42:55 +00:00
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
// Scripts
|
2023-10-15 01:51:17 +00:00
|
|
|
Util::addScript(Application::APPNAME, 'memories-main');
|
2023-01-18 03:02:00 +00:00
|
|
|
|
2023-05-25 04:59:03 +00:00
|
|
|
// Share info
|
2023-11-14 09:09:59 +00:00
|
|
|
$this->initialState->provideInitialState('share_title', $title);
|
2023-10-20 05:32:34 +00:00
|
|
|
$this->initialState->provideInitialState('share_type', 'album');
|
2023-05-25 04:59:03 +00:00
|
|
|
|
2023-05-23 04:21:22 +00:00
|
|
|
// Render main template
|
2023-10-15 01:51:17 +00:00
|
|
|
$response = new PublicTemplateResponse(Application::APPNAME, 'main', PageController::getMainParams());
|
2023-11-14 09:09:59 +00:00
|
|
|
$response->setHeaderTitle($title);
|
2023-01-18 03:02:00 +00:00
|
|
|
$response->setFooterVisible(false); // wth is that anyway?
|
2023-01-18 03:09:02 +00:00
|
|
|
$response->setContentSecurityPolicy(PageController::getCSP());
|
2023-01-18 03:02:00 +00:00
|
|
|
|
2023-05-23 04:21:22 +00:00
|
|
|
// Add download link
|
|
|
|
$dlUrl = $this->urlGenerator->linkToRoute('memories.PublicAlbum.download', [
|
|
|
|
'token' => $token, // share identification
|
|
|
|
'albums' => 1, // identify backend for share
|
|
|
|
]);
|
|
|
|
$dlAction = new LinkMenuAction($this->l10n->t('Download'), 'icon-download', $dlUrl);
|
|
|
|
$response->setHeaderActions([$dlAction]);
|
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
return $response;
|
|
|
|
}
|
2023-03-14 20:42:55 +00:00
|
|
|
|
2023-05-23 04:21:22 +00:00
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
2023-10-14 23:06:25 +00:00
|
|
|
public function download(string $token): Response
|
2023-05-23 04:21:22 +00:00
|
|
|
{
|
|
|
|
$album = $this->albumsQuery->getAlbumByLink($token);
|
|
|
|
if (!$album) {
|
|
|
|
return new TemplateResponse('core', '404', [], 'guest');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list of files
|
|
|
|
$albumId = (int) $album['album_id'];
|
2023-10-14 23:06:25 +00:00
|
|
|
$files = $this->albumsQuery->getAlbumPhotos($albumId, null);
|
2023-08-30 18:10:08 +00:00
|
|
|
$fileIds = array_map(static fn ($file) => (int) $file['file_id'], $files);
|
2023-05-23 04:21:22 +00:00
|
|
|
|
|
|
|
// Get download handle
|
|
|
|
$downloadController = \OC::$server->get(\OCA\Memories\Controller\DownloadController::class);
|
|
|
|
$handle = $downloadController::createHandle($album['name'], $fileIds);
|
|
|
|
|
|
|
|
// Start download
|
|
|
|
return $downloadController->file($handle);
|
|
|
|
}
|
|
|
|
|
2023-11-14 09:09:59 +00:00
|
|
|
private function addOgMetadata(array $album, string $title, string $token): void
|
2023-03-14 20:42:55 +00:00
|
|
|
{
|
|
|
|
$fileId = (int) $album['last_added_photo'];
|
|
|
|
$albumId = (int) $album['album_id'];
|
2023-03-23 23:58:49 +00:00
|
|
|
$owner = $this->albumsQuery->hasFile($albumId, $fileId);
|
2023-03-14 20:42:55 +00:00
|
|
|
if (!$owner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
|
|
|
|
if (0 === \count($nodes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$node = $nodes[0];
|
|
|
|
|
|
|
|
$params = ['token' => $token];
|
|
|
|
$url = $this->urlGenerator->linkToRouteAbsolute('memories.PublicAlbum.showShare', $params);
|
2023-11-14 09:09:59 +00:00
|
|
|
\OCA\Memories\Util::addOGMetadata($node, $title, $url, array_merge($params, ['albums' => true]));
|
2023-03-14 20:42:55 +00:00
|
|
|
}
|
2023-01-18 03:02:00 +00:00
|
|
|
}
|