Skip to content

Commit

Permalink
Create photos sidebar tab
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Nov 9, 2023
1 parent 5d5bcee commit 88f9312
Show file tree
Hide file tree
Showing 9 changed files with 659 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\Files\Event\LoadSidebar;
use OCA\Photos\Listener\AlbumsManagementEventListener;
use OCA\Photos\Listener\ExifMetadataProvider;
use OCA\Photos\Listener\OriginalDateTimeMetadataProvider;
Expand All @@ -43,6 +44,7 @@
use OCP\FilesMetadata\Event\MetadataLiveEvent;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Share\Events\ShareDeletedEvent;
use OCP\SystemTag\MapperEvent;
use OCP\User\Events\UserDeletedEvent;
Expand Down Expand Up @@ -79,6 +81,10 @@ public function register(IRegistrationContext $context): void {
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);

$context->registerEventListener(LoadSidebar::class, LoadSidebarScripts::class);

$context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class);

// Metadata
$context->registerEventListener(MetadataLiveEvent::class, ExifMetadataProvider::class);
$context->registerEventListener(MetadataLiveEvent::class, SizeMetadataProvider::class);
Expand Down
51 changes: 51 additions & 0 deletions lib/Listener/CSPListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2023, Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\Listener;

use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;

/**
* @template-implements IEventListener<Event>
*/
class CSPListener implements IEventListener {

public function __construct(
) {
}

public function handle(Event $event): void {
if (!($event instanceof AddContentSecurityPolicyEvent)) {
return;
}

$csp = new ContentSecurityPolicy();
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$event->addPolicy($csp);
}
}
24 changes: 18 additions & 6 deletions lib/Listener/ExifMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

namespace OCA\Photos\Listener;
namespace OCA\Photos\MetadataProvider;

use OCA\Photos\AppInfo\Application;
use OCP\EventDispatcher\Event;
Expand Down Expand Up @@ -80,11 +80,11 @@ public function handle(Event $event): void {
}

if ($rawExifData && array_key_exists('EXIF', $rawExifData)) {
$event->getMetadata()->setArray('photos-exif', $this->base64Encode($rawExifData['EXIF']));
$event->getMetadata()->setArray('photos-exif', $this->sanitizeEntries($rawExifData['EXIF']));
}

if ($rawExifData && array_key_exists('IFD0', $rawExifData)) {
$event->getMetadata()->setArray('photos-ifd0', $this->base64Encode($rawExifData['IFD0']));
$event->getMetadata()->setArray('photos-ifd0', $this->sanitizeEntries($rawExifData['IFD0']));
}

if (
Expand Down Expand Up @@ -142,14 +142,26 @@ private function parseGPSData(string $rawData): float {
/**
* Exif data can contain anything.
* This method will base 64 encode any non UTF-8 string in an array.
* This will also remove control characters from UTF-8 strings.
*/
private function base64Encode(array $data): array {
private function sanitizeEntries(array $data): array {
$cleanData = [];

foreach ($data as $key => $value) {
if (is_string($value) && !mb_check_encoding($value, 'UTF-8')) {
$data[$key] = 'base64:'.base64_encode($value);
$value = 'base64:'.base64_encode($value);
} elseif (is_string($value)) {
// TODO: Can be remove when the Sidebar use the @nextcloud/files to fetch and parse the DAV response.
$value = preg_replace('/[[:cntrl:]]/u', '', $value);
}

if (preg_match('/[^a-zA-Z]/', $key) !== 0) {
$key = preg_replace('/[^a-zA-Z]/', '_', $key);
}

$cleanData[$key] = $value;
}

return $data;
return $cleanData;
}
}
57 changes: 57 additions & 0 deletions lib/Listener/LoadSidebarScripts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2023, Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\Listener;

use OCA\Files\Event\LoadSidebar;
use OCA\Photos\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IRequest;
use OCP\Util;

/**
* @template-implements IEventListener<Event>
*/
class LoadSidebarScripts implements IEventListener {

public function __construct(
private IRequest $request,
) {
}

public function handle(Event $event): void {
if (!($event instanceof LoadSidebar)) {
return;
}

// Only load sidebar tab in the photos app.
if (!preg_match('/^photos\.page\..+/', $this->request->getParams()['_route'])) {
return;
}

Util::addScript(Application::APP_ID, 'photos-sidebar');
}
}
45 changes: 42 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"@essentials/request-timeout": "^1.3.0",
"@mdi/svg": "^7.1.96",
"@mdi/svg": "^7.3.67",
"@nextcloud/auth": "^2.1.0",
"@nextcloud/axios": "^2.1.0",
"@nextcloud/dialogs": "^4.1.0",
Expand All @@ -57,6 +57,7 @@
"camelcase": "^7.0.0",
"debounce": "^1.2.1",
"he": "^1.2.0",
"leaflet-defaulticon-compatibility": "^0.1.2",
"path-posix": "^1.0.0",
"qs": "^6.11.2",
"url-parse": "^1.5.10",
Expand All @@ -65,6 +66,7 @@
"vue-router": "^3.6.5",
"vue-template-compiler": "^2.7.14",
"vue-virtual-grid": "^2.5.0",
"vue2-leaflet": "^2.7.1",
"vuex": "^3.6.2",
"vuex-router-sync": "^5.0.0",
"webdav": "^4.11.0"
Expand Down
Loading

0 comments on commit 88f9312

Please sign in to comment.