Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2024-09-14 20:21:41 +02:00
Родитель 749fa2d058
Коммит b059d12c92
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A3E2F658B28C760A
82 изменённых файлов: 8736 добавлений и 9009 удалений

Просмотреть файл

@ -8,32 +8,32 @@
* it's instantiated in there
*/
return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#indexMyMap', 'url' => '/m/{myMapId}', 'verb' => 'GET'],
['name' => 'page#do_echo', 'url' => '/echo', 'verb' => 'POST'],
['name' => 'page#openGeoLink', 'url' => '/openGeoLink/{url}', 'verb' => 'GET'],
['name' => 'public_favorite_page#sharedFavoritesCategory', 'url' => '/s/favorites/{token}', 'verb' => 'GET'],
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#indexMyMap', 'url' => '/m/{myMapId}', 'verb' => 'GET'],
['name' => 'page#do_echo', 'url' => '/echo', 'verb' => 'POST'],
['name' => 'page#openGeoLink', 'url' => '/openGeoLink/{url}', 'verb' => 'GET'],
['name' => 'public_favorite_page#sharedFavoritesCategory', 'url' => '/s/favorites/{token}', 'verb' => 'GET'],
['name' => 'PublicPage#showShare', 'url' => '/s/{token}', 'verb' => 'GET'],
['name' => 'PublicPage#showAuthenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'GET'],
['name' => 'PublicPage#authenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'POST'],
// utils
['name' => 'utils#getOptionsValues', 'url' => '/getOptionsValues', 'verb' => 'GET'],
['name' => 'utils#saveOptionValue', 'url' => '/saveOptionValue', 'verb' => 'POST'],
['name' => 'utils#setRoutingSettings', 'url' => '/setRoutingSettings', 'verb' => 'POST'],
['name' => 'utils#getTrafficStyle', 'url' => '/style/traffic', 'verb' => 'GET'],
// utils
['name' => 'utils#getOptionsValues', 'url' => '/getOptionsValues', 'verb' => 'GET'],
['name' => 'utils#saveOptionValue', 'url' => '/saveOptionValue', 'verb' => 'POST'],
['name' => 'utils#setRoutingSettings', 'url' => '/setRoutingSettings', 'verb' => 'POST'],
['name' => 'utils#getTrafficStyle', 'url' => '/style/traffic', 'verb' => 'GET'],
['name' => 'PublicUtils#getOptionsValues', 'url' => '/s/{token}/getOptionsValues', 'verb' => 'GET'],
['name' => 'PublicUtils#saveOptionValue', 'url' => '/s/{token}/saveOptionValue', 'verb' => 'POST'],
['name' => 'PublicUtils#setRoutingSettings', 'url' => '/s/{token}/setRoutingSettings', 'verb' => 'POST'],
['name' => 'PublicUtils#getTrafficStyle', 'url' => '/s/{token}/style/traffic', 'verb' => 'GET'],
// photos
['name' => 'photos#getPhotos', 'url' => '/photos', 'verb' => 'GET'],
['name' => 'photos#getNonLocalizedPhotos', 'url' => '/photos/nonlocalized', 'verb' => 'GET'],
['name' => 'photos#placePhotos', 'url' => '/photos', 'verb' => 'POST'],
['name' => 'photos#resetPhotosCoords', 'url' => '/photos', 'verb' => 'DELETE'],
// photos
['name' => 'photos#getPhotos', 'url' => '/photos', 'verb' => 'GET'],
['name' => 'photos#getNonLocalizedPhotos', 'url' => '/photos/nonlocalized', 'verb' => 'GET'],
['name' => 'photos#placePhotos', 'url' => '/photos', 'verb' => 'POST'],
['name' => 'photos#resetPhotosCoords', 'url' => '/photos', 'verb' => 'DELETE'],
['name' => 'photos#clearCache', 'url' => '/photos/clearCache', 'verb' => 'GET'],
['name' => 'photos#getBackgroundJobStatus', 'url' => '/photos/backgroundJobStatus', 'verb' => 'GET'],
@ -41,51 +41,51 @@ return [
['name' => 'PublicPhotos#getNonLocalizedPhotos', 'url' => '/s/{token}/photos/nonlocalized', 'verb' => 'GET'],
['name' => 'PublicPhotos#clearCache', 'url' => '/s/{token}/photos/clearCache', 'verb' => 'GET'],
// contacts
['name' => 'contacts#getContacts', 'url' => '/contacts', 'verb' => 'GET'],
['name' => 'contacts#searchContacts', 'url' => '/contacts-search', 'verb' => 'GET'],
['name' => 'contacts#placeContact', 'url' => '/contacts/{bookid}/{uri}', 'verb' => 'PUT'],
// contacts
['name' => 'contacts#getContacts', 'url' => '/contacts', 'verb' => 'GET'],
['name' => 'contacts#searchContacts', 'url' => '/contacts-search', 'verb' => 'GET'],
['name' => 'contacts#placeContact', 'url' => '/contacts/{bookid}/{uri}', 'verb' => 'PUT'],
['name' => 'contacts#addContactToMap', 'url' => '/contacts/{bookid}/{uri}/add-to-map/', 'verb' => 'PUT'],
['name' => 'contacts#deleteContactAddress', 'url' => '/contacts/{bookid}/{uri}', 'verb' => 'DELETE'],
['name' => 'contacts#getContactLetterAvatar', 'url' => '/contacts-avatar', 'verb' => 'GET'],
['name' => 'contacts#deleteContactAddress', 'url' => '/contacts/{bookid}/{uri}', 'verb' => 'DELETE'],
['name' => 'contacts#getContactLetterAvatar', 'url' => '/contacts-avatar', 'verb' => 'GET'],
['name' => 'PublicContacts#getContacts', 'url' => '/s/{token}/contacts', 'verb' => 'GET'],
['name' => 'PublicContacts#getContactLetterAvatar', 'url' => '/s/{token}/contacts-avatar', 'verb' => 'GET'],
// routing
['name' => 'routing#exportRoute', 'url' => '/exportRoute', 'verb' => 'POST'],
// routing
['name' => 'routing#exportRoute', 'url' => '/exportRoute', 'verb' => 'POST'],
// favorites API
[
'name' => 'favorites_api#preflighted_cors',
'url' => '/api/1.0/favorites{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'favorites_api#getFavorites', 'url' => '/api/{apiversion}/favorites', 'verb' => 'GET'],
['name' => 'favorites_api#addFavorite', 'url' => '/api/{apiversion}/favorites', 'verb' => 'POST'],
['name' => 'favorites_api#editFavorite', 'url' => '/api/{apiversion}/favorites/{id}', 'verb' => 'PUT'],
['name' => 'favorites_api#deleteFavorite', 'url' => '/api/{apiversion}/favorites/{id}', 'verb' => 'DELETE'],
// favorites API
[
'name' => 'favorites_api#preflighted_cors',
'url' => '/api/1.0/favorites{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'favorites_api#getFavorites', 'url' => '/api/{apiversion}/favorites', 'verb' => 'GET'],
['name' => 'favorites_api#addFavorite', 'url' => '/api/{apiversion}/favorites', 'verb' => 'POST'],
['name' => 'favorites_api#editFavorite', 'url' => '/api/{apiversion}/favorites/{id}', 'verb' => 'PUT'],
['name' => 'favorites_api#deleteFavorite', 'url' => '/api/{apiversion}/favorites/{id}', 'verb' => 'DELETE'],
// public favorites API
[
'name' => 'favorites_api#preflighted_cors',
'url' => '/api/1.0/public/favorites{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'public_favorites_api#getFavorites', 'url' => '/api/1.0/public/{token}/favorites', 'verb' => 'GET'],
// ['name' => 'public_favorites_api#addFavorite', 'url' => '/api/1.0/public/{token}/favorites', 'verb' => 'POST'],
// ['name' => 'public_favorites_api#editFavorite', 'url' => '/api/1.0/public/{token}/favorites/{id}', 'verb' => 'PUT'],
// ['name' => 'public_favorites_api#deleteFavorite', 'url' => '/api/1.0/public/{token}/favorites/{id}', 'verb' => 'DELETE'],
// public favorites API
[
'name' => 'favorites_api#preflighted_cors',
'url' => '/api/1.0/public/favorites{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'public_favorites_api#getFavorites', 'url' => '/api/1.0/public/{token}/favorites', 'verb' => 'GET'],
// ['name' => 'public_favorites_api#addFavorite', 'url' => '/api/1.0/public/{token}/favorites', 'verb' => 'POST'],
// ['name' => 'public_favorites_api#editFavorite', 'url' => '/api/1.0/public/{token}/favorites/{id}', 'verb' => 'PUT'],
// ['name' => 'public_favorites_api#deleteFavorite', 'url' => '/api/1.0/public/{token}/favorites/{id}', 'verb' => 'DELETE'],
// favorites
['name' => 'favorites#getFavorites', 'url' => '/favorites', 'verb' => 'GET'],
['name' => 'favorites#addFavorite', 'url' => '/favorite', 'verb' => 'POST'],
// favorites
['name' => 'favorites#getFavorites', 'url' => '/favorites', 'verb' => 'GET'],
['name' => 'favorites#addFavorite', 'url' => '/favorite', 'verb' => 'POST'],
['name' => 'favorites#addFavorites', 'url' => '/favorites', 'verb' => 'POST'],
['name' => 'favorites#editFavorite', 'url' => '/favorites/{id}', 'verb' => 'PUT'],
['name' => 'favorites#deleteFavorite', 'url' => '/favorites/{id}', 'verb' => 'DELETE'],
['name' => 'favorites#deleteFavorites', 'url' => '/favorites', 'verb' => 'DELETE'],
['name' => 'favorites#editFavorite', 'url' => '/favorites/{id}', 'verb' => 'PUT'],
['name' => 'favorites#deleteFavorite', 'url' => '/favorites/{id}', 'verb' => 'DELETE'],
['name' => 'favorites#deleteFavorites', 'url' => '/favorites', 'verb' => 'DELETE'],
['name' => 'PublicFavorites#getFavorites', 'url' => '/s/{token}/favorites', 'verb' => 'GET'],
['name' => 'PublicFavorites#addFavorite', 'url' => '/s/{token}/favorite', 'verb' => 'POST'],
@ -94,54 +94,54 @@ return [
['name' => 'PublicFavorites#deleteFavorite', 'url' => '/s/{token}/favorites/{id}', 'verb' => 'DELETE'],
['name' => 'PublicFavorites#deleteFavorites', 'url' => '/s/{token}/favorites', 'verb' => 'DELETE'],
// favorite categories
['name' => 'favorites#renameCategories', 'url' => '/favorites-category', 'verb' => 'PUT'],
['name' => 'favorites#getSharedCategories', 'url' => '/favorites-category/shared', 'verb' => 'GET'],
['name' => 'favorites#shareCategory', 'url' => '/favorites-category/{category}/share', 'verb' => 'POST'],
['name' => 'favorites#unShareCategory', 'url' => '/favorites-category/{category}/un-share', 'verb' => 'POST'],
// favorite categories
['name' => 'favorites#renameCategories', 'url' => '/favorites-category', 'verb' => 'PUT'],
['name' => 'favorites#getSharedCategories', 'url' => '/favorites-category/shared', 'verb' => 'GET'],
['name' => 'favorites#shareCategory', 'url' => '/favorites-category/{category}/share', 'verb' => 'POST'],
['name' => 'favorites#unShareCategory', 'url' => '/favorites-category/{category}/un-share', 'verb' => 'POST'],
['name' => 'favorites#addShareCategoryToMap', 'url' => '/favorites-category/{category}/add-to-map/{targetMapId}', 'verb' => 'PUT'],
['name' => 'favorites#removeShareCategoryFromMap', 'url' => '/favorites-category/{category}/', 'verb' => 'DELETE'],
['name' => 'favorites#exportFavorites', 'url' => '/export/favorites', 'verb' => 'POST'],
['name' => 'favorites#importFavorites', 'url' => '/import/favorites', 'verb' => 'POST'],
['name' => 'favorites#exportFavorites', 'url' => '/export/favorites', 'verb' => 'POST'],
['name' => 'favorites#importFavorites', 'url' => '/import/favorites', 'verb' => 'POST'],
['name' => 'PublicFavorites#renameCategories', 'url' => '/s/{token}/favorites-category', 'verb' => 'PUT'],
['name' => 'PublicFavorites#getSharedCategories', 'url' => '/s/{token}/favorites-category/shared', 'verb' => 'GET'],
// tracks
['name' => 'tracks#getTracks', 'url' => '/tracks', 'verb' => 'GET'],
['name' => 'tracks#getTrackFileContent', 'url' => '/tracks/{id}', 'verb' => 'GET'],
// tracks
['name' => 'tracks#getTracks', 'url' => '/tracks', 'verb' => 'GET'],
['name' => 'tracks#getTrackFileContent', 'url' => '/tracks/{id}', 'verb' => 'GET'],
['name' => 'tracks#getTrackContentByFileId', 'url' => '/tracks/file/{id}', 'verb' => 'GET'],
['name' => 'tracks#editTrack', 'url' => '/tracks/{id}', 'verb' => 'PUT'],
['name' => 'tracks#editTrack', 'url' => '/tracks/{id}', 'verb' => 'PUT'],
['name' => 'PublicTracks#getTracks', 'url' => '/s/{token}/tracks', 'verb' => 'GET'],
['name' => 'PublicTracks#getTrackFileContent', 'url' => '/s/{token}/tracks/{id}', 'verb' => 'GET'],
['name' => 'PublicTracks#getTrackContentByFileId', 'url' => '/s/{token}/tracks/file/{id}', 'verb' => 'GET'],
['name' => 'PublicTracks#editTrack', 'url' => '/s/{token}/tracks/{id}', 'verb' => 'PUT'],
// devices API
[
'name' => 'devices_api#preflighted_cors',
'url' => '/api/1.0/devices{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'devices_api#getDevices', 'url' => '/api/{apiversion}/devices', 'verb' => 'GET'],
['name' => 'devices_api#getDevicePoints', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'GET'],
['name' => 'devices_api#addDevicePoint', 'url' => '/api/{apiversion}/devices', 'verb' => 'POST'],
['name' => 'devices_api#editDevice', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'PUT'],
['name' => 'devices_api#deleteDevice', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'DELETE'],
// devices API
[
'name' => 'devices_api#preflighted_cors',
'url' => '/api/1.0/devices{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
['name' => 'devices_api#getDevices', 'url' => '/api/{apiversion}/devices', 'verb' => 'GET'],
['name' => 'devices_api#getDevicePoints', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'GET'],
['name' => 'devices_api#addDevicePoint', 'url' => '/api/{apiversion}/devices', 'verb' => 'POST'],
['name' => 'devices_api#editDevice', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'PUT'],
['name' => 'devices_api#deleteDevice', 'url' => '/api/{apiversion}/devices/{id}', 'verb' => 'DELETE'],
// devices
['name' => 'devices#getDevices', 'url' => '/devices', 'verb' => 'GET'],
// devices
['name' => 'devices#getDevices', 'url' => '/devices', 'verb' => 'GET'],
['name' => 'devices#getDevicesByTokens', 'url' => '/devices/by-token', 'verb' => 'GET'],
['name' => 'devices#getDevicePoints', 'url' => '/devices/{id}', 'verb' => 'GET'],
['name' => 'devices#addDevicePoint', 'url' => '/devices', 'verb' => 'POST'],
['name' => 'devices#editDevice', 'url' => '/devices/{id}', 'verb' => 'PUT'],
['name' => 'devices#deleteDevice', 'url' => '/devices/{id}', 'verb' => 'DELETE'],
['name' => 'devices#getDevicePoints', 'url' => '/devices/{id}', 'verb' => 'GET'],
['name' => 'devices#addDevicePoint', 'url' => '/devices', 'verb' => 'POST'],
['name' => 'devices#editDevice', 'url' => '/devices/{id}', 'verb' => 'PUT'],
['name' => 'devices#deleteDevice', 'url' => '/devices/{id}', 'verb' => 'DELETE'],
['name' => 'devices#shareDevice', 'url' => '/devices/{id}/share', 'verb' => 'POST'],
['name' => 'devices#exportDevices', 'url' => '/export/devices', 'verb' => 'POST'],
['name' => 'devices#importDevices', 'url' => '/import/devices', 'verb' => 'POST'],
['name' => 'devices#exportDevices', 'url' => '/export/devices', 'verb' => 'POST'],
['name' => 'devices#importDevices', 'url' => '/import/devices', 'verb' => 'POST'],
// devices sharing
['name' => 'devices#getSharedDevices', 'url' => '/devices/s/', 'verb' => 'GET'],
// ['name' => 'devices#shareDevice', 'url' => '/devices/s/', 'verb' => 'POST'],
@ -149,10 +149,10 @@ return [
['name' => 'devices#addSharedDeviceToMap', 'url' => '/devices/s/{token}/map-link/{targetMapId}', 'verb' => 'POST'],
['name' => 'devices#removeSharedDeviceFromMap', 'url' => '/devices/s/{token}/map-link/{targetMapId}', 'verb' => 'DELETE'],
//MyMaps
['name' => 'my_maps#getMyMaps', 'url' => '/maps', 'verb' => 'GET'],
['name' => 'my_maps#addMyMap' , 'url' => '/maps', 'verb' => 'POST'],
['name' => 'my_maps#updateMyMap' , 'url' => '/maps/{id}', 'verb' => 'PUT'],
['name' => 'my_maps#deleteMyMap' , 'url' => '/maps/{id}', 'verb' => 'DELETE'],
]
//MyMaps
['name' => 'my_maps#getMyMaps', 'url' => '/maps', 'verb' => 'GET'],
['name' => 'my_maps#addMyMap' , 'url' => '/maps', 'verb' => 'POST'],
['name' => 'my_maps#updateMyMap' , 'url' => '/maps/{id}', 'verb' => 'PUT'],
['name' => 'my_maps#deleteMyMap' , 'url' => '/maps/{id}', 'verb' => 'DELETE'],
]
];

Просмотреть файл

@ -11,36 +11,34 @@
namespace OCA\Maps\AppInfo;
use OCA\DAV\Events\CardCreatedEvent;
use OCA\DAV\Events\CardDeletedEvent;
use OCA\DAV\Events\CardUpdatedEvent;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files\Event\LoadSidebar;
use OCA\Maps\Listener\LoadSidebarListener;
use \OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Http\EmptyFeaturePolicy;
use OCP\EventDispatcher\IEventDispatcher;
use \OCP\IServerContainer;
use OCA\Maps\Hooks\FileHooks;
use OCA\Maps\Listener\CardCreatedListener;
use OCA\Maps\Listener\CardDeletedListener;
use OCA\Maps\Listener\CardUpdatedListener;
use OCA\Maps\Listener\LoadAdditionalScriptsListener;
use OCA\Maps\Listener\LoadSidebarListener;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\Service\TracksService;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\DAV\Events\CardCreatedEvent;
use OCA\DAV\Events\CardUpdatedEvent;
use OCA\DAV\Events\CardDeletedEvent;
use OCA\Maps\Listener\LoadAdditionalScriptsListener;
use OCA\Maps\Listener\CardCreatedListener;
use OCA\Maps\Listener\CardUpdatedListener;
use OCA\Maps\Listener\CardDeletedListener;
use OCP\AppFramework\Http\EmptyFeaturePolicy;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IServerContainer;
use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent;
class Application extends App implements IBootstrap {
public const APP_ID = 'maps';
public function __construct (array $urlParams=array()) {
parent::__construct('maps', $urlParams);
}
public function __construct(array $urlParams = []) {
parent::__construct('maps', $urlParams);
}
public function register(IRegistrationContext $context): void {
// ... registration logic goes here ...
@ -73,7 +71,7 @@ class Application extends App implements IBootstrap {
public function boot(IBootContext $context): void {
// ... boot logic goes here ...
$context->getAppContainer()->registerService('FileHooks', function($c) {
$context->getAppContainer()->registerService('FileHooks', function ($c) {
return new FileHooks(
$c->query(IServerContainer::class)->getRootFolder(),
\OC::$server->query(PhotofilesService::class),

Просмотреть файл

@ -12,17 +12,13 @@
namespace OCA\Maps\BackgroundJob;
use \OCP\BackgroundJob\QueuedJob;
use \OCP\BackgroundJob\IJobList;
use \OCP\AppFramework\Utility\ITimeFactory;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\Files\IRootFolder;
use OCA\Maps\Service\PhotofilesService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\IRootFolder;
use OCP\ICache;
use OCP\ICacheFactory;
class AddPhotoJob extends QueuedJob {
@ -38,35 +34,35 @@ class AddPhotoJob extends QueuedJob {
/** @var ICache */
private ICache $backgroundJobCache;
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param ITimeFactory $timeFactory
* @param PhotofilesService $photofilesService
*/
public function __construct(ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
*/
public function __construct(ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
$this->cacheFactory = $cacheFactory;
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}
}
public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['photoId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->addPhotoNow($file, $arguments['userId']);
public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['photoId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->addPhotoNow($file, $arguments['userId']);
$counter = $this->backgroundJobCache->get('recentlyAdded:'.$arguments['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyAdded:'.$arguments['userId'], (int) $counter + 1, 60 * 60 * 3);
}
$this->backgroundJobCache->set('recentlyAdded:'.$arguments['userId'], (int)$counter + 1, 60 * 60 * 3);
}
}

Просмотреть файл

@ -12,35 +12,33 @@
namespace OCA\Maps\BackgroundJob;
use \OCP\BackgroundJob\QueuedJob;
use \OCP\BackgroundJob\IJobList;
use \OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUserManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IUser;
use \OCA\Maps\BackgroundJob\UserInstallScanJob;
use OCP\IUserManager;
class LaunchUsersInstallScanJob extends QueuedJob {
private $jobList;
private $jobList;
/**
* LaunchUsersInstallScanJob constructor.
*
* A QueuedJob to launch a scan job for each user
*
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, IJobList $jobList, IUserManager $userManager) {
parent::__construct($timeFactory);
$this->jobList = $jobList;
$this->userManager = $userManager;
}
/**
* LaunchUsersInstallScanJob constructor.
*
* A QueuedJob to launch a scan job for each user
*
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, IJobList $jobList, IUserManager $userManager) {
parent::__construct($timeFactory);
$this->jobList = $jobList;
$this->userManager = $userManager;
}
public function run($arguments) {
\OC::$server->getLogger()->debug('Launch users install scan jobs cronjob executed');
$this->userManager->callForSeenUsers(function (IUser $user) {
$this->jobList->add(UserInstallScanJob::class, ['userId' => $user->getUID()]);
});
}
public function run($arguments) {
\OC::$server->getLogger()->debug('Launch users install scan jobs cronjob executed');
$this->userManager->callForSeenUsers(function (IUser $user) {
$this->jobList->add(UserInstallScanJob::class, ['userId' => $user->getUID()]);
});
}
}

Просмотреть файл

@ -12,39 +12,39 @@
namespace OCA\Maps\BackgroundJob;
use \OCA\Maps\Service\AddressService;
use \OCP\BackgroundJob\QueuedJob;
use \OCP\BackgroundJob\IJobList;
use \OCP\AppFramework\Utility\ITimeFactory;
use OCA\Maps\Service\AddressService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
class LookupMissingGeoJob extends QueuedJob {
/** @var AddressService */
private $addressService;
/** @var AddressService */
private $addressService;
/** @var AddressService */
private $jobList;
/** @var AddressService */
private $jobList;
/**
* LookupMissingGeoJob constructor.
*
* A QueuedJob to lookup missing geo information of addresses
*
* @param AddressService $service
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, AddressService $service, IJobList $jobList) {
parent::__construct($timeFactory);
$this->addressService = $service;
$this->jobList = $jobList;
}
/**
* LookupMissingGeoJob constructor.
*
* A QueuedJob to lookup missing geo information of addresses
*
* @param AddressService $service
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, AddressService $service, IJobList $jobList) {
parent::__construct($timeFactory);
$this->addressService = $service;
$this->jobList = $jobList;
}
public function run($arguments) {
\OC::$server->getLogger()->debug('Maps address lookup cronjob executed');
// lookup at most 200 addresses
if (!$this->addressService->lookupMissingGeo(200)){
// if not all addresses where looked up successfully add a new job for next time
$this->jobList->add(LookupMissingGeoJob::class, []);
}
}
public function run($arguments) {
\OC::$server->getLogger()->debug('Maps address lookup cronjob executed');
// lookup at most 200 addresses
if (!$this->addressService->lookupMissingGeo(200)) {
// if not all addresses where looked up successfully add a new job for next time
$this->jobList->add(LookupMissingGeoJob::class, []);
}
}
}

Просмотреть файл

@ -12,17 +12,13 @@
namespace OCA\Maps\BackgroundJob;
use \OCP\BackgroundJob\QueuedJob;
use \OCP\BackgroundJob\IJobList;
use \OCP\AppFramework\Utility\ITimeFactory;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\Files\IRootFolder;
use OCA\Maps\Service\PhotofilesService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\IRootFolder;
use OCP\ICache;
use OCP\ICacheFactory;
class UpdatePhotoByFileJob extends QueuedJob {
@ -38,35 +34,35 @@ class UpdatePhotoByFileJob extends QueuedJob {
/** @var ICache */
private ICache $backgroundJobCache;
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param ITimeFactory $timeFactory
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param ITimeFactory $timeFactory
* @param PhotofilesService $photofilesService
*/
public function __construct(ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
*/
public function __construct(ITimeFactory $timeFactory,
IRootFolder $root,
PhotofilesService $photofilesService,
ICacheFactory $cacheFactory) {
parent::__construct($timeFactory);
$this->photofilesService = $photofilesService;
$this->root = $root;
$this->cacheFactory = $cacheFactory;
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}
}
public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['fileId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->updateByFileNow($file);
public function run($arguments) {
$userFolder = $this->root->getUserFolder($arguments['userId']);
$files = $userFolder->getById($arguments['fileId']);
if (empty($files)) {
return;
}
$file = array_shift($files);
$this->photofilesService->updateByFileNow($file);
$counter = $this->backgroundJobCache->get('recentlyUpdated:'.$arguments['userId']) ?? 0;
$this->backgroundJobCache->set('recentlyUpdated:'.$arguments['userId'], (int) $counter + 1, 60 * 60 * 3);
}
$this->backgroundJobCache->set('recentlyUpdated:'.$arguments['userId'], (int)$counter + 1, 60 * 60 * 3);
}
}

Просмотреть файл

@ -12,68 +12,68 @@
namespace OCA\Maps\BackgroundJob;
use \OCP\BackgroundJob\QueuedJob;
use \OCP\BackgroundJob\IJobList;
use \OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUserManager;
use OCP\IConfig;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\Service\TracksService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IConfig;
use OCP\IUserManager;
class UserInstallScanJob extends QueuedJob {
private IJobList $jobList;
private IConfig $config;
private IUserManager $userManager;
private PhotofilesService $photofilesService;
private TracksService $tracksService;
private IJobList $jobList;
private IConfig $config;
private IUserManager $userManager;
private PhotofilesService $photofilesService;
private TracksService $tracksService;
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, IJobList $jobList,
IUserManager $userManager,
IConfig $config,
PhotofilesService $photofilesService,
TracksService $tracksService) {
parent::__construct($timeFactory);
$this->config = $config;
$this->jobList = $jobList;
$this->userManager = $userManager;
$this->photofilesService = $photofilesService;
$this->tracksService = $tracksService;
}
public function run($arguments) {
$userId = $arguments['userId'];
\OC::$server->getLogger()->debug('Launch user install scan job for '.$userId.' cronjob executed');
// scan photos and tracks for given user
$this->rescanUserPhotos($userId);
$this->rescanUserTracks($userId);
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
private function rescanUserPhotos($userId) {
//$this->output->info('======== User '.$userId.' ========'."\n");
$c = 1;
foreach ($this->photofilesService->rescan($userId) as $path) {
//$this->output->info('['.$c.'] Photo "'.$path.'" added'."\n");
$c++;
}
/**
* UserInstallScanJob constructor.
*
* A QueuedJob to scan user storage for photos and tracks
*
* @param IJobList $jobList
*/
public function __construct(ITimeFactory $timeFactory, IJobList $jobList,
IUserManager $userManager,
IConfig $config,
PhotofilesService $photofilesService,
TracksService $tracksService) {
parent::__construct($timeFactory);
$this->config = $config;
$this->jobList = $jobList;
$this->userManager = $userManager;
$this->photofilesService = $photofilesService;
$this->tracksService = $tracksService;
}
private function rescanUserTracks($userId) {
//$this->output->info('======== User '.$userId.' ========'."\n");
$c = 1;
foreach ($this->tracksService->rescan($userId) as $path) {
//$this->output->info('['.$c.'] Track "'.$path.'" added'."\n");
$c++;
}
}
public function run($arguments) {
$userId = $arguments['userId'];
\OC::$server->getLogger()->debug('Launch user install scan job for '.$userId.' cronjob executed');
// scan photos and tracks for given user
$this->rescanUserPhotos($userId);
$this->rescanUserTracks($userId);
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
private function rescanUserPhotos($userId) {
//$this->output->info('======== User '.$userId.' ========'."\n");
$c = 1;
foreach ($this->photofilesService->rescan($userId) as $path) {
//$this->output->info('['.$c.'] Photo "'.$path.'" added'."\n");
$c++;
}
}
private function rescanUserTracks($userId) {
//$this->output->info('======== User '.$userId.' ========'."\n");
$c = 1;
foreach ($this->tracksService->rescan($userId) as $path) {
//$this->output->info('['.$c.'] Track "'.$path.'" added'."\n");
$c++;
}
}
}

Просмотреть файл

@ -13,44 +13,38 @@
namespace OCA\Maps\Command;
use OCA\Maps\Service\MimetypeService;
use OCP\Encryption\IManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use OCP\IConfig;
use OCA\Maps\Service\PhotofilesService;
class RegisterMimetypes extends Command {
protected MimetypeService $mimetypeService;
protected MimetypeService $mimetypeService;
public function __construct(MimetypeService $mimetypeService) {
parent::__construct();
$this->mimetypeService = $mimetypeService;
}
public function __construct(MimetypeService $mimetypeService) {
parent::__construct();
$this->mimetypeService = $mimetypeService;
}
/**
* @return void
*/
protected function configure() {
$this->setName('maps:register-mimetypes')
->setDescription('Registers the maps mimetypes for existing and new files.');
}
protected function configure() {
$this->setName('maps:register-mimetypes')
->setDescription('Registers the maps mimetypes for existing and new files.');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->output = $output;
$output->writeln("Register mimetypes for existing files");
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->output = $output;
$output->writeln('Register mimetypes for existing files');
$this->mimetypeService->registerForExistingFiles();
$output->writeln("Register mimetypes for new files");
$output->writeln('Register mimetypes for new files');
$this->mimetypeService->registerForNewFiles();
return 0;
}
return 0;
}
}

Просмотреть файл

@ -12,85 +12,84 @@
namespace OCA\Maps\Command;
use OCA\Maps\Service\PhotofilesService;
use OCP\Encryption\IManager;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use OCP\IConfig;
use OCA\Maps\Service\PhotofilesService;
use Symfony\Component\Console\Output\OutputInterface;
class RescanPhotos extends Command {
protected IUserManager $userManager;
protected OutputInterface $output;
protected IManager $encryptionManager;
protected PhotofilesService $photofilesService;
protected IConfig $config;
protected IUserManager $userManager;
protected OutputInterface $output;
protected IManager $encryptionManager;
protected PhotofilesService $photofilesService;
protected IConfig $config;
public function __construct(IUserManager $userManager,
IManager $encryptionManager,
PhotofilesService $photofilesService,
IConfig $config) {
parent::__construct();
$this->userManager = $userManager;
$this->encryptionManager = $encryptionManager;
$this->photofilesService = $photofilesService;
$this->config = $config;
}
public function __construct(IUserManager $userManager,
IManager $encryptionManager,
PhotofilesService $photofilesService,
IConfig $config) {
parent::__construct();
$this->userManager = $userManager;
$this->encryptionManager = $encryptionManager;
$this->photofilesService = $photofilesService;
$this->config = $config;
}
/**
* @return void
*/
protected function configure() {
$this->setName('maps:scan-photos')
->setDescription('Rescan photos GPS exif data')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'Rescan photos GPS exif data for the given user'
)
protected function configure() {
$this->setName('maps:scan-photos')
->setDescription('Rescan photos GPS exif data')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'Rescan photos GPS exif data for the given user'
)
->addOption(
'now',
null,
InputOption::VALUE_NONE,
'Dot the rescan now and not as background jobs. Doing it now might run out of memory.'
);
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($this->encryptionManager->isEnabled()) {
$output->writeln('Encryption is enabled. Aborted.');
return 1;
}
$this->output = $output;
$userId = $input->getArgument('user_id');
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($this->encryptionManager->isEnabled()) {
$output->writeln('Encryption is enabled. Aborted.');
return 1;
}
$this->output = $output;
$userId = $input->getArgument('user_id');
$inBackground = !($input->getOption('now') ?? true);
if ($inBackground) {
echo "Extracting coordinates from photo is performed in a BackgroundJob \n";
}
if ($userId === null) {
$this->userManager->callForSeenUsers(function (IUser $user) use ($inBackground) {
$this->rescanUserPhotos($user->getUID(), $inBackground);
});
} else {
$user = $this->userManager->get($userId);
if ($user !== null) {
$this->rescanUserPhotos($userId, $inBackground);
}
}
return 0;
}
if ($userId === null) {
$this->userManager->callForSeenUsers(function (IUser $user) use ($inBackground) {
$this->rescanUserPhotos($user->getUID(), $inBackground);
});
} else {
$user = $this->userManager->get($userId);
if ($user !== null) {
$this->rescanUserPhotos($userId, $inBackground);
}
}
return 0;
}
/**
* @param string $userId
@ -98,13 +97,13 @@ class RescanPhotos extends Command {
* @return void
* @throws \OCP\PreConditionNotMetException
*/
private function rescanUserPhotos(string $userId, bool $inBackground=true) {
echo '======== User '.$userId.' ========'."\n";
$c = 1;
foreach ($this->photofilesService->rescan($userId, $inBackground) as $path) {
echo '['.$c.'] Photo "'.$path.'" added'."\n";
$c++;
}
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
private function rescanUserPhotos(string $userId, bool $inBackground = true) {
echo '======== User '.$userId.' ========'."\n";
$c = 1;
foreach ($this->photofilesService->rescan($userId, $inBackground) as $path) {
echo '['.$c.'] Photo "'.$path.'" added'."\n";
$c++;
}
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
}

Просмотреть файл

@ -12,73 +12,72 @@
namespace OCA\Maps\Command;
use OCA\Maps\Service\TracksService;
use OCP\Encryption\IManager;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCP\IConfig;
use OCA\Maps\Service\TracksService;
use Symfony\Component\Console\Output\OutputInterface;
class RescanTracks extends Command {
protected IUserManager $userManager;
protected OutputInterface $output;
protected IManager $encryptionManager;
protected TracksService $tracksService;
protected IConfig $config;
protected IUserManager $userManager;
protected OutputInterface $output;
protected IManager $encryptionManager;
protected TracksService $tracksService;
protected IConfig $config;
public function __construct(IUserManager $userManager,
IManager $encryptionManager,
TracksService $tracksService,
IConfig $config) {
parent::__construct();
$this->userManager = $userManager;
$this->encryptionManager = $encryptionManager;
$this->tracksService = $tracksService;
$this->config = $config;
}
protected function configure() {
$this->setName('maps:scan-tracks')
->setDescription('Rescan track files')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'Rescan track files for the given user'
);
}
public function __construct(IUserManager $userManager,
IManager $encryptionManager,
TracksService $tracksService,
IConfig $config) {
parent::__construct();
$this->userManager = $userManager;
$this->encryptionManager = $encryptionManager;
$this->tracksService = $tracksService;
$this->config = $config;
}
protected function configure() {
$this->setName('maps:scan-tracks')
->setDescription('Rescan track files')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'Rescan track files for the given user'
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->encryptionManager->isEnabled()) {
$output->writeln('Encryption is enabled. Aborted.');
return 1;
}
$this->output = $output;
$userId = $input->getArgument('user_id');
if ($userId === null) {
$this->userManager->callForSeenUsers(function (IUser $user) {
$this->rescanUserTracks($user->getUID());
});
} else {
$user = $this->userManager->get($userId);
if ($user !== null) {
$this->rescanUserTracks($userId);
}
}
return 0;
}
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->encryptionManager->isEnabled()) {
$output->writeln('Encryption is enabled. Aborted.');
return 1;
}
$this->output = $output;
$userId = $input->getArgument('user_id');
if ($userId === null) {
$this->userManager->callForSeenUsers(function (IUser $user) {
$this->rescanUserTracks($user->getUID());
});
} else {
$user = $this->userManager->get($userId);
if ($user !== null) {
$this->rescanUserTracks($userId);
}
}
return 0;
}
private function rescanUserTracks($userId) {
echo '======== User '.$userId.' ========'."\n";
$c = 1;
foreach ($this->tracksService->rescan($userId) as $path) {
echo '['.$c.'] Track "'.$path.'" added'."\n";
$c++;
}
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
private function rescanUserTracks($userId) {
echo '======== User '.$userId.' ========'."\n";
$c = 1;
foreach ($this->tracksService->rescan($userId) as $path) {
echo '['.$c.'] Track "'.$path.'" added'."\n";
$c++;
}
$this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes');
}
}

Просмотреть файл

@ -13,22 +13,22 @@
namespace OCA\Maps\Controller;
use OC\Files\Node\Node;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IRequest;
use OCP\IAvatarManager;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\Maps\Service\AddressService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\ILogger;
use OCP\IDBConnection;
use OCP\AppFramework\Controller;
use OCP\Contacts\IManager;
use OCA\Maps\Service\AddressService;
use \OCP\DB\QueryBuilder\IQueryBuilder;
use \OCA\DAV\CardDAV\CardDavBackend;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use \Sabre\VObject\Property\Text;
use \Sabre\VObject\Reader;
use Sabre\VObject\Property\Text;
use Sabre\VObject\Reader;
class ContactsController extends Controller {
private $userId;
@ -40,8 +40,8 @@ class ContactsController extends Controller {
private $cdBackend;
private $avatarManager;
private $root;
private $urlGenerator;
private $geoDistanceMax; // Max distance in meters to consider that 2 addresses are the same location
private $urlGenerator;
private $geoDistanceMax; // Max distance in meters to consider that 2 addresses are the same location
/**
* @param $AppName
@ -56,17 +56,17 @@ class ContactsController extends Controller {
* @param IRootFolder $root
*/
public function __construct(
$AppName,
ILogger $logger,
IRequest $request,
IDBConnection $dbconnection,
IManager $contactsManager,
AddressService $addressService,
$UserId,
CardDavBackend $cdBackend,
IAvatarManager $avatarManager,
IRootFolder $root,
IURLGenerator $urlGenerator){
$AppName,
ILogger $logger,
IRequest $request,
IDBConnection $dbconnection,
IManager $contactsManager,
AddressService $addressService,
$UserId,
CardDavBackend $cdBackend,
IAvatarManager $avatarManager,
IRootFolder $root,
IURLGenerator $urlGenerator) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->userId = $UserId;
@ -77,63 +77,63 @@ class ContactsController extends Controller {
$this->qb = $dbconnection->getQueryBuilder();
$this->cdBackend = $cdBackend;
$this->root = $root;
$this->urlGenerator = $urlGenerator;
$this->geoDistanceMax = 5;
$this->urlGenerator = $urlGenerator;
$this->geoDistanceMax = 5;
}
/**
* Converts a geo string as a float array
* @param string formatted as "lat;lon"
* @return float array containing [lat;lon]
*/
private function geoAsFloatArray($geo) {
$res = array_map(function ($value) {return floatval($value);}, explode(';', $geo));
return $res;
}
/**
* Converts a geo string as a float array
* @param string formatted as "lat;lon"
* @return float array containing [lat;lon]
*/
private function geoAsFloatArray($geo) {
$res = array_map(function($value) {return floatval($value);}, explode(";", $geo) );
return $res;
}
/**
* check if geographical address is duplicated
* @param array containing contact's previous different addresses
* @param contact's address to check
* @return integer : -1 if address is new, index of duplicated address in other cases
*/
private function isNewAddress($prevGeo, $geo) {
if (empty($geo)) { // Address not converted to geo coords
return -1;
}
$result = -1;
$counter = 0;
foreach ($prevGeo as $prev) {
if ($this->getDistance($prev, $geo) <= $this->geoDistanceMax) {
$result = $counter;
break;
}
$counter++;
}
return $result;
}
/**
* check if geographical address is duplicated
* @param array containing contact's previous different addresses
* @param contact's address to check
* @return integer : -1 if address is new, index of duplicated address in other cases
*/
private function isNewAddress($prevGeo, $geo) {
if (empty($geo)) { // Address not converted to geo coords
return -1;
}
$result = -1;
$counter = 0;
foreach ($prevGeo as $prev) {
if ($this->getDistance($prev, $geo) <= $this->geoDistanceMax) {
$result = $counter;
break;
}
$counter++;
}
return $result;
}
/**
* get distance between two geo points
* @param GPS coordinates of first point
* @param GPS coordinates of second point
* @return Distance in meters between these two points
*/
private function getDistance($coordsA, $coordsB) {
if (empty($coordsA) || empty($coordsB) ) {
return 9E999;
}
$latA = deg2rad($coordsA[0]);
$lonA = deg2rad($coordsA[1]);
$latB = deg2rad($coordsB[0]);
$lonB = deg2rad($coordsB[1]);
$earthRadius = 6378137; // in m
$dlon = ($lonB - $lonA) / 2;
$dlat = ($latB - $latA) / 2;
$a = (sin($dlat) * sin($dlat)) + cos($latA) * cos($latB) * (sin($dlon) * sin($dlon
));
$d = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $d * $earthRadius;
}
/**
* get distance between two geo points
* @param GPS coordinates of first point
* @param GPS coordinates of second point
* @return Distance in meters between these two points
*/
private function getDistance($coordsA, $coordsB) {
if (empty($coordsA) || empty($coordsB)) {
return 9E999;
}
$latA = deg2rad($coordsA[0]);
$lonA = deg2rad($coordsA[1]);
$latB = deg2rad($coordsB[0]);
$lonB = deg2rad($coordsB[1]);
$earthRadius = 6378137; // in m
$dlon = ($lonB - $lonA) / 2;
$dlat = ($latB - $latA) / 2;
$a = (sin($dlat) * sin($dlat)) + cos($latA) * cos($latB) * (sin($dlon) * sin($dlon
));
$d = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $d * $earthRadius;
}
/**
* get contacts with coordinates
@ -144,12 +144,12 @@ class ContactsController extends Controller {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function getContacts($myMapId=null): DataResponse {
public function getContacts($myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$contacts = $this->contactsManager->search('', ['GEO', 'ADR'], ['types' => false]);
$addressBooks = $this->contactsManager->getUserAddressBooks();
$result = [];
$userid = trim($this->userId);
$userid = trim($this->userId);
foreach ($contacts as $c) {
$addressBookUri = $addressBooks[$c['addressbook-key']]->getUri();
@ -180,7 +180,7 @@ class ContactsController extends Controller {
'isDeletable' => true,
'isUpdateable' => true,
];
} elseif (is_countable($geo) && count($geo)>0 && is_iterable($geo)) {
} elseif (is_countable($geo) && count($geo) > 0 && is_iterable($geo)) {
foreach ($geo as $g) {
if (is_string($g) && strlen($g) > 1) {
$result[] = [
@ -206,44 +206,44 @@ class ContactsController extends Controller {
$card = $this->cdBackend->getContact($c['addressbook-key'], $c['URI']);
if ($card) {
$vcard = Reader::read($card['carddata']);
if (isset($vcard->ADR) && count($vcard->ADR) > 0) {
$prevGeo = [];
$prevRes = [];
foreach ($vcard->ADR as $adr) {
$geo = $this->addressService->addressToGeo($adr->getValue(), $c['URI']);
$geof = $this->geoAsFloatArray($geo);
$duplicatedIndex = $this->isNewAddress($prevGeo, $geof);
$adrtype = '';
if (isset($adr->parameters()['TYPE'])) {
$adrtype = $adr->parameters()['TYPE']->getValue();
}
if (is_string($geo) && strlen($geo) > 1) {
if ($duplicatedIndex < 0 ) {
array_push($prevGeo, $geof);
array_push($prevRes, count($result)); // Add index of new item so that we can update the ADRTYPE in case of duplicate address
$result[] = [
'FN' => $c['FN'] ?? $this->N2FN($c['N']) ?? '???',
'URI' => $c['URI'],
'UID' => $c['UID'],
'URL' => $url,
'ADR' => $adr->getValue(),
'ADRTYPE' => array($adrtype),
'HAS_PHOTO' => (isset($c['PHOTO']) && $c['PHOTO'] !== null),
'BOOKID' => $c['addressbook-key'],
'BOOKURI' => $addressBookUri,
'GEO' => $geo,
'GROUPS' => $c['CATEGORIES'] ?? null,
'isDeletable' => true,
'isUpdateable' => true,
];
} else {
// Concatenate AddressType to the corresponding record
array_push($result[$prevRes[$duplicatedIndex]]['ADRTYPE'], $adrtype);
$result[$prevRes[$duplicatedIndex]]['isUpdateable'] = false;
$result[$prevRes[$duplicatedIndex]]['isDeletable'] = false;
$result[$prevRes[$duplicatedIndex]]['isShareable'] = false;
}
}
if (isset($vcard->ADR) && count($vcard->ADR) > 0) {
$prevGeo = [];
$prevRes = [];
foreach ($vcard->ADR as $adr) {
$geo = $this->addressService->addressToGeo($adr->getValue(), $c['URI']);
$geof = $this->geoAsFloatArray($geo);
$duplicatedIndex = $this->isNewAddress($prevGeo, $geof);
$adrtype = '';
if (isset($adr->parameters()['TYPE'])) {
$adrtype = $adr->parameters()['TYPE']->getValue();
}
if (is_string($geo) && strlen($geo) > 1) {
if ($duplicatedIndex < 0) {
array_push($prevGeo, $geof);
array_push($prevRes, count($result)); // Add index of new item so that we can update the ADRTYPE in case of duplicate address
$result[] = [
'FN' => $c['FN'] ?? $this->N2FN($c['N']) ?? '???',
'URI' => $c['URI'],
'UID' => $c['UID'],
'URL' => $url,
'ADR' => $adr->getValue(),
'ADRTYPE' => [$adrtype],
'HAS_PHOTO' => (isset($c['PHOTO']) && $c['PHOTO'] !== null),
'BOOKID' => $c['addressbook-key'],
'BOOKURI' => $addressBookUri,
'GEO' => $geo,
'GROUPS' => $c['CATEGORIES'] ?? null,
'isDeletable' => true,
'isUpdateable' => true,
];
} else {
// Concatenate AddressType to the corresponding record
array_push($result[$prevRes[$duplicatedIndex]]['ADRTYPE'], $adrtype);
$result[$prevRes[$duplicatedIndex]]['isUpdateable'] = false;
$result[$prevRes[$duplicatedIndex]]['isDeletable'] = false;
$result[$prevRes[$duplicatedIndex]]['isShareable'] = false;
}
}
}
}
}
@ -254,7 +254,7 @@ class ContactsController extends Controller {
//Fixme add contacts for my-maps
$result = [];
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folders = $userFolder->getById($myMapId);
if (empty($folders)) {
return new DataResponse($result);
}
@ -264,7 +264,7 @@ class ContactsController extends Controller {
}
$files = $folder->search('.vcf');
foreach ($files as $file) {
// $cards = explode("END:VCARD\r\n", $file->getContent());
// $cards = explode("END:VCARD\r\n", $file->getContent());
$cards = [$file->getContent()];
foreach ($cards as $card) {
$vcard = Reader::read($card."END:VCARD\r\n");
@ -272,43 +272,43 @@ class ContactsController extends Controller {
$geo = $vcard->GEO;
if (is_string($geo) && strlen($geo->getValue()) > 1) {
$result[] = $this->vCardToArray($file, $vcard, $geo->getValue());
} elseif (is_countable($geo) && count($geo)>0 && is_iterable($geo)) {
$prevGeo = "";
foreach ($geo as $g) {
if (strcmp($prevGeo, $g->getValue()) != 0) {
$prevGeo = $g->getValue();
if (strlen($g->getValue()) > 1) {
$result[] = $this->vCardToArray($file, $vcard, $g->getValue());
}
}
} elseif (is_countable($geo) && count($geo) > 0 && is_iterable($geo)) {
$prevGeo = '';
foreach ($geo as $g) {
if (strcmp($prevGeo, $g->getValue()) != 0) {
$prevGeo = $g->getValue();
if (strlen($g->getValue()) > 1) {
$result[] = $this->vCardToArray($file, $vcard, $g->getValue());
}
}
}
}
}
if (isset($vcard->ADR) && count($vcard->ADR) > 0) {
$prevGeo = [];
$prevRes = [];
foreach ($vcard->ADR as $adr) {
$prevGeo = [];
$prevRes = [];
foreach ($vcard->ADR as $adr) {
$geo = $this->addressService->addressToGeo($adr->getValue(), $file->getId());
$geof = $this->geoAsFloatArray($geo);
$duplicatedIndex = $this->isNewAddress($prevGeo, $geof);
//var_dump($adr->parameters()['TYPE']->getValue());
$adrtype = '';
if (isset($adr->parameters()['TYPE'])) {
$adrtype = $adr->parameters()['TYPE']->getValue();
}
if (is_string($geo) && strlen($geo) > 1) {
if ($duplicatedIndex < 0 ) {
array_push($prevGeo, $geof);
array_push($prevRes, count($result)); // Add index of new item so that we can update the ADRTYPE in case of duplicate address
$result[] = $this->vCardToArray($file, $vcard, $geo, $adrtype, $adr->getValue(), $file->getId());
} else {
// Concatenate AddressType to the corresponding record
array_push($result[$prevRes[$duplicatedIndex]]['ADRTYPE'], $adrtype);
$result[$prevRes[$duplicatedIndex]]['isUpdateable'] = false;
$result[$prevRes[$duplicatedIndex]]['isDeletable'] = false;
$result[$prevRes[$duplicatedIndex]]['isShareable'] = false;
}
}
$geof = $this->geoAsFloatArray($geo);
$duplicatedIndex = $this->isNewAddress($prevGeo, $geof);
//var_dump($adr->parameters()['TYPE']->getValue());
$adrtype = '';
if (isset($adr->parameters()['TYPE'])) {
$adrtype = $adr->parameters()['TYPE']->getValue();
}
if (is_string($geo) && strlen($geo) > 1) {
if ($duplicatedIndex < 0) {
array_push($prevGeo, $geof);
array_push($prevRes, count($result)); // Add index of new item so that we can update the ADRTYPE in case of duplicate address
$result[] = $this->vCardToArray($file, $vcard, $geo, $adrtype, $adr->getValue(), $file->getId());
} else {
// Concatenate AddressType to the corresponding record
array_push($result[$prevRes[$duplicatedIndex]]['ADRTYPE'], $adrtype);
$result[$prevRes[$duplicatedIndex]]['isUpdateable'] = false;
$result[$prevRes[$duplicatedIndex]]['isDeletable'] = false;
$result[$prevRes[$duplicatedIndex]]['isShareable'] = false;
}
}
}
}
}
@ -341,7 +341,7 @@ class ContactsController extends Controller {
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
private function vCardToArray(Node $file, \Sabre\VObject\Document $vcard, string $geo, ?string $adrtype=null, ?string $adr=null, ?int $fileId = null): array {
private function vCardToArray(Node $file, \Sabre\VObject\Document $vcard, string $geo, ?string $adrtype = null, ?string $adr = null, ?int $fileId = null): array {
$FNArray = $vcard->FN ? $vcard->FN->getJsonValue() : [];
$fn = array_shift($FNArray);
$NArray = $vcard->N ? $vcard->N->getJsonValue() : [];
@ -387,12 +387,10 @@ class ContactsController extends Controller {
$spl = explode($n, ';');
if (count($spl) >= 4) {
return $spl[3] . ' ' . $spl[1] . ' ' . $spl[0];
}
else {
} else {
return null;
}
}
else {
} else {
return null;
}
}
@ -405,7 +403,7 @@ class ContactsController extends Controller {
* @return DataResponse
*/
public function searchContacts(string $query = ''): DataResponse {
$contacts = $this->contactsManager->search($query, ['FN'], ['types'=>false]);
$contacts = $this->contactsManager->search($query, ['FN'], ['types' => false]);
$booksReadOnly = $this->getAddressBooksReadOnly();
$addressBooks = $this->contactsManager->getUserAddressBooks();
$result = [];
@ -461,17 +459,17 @@ class ContactsController extends Controller {
string $uid,
?float $lat,
?float $lng,
string $attraction='',
string $house_number='',
string $road='',
string $postcode='',
string $city='',
string $state='',
string $country='',
string $type='',
?string $address_string=null,
?int $fileId=null,
?int $myMapId=null): DataResponse {
string $attraction = '',
string $house_number = '',
string $road = '',
string $postcode = '',
string $city = '',
string $state = '',
string $country = '',
string $type = '',
?string $address_string = null,
?int $fileId = null,
?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
// do not edit 'user' contact even myself
if (strcmp($uri, 'Database:'.$uid.'.vcf') === 0 or
@ -484,7 +482,7 @@ class ContactsController extends Controller {
if ($lat !== null && $lng !== null) {
// we set the geo tag
if (!$attraction && !$house_number && !$road && !$postcode && !$city && !$state && !$country && !$address_string) {
$result = $this->contactsManager->createOrUpdate(['URI'=>$uri, 'GEO'=>$lat.';'.$lng], $bookid);
$result = $this->contactsManager->createOrUpdate(['URI' => $uri, 'GEO' => $lat.';'.$lng], $bookid);
}
// we set the address
elseif (!$address_string) {
@ -497,49 +495,49 @@ class ContactsController extends Controller {
// set the address in the vcard
$card = $this->cdBackend->getContact($bookid, $uri);
if ($card) {
$vcard = Reader::read($card['carddata']);;
$vcard->add(new Text($vcard, 'ADR', ['', '', $street, $city, $state, $postcode, $country], ['TYPE'=>$type]));
$vcard = Reader::read($card['carddata']);
;
$vcard->add(new Text($vcard, 'ADR', ['', '', $street, $city, $state, $postcode, $country], ['TYPE' => $type]));
$result = $this->cdBackend->updateCard($bookid, $uri, $vcard->serialize());
}
} else {
$card = $this->cdBackend->getContact($bookid, $uri);
if ($card) {
$vcard = Reader::read($card['carddata']);;
$vcard->add(new Text($vcard, 'ADR', explode(';',$address_string), ['TYPE'=>$type]));
$vcard = Reader::read($card['carddata']);
;
$vcard->add(new Text($vcard, 'ADR', explode(';', $address_string), ['TYPE' => $type]));
$result = $this->cdBackend->updateCard($bookid, $uri, $vcard->serialize());
}
}
}
else {
} else {
// TODO find out how to remove a property
// following does not work properly
$result = $this->contactsManager->createOrUpdate(['URI'=>$uri, 'GEO'=>null], $bookid);
$result = $this->contactsManager->createOrUpdate(['URI' => $uri, 'GEO' => null], $bookid);
}
return new DataResponse('EDITED');
}
else {
} else {
return new DataResponse('READONLY', 400);
}
}
} else {
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folders = $userFolder->getById($myMapId);
if (empty($folders)) {
return new DataResponse('MAP NOT FOUND', 404);
}
$mapsFolder = array_shift($folders);
if (is_null($mapsFolder)) {
return new DataResponse('MAP NOT FOUND',404);
return new DataResponse('MAP NOT FOUND', 404);
}
if (is_null($fileId)) {
$card = $this->cdBackend->getContact($bookid, $uri);
try {
$file=$mapsFolder->get($uri);
$file = $mapsFolder->get($uri);
} catch (NotFoundException $e) {
if (!$mapsFolder->isCreatable()) {
return new DataResponse('CONTACT NOT WRITABLE', 400);
}
$file=$mapsFolder->newFile($uri);
$file = $mapsFolder->newFile($uri);
}
} else {
$files = $mapsFolder->getById($fileId);
@ -559,7 +557,7 @@ class ContactsController extends Controller {
$vcard = Reader::read($card['carddata']);
if ($lat !== null && $lng !== null) {
if (!$attraction && !$house_number && !$road && !$postcode && !$city && !$state && !$country && !$address_string) {
$vcard->add('GEO',$lat.';'.$lng);
$vcard->add('GEO', $lat.';'.$lng);
} elseif (!$address_string) {
$street = trim($attraction.' '.$house_number.' '.$road);
$stringAddress = ';;'.$street.';'.$city.';'.$state.';'.$postcode.';'.$country;
@ -568,7 +566,7 @@ class ContactsController extends Controller {
$lng = floatval($lng);
$this->setAddressCoordinates($lat, $lng, $stringAddress, $uri);
$vcard = Reader::read($card['carddata']);
$vcard->add( 'ADR', ['', '', $street, $city, $state, $postcode, $country], ['TYPE'=>$type]);
$vcard->add('ADR', ['', '', $street, $city, $state, $postcode, $country], ['TYPE' => $type]);
} else {
$stringAddress = $address_string;
// set the coordinates in the DB
@ -576,7 +574,7 @@ class ContactsController extends Controller {
$lng = floatval($lng);
$this->setAddressCoordinates($lat, $lng, $stringAddress, $uri);
$vcard = Reader::read($card['carddata']);
$vcard->add( 'ADR', explode(';',$address_string), ['TYPE'=>$type]);
$vcard->add('ADR', explode(';', $address_string), ['TYPE' => $type]);
}
} else {
$vcard->remove('GEO');
@ -599,25 +597,25 @@ class ContactsController extends Controller {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function addContactToMap(string $bookid, string $uri, int $myMapId, ?int $fileId=null): DataResponse {
public function addContactToMap(string $bookid, string $uri, int $myMapId, ?int $fileId = null): DataResponse {
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folders = $userFolder->getById($myMapId);
if (empty($folders)) {
return new DataResponse('MAP NOT FOUND', 404);
}
$mapsFolder = array_shift($folders);
if (is_null($mapsFolder)) {
return new DataResponse('MAP NOT FOUND',404);
return new DataResponse('MAP NOT FOUND', 404);
}
if (is_null($fileId)) {
$card = $this->cdBackend->getContact($bookid, $uri);
try {
$file=$mapsFolder->get($uri);
$file = $mapsFolder->get($uri);
} catch (NotFoundException $e) {
if (!$mapsFolder->isCreatable()) {
return new DataResponse('CONTACT NOT WRITABLE', 400);
}
$file=$mapsFolder->newFile($uri);
$file = $mapsFolder->newFile($uri);
}
} else {
$files = $mapsFolder->getById($fileId);
@ -697,21 +695,20 @@ class ContactsController extends Controller {
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$req = $qb->execute();
}
else {
} else {
$qb->insert('maps_address_geo')
->values([
'adr'=>$qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm'=>$qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri'=>$qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat'=>$qb->createNamedParameter($lat, IQueryBuilder::PARAM_STR),
'lng'=>$qb->createNamedParameter($lng, IQueryBuilder::PARAM_STR),
'looked_up'=>$qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL),
'adr' => $qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm' => $qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri' => $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat' => $qb->createNamedParameter($lat, IQueryBuilder::PARAM_STR),
'lng' => $qb->createNamedParameter($lng, IQueryBuilder::PARAM_STR),
'looked_up' => $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL),
]);
$req = $qb->execute();
$id = $qb->getLastInsertId();
}$qb = $qb->resetQueryParts();
}
}$qb = $qb->resetQueryParts();
}
/**
@ -744,7 +741,7 @@ class ContactsController extends Controller {
* @param ?int $myMapId
* @return DataResponse
*/
public function deleteContactAddress($bookid, $uri, $uid, $adr, $geo, $fileId=null, $myMapId=null): DataResponse {
public function deleteContactAddress($bookid, $uri, $uid, $adr, $geo, $fileId = null, $myMapId = null): DataResponse {
// vcard
$card = $this->cdBackend->getContact($bookid, $uri);
@ -770,12 +767,10 @@ class ContactsController extends Controller {
$this->cdBackend->updateCard($bookid, $uri, $vcard->serialize());
// no need to cleanup db here, it will be done when catching vcard change hook
return new DataResponse('DELETED');
}
else {
} else {
return new DataResponse('READONLY', 400);
}
}
else {
} else {
return new DataResponse('FAILED', 400);
}
}

Просмотреть файл

@ -11,82 +11,75 @@
namespace OCA\Maps\Controller;
use OCP\App\IAppManager;
use OCA\Maps\Service\DevicesService;
use OCP\IURLGenerator;
use OCP\IConfig;
use \OCP\IL10N;
use OCP\App\IAppManager;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\ApiController;
use OCP\Constants;
use OCP\Share;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCA\Maps\Service\DevicesService;
use OCP\Share\IManager;
class DevicesApiController extends ApiController {
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $devicesService;
protected $appName;
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $devicesService;
protected $appName;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
DevicesService $devicesService,
$UserId){
parent::__construct($AppName, $request,
'PUT, POST, GET, DELETE, PATCH, OPTIONS',
'Authorization, Content-Type, Accept',
1728000);
$this->devicesService = $devicesService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null){
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
DevicesService $devicesService,
$UserId) {
parent::__construct($AppName, $request,
'PUT, POST, GET, DELETE, PATCH, OPTIONS',
'Authorization, Content-Type, Accept',
1728000);
$this->devicesService = $devicesService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
/**
* @NoAdminRequired
@ -95,19 +88,19 @@ class DevicesApiController extends ApiController {
* @param $apiversion
* @return DataResponse
*/
public function getDevices($apiversion): DataResponse {
$now = new \DateTime();
public function getDevices($apiversion): DataResponse {
$now = new \DateTime();
$devices = $this->devicesService->getDevicesFromDB($this->userId);
$devices = $this->devicesService->getDevicesFromDB($this->userId);
$etag = md5(json_encode($devices));
if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
return (new DataResponse($devices))
->setLastModified($now)
->setETag($etag);
}
$etag = md5(json_encode($devices));
if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
return (new DataResponse($devices))
->setLastModified($now)
->setETag($etag);
}
/**
* @NoAdminRequired
@ -117,10 +110,10 @@ class DevicesApiController extends ApiController {
* @param int $pruneBefore
* @return DataResponse
*/
public function getDevicePoints($id, int $pruneBefore=0): DataResponse {
$points = $this->devicesService->getDevicePointsFromDB($this->userId, $id, $pruneBefore);
return new DataResponse($points);
}
public function getDevicePoints($id, int $pruneBefore = 0): DataResponse {
$points = $this->devicesService->getDevicePointsFromDB($this->userId, $id, $pruneBefore);
return new DataResponse($points);
}
/**
* @NoAdminRequired
@ -136,31 +129,30 @@ class DevicesApiController extends ApiController {
* @param $accuracy
* @return DataResponse
*/
public function addDevicePoint($apiversion, $lat, $lng, $timestamp=null, $user_agent=null, $altitude=null, $battery=null, $accuracy=null): DataResponse {
if (is_numeric($lat) and is_numeric($lng)) {
$timestamp = $this->normalizeOptionalNumber($timestamp);
$altitude = $this->normalizeOptionalNumber($altitude);
$battery = $this->normalizeOptionalNumber($battery);
$accuracy = $this->normalizeOptionalNumber($accuracy);
$ts = $timestamp;
if ($timestamp === null) {
$ts = (new \DateTime())->getTimestamp();
}
$ua = $user_agent;
if ($user_agent === null) {
$ua = $_SERVER['HTTP_USER_AGENT'];
}
$deviceId = $this->devicesService->getOrCreateDeviceFromDB($this->userId, $ua);
$pointId = $this->devicesService->addPointToDB($deviceId, $lat, $lng, $ts, $altitude, $battery, $accuracy);
return new DataResponse([
'deviceId'=>$deviceId,
'pointId'=>$pointId
]);
}
else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
public function addDevicePoint($apiversion, $lat, $lng, $timestamp = null, $user_agent = null, $altitude = null, $battery = null, $accuracy = null): DataResponse {
if (is_numeric($lat) and is_numeric($lng)) {
$timestamp = $this->normalizeOptionalNumber($timestamp);
$altitude = $this->normalizeOptionalNumber($altitude);
$battery = $this->normalizeOptionalNumber($battery);
$accuracy = $this->normalizeOptionalNumber($accuracy);
$ts = $timestamp;
if ($timestamp === null) {
$ts = (new \DateTime())->getTimestamp();
}
$ua = $user_agent;
if ($user_agent === null) {
$ua = $_SERVER['HTTP_USER_AGENT'];
}
$deviceId = $this->devicesService->getOrCreateDeviceFromDB($this->userId, $ua);
$pointId = $this->devicesService->addPointToDB($deviceId, $lat, $lng, $ts, $altitude, $battery, $accuracy);
return new DataResponse([
'deviceId' => $deviceId,
'pointId' => $pointId
]);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
/**
* @NoAdminRequired
@ -170,22 +162,20 @@ class DevicesApiController extends ApiController {
* @param $color
* @return DataResponse
*/
public function editDevice($id, $color): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
if (is_string($color) && strlen($color) > 0) {
$this->devicesService->editDeviceInDB($id, $color, null);
$editedDevice = $this->devicesService->getDeviceFromDB($id, $this->userId);
return new DataResponse($editedDevice);
}
else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
public function editDevice($id, $color): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
if (is_string($color) && strlen($color) > 0) {
$this->devicesService->editDeviceInDB($id, $color, null);
$editedDevice = $this->devicesService->getDeviceFromDB($id, $this->userId);
return new DataResponse($editedDevice);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
/**
* @NoAdminRequired
@ -194,26 +184,25 @@ class DevicesApiController extends ApiController {
* @param $id
* @return DataResponse
*/
public function deleteDevice($id): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
$this->devicesService->deleteDeviceFromDB($id);
return new DataResponse('DELETED');
}
else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
public function deleteDevice($id): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
$this->devicesService->deleteDeviceFromDB($id);
return new DataResponse('DELETED');
} else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
/**
* @param $value
* @return float|int|string|null
*/
private function normalizeOptionalNumber($value) {
if (!is_numeric($value)) {
return null;
}
return $value;
}
private function normalizeOptionalNumber($value) {
if (!is_numeric($value)) {
return null;
}
return $value;
}
}

Просмотреть файл

@ -13,95 +13,88 @@ namespace OCA\Maps\Controller;
use OCA\Files_External\NotFoundException;
use OCA\Maps\DB\DeviceShareMapper;
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IURLGenerator;
use OCP\IConfig;
use \OCP\IL10N;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\ApiController;
use OCP\Constants;
use OCP\Share;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IDateTimeZone;
use OCA\Maps\Service\DevicesService;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\IManager;
//use function \OCA\Maps\Service\endswith;
class DevicesController extends Controller {
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $devicesService;
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $devicesService;
private $deviceShareMapper;
private $dateTimeZone;
private $dateTimeZone;
private $root;
protected $appName;
protected $appName;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
DevicesService $devicesService,
DeviceShareMapper $deviceShareMapper,
IDateTimeZone $dateTimeZone,
IRootFolder $root,
$UserId){
parent::__construct($AppName, $request);
$this->devicesService = $devicesService;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
DevicesService $devicesService,
DeviceShareMapper $deviceShareMapper,
IDateTimeZone $dateTimeZone,
IRootFolder $root,
$UserId) {
parent::__construct($AppName, $request);
$this->devicesService = $devicesService;
$this->deviceShareMapper = $deviceShareMapper;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->root = $root;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null){
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
/**
* @NoAdminRequired
@ -109,9 +102,9 @@ class DevicesController extends Controller {
* @param ?int $myMapId
* @return DataResponse
*/
public function getDevices($tokens=null, $myMapId=null): DataResponse {
public function getDevices($tokens = null, $myMapId = null): DataResponse {
if (is_null($tokens)) {
$tokens = array();
$tokens = [];
}
if (is_null($myMapId)) {
$devices = $this->devicesService->getDevicesFromDB($this->userId);
@ -125,7 +118,7 @@ class DevicesController extends Controller {
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
if(is_null($folder)) {
if (is_null($folder)) {
return new DataResponse($this->l->t('Map not Found'), 404);
}
$shares = $this->devicesService->getSharedDevicesFromFolder($folder);
@ -134,8 +127,8 @@ class DevicesController extends Controller {
}
$td = $this->devicesService->getDevicesByTokens($tokens);
$devices = $td + $devices;
return new DataResponse(array_values($devices));
}
return new DataResponse(array_values($devices));
}
/**
* @NoAdminRequired
@ -153,14 +146,14 @@ class DevicesController extends Controller {
* @param int $pruneBefore
* @return DataResponse
*/
public function getDevicePoints($id, ?int $pruneBefore=0, ?int $limit=10000, ?int $offset=0, ?array $tokens=null): DataResponse {
public function getDevicePoints($id, ?int $pruneBefore = 0, ?int $limit = 10000, ?int $offset = 0, ?array $tokens = null): DataResponse {
if (is_null($tokens)) {
$points = $this->devicesService->getDevicePointsFromDB($this->userId, $id, $pruneBefore, $limit, $offset);
} else {
$points = $this->devicesService->getDevicePointsByTokens($tokens, $pruneBefore, $limit, $offset);
}
return new DataResponse($points);
}
return new DataResponse($points);
}
/**
* @NoAdminRequired
@ -173,27 +166,26 @@ class DevicesController extends Controller {
* @param null $accuracy
* @return DataResponse
*/
public function addDevicePoint($lat, $lng, $timestamp=null, $user_agent=null, $altitude=null, $battery=null, $accuracy=null): DataResponse {
if (is_numeric($lat) and is_numeric($lng)) {
$ts = $timestamp;
if ($timestamp === null) {
$ts = (new \DateTime())->getTimestamp();
}
$ua = $user_agent;
if ($user_agent === null) {
$ua = $_SERVER['HTTP_USER_AGENT'];
}
$deviceId = $this->devicesService->getOrCreateDeviceFromDB($this->userId, $ua);
$pointId = $this->devicesService->addPointToDB($deviceId, $lat, $lng, $ts, $altitude, $battery, $accuracy);
return new DataResponse([
'deviceId'=>$deviceId,
'pointId'=>$pointId
]);
}
else {
return new DataResponse('Invalid values', 400);
}
}
public function addDevicePoint($lat, $lng, $timestamp = null, $user_agent = null, $altitude = null, $battery = null, $accuracy = null): DataResponse {
if (is_numeric($lat) and is_numeric($lng)) {
$ts = $timestamp;
if ($timestamp === null) {
$ts = (new \DateTime())->getTimestamp();
}
$ua = $user_agent;
if ($user_agent === null) {
$ua = $_SERVER['HTTP_USER_AGENT'];
}
$deviceId = $this->devicesService->getOrCreateDeviceFromDB($this->userId, $ua);
$pointId = $this->devicesService->addPointToDB($deviceId, $lat, $lng, $ts, $altitude, $battery, $accuracy);
return new DataResponse([
'deviceId' => $deviceId,
'pointId' => $pointId
]);
} else {
return new DataResponse('Invalid values', 400);
}
}
/**
* @NoAdminRequired
@ -202,41 +194,38 @@ class DevicesController extends Controller {
* @param $name
* @return DataResponse
*/
public function editDevice($id, $color, $name): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
if ((is_string($color) && strlen($color) > 0) ||
(is_string($name) && strlen($name) > 0)
) {
$this->devicesService->editDeviceInDB($id, $color, $name);
$editedDevice = $this->devicesService->getDeviceFromDB($id, $this->userId);
return new DataResponse($editedDevice);
}
else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
public function editDevice($id, $color, $name): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
if ((is_string($color) && strlen($color) > 0) ||
(is_string($name) && strlen($name) > 0)
) {
$this->devicesService->editDeviceInDB($id, $color, $name);
$editedDevice = $this->devicesService->getDeviceFromDB($id, $this->userId);
return new DataResponse($editedDevice);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
/**
* @NoAdminRequired
* @param $id
* @return DataResponse
*/
public function deleteDevice($id): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
$this->devicesService->deleteDeviceFromDB($id);
public function deleteDevice($id): DataResponse {
$device = $this->devicesService->getDeviceFromDB($id, $this->userId);
if ($device !== null) {
$this->devicesService->deleteDeviceFromDB($id);
$this->deviceShareMapper->removeAllByDeviceId($id);
return new DataResponse('DELETED');
}
else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
return new DataResponse('DELETED');
} else {
return new DataResponse($this->l->t('No such device'), 400);
}
}
/**
* @NoAdminRequired
@ -248,55 +237,53 @@ class DevicesController extends Controller {
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
*/
public function exportDevices($deviceIdList, $begin, $end, bool $all=false): DataResponse {
// sorry about ugly deviceIdList management:
// when an empty list is passed in http request, we get null here
if ($deviceIdList === null or (is_array($deviceIdList) and count($deviceIdList) === 0)) {
return new DataResponse($this->l->t('No device to export'), 400);
}
public function exportDevices($deviceIdList, $begin, $end, bool $all = false): DataResponse {
// sorry about ugly deviceIdList management:
// when an empty list is passed in http request, we get null here
if ($deviceIdList === null or (is_array($deviceIdList) and count($deviceIdList) === 0)) {
return new DataResponse($this->l->t('No device to export'), 400);
}
// create /Maps directory if necessary
$userFolder = $this->userfolder;
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
return new DataResponse($this->l->t('/Maps is not a directory'), 400);
}
else if (!$mapsFolder->isCreatable()) {
return new DataResponse($this->l->t('/Maps directory is not writeable'), 400);
}
}
else {
return new DataResponse($this->l->t('Impossible to create /Maps directory'), 400);
}
// create /Maps directory if necessary
$userFolder = $this->userfolder;
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
return new DataResponse($this->l->t('/Maps is not a directory'), 400);
} elseif (!$mapsFolder->isCreatable()) {
return new DataResponse($this->l->t('/Maps directory is not writeable'), 400);
}
} else {
return new DataResponse($this->l->t('Impossible to create /Maps directory'), 400);
}
$nbDevices = $this->devicesService->countPoints($this->userId, $deviceIdList, $begin, $end);
if ($nbDevices === 0) {
return new DataResponse($this->l->t('Nothing to export'), 400);
}
$nbDevices = $this->devicesService->countPoints($this->userId, $deviceIdList, $begin, $end);
if ($nbDevices === 0) {
return new DataResponse($this->l->t('Nothing to export'), 400);
}
// generate export file name
$prefix = $all ? '' : 'filtered-';
$tz = $this->dateTimeZone->getTimeZone();
$now = new \DateTime('now', $tz);
$dateStr = $now->format('Y-m-d H:i:s (P)');
$filename = $dateStr.' '.$prefix.'devices.gpx';
// generate export file name
$prefix = $all ? '' : 'filtered-';
$tz = $this->dateTimeZone->getTimeZone();
$now = new \DateTime('now', $tz);
$dateStr = $now->format('Y-m-d H:i:s (P)');
$filename = $dateStr.' '.$prefix.'devices.gpx';
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
$file = $mapsFolder->newFile($filename);
$handler = $file->fopen('w');
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
$file = $mapsFolder->newFile($filename);
$handler = $file->fopen('w');
$this->devicesService->exportDevices($this->userId, $handler, $deviceIdList, $begin, $end, $this->appVersion, $filename);
$this->devicesService->exportDevices($this->userId, $handler, $deviceIdList, $begin, $end, $this->appVersion, $filename);
fclose($handler);
$file->touch();
return new DataResponse('/Maps/'.$filename);
}
fclose($handler);
$file->touch();
return new DataResponse('/Maps/'.$filename);
}
/**
* @NoAdminRequired
@ -305,46 +292,45 @@ class DevicesController extends Controller {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
*/
public function importDevices($path): DataResponse {
$userFolder = $this->userfolder;
$cleanpath = str_replace(array('../', '..\\'), '', $path);
public function importDevices($path): DataResponse {
$userFolder = $this->userfolder;
$cleanpath = str_replace(['../', '..\\'], '', $path);
if ($userFolder->nodeExists($cleanpath)){
$file = $userFolder->get($cleanpath);
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE and
$file->isReadable()){
$lowerFileName = strtolower($file->getName());
if ($this->endsWith($lowerFileName, '.gpx') or $this->endsWith($lowerFileName, '.kml') or $this->endsWith($lowerFileName, '.kmz')) {
$nbImported = $this->devicesService->importDevices($this->userId, $file);
return new DataResponse($nbImported);
}
else {
// invalid extension
return new DataResponse($this->l->t('Invalid file extension'), 400);
}
}
else {
// directory or not readable
return new DataResponse($this->l->t('Impossible to read the file'), 400);
}
}
else {
// does not exist
return new DataResponse($this->l->t('File does not exist'), 400);
}
}
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE and
$file->isReadable()) {
$lowerFileName = strtolower($file->getName());
if ($this->endsWith($lowerFileName, '.gpx') or $this->endsWith($lowerFileName, '.kml') or $this->endsWith($lowerFileName, '.kmz')) {
$nbImported = $this->devicesService->importDevices($this->userId, $file);
return new DataResponse($nbImported);
} else {
// invalid extension
return new DataResponse($this->l->t('Invalid file extension'), 400);
}
} else {
// directory or not readable
return new DataResponse($this->l->t('Impossible to read the file'), 400);
}
} else {
// does not exist
return new DataResponse($this->l->t('File does not exist'), 400);
}
}
/**
* @param $string
* @param $test
* @return bool
*/
private function endsWith($string, $test): bool {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
private function endsWith($string, $test): bool {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) {
return false;
}
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
/**
* @NoAdminRequired
@ -353,7 +339,7 @@ class DevicesController extends Controller {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function getSharedDevices(?int $myMapId=null): DataResponse {
public function getSharedDevices(?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$sharedDevices = [];
} else {
@ -378,10 +364,9 @@ class DevicesController extends Controller {
$share = $this->deviceShareMapper->create($id, $timestampFrom, $timestampTo);
if ($share === null) {
return new DataResponse($this->l->t("Error sharing device"), Http::STATUS_INTERNAL_SERVER_ERROR);
return new DataResponse($this->l->t('Error sharing device'), Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
else {
} else {
return new DataResponse($this->l->t('No such device'), 400);
}
@ -398,7 +383,7 @@ class DevicesController extends Controller {
public function removeDeviceShare(int $token): DataResponse {
try {
$share = $this->deviceShareMapper->findByToken($token);
} catch ( DoesNotExistException $e ) {
} catch (DoesNotExistException $e) {
throw new NotFoundException();
}
$device = $this->devicesService->getDeviceFromDB($share->getDeviceId(), $this->userId);
@ -419,18 +404,18 @@ class DevicesController extends Controller {
public function addSharedDeviceToMap(string $token, $targetMapId): DataResponse {
try {
$share = $this->deviceShareMapper->findByToken($token);
} catch ( DoesNotExistException $e ) {
} catch (DoesNotExistException $e) {
return new DataResponse($this->l->t('Share not Found'), 404);
}
$folders = $this->userfolder->getById($targetMapId);
$folder = array_shift($folders);
if(is_null($folder)) {
if (is_null($folder)) {
return new DataResponse($this->l->t('Map not Found'), 404);
}
try {
$file=$folder->get(".device_shares.json");
$file = $folder->get('.device_shares.json');
} catch (\OCP\Files\NotFoundException $e) {
$file=$folder->newFile(".device_shares.json", $content = '[]');
$file = $folder->newFile('.device_shares.json', $content = '[]');
}
$data = json_decode($file->getContent(), true);
foreach ($data as $s) {
@ -439,27 +424,27 @@ class DevicesController extends Controller {
}
}
$data[] = $share;
$file->putContent(json_encode($data,JSON_PRETTY_PRINT));
$file->putContent(json_encode($data, JSON_PRETTY_PRINT));
return new DataResponse('Done');
}
public function removeSharedDeviceFromMap(string $token, int $myMapId): DataResponse {
$folders = $this->userfolder->getById($myMapId);
$folder = array_shift($folders);
if(is_null($folder)) {
if (is_null($folder)) {
return new DataResponse($this->l->t('Map not Found'), 404);
}
try {
$file=$folder->get(".device_shares.json");
$file = $folder->get('.device_shares.json');
} catch (\OCP\Files\NotFoundException $e) {
$file=$folder->newFile(".device_shares.json", $content = '[]');
$file = $folder->newFile('.device_shares.json', $content = '[]');
}
$data = json_decode($file->getContent(), true);
$shares = [];
$deleted = null;
foreach ($data as $share) {
$t = $share["token"];
if($t === $token) {
$t = $share['token'];
if ($t === $token) {
$deleted = $share;
} else {
$shares[] = $share;

Просмотреть файл

@ -16,64 +16,64 @@ use OCP\App\IAppManager;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IConfig;
class FavoritesApiController extends ApiController {
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $favoritesService;
protected $appName;
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $favoritesService;
protected $appName;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
$UserId) {
parent::__construct($AppName, $request,
'PUT, POST, GET, DELETE, PATCH, OPTIONS',
'Authorization, Content-Type, Accept',
1728000);
$this->favoritesService = $favoritesService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
$UserId) {
parent::__construct($AppName, $request,
'PUT, POST, GET, DELETE, PATCH, OPTIONS',
'Authorization, Content-Type, Accept',
1728000);
$this->favoritesService = $favoritesService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
/**
* @NoAdminRequired
@ -83,19 +83,19 @@ class FavoritesApiController extends ApiController {
* @param int $pruneBefore
* @return DataResponse
*/
public function getFavorites($apiversion, int $pruneBefore = 0): DataResponse {
$now = new \DateTime();
public function getFavorites($apiversion, int $pruneBefore = 0): DataResponse {
$now = new \DateTime();
$favorites = $this->favoritesService->getFavoritesFromDB($this->userId, $pruneBefore);
$favorites = $this->favoritesService->getFavoritesFromDB($this->userId, $pruneBefore);
$etag = md5(json_encode($favorites));
if ($this->request->getHeader('If-None-Match') === '"' . $etag . '"') {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
return (new DataResponse($favorites))
->setLastModified($now)
->setETag($etag);
}
$etag = md5(json_encode($favorites));
if ($this->request->getHeader('If-None-Match') === '"' . $etag . '"') {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
return (new DataResponse($favorites))
->setLastModified($now)
->setETag($etag);
}
/**
* @NoAdminRequired
@ -110,15 +110,15 @@ class FavoritesApiController extends ApiController {
* @param $extensions
* @return DataResponse
*/
public function addFavorite($apiversion, $name, $lat, $lng, $category, $comment, $extensions): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
$favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
return new DataResponse($favorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
public function addFavorite($apiversion, $name, $lat, $lng, $category, $comment, $extensions): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
$favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
return new DataResponse($favorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
/**
* @NoAdminRequired
@ -133,22 +133,22 @@ class FavoritesApiController extends ApiController {
* @param $extensions
* @return DataResponse
*/
public function editFavorite($id, $name, $lat, $lng, $category, $comment, $extensions): DataResponse {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
if (($lat === null || is_numeric($lat)) &&
($lng === null || is_numeric($lng))
) {
$this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $category, $comment, $extensions);
$editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
return new DataResponse($editedFavorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('No such favorite'), 400);
}
}
public function editFavorite($id, $name, $lat, $lng, $category, $comment, $extensions): DataResponse {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
if (($lat === null || is_numeric($lat)) &&
($lng === null || is_numeric($lng))
) {
$this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $category, $comment, $extensions);
$editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
return new DataResponse($editedFavorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('No such favorite'), 400);
}
}
/**
* @NoAdminRequired
@ -157,14 +157,14 @@ class FavoritesApiController extends ApiController {
* @param $id
* @return DataResponse
*/
public function deleteFavorite($id): DataResponse {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
$this->favoritesService->deleteFavoriteFromDB($id);
return new DataResponse('DELETED');
} else {
return new DataResponse($this->l->t('No such favorite'), 400);
}
}
public function deleteFavorite($id): DataResponse {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
$this->favoritesService->deleteFavoriteFromDB($id);
return new DataResponse('DELETED');
} else {
return new DataResponse($this->l->t('No such favorite'), 400);
}
}
}

Просмотреть файл

@ -13,97 +13,97 @@
namespace OCA\Maps\Controller;
use OCP\Files\NotFoundException;
use OCA\Maps\DB\FavoriteShareMapper;
use OCA\Maps\Service\FavoritesService;
use OCP\App\IAppManager;
use OCP\Share\IManager;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\IManager;
class FavoritesController extends Controller {
private string $userId;
private \OCP\Files\Folder $userFolder;
private IConfig $config;
private string $appVersion;
private IManager $shareManager;
private IUserManager $userManager;
private IGroupManager $groupManager;
private string $dbtype;
private string $userId;
private \OCP\Files\Folder $userFolder;
private IConfig $config;
private string $appVersion;
private IManager $shareManager;
private IUserManager $userManager;
private IGroupManager $groupManager;
private string $dbtype;
private IL10N $l;
private ILogger $logger;
private FavoritesService $favoritesService;
private IDateTimeZone $dateTimeZone;
private ?string $defaultFavoritsJSON;
protected $appName;
private ILogger $logger;
private FavoritesService $favoritesService;
private IDateTimeZone $dateTimeZone;
private ?string $defaultFavoritsJSON;
protected $appName;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
IDateTimeZone $dateTimeZone,
FavoriteShareMapper $favoriteShareMapper,
$UserId) {
parent::__construct($AppName, $request);
$this->favoritesService = $favoritesService;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userFolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
$this->favoriteShareMapper = $favoriteShareMapper;
$this->defaultFavoritsJSON = json_encode([
"type" => "FeatureCollection",
"features"=> []
],JSON_PRETTY_PRINT);
}
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
IDateTimeZone $dateTimeZone,
FavoriteShareMapper $favoriteShareMapper,
$UserId) {
parent::__construct($AppName, $request);
$this->favoritesService = $favoritesService;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userFolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
$this->favoriteShareMapper = $favoriteShareMapper;
$this->defaultFavoritsJSON = json_encode([
'type' => 'FeatureCollection',
'features' => []
], JSON_PRETTY_PRINT);
}
/**
* @param \OCP\Files\Folder $folder
* @return mixed
* @throws \OCP\Files\NotPermittedException
*/
private function getJSONFavoritesFile(\OCP\Files\Folder $folder): \OCP\Files\Node {
try {
$file = $folder->get('.favorites.json');
} catch (NotFoundException $e) {
$file = $folder->newFile('.favorites.json', $content = $this->defaultFavoritsJSON);
}
return $file;
}
private function getJSONFavoritesFile(\OCP\Files\Folder $folder): \OCP\Files\Node {
try {
$file = $folder->get('.favorites.json');
} catch (NotFoundException $e) {
$file = $folder->newFile('.favorites.json', $content = $this->defaultFavoritsJSON);
}
return $file;
}
/**
* @NoAdminRequired
@ -111,17 +111,17 @@ class FavoritesController extends Controller {
* @return DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function getFavorites(int $myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$favorites = $this->favoritesService->getFavoritesFromDB($this->userId);
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$favorites = $this->favoritesService->getFavoritesFromJSON($file);
}
return new DataResponse($favorites);
}
public function getFavorites(?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$favorites = $this->favoritesService->getFavoritesFromDB($this->userId);
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$favorites = $this->favoritesService->getFavoritesFromJSON($file);
}
return new DataResponse($favorites);
}
/**
* @NoAdminRequired
@ -137,31 +137,31 @@ class FavoritesController extends Controller {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotPermittedException
*/
public function addFavorite(?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId=null): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
if (is_null($myMapId) || $myMapId === '') {
$favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
return new DataResponse($favorite);
} else {
$folders = $this->userFolder->getById($myMapId);
if(!empty($folders) && $this->userFolder->getId() === $myMapId) {
public function addFavorite(?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId = null): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
if (is_null($myMapId) || $myMapId === '') {
$favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
return new DataResponse($favorite);
} else {
$folders = $this->userFolder->getById($myMapId);
if (!empty($folders) && $this->userFolder->getId() === $myMapId) {
$folders[] = $this->userFolder;
}
$folder = array_shift($folders);
if(is_null($folder)) {
$folder = array_shift($folders);
if (is_null($folder)) {
return new DataResponse('Map not found', 404);
}
$file = $this->getJSONFavoritesFile($folder);
$favoriteId = $this->favoritesService->addFavoriteToJSON($file, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromJSON($file, $favoriteId);
return new DataResponse($favorite);
}
$file = $this->getJSONFavoritesFile($folder);
$favoriteId = $this->favoritesService->addFavoriteToJSON($file, $name, $lat, $lng, $category, $comment, $extensions);
$favorite = $this->favoritesService->getFavoriteFromJSON($file, $favoriteId);
return new DataResponse($favorite);
}
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
/**
* @NoAdminRequired
@ -172,32 +172,32 @@ class FavoritesController extends Controller {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotPermittedException
*/
public function addFavorites(array $favorites, ?int $myMapId=null): DataResponse {
public function addFavorites(array $favorites, ?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$favoritesAfter = [];
forEach ($favorites as $favorite) {
foreach ($favorites as $favorite) {
if (is_numeric($favorite->lat) && is_numeric($favorite->lng)) {
$favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $favorite->name, $favorite->lat, $favorite->lng, $favorite->category, $favorite->comment, $favorite->extensions);
$favoritesAfter[] = $this->favoritesService->getFavoriteFromDB($favoriteId);
} else {
} else {
return new DataResponse('invalid values', 400);
}
}
return new DataResponse($favoritesAfter);
} else {
$folders = $this->userFolder->getById($myMapId);
if(!empty($folders) && $this->userFolder->getId() === $myMapId) {
if (!empty($folders) && $this->userFolder->getId() === $myMapId) {
$folders[] = $this->userFolder;
}
$folder = array_shift($folders);
if(is_null($folder)) {
if (is_null($folder)) {
return new DataResponse('Map not found', 404);
}
$file = $this->getJSONFavoritesFile($folder);
$favoriteIds = $this->favoritesService->addFavoritesToJSON($file, $favorites);
$favoritesAfter = [];
forEach ($this->favoritesService->getFavoritesFromJSON($file) as $favorite) {
if (in_array($favorite['id'],$favoriteIds)) {
foreach ($this->favoritesService->getFavoritesFromJSON($file) as $favorite) {
if (in_array($favorite['id'], $favoriteIds)) {
$favoritesAfter[] = $favorite;
}
};
@ -218,26 +218,26 @@ class FavoritesController extends Controller {
* @return DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId==='') {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
if (($lat === null || is_numeric($lat)) &&
($lng === null || is_numeric($lng))
) {
$this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $category, $comment, $extensions);
$editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
return new DataResponse($editedFavorite);
} else {
return new DataResponse($this->l->t('invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('no such favorite'), 400);
}
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
if (($lat === null || is_numeric($lat)) &&
($lng === null || is_numeric($lng))
) {
$this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $category, $comment, $extensions);
$editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
return new DataResponse($editedFavorite);
} else {
return new DataResponse($this->l->t('invalid values'), 400);
}
} else {
return new DataResponse($this->l->t('no such favorite'), 400);
}
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$favorite = $this->favoritesService->getFavoriteFromJSON($file, $id, $this->userId);
if ($favorite !== null) {
if (($lat === null || is_numeric($lat)) &&
@ -252,8 +252,8 @@ class FavoritesController extends Controller {
} else {
return new DataResponse($this->l->t('no such favorite'), 400);
}
}
}
}
}
/**
* @NoAdminRequired
@ -264,29 +264,29 @@ class FavoritesController extends Controller {
* @throws \OCP\DB\Exception
* @throws \OCP\Files\NotPermittedException
*/
public function renameCategories(array $categories, string $newName, ?int $myMapId=null): DataResponse {
if (is_array($categories)) {
foreach ($categories as $cat) {
if (is_null($myMapId) || $myMapId === ""){
$this->favoritesService->renameCategoryInDB($this->userId, $cat, $newName);
public function renameCategories(array $categories, string $newName, ?int $myMapId = null): DataResponse {
if (is_array($categories)) {
foreach ($categories as $cat) {
if (is_null($myMapId) || $myMapId === '') {
$this->favoritesService->renameCategoryInDB($this->userId, $cat, $newName);
// Rename share if one exists
try {
$share = $this->favoriteShareMapper->findByOwnerAndCategory($this->userId, $cat);
$share->setCategory($newName);
$this->favoriteShareMapper->update($share);
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
}
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$this->favoritesService->renameCategoryInJSON($file, $cat, $newName);
}
}
}
return new DataResponse('RENAMED');
}
// Rename share if one exists
try {
$share = $this->favoriteShareMapper->findByOwnerAndCategory($this->userId, $cat);
$share->setCategory($newName);
$this->favoriteShareMapper->update($share);
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
}
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$this->favoritesService->renameCategoryInJSON($file, $cat, $newName);
}
}
}
return new DataResponse('RENAMED');
}
/**
* @NoAdminRequired
@ -295,15 +295,15 @@ class FavoritesController extends Controller {
* @return DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function deleteFavorite(int $id, ?int $myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId === "") {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
$this->favoritesService->deleteFavoriteFromDB($id);
return new DataResponse('DELETED');
}
public function deleteFavorite(int $id, ?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
if ($favorite !== null) {
$this->favoritesService->deleteFavoriteFromDB($id);
return new DataResponse('DELETED');
}
return new DataResponse($this->l->t('no such favorite'), 400);
}
}
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
@ -311,7 +311,7 @@ class FavoritesController extends Controller {
return new DataResponse('DELETED');
}
return new DataResponse($this->l->t('no such favorite'), 400);
}
}
/**
* @NoAdminRequired
@ -320,17 +320,17 @@ class FavoritesController extends Controller {
* @return DataResponse
* @throws \OCP\Files\NotPermittedException
*/
public function deleteFavorites(array $ids, ?int $myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId === "") {
$this->favoritesService->deleteFavoritesFromDB($ids, $this->userId);
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$this->favoritesService->deleteFavoritesFromJSON($file, $ids);
}
return new DataResponse('DELETED');
}
public function deleteFavorites(array $ids, ?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$this->favoritesService->deleteFavoritesFromDB($ids, $this->userId);
} else {
$folders = $this->userFolder->getById($myMapId);
$folder = array_shift($folders);
$file = $this->getJSONFavoritesFile($folder);
$this->favoritesService->deleteFavoritesFromJSON($file, $ids);
}
return new DataResponse('DELETED');
}
/**
* @NoAdminRequired
@ -339,51 +339,51 @@ class FavoritesController extends Controller {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function getSharedCategories(?int $myMapId=null): DataResponse {
public function getSharedCategories(?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$categories = $this->favoriteShareMapper->findAllByOwner($this->userId);
} else {
$categories = $this->favoriteShareMapper->findAllByMapId($this->userId, $myMapId);
}
return new DataResponse($categories);
}
return new DataResponse($categories);
}
/**
* @NoAdminRequired
* @param string $category
* @return DataResponse
*/
public function shareCategory(string $category): DataResponse {
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
return new DataResponse($this->l->t("Unknown category"), Http::STATUS_BAD_REQUEST);
}
public function shareCategory(string $category): DataResponse {
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
return new DataResponse($this->l->t('Unknown category'), Http::STATUS_BAD_REQUEST);
}
$share = $this->favoriteShareMapper->findOrCreateByOwnerAndCategory($this->userId, $category);
$share = $this->favoriteShareMapper->findOrCreateByOwnerAndCategory($this->userId, $category);
if ($share === null) {
return new DataResponse($this->l->t("Error sharing favorite"), Http::STATUS_INTERNAL_SERVER_ERROR);
}
if ($share === null) {
return new DataResponse($this->l->t('Error sharing favorite'), Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new DataResponse($share);
}
return new DataResponse($share);
}
/**
* @NoAdminRequired
* @param string $category
* @return DataResponse
*/
public function unShareCategory(string $category): DataResponse {
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
return new DataResponse($this->l->t("Unknown category"), Http::STATUS_BAD_REQUEST);
}
public function unShareCategory(string $category): DataResponse {
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
return new DataResponse($this->l->t('Unknown category'), Http::STATUS_BAD_REQUEST);
}
$didExist = $this->favoriteShareMapper->removeByOwnerAndCategory($this->userId, $category);
$didExist = $this->favoriteShareMapper->removeByOwnerAndCategory($this->userId, $category);
return new DataResponse([
'did_exist' => $didExist
]);
}
return new DataResponse([
'did_exist' => $didExist
]);
}
/**
* @NoAdminRequired
@ -396,21 +396,21 @@ class FavoritesController extends Controller {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function addShareCategoryToMap(string $category, int $targetMapId, ?int $myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId==='') {
public function addShareCategoryToMap(string $category, int $targetMapId, ?int $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$share = $this->favoriteShareMapper->findByOwnerAndCategory($this->userId, $category);
} else {
$share = $this->favoriteShareMapper->findByMapIdAndCategory($this->userId, $myMapId, $category);
}
$folders = $this->userFolder->getById($targetMapId);
$folder = array_shift($folders);
if(is_null($folder)) {
if (is_null($folder)) {
return new DataResponse($this->l->t('Map not Found'), 404);
}
try {
$file=$folder->get(".favorite_shares.json");
$file = $folder->get('.favorite_shares.json');
} catch (NotFoundException $e) {
$file=$folder->newFile(".favorite_shares.json", $content = '[]');
$file = $folder->newFile('.favorite_shares.json', $content = '[]');
}
$data = json_decode($file->getContent(), true);
foreach ($data as $s) {
@ -420,7 +420,7 @@ class FavoritesController extends Controller {
}
$share->id = count($data);
$data[] = $share;
$file->putContent(json_encode($data,JSON_PRETTY_PRINT));
$file->putContent(json_encode($data, JSON_PRETTY_PRINT));
return new DataResponse('Done');
}
@ -448,60 +448,58 @@ class FavoritesController extends Controller {
* @throws NotFoundException
* @throws \OCP\Files\NotPermittedException
*/
public function exportFavorites(?array $categoryList = null, ?int $begin = null, ?int $end = null, bool $all = false): DataResponse {
// sorry about ugly categoryList management:
// when an empty list is passed in http request, we get null here
if ($categoryList === null or (is_array($categoryList) and count($categoryList) === 0)) {
$response = new DataResponse($this->l->t('Nothing to export'), 400);
return $response;
}
public function exportFavorites(?array $categoryList = null, ?int $begin = null, ?int $end = null, bool $all = false): DataResponse {
// sorry about ugly categoryList management:
// when an empty list is passed in http request, we get null here
if ($categoryList === null or (is_array($categoryList) and count($categoryList) === 0)) {
$response = new DataResponse($this->l->t('Nothing to export'), 400);
return $response;
}
// create /Maps directory if necessary
$userFolder = $this->userFolder;
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
$response = new DataResponse($this->l->t('/Maps is not a directory'), 400);
return $response;
}
else if (!$mapsFolder->isCreatable()) {
$response = new DataResponse($this->l->t('/Maps directory is not writeable'), 400);
return $response;
}
}
else {
$response = new DataResponse($this->l->t('Impossible to create /Maps directory'), 400);
return $response;
}
// create /Maps directory if necessary
$userFolder = $this->userFolder;
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
$response = new DataResponse($this->l->t('/Maps is not a directory'), 400);
return $response;
} elseif (!$mapsFolder->isCreatable()) {
$response = new DataResponse($this->l->t('/Maps directory is not writeable'), 400);
return $response;
}
} else {
$response = new DataResponse($this->l->t('Impossible to create /Maps directory'), 400);
return $response;
}
$nbFavorites = $this->favoritesService->countFavorites($this->userId, $categoryList, $begin, $end);
if ($nbFavorites === 0) {
$response = new DataResponse($this->l->t('Nothing to export'), 400);
return $response;
}
$nbFavorites = $this->favoritesService->countFavorites($this->userId, $categoryList, $begin, $end);
if ($nbFavorites === 0) {
$response = new DataResponse($this->l->t('Nothing to export'), 400);
return $response;
}
// generate export file name
$prefix = $all ? '' : 'filtered-';
$tz = $this->dateTimeZone->getTimeZone();
$now = new \DateTime('now', $tz);
$dateStr = $now->format('Y-m-d H:i:s (P)');
$filename = $dateStr.' '.$prefix.'favorites.gpx';
// generate export file name
$prefix = $all ? '' : 'filtered-';
$tz = $this->dateTimeZone->getTimeZone();
$now = new \DateTime('now', $tz);
$dateStr = $now->format('Y-m-d H:i:s (P)');
$filename = $dateStr.' '.$prefix.'favorites.gpx';
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
$file = $mapsFolder->newFile($filename);
$handler = $file->fopen('w');
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
$file = $mapsFolder->newFile($filename);
$handler = $file->fopen('w');
$this->favoritesService->exportFavorites($this->userId, $handler, $categoryList, $begin, $end, $this->appVersion);
$this->favoritesService->exportFavorites($this->userId, $handler, $categoryList, $begin, $end, $this->appVersion);
fclose($handler);
$file->touch();
return new DataResponse('/Maps/'.$filename);
}
fclose($handler);
$file->touch();
return new DataResponse('/Maps/'.$filename);
}
/**
* @NoAdminRequired
@ -510,45 +508,44 @@ class FavoritesController extends Controller {
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
public function importFavorites(string $path): DataResponse {
$userFolder = $this->userFolder;
$cleanpath = str_replace(array('../', '..\\'), '',$path);
public function importFavorites(string $path): DataResponse {
$userFolder = $this->userFolder;
$cleanpath = str_replace(['../', '..\\'], '', $path);
if ($userFolder->nodeExists($cleanpath)){
$file = $userFolder->get($cleanpath);
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE and
$file->isReadable()){
$lowerFileName = strtolower($file->getName());
if ($this->endswith($lowerFileName, '.gpx') or $this->endswith($lowerFileName, '.kml') or $this->endswith($lowerFileName, '.kmz') or $this->endswith($lowerFileName, '.json') or $this->endswith($lowerFileName, '.geojson')) {
$result = $this->favoritesService->importFavorites($this->userId, $file);
return new DataResponse($result);
}
else {
// invalid extension
return new DataResponse($this->l->t('Invalid file extension'), 400);
}
}
else {
// directory or not readable
return new DataResponse($this->l->t('Impossible to read the file'), 400);
}
}
else {
// does not exist
return new DataResponse($this->l->t('File does not exist'), 400);
}
}
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE and
$file->isReadable()) {
$lowerFileName = strtolower($file->getName());
if ($this->endswith($lowerFileName, '.gpx') or $this->endswith($lowerFileName, '.kml') or $this->endswith($lowerFileName, '.kmz') or $this->endswith($lowerFileName, '.json') or $this->endswith($lowerFileName, '.geojson')) {
$result = $this->favoritesService->importFavorites($this->userId, $file);
return new DataResponse($result);
} else {
// invalid extension
return new DataResponse($this->l->t('Invalid file extension'), 400);
}
} else {
// directory or not readable
return new DataResponse($this->l->t('Impossible to read the file'), 400);
}
} else {
// does not exist
return new DataResponse($this->l->t('File does not exist'), 400);
}
}
/**
* @param string $string
* @param string $test
* @return bool
*/
private function endswith(string $string, string $test): bool {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
private function endswith(string $string, string $test): bool {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) {
return false;
}
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
}

Просмотреть файл

@ -14,61 +14,56 @@
namespace OCA\Maps\Controller;
use OCA\Maps\Service\MyMapsService;
use OCP\Files\NotFoundException;
use OCA\Maps\DB\FavoriteShareMapper;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class MyMapsController extends Controller {
/* @var MyMapsService */
private $myMapsService;
/* @var MyMapsService */
private $myMapsService;
private $userId;
public function __construct(string $AppName, IRequest $request, MyMapsService $myMapsService, $userId) {
parent::__construct($AppName, $request);
$this->myMapsService = $myMapsService;
public function __construct(string $AppName, IRequest $request, MyMapsService $myMapsService, $userId) {
parent::__construct($AppName, $request);
$this->myMapsService = $myMapsService;
$this->userId = $userId;
}
}
/**
* @NoAdminRequired
*/
public function addMyMap(array $values): DataResponse {
$newName = $values["newName"] ?? "New Map";
$myMap = $this->myMapsService->addMyMap($newName, $this->userId);
if (is_string($myMap)) {
new DataResponse($myMap, 400);
}
return new DataResponse($myMap);
}
/**
* @NoAdminRequired
*/
public function addMyMap(array $values): DataResponse {
$newName = $values['newName'] ?? 'New Map';
$myMap = $this->myMapsService->addMyMap($newName, $this->userId);
if (is_string($myMap)) {
new DataResponse($myMap, 400);
}
return new DataResponse($myMap);
}
/**
* @NoAdminRequired
*/
public function updateMyMap(int $id, array $values): DataResponse {
$myMap = $this->myMapsService->updateMyMap($id, $values, $this->userId);
return new DataResponse($myMap);
}
/**
* @NoAdminRequired
*/
public function updateMyMap(int $id, array $values): DataResponse {
$myMap = $this->myMapsService->updateMyMap($id, $values, $this->userId);
return new DataResponse($myMap);
}
/**
* @NoAdminRequired
*/
public function deleteMyMap(int $id): DataResponse {
$result = $this->myMapsService->deleteMyMap($id, $this->userId);
return new DataResponse($result);
}
/**
* @NoAdminRequired
*/
public function deleteMyMap(int $id): DataResponse {
$result = $this->myMapsService->deleteMyMap($id, $this->userId);
return new DataResponse($result);
}
/**
* @NoAdminRequired
*/
public function getMyMaps(): DataResponse {
$myMaps = $this->myMapsService->getAllMyMaps($this->userId);
return new DataResponse($myMaps);
}
/**
* @NoAdminRequired
*/
public function getMyMaps(): DataResponse {
$myMaps = $this->myMapsService->getAllMyMaps($this->userId);
return new DataResponse($myMaps);
}
}

Просмотреть файл

@ -13,32 +13,32 @@ namespace OCA\Maps\Controller;
use OCA\Files\Event\LoadSidebar;
use OCA\Viewer\Event\LoadViewer;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Controller;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
class PageController extends Controller {
private $userId;
private $config;
private $userId;
private $config;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var IEventDispatcher */
private $eventDispatcher;
public function __construct($AppName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
$UserId){
parent::__construct($AppName, $request);
$this->userId = $UserId;
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->initialStateService = $initialStateService;
}
public function __construct($AppName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
$UserId) {
parent::__construct($AppName, $request);
$this->userId = $UserId;
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->initialStateService = $initialStateService;
}
/**
* CAUTION: the @Stuff turns off security checks; for this page no admin is
@ -51,35 +51,35 @@ class PageController extends Controller {
* @NoCSRFRequired
* @return TemplateResponse
*/
public function index(): TemplateResponse {
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
public function index(): TemplateResponse {
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$params = array('user' => $this->userId);
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new TemplateResponse('maps', 'main', $params);
$params = ['user' => $this->userId];
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new TemplateResponse('maps', 'main', $params);
$this->addCsp($response);
$this->addCsp($response);
return $response;
}
return $response;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function indexMyMap($myMapId): TemplateResponse {
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function indexMyMap($myMapId): TemplateResponse {
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$params = array('user' => $this->userId);
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new TemplateResponse('maps', 'main', $params);
$params = ['user' => $this->userId];
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new TemplateResponse('maps', 'main', $params);
$this->addCsp($response);
$this->addCsp($response);
return $response;
}
return $response;
}
/**
* @NoAdminRequired
@ -87,64 +87,64 @@ class PageController extends Controller {
* @param $url
* @return TemplateResponse
*/
public function openGeoLink($url): TemplateResponse {
return $this->index();
}
public function openGeoLink($url): TemplateResponse {
return $this->index();
}
/**
* @param $response
* @return void
*/
private function addCsp($response): void {
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
private function addCsp($response): void {
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://events.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://events.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
$csp->addAllowedChildSrcDomain('blob:');
$csp->addAllowedWorkerSrcDomain('blob:');
$csp->addAllowedScriptDomain('https://unpkg.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL',
$csp->addAllowedChildSrcDomain('blob:');
$csp->addAllowedWorkerSrcDomain('blob:');
$csp->addAllowedScriptDomain('https://unpkg.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
//$csp->addAllowedConnectDomain('http://192.168.0.66:5000');
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
//$csp->addAllowedConnectDomain('http://192.168.0.66:5000');
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
}

Просмотреть файл

@ -13,41 +13,40 @@
namespace OCA\Maps\Controller;
use OC\User\NoUserException;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\DB\Exception;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\ILogger;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCP\ILogger;
use OCP\IRequest;
class PhotosController extends Controller {
private $userId;
private $geophotoService;
private $photofilesService;
private $logger;
private $userId;
private $geophotoService;
private $photofilesService;
private $logger;
private $root;
public function __construct($AppName,
ILogger $logger,
IRequest $request,
GeophotoService $GeophotoService,
PhotofilesService $photofilesService,
IRootFolder $root,
$UserId) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->userId = $UserId;
$this->geophotoService = $GeophotoService;
$this->photofilesService = $photofilesService;
public function __construct($AppName,
ILogger $logger,
IRequest $request,
GeophotoService $GeophotoService,
PhotofilesService $photofilesService,
IRootFolder $root,
$UserId) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->userId = $UserId;
$this->geophotoService = $GeophotoService;
$this->photofilesService = $photofilesService;
$this->root = $root;
}
}
/**
* @NoAdminRequired
@ -62,17 +61,17 @@ class PhotosController extends Controller {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getPhotos($myMapId=null, $respectNoMediaAndNoimage=null, $hideImagesOnCustomMaps=null, $hideImagesInMapsFolder=null): DataResponse {
public function getPhotos($myMapId = null, $respectNoMediaAndNoimage = null, $hideImagesOnCustomMaps = null, $hideImagesInMapsFolder = null): DataResponse {
$userFolder = $this->root->getUserFolder($this->userId);
if (is_null($myMapId) || $myMapId === "") {
$result = $this->geophotoService->getAll($this->userId, $userFolder, $respectNoMediaAndNoimage??true, $hideImagesOnCustomMaps??false, $hideImagesInMapsFolder??true);
} else {
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
$result = $this->geophotoService->getAll($this->userId, $folder, $respectNoMediaAndNoimage??true, $hideImagesOnCustomMaps??false, $hideImagesInMapsFolder??false);
}
return new DataResponse($result);
}
if (is_null($myMapId) || $myMapId === '') {
$result = $this->geophotoService->getAll($this->userId, $userFolder, $respectNoMediaAndNoimage ?? true, $hideImagesOnCustomMaps ?? false, $hideImagesInMapsFolder ?? true);
} else {
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
$result = $this->geophotoService->getAll($this->userId, $folder, $respectNoMediaAndNoimage ?? true, $hideImagesOnCustomMaps ?? false, $hideImagesInMapsFolder ?? false);
}
return new DataResponse($result);
}
/**
* @NoAdminRequired
@ -90,17 +89,17 @@ class PhotosController extends Controller {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getNonLocalizedPhotos(?int $myMapId=null, ?string $timezone=null, int $limit=250, int $offset=0, $respectNoMediaAndNoimage=null, $hideImagesOnCustomMaps=null, $hideImagesInMapsFolder=null): DataResponse {
public function getNonLocalizedPhotos(?int $myMapId = null, ?string $timezone = null, int $limit = 250, int $offset = 0, $respectNoMediaAndNoimage = null, $hideImagesOnCustomMaps = null, $hideImagesInMapsFolder = null): DataResponse {
$userFolder = $this->root->getUserFolder($this->userId);
if (is_null($myMapId) || $myMapId === "") {
$result = $this->geophotoService->getNonLocalized($this->userId, $userFolder, $respectNoMediaAndNoimage??true, $hideImagesOnCustomMaps??false, $hideImagesInMapsFolder??true, $timezone, $limit, $offset);
if (is_null($myMapId) || $myMapId === '') {
$result = $this->geophotoService->getNonLocalized($this->userId, $userFolder, $respectNoMediaAndNoimage ?? true, $hideImagesOnCustomMaps ?? false, $hideImagesInMapsFolder ?? true, $timezone, $limit, $offset);
} else {
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
$result = $this->geophotoService->getNonLocalized($this->userId, $folder, $respectNoMediaAndNoimage??true, $hideImagesOnCustomMaps??false, $hideImagesInMapsFolder??false, $timezone, $limit, $offset);
$result = $this->geophotoService->getNonLocalized($this->userId, $folder, $respectNoMediaAndNoimage ?? true, $hideImagesOnCustomMaps ?? false, $hideImagesInMapsFolder ?? false, $timezone, $limit, $offset);
}
return new DataResponse($result);
}
return new DataResponse($result);
}
/**
@ -117,48 +116,48 @@ class PhotosController extends Controller {
* @throws NotPermittedException
* @throws InvalidPathException
*/
public function placePhotos($paths, $lats, $lngs, bool $directory=false, $myMapId=null, bool $relative=false): DataResponse {
public function placePhotos($paths, $lats, $lngs, bool $directory = false, $myMapId = null, bool $relative = false): DataResponse {
$userFolder = $this->root->getUserFolder($this->userId);
if (!is_null($myMapId) and $myMapId !== '') {
// forbid folder placement in my-maps
if ($directory === 'true') {
throw new NotPermittedException();
}
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
// photo's path is relative to this map's folder => get full path, don't copy
if ($relative === 'true') {
foreach ($paths as $key => $path) {
$photoFile = $folder->get($path);
$paths[$key] = $userFolder->getRelativePath($photoFile->getPath());
}
} else {
// here the photo path is good, copy it in this map's folder if it's not already there
foreach ($paths as $key => $path) {
$photoFile = $userFolder->get($path);
// is the photo in this map's folder?
if (!$folder->getById($photoFile->getId())) {
$copiedFile = $photoFile->copy($folder->getPath() . '/' . $photoFile->getName());
$paths[$key] = $userFolder->getRelativePath($copiedFile->getPath());
}
}
}
}
$result = $this->photofilesService->setPhotosFilesCoords($this->userId, $paths, $lats, $lngs, $directory);
return new DataResponse($result);
}
if (!is_null($myMapId) and $myMapId !== '') {
// forbid folder placement in my-maps
if ($directory === 'true') {
throw new NotPermittedException();
}
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
// photo's path is relative to this map's folder => get full path, don't copy
if ($relative === 'true') {
foreach ($paths as $key => $path) {
$photoFile = $folder->get($path);
$paths[$key] = $userFolder->getRelativePath($photoFile->getPath());
}
} else {
// here the photo path is good, copy it in this map's folder if it's not already there
foreach ($paths as $key => $path) {
$photoFile = $userFolder->get($path);
// is the photo in this map's folder?
if (!$folder->getById($photoFile->getId())) {
$copiedFile = $photoFile->copy($folder->getPath() . '/' . $photoFile->getName());
$paths[$key] = $userFolder->getRelativePath($copiedFile->getPath());
}
}
}
}
$result = $this->photofilesService->setPhotosFilesCoords($this->userId, $paths, $lats, $lngs, $directory);
return new DataResponse($result);
}
/**
* @NoAdminRequired
* @param $paths
* @return DataResponse
*/
public function resetPhotosCoords($paths, $myMapId=null): DataResponse {
public function resetPhotosCoords($paths, $myMapId = null): DataResponse {
$userFolder = $this->root->getUserFolder($this->userId);
$result = [];
if (sizeof($paths) > 0) {
$result = [];
if (sizeof($paths) > 0) {
$result = $this->photofilesService->resetPhotosFilesCoords($this->userId, $paths);
}
}
if (!is_null($myMapId) and $myMapId !== '') {
foreach ($paths as $key => $path) {
$folders = $userFolder->getById($myMapId);
@ -170,8 +169,8 @@ class PhotosController extends Controller {
}
}
}
return new DataResponse($result);
}
return new DataResponse($result);
}
/**
* @NoAdminRequired
@ -180,9 +179,9 @@ class PhotosController extends Controller {
public function clearCache(): DataResponse {
$result = $this->geophotoService->clearCache();
if ($result) {
return new DataResponse("Cache cleared");
return new DataResponse('Cache cleared');
} else {
return new DataResponse("Failed to clear Cache", 400);
return new DataResponse('Failed to clear Cache', 400);
}
}

Просмотреть файл

@ -13,29 +13,27 @@
namespace OCA\Maps\Controller;
use OC\Files\Node\Node;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\Maps\Service\AddressService;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\Contacts\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IAvatarManager;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\IDBConnection;
use OCP\AppFramework\Controller;
use OCP\Contacts\IManager;
use OCA\Maps\Service\AddressService;
use \OCP\DB\QueryBuilder\IQueryBuilder;
use \OCA\DAV\CardDAV\CardDavBackend;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use \Sabre\VObject\Property\Text;
use \Sabre\VObject\Reader;
use Sabre\VObject\Reader;
class PublicContactsController extends PublicPageController {
protected IManager $contactsManager;
@ -63,20 +61,20 @@ class PublicContactsController extends PublicPageController {
* @param IRootFolder $root
*/
public function __construct($appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
IManager $contactsManager,
IDBConnection $dbconnection,
AddressService $addressService,
CardDavBackend $cdBackend,
IAvatarManager $avatarManager,
IRootFolder $root){
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
IManager $contactsManager,
IDBConnection $dbconnection,
AddressService $addressService,
CardDavBackend $cdBackend,
IAvatarManager $avatarManager,
IRootFolder $root) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->avatarManager = $avatarManager;
$this->contactsManager = $contactsManager;
@ -151,13 +149,13 @@ class PublicContactsController extends PublicPageController {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if ($isReadable) {
//Fixme add contacts for my-maps
$result = [];
$files = $folder->search('.vcf');
foreach ($files as $file) {
// $cards = explode("END:VCARD\r\n", $file->getContent());
// $cards = explode("END:VCARD\r\n", $file->getContent());
$cards = [$file->getContent()];
foreach ($cards as $card) {
$vcard = Reader::read($card."END:VCARD\r\n");
@ -165,7 +163,7 @@ class PublicContactsController extends PublicPageController {
$geo = $vcard->GEO;
if (is_string($geo->getValue()) && strlen($geo->getValue()) > 1) {
$result[] = $this->vCardToArray($permissions, $file, $vcard, $geo->getValue());
} elseif (is_countable($geo) && count($geo)>0 && is_iterable($geo)) {
} elseif (is_countable($geo) && count($geo) > 0 && is_iterable($geo)) {
foreach ($geo as $g) {
if (strlen($g->getValue()) > 1) {
$result[] = $this->vCardToArray($permissions, $file, $vcard, $g->getValue());
@ -206,7 +204,7 @@ class PublicContactsController extends PublicPageController {
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
private function vCardToArray(int $sharePermissions, Node $file, \Sabre\VObject\Document $vcard, string $geo, ?string $adrtype=null, ?string $adr=null, ?int $fileId = null): array {
private function vCardToArray(int $sharePermissions, Node $file, \Sabre\VObject\Document $vcard, string $geo, ?string $adrtype = null, ?string $adr = null, ?int $fileId = null): array {
$FNArray = $vcard->FN ? $vcard->FN->getJsonValue() : [];
$fn = array_shift($FNArray);
$NArray = $vcard->N ? $vcard->N->getJsonValue() : [];
@ -253,12 +251,10 @@ class PublicContactsController extends PublicPageController {
$spl = explode($n, ';');
if (count($spl) >= 4) {
return $spl[3] . ' ' . $spl[1] . ' ' . $spl[0];
}
else {
} else {
return null;
}
}
else {
} else {
return null;
}
}

Просмотреть файл

@ -39,61 +39,61 @@ use OCP\ISession;
use OCP\Util;
class PublicFavoritePageController extends PublicShareController {
private $config;
private $logger;
private $config;
private $logger;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
public function __construct(
$appName,
IRequest $request,
ISession $session,
IConfig $config,
ILogger $logger,
FavoriteShareMapper $favoriteShareMapper
) {
parent::__construct($appName, $request, $session);
$this->config = $config;
$this->logger = $logger;
$this->favoriteShareMapper = $favoriteShareMapper;
}
public function __construct(
$appName,
IRequest $request,
ISession $session,
IConfig $config,
ILogger $logger,
FavoriteShareMapper $favoriteShareMapper
) {
parent::__construct($appName, $request, $session);
$this->config = $config;
$this->logger = $logger;
$this->favoriteShareMapper = $favoriteShareMapper;
}
/**
* @param $token
*
* @return DataResponse|PublicTemplateResponse
*
* @PublicPage
* @NoCSRFRequired
*/
public function sharedFavoritesCategory($token) {
if ($token === '') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
/**
* @param $token
*
* @return DataResponse|PublicTemplateResponse
*
* @PublicPage
* @NoCSRFRequired
*/
public function sharedFavoritesCategory($token) {
if ($token === '') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
try {
$share = $this->favoriteShareMapper->findByToken($token);
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (MultipleObjectsReturnedException $e) {
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
try {
$share = $this->favoriteShareMapper->findByToken($token);
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (MultipleObjectsReturnedException $e) {
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
Util::addStyle($this->appName, 'merged-public-favorite-share');
Util::addScript($this->appName, 'maps-publicFavoriteShare');
Util::addStyle($this->appName, 'merged-public-favorite-share');
Util::addScript($this->appName, 'maps-publicFavoriteShare');
$response = new PublicTemplateResponse('maps', 'public/favorites_index', []);
$response = new PublicTemplateResponse('maps', 'public/favorites_index', []);
$ownerName = OC::$server->getUserManager()->get($share->getOwner())->getDisplayName();
$ownerName = OC::$server->getUserManager()->get($share->getOwner())->getDisplayName();
$response->setHeaderTitle($share->getCategory());
$response->setHeaderDetails('shared by ' . $ownerName);
$response->setHeaderTitle($share->getCategory());
$response->setHeaderDetails('shared by ' . $ownerName);
$this->addCsp($response);
$this->addCsp($response);
return $response;
}
return $response;
}
/**
* Get a hash of the password for this share
@ -104,9 +104,9 @@ class PublicFavoritePageController extends PublicShareController {
* @return string
* @since 14.0.0
*/
protected function getPasswordHash(): string {
return "";
}
protected function getPasswordHash(): string {
return '';
}
/**
* Is the provided token a valid token
@ -116,15 +116,15 @@ class PublicFavoritePageController extends PublicShareController {
* @return bool
* @since 14.0.0
*/
public function isValidToken(): bool {
try {
$this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
return false;
}
public function isValidToken(): bool {
try {
$this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return false;
}
return true;
}
return true;
}
/**
* Is a share with this token password protected
@ -132,57 +132,57 @@ class PublicFavoritePageController extends PublicShareController {
* @return bool
* @since 14.0.0
*/
protected function isPasswordProtected(): bool {
return false;
}
protected function isPasswordProtected(): bool {
return false;
}
/**
* @param $response
* @return void
*/
private function addCsp($response): void {
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
//$csp->addAllowedConnectDomain('http://192.168.0.66:5000');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
//$csp->addAllowedConnectDomain('http://192.168.0.66:5000');
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
}

Просмотреть файл

@ -36,193 +36,193 @@ use OCP\IRequest;
use OCP\ISession;
class PublicFavoritesApiController extends PublicShareController {
/* @var FavoritesService */
private $favoritesService;
/* @var FavoritesService */
private $favoritesService;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
public function __construct(
$appName,
IRequest $request,
ISession $session,
FavoritesService $favoritesService,
FavoriteShareMapper $favoriteShareMapper
) {
parent::__construct($appName, $request, $session);
public function __construct(
$appName,
IRequest $request,
ISession $session,
FavoritesService $favoritesService,
FavoriteShareMapper $favoriteShareMapper
) {
parent::__construct($appName, $request, $session);
$this->favoriteShareMapper = $favoriteShareMapper;
$this->favoritesService = $favoritesService;
}
$this->favoriteShareMapper = $favoriteShareMapper;
$this->favoritesService = $favoritesService;
}
/**
* @return string
*/
public function getPasswordHash(): string {
return '';
}
public function getPasswordHash(): string {
return '';
}
/**
* @return bool
*/
protected function isPasswordProtected(): bool {
return false;
}
protected function isPasswordProtected(): bool {
return false;
}
/**
* @return bool
*/
public function isValidToken(): bool {
try {
$this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
return false;
}
public function isValidToken(): bool {
try {
$this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return false;
}
return true;
}
return true;
}
/**
* @return bool
*/
public function canEdit(): bool {
try {
$share = $this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
return false;
}
public function canEdit(): bool {
try {
$share = $this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return false;
}
return $share->getAllowEdits();
}
return $share->getAllowEdits();
}
/**
* @PublicPage
*
* @return DataResponse
*/
public function getFavorites(): DataResponse {
try {
$share = $this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (MultipleObjectsReturnedException $e) {
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
/**
* @PublicPage
*
* @return DataResponse
*/
public function getFavorites(): DataResponse {
try {
$share = $this->favoriteShareMapper->findByToken($this->getToken());
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (MultipleObjectsReturnedException $e) {
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
$favorites = $this->favoritesService->getFavoritesFromDB($share->getOwner(), 0, $share->getCategory(), false, false, true);
$favorites = $this->favoritesService->getFavoritesFromDB($share->getOwner(), 0, $share->getCategory(), false, false, true);
return new DataResponse([
'share' => $share,
'favorites' => $favorites
]);
}
return new DataResponse([
'share' => $share,
'favorites' => $favorites
]);
}
// /**
// * @PublicPage
// *
// * @param $lat
// * @param $lng
// * @param $name
// * @param $comment
// * @param $extensions
// * @return DataResponse
// */
// public function addFavorite($lat, $lng, $name, $comment, $extensions)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to add favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $category = $share->getCategory();
//
//
// if (is_numeric($lat) && is_numeric($lng)) {
// $favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
// $favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
// return new DataResponse($favorite);
// } else {
// return new DataResponse('invalid values', 400);
// }
// }
// /**
// * @PublicPage
// *
// * @param $lat
// * @param $lng
// * @param $name
// * @param $comment
// * @param $extensions
// * @return DataResponse
// */
// public function addFavorite($lat, $lng, $name, $comment, $extensions)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to add favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $category = $share->getCategory();
//
//
// if (is_numeric($lat) && is_numeric($lng)) {
// $favoriteId = $this->favoritesService->addFavoriteToDB($this->userId, $name, $lat, $lng, $category, $comment, $extensions);
// $favorite = $this->favoritesService->getFavoriteFromDB($favoriteId);
// return new DataResponse($favorite);
// } else {
// return new DataResponse('invalid values', 400);
// }
// }
// /**
// * @PublicPage
// *
// * @param $id
// * @param $lat
// * @param $lng
// * @param $name
// * @param $comment
// * @param $extensions
// * @return DataResponse
// */
// public function editFavorite($id, $lat, $lng, $name, $comment, $extensions)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to edit favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $favorite = $this->favoritesService->getFavoriteFromDB($id, $share->getOwner(), $share->getCategory());
//
// if ($favorite !== null) {
// if (($lat === null || is_numeric($lat)) &&
// ($lng === null || is_numeric($lng))
// ) {
// $this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $favorite['category'], $comment, $extensions);
// $editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
//
// return new DataResponse($editedFavorite);
// } else {
// return new DataResponse('invalid values', 400);
// }
// } else {
// return new DataResponse('no such favorite', 400);
// }
// }
// /**
// * @PublicPage
// *
// * @param $id
// * @param $lat
// * @param $lng
// * @param $name
// * @param $comment
// * @param $extensions
// * @return DataResponse
// */
// public function editFavorite($id, $lat, $lng, $name, $comment, $extensions)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to edit favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $favorite = $this->favoritesService->getFavoriteFromDB($id, $share->getOwner(), $share->getCategory());
//
// if ($favorite !== null) {
// if (($lat === null || is_numeric($lat)) &&
// ($lng === null || is_numeric($lng))
// ) {
// $this->favoritesService->editFavoriteInDB($id, $name, $lat, $lng, $favorite['category'], $comment, $extensions);
// $editedFavorite = $this->favoritesService->getFavoriteFromDB($id);
//
// return new DataResponse($editedFavorite);
// } else {
// return new DataResponse('invalid values', 400);
// }
// } else {
// return new DataResponse('no such favorite', 400);
// }
// }
// /**
// * @PublicPage
// *
// * @param $id
// * @return DataResponse
// */
// public function deleteFavorite($id)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to delete favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $favorite = $this->favoritesService->getFavoriteFromDB($id, $share->getOwner(), $share->getCategory());
//
// if ($favorite !== null) {
// $this->favoritesService->deleteFavoriteFromDB($id);
// return new DataResponse('deleted');
// } else {
// return new DataResponse('no such favorite', 400);
// }
// }
// /**
// * @PublicPage
// *
// * @param $id
// * @return DataResponse
// */
// public function deleteFavorite($id)
// {
// if (!$this->canEdit()) {
// return new DataResponse('Not authorized to delete favorite', Http::STATUS_UNAUTHORIZED);
// }
//
// try {
// $share = $this->favoriteShareMapper->findByToken($this->getToken());
// } catch (DoesNotExistException $e) {
// return new DataResponse([], Http::STATUS_NOT_FOUND);
// } catch (MultipleObjectsReturnedException $e) {
// return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
// }
//
// $favorite = $this->favoritesService->getFavoriteFromDB($id, $share->getOwner(), $share->getCategory());
//
// if ($favorite !== null) {
// $this->favoritesService->deleteFavoriteFromDB($id);
// return new DataResponse('deleted');
// } else {
// return new DataResponse('no such favorite', 400);
// }
// }
}

Просмотреть файл

@ -13,79 +13,75 @@
namespace OCA\Maps\Controller;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCA\Maps\DB\FavoriteShareMapper;
use OCA\Maps\Service\FavoritesService;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IDateTimeZone;
use OCP\IL10N;
use OCP\IRequest;
class PublicFavoritesController extends PublicPageController {
private string $appVersion;
private string $appVersion;
private IL10N $l;
private FavoritesService $favoritesService;
private IDateTimeZone $dateTimeZone;
private ?string $defaultFavoritsJSON;
protected $appName;
private FavoritesService $favoritesService;
private IDateTimeZone $dateTimeZone;
private ?string $defaultFavoritsJSON;
protected $appName;
protected $groupManager;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
public function __construct($appName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IURLGenerator $urlGenerator,
IInitialStateService $initialStateService,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
IDateTimeZone $dateTimeZone,
ISession $session,
FavoriteShareMapper $favoriteShareMapper,
IEventDispatcher $eventDispatcher) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->favoritesService = $favoritesService;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $appName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->config = $config;
$this->shareManager = $shareManager;
$this->favoriteShareMapper = $favoriteShareMapper;
$this->defaultFavoritsJSON = json_encode([
"type" => "FeatureCollection",
"features"=> []
],JSON_PRETTY_PRINT);
}
public function __construct($appName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IURLGenerator $urlGenerator,
IInitialStateService $initialStateService,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
FavoritesService $favoritesService,
IDateTimeZone $dateTimeZone,
ISession $session,
FavoriteShareMapper $favoriteShareMapper,
IEventDispatcher $eventDispatcher) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->favoritesService = $favoritesService;
$this->dateTimeZone = $dateTimeZone;
$this->logger = $logger;
$this->appName = $appName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->config = $config;
$this->shareManager = $shareManager;
$this->favoriteShareMapper = $favoriteShareMapper;
$this->defaultFavoritsJSON = json_encode([
'type' => 'FeatureCollection',
'features' => []
], JSON_PRETTY_PRINT);
}
/**
* Validate the permissions of the share
@ -146,19 +142,19 @@ class PublicFavoritesController extends PublicPageController {
* @return mixed
* @throws NotPermittedException
*/
private function getJSONFavoritesFile(\OCP\Files\Folder $folder, $isCreatable): \OCP\Files\Node {
try {
$file = $folder->get('.favorites.json');
} catch (NotFoundException $e) {
private function getJSONFavoritesFile(\OCP\Files\Folder $folder, $isCreatable): \OCP\Files\Node {
try {
$file = $folder->get('.favorites.json');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file = $folder->newFile('.favorites.json', $content = $this->defaultFavoritsJSON);
} else {
throw new NotPermittedException();
}
}
return $file;
}
}
return $file;
}
/**
* @PublicPage
@ -166,14 +162,14 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getFavorites(): DataResponse {
public function getFavorites(): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
$file = $this->getJSONFavoritesFile($folder, $isCreatable);
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if ($isReadable) {
$favorites = $this->favoritesService->getFavoritesFromJSON($file);
$favorites = array_map(function ($favorite) use ($permissions) {
@ -185,8 +181,8 @@ class PublicFavoritesController extends PublicPageController {
} else {
throw new NotPermittedException();
}
return new DataResponse($favorites);
}
return new DataResponse($favorites);
}
/**
* @PublicPage
@ -201,8 +197,8 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
*/
public function addFavorite(?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
public function addFavorite(?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions): DataResponse {
if (is_numeric($lat) && is_numeric($lng)) {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
@ -216,11 +212,11 @@ class PublicFavoritesController extends PublicPageController {
} else {
throw new NotPermittedException();
}
return new DataResponse($favorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
return new DataResponse($favorite);
} else {
return new DataResponse($this->l->t('Invalid values'), 400);
}
}
/**
* @PublicPage
@ -240,8 +236,8 @@ class PublicFavoritesController extends PublicPageController {
if ($isUpdateable) {
$favoriteIds = $this->favoritesService->addFavoritesToJSON($file, $favorites);
$favoritesAfter = [];
forEach ($this->favoritesService->getFavoritesFromJSON($file) as $favorite) {
if (in_array($favorite['id'],$favoriteIds)) {
foreach ($this->favoritesService->getFavoritesFromJSON($file) as $favorite) {
if (in_array($favorite['id'], $favoriteIds)) {
$favorite['isDeletable'] = ($permissions & (1 << 3)) && $favorite['isDeletable'];
$favoritesAfter[] = $favorite;
}
@ -266,7 +262,7 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
*/
public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions): DataResponse {
public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
@ -289,10 +285,10 @@ class PublicFavoritesController extends PublicPageController {
} else {
return new DataResponse($this->l->t('no such favorite'), 400);
}
} else {
} else {
throw new NotPermittedException();
}
}
}
/**
* @PublicPage
@ -303,8 +299,8 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
*/
public function renameCategories(array $categories, string $newName): DataResponse {
if (is_array($categories)) {
public function renameCategories(array $categories, string $newName): DataResponse {
if (is_array($categories)) {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
@ -312,16 +308,16 @@ class PublicFavoritesController extends PublicPageController {
$file = $this->getJSONFavoritesFile($folder, $isCreatable);
$isUpdateable = ($permissions & (1 << 1)) && $file->isUpdateable();
if ($isUpdateable) {
foreach ($categories as $cat) {
$this->favoritesService->renameCategoryInJSON($file, $cat, $newName);
}
} else {
foreach ($categories as $cat) {
$this->favoritesService->renameCategoryInJSON($file, $cat, $newName);
}
} else {
throw new NotPermittedException();
}
return new DataResponse('RENAMED');
}
}
throw new NotFoundException();
}
}
/**
* @PublicPage
@ -331,7 +327,7 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
*/
public function deleteFavorite(int $id): DataResponse {
public function deleteFavorite(int $id): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
@ -347,7 +343,7 @@ class PublicFavoritesController extends PublicPageController {
throw new NotPermittedException();
}
}
}
/**
* @PublicPage
@ -357,20 +353,20 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
*/
public function deleteFavorites(array $ids): DataResponse {
public function deleteFavorites(array $ids): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
$file = $this->getJSONFavoritesFile($folder, $isCreatable);
$isDeleteable = ($permissions & (1 << 3)) && $file->isUpdateable();
if ($isDeleteable){
$this->favoritesService->deleteFavoritesFromJSON($file, $ids);
} else {
if ($isDeleteable) {
$this->favoritesService->deleteFavoritesFromJSON($file, $ids);
} else {
throw new NotPermittedException();
}
return new DataResponse('DELETED');
}
return new DataResponse('DELETED');
}
/**
* @PublicPage
@ -378,7 +374,7 @@ class PublicFavoritesController extends PublicPageController {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getSharedCategories(): DataResponse {
public function getSharedCategories(): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
@ -389,6 +385,6 @@ class PublicFavoritesController extends PublicPageController {
} else {
throw new NotPermittedException();
}
return new DataResponse($categories);
}
return new DataResponse($categories);
}
}

Просмотреть файл

@ -18,12 +18,12 @@ use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\ILogger;
use OCP\IRequest;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IInitialStateService;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
@ -39,23 +39,23 @@ class PublicPageController extends AuthPublicShareController {
protected ShareManager $shareManager;
protected IUserManager $userManager;
public function __construct($AppName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session
) {
parent::__construct($AppName, $request, $session, $urlGenerator);
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->initialStateService = $initialStateService;
public function __construct($AppName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session
) {
parent::__construct($AppName, $request, $session, $urlGenerator);
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->shareManager = $shareManager;
$this->userManager = $userManager;
}
}
public function isValidToken(): bool {
try {
@ -123,25 +123,25 @@ class PublicPageController extends AuthPublicShareController {
return $share->getNode();
}
/**
* @PublicPage
* @NoCSRFRequired
*/
public function showShare(): PublicTemplateResponse {
/**
* @PublicPage
* @NoCSRFRequired
*/
public function showShare(): PublicTemplateResponse {
$shareNode = $this->getShareNode();
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$params = [];
$params = [];
$params['sharingToken'] = $this->getToken();
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new PublicTemplateResponse('maps', 'public/main', $params);
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
$response = new PublicTemplateResponse('maps', 'public/main', $params);
$this->addCsp($response);
$this->addCsp($response);
return $response;
}
return $response;
}
/**
* @PublicPage
@ -209,53 +209,53 @@ class PublicPageController extends AuthPublicShareController {
* @param $response
* @return void
*/
private function addCsp($response): void {
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
private function addCsp($response): void {
if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
// map tiles
$csp->addAllowedImageDomain('https://*.tile.openstreetmap.org');
$csp->addAllowedImageDomain('https://server.arcgisonline.com');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.opentopomap.org');
$csp->addAllowedImageDomain('https://*.cartocdn.com');
$csp->addAllowedImageDomain('https://*.ssl.fastly.net');
$csp->addAllowedImageDomain('https://*.openstreetmap.se');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://events.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
// default routing engine
$csp->addAllowedConnectDomain('https://*.project-osrm.org');
$csp->addAllowedConnectDomain('https://api.mapbox.com');
$csp->addAllowedConnectDomain('https://events.mapbox.com');
$csp->addAllowedConnectDomain('https://graphhopper.com');
$csp->addAllowedChildSrcDomain('blob:');
$csp->addAllowedWorkerSrcDomain('blob:');
$csp->addAllowedScriptDomain('https://unpkg.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
$csp->addAllowedChildSrcDomain('blob:');
$csp->addAllowedWorkerSrcDomain('blob:');
$csp->addAllowedScriptDomain('https://unpkg.com');
// allow connections to custom routing engines
$urlKeys = [
'osrmBikeURL',
'osrmCarURL',
'osrmFootURL',
'graphhopperURL'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
if ($url !== '') {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$cleanUrl = $scheme . '://' . $host;
if ($port && $port !== '') {
$cleanUrl .= ':' . $port;
}
$csp->addAllowedConnectDomain($cleanUrl);
}
}
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
// poi images
$csp->addAllowedImageDomain('https://nominatim.openstreetmap.org');
// search and geocoder
$csp->addAllowedConnectDomain('https://nominatim.openstreetmap.org');
$response->setContentSecurityPolicy($csp);
}
}
}

Просмотреть файл

@ -12,20 +12,17 @@
namespace OCA\Maps\Controller;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\ILogger;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserManager;
@ -33,27 +30,27 @@ use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
class PublicPhotosController extends PublicPageController {
protected GeophotoService $geophotoService;
protected GeophotoService $geophotoService;
protected PhotofilesService $photofilesService;
protected IRootFolder $root;
public function __construct($appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
GeophotoService $GeophotoService,
PhotofilesService $photofilesService,
IRootFolder $root) {
public function __construct($appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
GeophotoService $GeophotoService,
PhotofilesService $photofilesService,
IRootFolder $root) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->geophotoService = $GeophotoService;
$this->photofilesService = $photofilesService;
$this->geophotoService = $GeophotoService;
$this->photofilesService = $photofilesService;
$this->root = $root;
}
}
/**
* Validate the permissions of the share
@ -115,17 +112,17 @@ class PublicPhotosController extends PublicPageController {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
public function getPhotos(): DataResponse {
public function getPhotos(): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if ($isReadable) {
$owner = $share->getShareOwner();
$pre_path = $this->root->getUserFolder($owner)->getPath();
$result = $this->geophotoService->getAll($owner, $folder, true, false, false);
$photos = array_map(function ($photo) use ($folder, $permissions, $pre_path) {
$photo_object = (object) $photo;
$photo_object = (object)$photo;
$photo_object->isCreatable = ($permissions & (1 << 2)) && $photo['isCreatable'];
$photo_object->isUpdateable = ($permissions & (1 << 1)) && $photo['isUpdateable'];
$photo_object->isDeletable = ($permissions & (1 << 3)) && $photo['isDeletable'];
@ -137,8 +134,8 @@ class PublicPhotosController extends PublicPageController {
throw new NotPermittedException();
}
return new DataResponse($photos);
}
return new DataResponse($photos);
}
/**
* @PublicPage
@ -148,17 +145,17 @@ class PublicPhotosController extends PublicPageController {
* @throws \OCP\Files\InvalidPathException
* @throws \OC\User\NoUserException
*/
public function getNonLocalizedPhotos(?string $timezone=null, int $limit=250, int $offset=0): DataResponse {
public function getNonLocalizedPhotos(?string $timezone = null, int $limit = 250, int $offset = 0): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if ($isReadable) {
$owner = $share->getShareOwner();
$pre_path = $this->root->getUserFolder($owner)->getPath();
$result = $this->geophotoService->getNonLocalized($owner, $folder, true, false, false, $timezone, $limit, $offset);
$photos = array_map(function ($photo) use ($folder, $permissions, $pre_path) {
$photo_object = (object) $photo;
$photo_object = (object)$photo;
$photo_object->isCreatable = ($permissions & (1 << 2)) && $photo['isCreatable'];
$photo_object->isUpdateable = ($permissions & (1 << 1)) && $photo['isUpdateable'];
$photo_object->isDeletable = ($permissions & (1 << 3)) && $photo['isDeletable'];
@ -171,7 +168,7 @@ class PublicPhotosController extends PublicPageController {
}
return new DataResponse($photos);
}
}
/**
* @PublicPage
@ -180,9 +177,9 @@ class PublicPhotosController extends PublicPageController {
public function clearCache(): DataResponse {
$result = $this->geophotoService->clearCache();
if ($result) {
return new DataResponse("Cache cleared");
return new DataResponse('Cache cleared');
} else {
return new DataResponse("Failed to clear Cache", 400);
return new DataResponse('Failed to clear Cache', 400);
}
}

Просмотреть файл

@ -11,38 +11,28 @@
namespace OCA\Maps\Controller;
use OCP\App\IAppManager;
use OCA\Maps\Service\TracksService;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IConfig;
use \OCP\IL10N;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\ApiController;
use OCP\Constants;
use OCP\Share;
use OCP\IUserManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\Share;
use OCA\Maps\Service\TracksService;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use function OCA\Maps\Helper\remove_utf8_bom;
@ -55,31 +45,31 @@ class PublicTracksController extends PublicPageController {
protected IL10N $l;
protected ILogger $logger;
protected TracksService $tracksService;
protected $appName;
protected $appName;
protected IRootFolder $root;
public function __construct($appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
IServerContainer $serverContainer,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
TracksService $tracksService,
IRootFolder $root) {
public function __construct($appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
IConfig $config,
IInitialStateService $initialStateService,
IURLGenerator $urlGenerator,
ShareManager $shareManager,
IUserManager $userManager,
ISession $session,
IServerContainer $serverContainer,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
TracksService $tracksService,
IRootFolder $root) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->groupManager = $groupManager;
$this->l = $l;
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->groupManager = $groupManager;
$this->l = $l;
$this->root = $root;
}
}
/**
* Validate the permissions of the share
@ -141,12 +131,12 @@ class PublicTracksController extends PublicPageController {
* @throws NotPermittedException
* @throws \OC\User\NoUserException
*/
public function getTracks(): DataResponse {
public function getTracks(): DataResponse {
$share = $this->getShare();
$hideDownload = (bool) $share->getHideDownload();
$hideDownload = (bool)$share->getHideDownload();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if ($isReadable) {
$owner = $share->getShareOwner();
$pre_path = $this->root->getUserFolder($owner)->getPath();
@ -163,8 +153,8 @@ class PublicTracksController extends PublicPageController {
} else {
throw new NotPermittedException();
}
return new DataResponse($new_tracks);
}
return new DataResponse($new_tracks);
}
/**
* @PublicPage
@ -178,7 +168,7 @@ class PublicTracksController extends PublicPageController {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isReadable = (bool) ($permissions & (1 << 0));
$isReadable = (bool)($permissions & (1 << 0));
if (!$isReadable) {
throw new NotPermittedException();
}
@ -194,20 +184,17 @@ class PublicTracksController extends PublicPageController {
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
}
else {
} else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata'=>$metadata,
'content'=>$trackContent
'metadata' => $metadata,
'content' => $trackContent
]);
}
else {
} else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
}
else {
} else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
@ -219,35 +206,32 @@ class PublicTracksController extends PublicPageController {
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
public function getTrackFileContent($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id);
$res = is_null($track) ? null : $this->getShareNode()->getById($track['file_id']);
if (is_array($res) and count($res) > 0) {
$trackFile = array_shift($res);
if ($trackFile->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
$trackContent = remove_utf8_bom($trackFile->getContent());
// compute metadata if necessary
// first time we get it OR the file changed
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
}
else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata'=>$metadata,
'content'=>$trackContent
]);
}
else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
}
else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
public function getTrackFileContent($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id);
$res = is_null($track) ? null : $this->getShareNode()->getById($track['file_id']);
if (is_array($res) and count($res) > 0) {
$trackFile = array_shift($res);
if ($trackFile->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
$trackContent = remove_utf8_bom($trackFile->getContent());
// compute metadata if necessary
// first time we get it OR the file changed
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
} else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata' => $metadata,
'content' => $trackContent
]);
} else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
} else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
/**
* @PublicPage
@ -259,36 +243,35 @@ class PublicTracksController extends PublicPageController {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function editTrack($id, $color, $metadata, $etag): DataResponse {
public function editTrack($id, $color, $metadata, $etag): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isUpdateable = (bool) ($permissions & (1 << 1));
$isUpdateable = (bool)($permissions & (1 << 1));
if ($isUpdateable) {
$owner = $share->getShareOwner();
$track = $this->tracksService->getTrackFromDB($id, $owner);
if ($track !== null) {
$this->tracksService->editTrackInDB($id, $color, $metadata, $etag);
return new DataResponse('EDITED');
}
else {
} else {
return new DataResponse($this->l->t('No such track'), 400);
}
} else {
throw new NotPermittedException();
}
}
}
/**
* @NoAdminRequired
* @param $id
* @return DataResponse
*/
public function deleteTrack($id): DataResponse {
public function deleteTrack($id): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isUpdateable = (bool) ($permissions & (1 << 1));
$isUpdateable = (bool)($permissions & (1 << 1));
//It's allowed to delete a track from the share, if the share is updateable
if ($isUpdateable) {
$owner = $share->getShareOwner();
@ -302,6 +285,6 @@ class PublicTracksController extends PublicPageController {
} else {
throw new NotPermittedException();
}
}
}
}

Просмотреть файл

@ -12,21 +12,20 @@
namespace OCA\Maps\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\IUserManager;
use OCP\Lock\LockedException;
use OCP\Share\Exceptions\ShareNotFound;
@ -36,20 +35,20 @@ class PublicUtilsController extends PublicPageController {
protected IRootFolder $root;
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator,
IInitialStateService $initialStateService,
IUserManager $userManager,
ShareManager $shareManager,
ISession $session,
IRootFolder $root,
IEventDispatcher $eventDispatcher
) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator,
IInitialStateService $initialStateService,
IUserManager $userManager,
ShareManager $shareManager,
ISession $session,
IRootFolder $root,
IEventDispatcher $eventDispatcher
) {
parent::__construct($appName, $request, $eventDispatcher, $config, $initialStateService, $urlGenerator, $shareManager, $userManager, $session);
$this->root = $root;
}
}
/**
* Validate the permissions of the share
@ -110,13 +109,13 @@ class PublicUtilsController extends PublicPageController {
* @PublicPage
* @return DataResponse
*/
public function deleteOptionsValues(): DataResponse {
$keys = $this->config->getUserKeys(Null, 'maps');
foreach ($keys as $key) {
$this->config->deleteUserValue(Null, 'maps', $key);
}
return new DataResponse(['done'=>1]);
}
public function deleteOptionsValues(): DataResponse {
$keys = $this->config->getUserKeys(null, 'maps');
foreach ($keys as $key) {
$this->config->deleteUserValue(null, 'maps', $key);
}
return new DataResponse(['done' => 1]);
}
/**
* Save options values to the DB for current user
@ -130,17 +129,17 @@ class PublicUtilsController extends PublicPageController {
* @throws InvalidPathException
* @throws NotPermittedException
*/
public function saveOptionValue($options, $myMapId=null): DataResponse {
public function saveOptionValue($options, $myMapId = null): DataResponse {
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
try {
$file=$folder->get(".index.maps");
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file=$folder->newFile(".index.maps", $content = "{}");
$file = $folder->newFile('.index.maps', $content = '{}');
} else {
throw new NotFoundException();
}
@ -151,16 +150,16 @@ class PublicUtilsController extends PublicPageController {
}
try {
$ov = json_decode($file->getContent(),true, 512);
$ov = json_decode($file->getContent(), true, 512);
foreach ($options as $key => $value) {
$ov[$key] = $value;
}
$file->putContent(json_encode($ov, JSON_PRETTY_PRINT));
} catch (LockedException $e){
return new DataResponse("File is locked", 500);
} catch (LockedException $e) {
return new DataResponse('File is locked', 500);
}
return new DataResponse(['done'=>1]);
}
return new DataResponse(['done' => 1]);
}
/**
* get options values from the config for current user
@ -172,23 +171,23 @@ class PublicUtilsController extends PublicPageController {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getOptionsValues(): DataResponse {
$ov = array();
public function getOptionsValues(): DataResponse {
$ov = [];
$share = $this->getShare();
$permissions = $share->getPermissions();
$folder = $this->getShareNode();
$isCreatable = ($permissions & (1 << 2)) && $folder->isCreatable();
try {
$file=$folder->get(".index.maps");
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file=$folder->newFile(".index.maps", $content = "{}");
$file = $folder->newFile('.index.maps', $content = '{}');
} else {
throw new NotFoundException();
}
}
$ov = json_decode($file->getContent(),true, 512);
$ov = json_decode($file->getContent(), true, 512);
// Maps content can be read mostly from the folder
$ov['isReadable'] = ($permissions & (1 << 0)) && $folder->isReadable();
@ -201,94 +200,94 @@ class PublicUtilsController extends PublicPageController {
$ov['isShareable'] = ($permissions & (1 << 4)) && $folder->isShareable();
// get routing-specific admin settings values
$settingsKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
// get routing-specific admin settings values
$settingsKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
$v = $this->config->getAppValue('maps', $k);
$ov[$k] = $v;
}
return new DataResponse(['values'=>$ov]);
}
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
$v = $this->config->getAppValue('maps', $k);
$ov[$k] = $v;
}
return new DataResponse(['values' => $ov]);
}
/**
* get content of mapbox traffic style
* @PublicPage
/**
* get content of mapbox traffic style
* @PublicPage
*
* @return DataResponse
*/
public function getTrafficStyle(): DataResponse {
$style = [
'version' => 8,
'name' => 'Mapbox Traffic tileset v1',
'sources' => [
'mapbox-traffic' => [
'url' => 'mapbox://mapbox.mapbox-traffic-v1',
'type' => 'vector'
]
],
'layers' => [
[
'id' => 'traffic',
'source' => 'mapbox-traffic',
'source-layer' => 'traffic',
'type' => 'line',
'paint' => [
'line-width' => 2.0,
'line-color' => [
'case',
[
'==',
'low',
[
'get',
'congestion'
]
],
'#00ff00',
[
'==',
'moderate',
[
'get',
'congestion'
]
],
'#ffad00',
[
'==',
'heavy',
[
'get',
'congestion'
]
],
'#ff0000',
[
'==',
'severe',
[
'get',
'congestion'
]
],
'#b43b71',
'#000000'
]
]
]
]
];
return new DataResponse($style);
}
* @return DataResponse
*/
public function getTrafficStyle(): DataResponse {
$style = [
'version' => 8,
'name' => 'Mapbox Traffic tileset v1',
'sources' => [
'mapbox-traffic' => [
'url' => 'mapbox://mapbox.mapbox-traffic-v1',
'type' => 'vector'
]
],
'layers' => [
[
'id' => 'traffic',
'source' => 'mapbox-traffic',
'source-layer' => 'traffic',
'type' => 'line',
'paint' => [
'line-width' => 2.0,
'line-color' => [
'case',
[
'==',
'low',
[
'get',
'congestion'
]
],
'#00ff00',
[
'==',
'moderate',
[
'get',
'congestion'
]
],
'#ffad00',
[
'==',
'heavy',
[
'get',
'congestion'
]
],
'#ff0000',
[
'==',
'severe',
[
'get',
'congestion'
]
],
'#b43b71',
'#000000'
]
]
]
]
];
return new DataResponse($style);
}
}

Просмотреть файл

@ -14,79 +14,71 @@ namespace OCA\Maps\Controller;
use OCA\Maps\Service\TracksService;
use OCP\App\IAppManager;
use OCP\IURLGenerator;
use OCP\IConfig;
use \OCP\IL10N;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\ApiController;
use OCP\Constants;
use OCP\Share;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IGroupManager;
use OCP\ILogger;
class RoutingController extends Controller {
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $dateTimeZone;
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $defaultDeviceId;
private $l;
private $logger;
private $dateTimeZone;
private $tracksService;
protected $appName;
protected $appName;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
IDateTimeZone $dateTimeZone,
TracksService $tracksService,
$UserId){
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->dateTimeZone = $dateTimeZone;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null){
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
IDateTimeZone $dateTimeZone,
TracksService $tracksService,
$UserId) {
parent::__construct($AppName, $request);
$this->logger = $logger;
$this->dateTimeZone = $dateTimeZone;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
// IConfig object
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
// path of user files folder relative to DATA folder
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
$this->tracksService = $tracksService;
}
}
/**
* @NoAdminRequired
@ -99,9 +91,9 @@ class RoutingController extends Controller {
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
*/
public function exportRoute($type, $coords, $name, $totDist, $totTime, $myMapId=null): DataResponse {
// create /Maps directory if necessary
$userFolder = $this->userfolder;
public function exportRoute($type, $coords, $name, $totDist, $totTime, $myMapId = null): DataResponse {
// create /Maps directory if necessary
$userFolder = $this->userfolder;
if (is_null($myMapId) || $myMapId === '') {
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
@ -111,13 +103,11 @@ class RoutingController extends Controller {
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
$response = new DataResponse($this->l->t('/Maps is not a directory'), 400);
return $response;
}
else if (!$mapsFolder->isCreatable()) {
} elseif (!$mapsFolder->isCreatable()) {
$response = new DataResponse($this->l->t('/Maps directory is not writeable'), 400);
return $response;
}
}
else {
} else {
$response = new DataResponse($this->l->t('Impossible to create /Maps directory'), 400);
return $response;
}
@ -134,52 +124,52 @@ class RoutingController extends Controller {
}
}
$filename = $name.'.gpx';
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
$filename = $name.'.gpx';
if ($mapsFolder->nodeExists($filename)) {
$mapsFolder->get($filename)->delete();
}
if ($mapsFolder->nodeExists($filename.'.tmp')) {
$mapsFolder->get($filename.'.tmp')->delete();
}
$file = $mapsFolder->newFile($filename.'tmp');
$fileHandler = $file->fopen('w');
$file = $mapsFolder->newFile($filename.'tmp');
$fileHandler = $file->fopen('w');
$dt = new \DateTime();
$date = $dt->format('Y-m-d\TH:i:s\Z');
$dt = new \DateTime();
$date = $dt->format('Y-m-d\TH:i:s\Z');
$gpxHeader = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
$gpxHeader = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<gpx version="1.1" creator="Nextcloud Maps '.$this->appVersion.'" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<name>'.$name.'</name>
<time>'.$date.'</time>
</metadata>';
fwrite($fileHandler, $gpxHeader."\n");
fwrite($fileHandler, $gpxHeader."\n");
if ($type === 'route') {
fwrite($fileHandler, ' <rte>'."\n");
fwrite($fileHandler, ' <name>'.$name.'</name>'."\n");
foreach ($coords as $ll) {
$line = ' <rtept lat="' . $ll['lat'] . '" lon="' . $ll['lng'] . '"></rtept>' . "\n";
fwrite($fileHandler, $line);
}
fwrite($fileHandler, ' </rte>'."\n");
} elseif ($type === 'track') {
fwrite($fileHandler, ' <trk>'."\n");
fwrite($fileHandler, ' <name>'.$name.'</name>'."\n");
fwrite($fileHandler, ' <trkseg>'."\n");
foreach ($coords as $ll) {
$line = ' <trkpt lat="' . $ll['lat'] . '" lon="' . $ll['lng'] . '"></trkpt>' . "\n";
fwrite($fileHandler, $line);
}
fwrite($fileHandler, ' </trkseg>'."\n");
fwrite($fileHandler, ' </trk>'."\n");
}
fwrite($fileHandler, '</gpx>'."\n");
fclose($fileHandler);
if ($type === 'route') {
fwrite($fileHandler, ' <rte>'."\n");
fwrite($fileHandler, ' <name>'.$name.'</name>'."\n");
foreach ($coords as $ll) {
$line = ' <rtept lat="' . $ll['lat'] . '" lon="' . $ll['lng'] . '"></rtept>' . "\n";
fwrite($fileHandler, $line);
}
fwrite($fileHandler, ' </rte>'."\n");
} elseif ($type === 'track') {
fwrite($fileHandler, ' <trk>'."\n");
fwrite($fileHandler, ' <name>'.$name.'</name>'."\n");
fwrite($fileHandler, ' <trkseg>'."\n");
foreach ($coords as $ll) {
$line = ' <trkpt lat="' . $ll['lat'] . '" lon="' . $ll['lng'] . '"></trkpt>' . "\n";
fwrite($fileHandler, $line);
}
fwrite($fileHandler, ' </trkseg>'."\n");
fwrite($fileHandler, ' </trk>'."\n");
}
fwrite($fileHandler, '</gpx>'."\n");
fclose($fileHandler);
$file->touch();
$file->move(substr($file->getPath(), 0, -3));
$file->move(substr($file->getPath(), 0, -3));
$track = $this->tracksService->getTrackByFileIDFromDB($file->getId(), $this->userId);
return new DataResponse($track);
}
return new DataResponse($track);
}
}

Просмотреть файл

@ -11,78 +11,70 @@
namespace OCA\Maps\Controller;
use OCP\App\IAppManager;
use OCP\IURLGenerator;
use OCP\IConfig;
use \OCP\IL10N;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\ApiController;
use OCP\Constants;
use OCP\Share;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\IServerContainer;
use OCP\IGroupManager;
use OCP\ILogger;
use OCA\Maps\Service\TracksService;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share\IManager;
use function OCA\Maps\Helper\remove_utf8_bom;
class TracksController extends Controller {
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $l;
private $logger;
private $tracksService;
protected $appName;
private $userId;
private $userfolder;
private $config;
private $appVersion;
private $shareManager;
private $userManager;
private $groupManager;
private $dbtype;
private $dbdblquotes;
private $l;
private $logger;
private $tracksService;
protected $appName;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
TracksService $tracksService,
$UserId){
parent::__construct($AppName, $request);
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null){
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IManager $shareManager,
IAppManager $appManager,
IUserManager $userManager,
IGroupManager $groupManager,
IL10N $l,
ILogger $logger,
TracksService $tracksService,
$UserId) {
parent::__construct($AppName, $request);
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->appName = $AppName;
$this->appVersion = $config->getAppValue('maps', 'installed_version');
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->l = $l;
$this->dbtype = $config->getSystemValue('dbtype');
$this->config = $config;
if ($UserId !== '' and $UserId !== null and $serverContainer !== null) {
$this->userfolder = $serverContainer->getUserFolder($UserId);
}
$this->shareManager = $shareManager;
}
/**
* @NoAdminRequired
@ -90,16 +82,16 @@ class TracksController extends Controller {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
*/
public function getTracks($myMapId=null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$tracks = $this->tracksService->getTracksFromDB($this->userId, $this->userfolder, true, false, true);
} else {
$folders = $this->userfolder->getById($myMapId);
$folder = array_shift($folders);
$tracks = $this->tracksService->getTracksFromDB($this->userId, $folder, true, false, false);
}
return new DataResponse($tracks);
}
public function getTracks($myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
$tracks = $this->tracksService->getTracksFromDB($this->userId, $this->userfolder, true, false, true);
} else {
$folders = $this->userfolder->getById($myMapId);
$folder = array_shift($folders);
$tracks = $this->tracksService->getTracksFromDB($this->userId, $folder, true, false, false);
}
return new DataResponse($tracks);
}
/**
* @NoAdminRequired
@ -116,20 +108,17 @@ class TracksController extends Controller {
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
}
else {
} else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata'=>$metadata,
'content'=>$trackContent
'metadata' => $metadata,
'content' => $trackContent
]);
}
else {
} else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
}
else {
} else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
@ -141,35 +130,32 @@ class TracksController extends Controller {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
*/
public function getTrackFileContent($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id);
$res = is_null($track) ? null : $this->userfolder->getById($track['file_id']);
if (is_array($res) and count($res) > 0) {
$trackFile = $res[0];
if ($trackFile->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
$trackContent = remove_utf8_bom($trackFile->getContent());
// compute metadata if necessary
// first time we get it OR the file changed
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
}
else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata'=>$metadata,
'content'=>$trackContent
]);
}
else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
}
else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
public function getTrackFileContent($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id);
$res = is_null($track) ? null : $this->userfolder->getById($track['file_id']);
if (is_array($res) and count($res) > 0) {
$trackFile = $res[0];
if ($trackFile->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
$trackContent = remove_utf8_bom($trackFile->getContent());
// compute metadata if necessary
// first time we get it OR the file changed
if (!$track['metadata'] || $track['etag'] !== $trackFile->getEtag()) {
$metadata = $this->tracksService->generateTrackMetadata($trackFile);
$this->tracksService->editTrackInDB($track['id'], null, $metadata, $trackFile->getEtag());
} else {
$metadata = $track['metadata'];
}
return new DataResponse([
'metadata' => $metadata,
'content' => $trackContent
]);
} else {
return new DataResponse($this->l->t('Bad file type'), 400);
}
} else {
return new DataResponse($this->l->t('File not found'), 400);
}
}
/**
* @NoAdminRequired
@ -179,31 +165,29 @@ class TracksController extends Controller {
* @param $etag
* @return DataResponse
*/
public function editTrack($id, $color, $metadata, $etag): DataResponse {
$track = $this->tracksService->getTrackFromDB($id, $this->userId);
if ($track !== null) {
$this->tracksService->editTrackInDB($id, $color, $metadata, $etag);
return new DataResponse('EDITED');
}
else {
return new DataResponse($this->l->t('No such track'), 400);
}
}
public function editTrack($id, $color, $metadata, $etag): DataResponse {
$track = $this->tracksService->getTrackFromDB($id, $this->userId);
if ($track !== null) {
$this->tracksService->editTrackInDB($id, $color, $metadata, $etag);
return new DataResponse('EDITED');
} else {
return new DataResponse($this->l->t('No such track'), 400);
}
}
/**
* @NoAdminRequired
* @param $id
* @return DataResponse
*/
public function deleteTrack($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id, $this->userId);
if ($track !== null) {
$this->tracksService->deleteTrackFromDB($id);
return new DataResponse('DELETED');
}
else {
return new DataResponse($this->l->t('No such track'), 400);
}
}
public function deleteTrack($id): DataResponse {
$track = $this->tracksService->getTrackFromDB($id, $this->userId);
if ($track !== null) {
$this->tracksService->deleteTrackFromDB($id);
return new DataResponse('DELETED');
} else {
return new DataResponse($this->l->t('No such track'), 400);
}
}
}

Просмотреть файл

@ -12,44 +12,39 @@
namespace OCA\Maps\Controller;
use League\Flysystem\FileNotFoundException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\App\IAppManager;
use OCP\IURLGenerator;
use OCP\IConfig;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Lock\LockedException;
use PhpParser\JsonDecoder;
class UtilsController extends Controller {
private $userId;
private $config;
private $userId;
private $config;
private $root;
public function __construct($AppName,
IRequest $request,
IConfig $config,
IAppManager $appManager,
IRootFolder $root,
$UserId){
parent::__construct($AppName, $request);
public function __construct($AppName,
IRequest $request,
IConfig $config,
IAppManager $appManager,
IRootFolder $root,
$UserId) {
parent::__construct($AppName, $request);
$this->root = $root;
$this->userId = $UserId;
// IConfig object
$this->config = $config;
}
$this->userId = $UserId;
// IConfig object
$this->config = $config;
}
/**
* Delete user options
@ -57,13 +52,13 @@ class UtilsController extends Controller {
* @NoAdminRequired
* @return DataResponse
*/
public function deleteOptionsValues(): DataResponse {
$keys = $this->config->getUserKeys($this->userId, 'maps');
foreach ($keys as $key) {
$this->config->deleteUserValue($this->userId, 'maps', $key);
}
return new DataResponse(['done'=>1]);
}
public function deleteOptionsValues(): DataResponse {
$keys = $this->config->getUserKeys($this->userId, 'maps');
foreach ($keys as $key) {
$this->config->deleteUserValue($this->userId, 'maps', $key);
}
return new DataResponse(['done' => 1]);
}
/**
* Save options values to the DB for current user
@ -73,32 +68,32 @@ class UtilsController extends Controller {
* @return DataResponse
* @throws \OCP\PreConditionNotMetException
*/
public function saveOptionValue($options, $myMapId=null): DataResponse {
if( is_null($myMapId) || $myMapId==="") {
foreach ($options as $key => $value) {
$this->config->setUserValue($this->userId, 'maps', $key, $value);
}
} else {
public function saveOptionValue($options, $myMapId = null): DataResponse {
if (is_null($myMapId) || $myMapId === '') {
foreach ($options as $key => $value) {
$this->config->setUserValue($this->userId, 'maps', $key, $value);
}
} else {
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file=$folder->get(".index.maps");
} catch (NotFoundException $e) {
$file=$folder->newFile(".index.maps", $content = "{}");
}
try {
$ov = json_decode($file->getContent(),true, 512);
foreach ($options as $key => $value) {
$ov[$key] = $value;
}
$file->putContent(json_encode($ov, JSON_PRETTY_PRINT));
} catch (LockedException $e){
return new DataResponse("File is locked", 500);
}
}
return new DataResponse(['done'=>1]);
}
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
$file = $folder->newFile('.index.maps', $content = '{}');
}
try {
$ov = json_decode($file->getContent(), true, 512);
foreach ($options as $key => $value) {
$ov[$key] = $value;
}
$file->putContent(json_encode($ov, JSON_PRETTY_PRINT));
} catch (LockedException $e) {
return new DataResponse('File is locked', 500);
}
}
return new DataResponse(['done' => 1]);
}
/**
* get options values from the config for current user
@ -106,31 +101,31 @@ class UtilsController extends Controller {
* @NoAdminRequired
* @return DataResponse
*/
public function getOptionsValues($myMapId=null): DataResponse {
$ov = array();
public function getOptionsValues($myMapId = null): DataResponse {
$ov = [];
if( is_null($myMapId) || $myMapId==="") {
// get all user values
$keys = $this->config->getUserKeys($this->userId, 'maps');
foreach ($keys as $key) {
$value = $this->config->getUserValue($this->userId, 'maps', $key);
$ov[$key] = $value;
}
if (is_null($myMapId) || $myMapId === '') {
// get all user values
$keys = $this->config->getUserKeys($this->userId, 'maps');
foreach ($keys as $key) {
$value = $this->config->getUserValue($this->userId, 'maps', $key);
$ov[$key] = $value;
}
$ov['isCreatable'] = true;
$ov['isDeletable'] = false;
$ov['isReadable'] = true;
$ov['isUpdateable'] = true;
$ov['isShareable'] = true;
} else {
} else {
$userFolder = $this->root->getUserFolder($this->userId);
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file=$folder->get(".index.maps");
} catch (NotFoundException $e) {
$file=$folder->newFile(".index.maps", $content = "{}");
}
$ov = json_decode($file->getContent(),true, 512);
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
$file = $folder->newFile('.index.maps', $content = '{}');
}
$ov = json_decode($file->getContent(), true, 512);
$ov['isCreatable'] = $folder->isCreatable();
//We can delete the map by deleting the folder or the .index.maps file
$ov['isDeletable'] = $folder->isDeletable() || $file->isDeletable();
@ -140,26 +135,26 @@ class UtilsController extends Controller {
$ov['isUpdateable'] = $file->isUpdateable();
// Share map by sharing the folder
$ov['isShareable'] = $folder->isShareable();
}
}
// get routing-specific admin settings values
$settingsKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
// get routing-specific admin settings values
$settingsKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
$v = $this->config->getAppValue('maps', $k);
$ov[$k] = $v;
}
return new DataResponse(['values'=>$ov]);
}
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
$v = $this->config->getAppValue('maps', $k);
$ov[$k] = $v;
}
return new DataResponse(['values' => $ov]);
}
/**
* set routing settings
@ -167,101 +162,101 @@ class UtilsController extends Controller {
* @param $values
* @return DataResponse
*/
public function setRoutingSettings($values): DataResponse {
$acceptedKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
public function setRoutingSettings($values): DataResponse {
$acceptedKeys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($values as $k=>$v) {
if (in_array($k, $acceptedKeys)) {
$this->config->setAppValue('maps', $k, $v);
}
}
$response = new DataResponse('DONE');
$csp = new ContentSecurityPolicy();
$csp->addAllowedImageDomain('*')
->addAllowedMediaDomain('*')
->addAllowedConnectDomain('*');
$response->setContentSecurityPolicy($csp);
return $response;
}
'graphhopperURL'
];
foreach ($values as $k => $v) {
if (in_array($k, $acceptedKeys)) {
$this->config->setAppValue('maps', $k, $v);
}
}
$response = new DataResponse('DONE');
$csp = new ContentSecurityPolicy();
$csp->addAllowedImageDomain('*')
->addAllowedMediaDomain('*')
->addAllowedConnectDomain('*');
$response->setContentSecurityPolicy($csp);
return $response;
}
/**
* get content of mapbox traffic style
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse
*/
public function getTrafficStyle(): DataResponse {
$style = [
'version' => 8,
'name' => 'Mapbox Traffic tileset v1',
'sources' => [
'mapbox-traffic' => [
'url' => 'mapbox://mapbox.mapbox-traffic-v1',
'type' => 'vector'
]
],
'layers' => [
[
'id' => 'traffic',
'source' => 'mapbox-traffic',
'source-layer' => 'traffic',
'type' => 'line',
'paint' => [
'line-width' => 2.0,
'line-color' => [
'case',
[
'==',
'low',
[
'get',
'congestion'
]
],
'#00ff00',
[
'==',
'moderate',
[
'get',
'congestion'
]
],
'#ffad00',
[
'==',
'heavy',
[
'get',
'congestion'
]
],
'#ff0000',
[
'==',
'severe',
[
'get',
'congestion'
]
],
'#b43b71',
'#000000'
]
]
]
]
];
return new DataResponse($style);
}
/**
* get content of mapbox traffic style
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse
*/
public function getTrafficStyle(): DataResponse {
$style = [
'version' => 8,
'name' => 'Mapbox Traffic tileset v1',
'sources' => [
'mapbox-traffic' => [
'url' => 'mapbox://mapbox.mapbox-traffic-v1',
'type' => 'vector'
]
],
'layers' => [
[
'id' => 'traffic',
'source' => 'mapbox-traffic',
'source-layer' => 'traffic',
'type' => 'line',
'paint' => [
'line-width' => 2.0,
'line-color' => [
'case',
[
'==',
'low',
[
'get',
'congestion'
]
],
'#00ff00',
[
'==',
'moderate',
[
'get',
'congestion'
]
],
'#ffad00',
[
'==',
'heavy',
[
'get',
'congestion'
]
],
'#ff0000',
[
'==',
'severe',
[
'get',
'congestion'
]
],
'#b43b71',
'#000000'
]
]
]
]
];
return new DataResponse($style);
}
}

Просмотреть файл

@ -37,15 +37,15 @@ use OCP\AppFramework\Db\Entity;
* @method string setTimestampTo(int $timestampTo)
*/
class DeviceShare extends Entity {
public $token;
public $deviceId;
public $token;
public $deviceId;
public $timestampFrom;
public $timestampTo;
public function __construct() {
$this->addType('token', 'string');
$this->addType('deviceId', 'int');
public function __construct() {
$this->addType('token', 'string');
$this->addType('deviceId', 'int');
$this->addType('timestampFrom', 'int');
$this->addType('timestampTo', 'int');
}
}
}

Просмотреть файл

@ -36,36 +36,35 @@ use OCP\Files\NotFoundException;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
class DeviceShareMapper extends QBMapper {
/* @var ISecureRandom */
private $secureRandom;
/* @var ISecureRandom */
private $secureRandom;
private $root;
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, IRootFolder $root) {
parent::__construct($db, 'maps_device_shares');
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, IRootFolder $root) {
parent::__construct($db, 'maps_device_shares');
$this->secureRandom = $secureRandom;
$this->secureRandom = $secureRandom;
$this->root = $root;
}
}
/**
* @param string $token
* @return Entity|null
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByToken($token) {
$qb = $this->db->getQueryBuilder();
/**
* @param string $token
* @return Entity|null
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByToken($token) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
->from($this->getTableName())
->where(
$qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))
);
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))
);
return $this->findEntity($qb);
}
return $this->findEntity($qb);
}
/**
* @param string[] $token
@ -75,7 +74,7 @@ class DeviceShareMapper extends QBMapper {
public function findByTokens($tokens) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->in('token', $qb->createNamedParameter($tokens, IQueryBuilder::PARAM_STR))
@ -84,44 +83,44 @@ class DeviceShareMapper extends QBMapper {
return $this->findEntities($qb);
}
/**
* @param $deviceId
/**
* @param $deviceId
* @param $timestampFrom
* @param $timestampTo
* @return Entity
*/
public function create($deviceId, $timestampFrom, $timestampTo): Entity {
$token = $this->secureRandom->generate(
Constants::TOKEN_LENGTH,
ISecureRandom::CHAR_HUMAN_READABLE
);
* @return Entity
*/
public function create($deviceId, $timestampFrom, $timestampTo): Entity {
$token = $this->secureRandom->generate(
Constants::TOKEN_LENGTH,
ISecureRandom::CHAR_HUMAN_READABLE
);
$newShare = new DeviceShare();
$newShare->setToken($token);
$newShare->setDeviceId($deviceId);
$newShare->setTimestampFrom($timestampFrom);
$newShare = new DeviceShare();
$newShare->setToken($token);
$newShare->setDeviceId($deviceId);
$newShare->setTimestampFrom($timestampFrom);
$newShare->setTimestampTo($timestampTo);
return $this->insert($newShare);
}
return $this->insert($newShare);
}
/**
* @param $deviceId
* @return Entity[]
* @throws DoesNotExistException
*/
public function findByDeviceId($deviceId) {
$qb = $this->db->getQueryBuilder();
/**
* @param $deviceId
* @return Entity[]
* @throws DoesNotExistException
*/
public function findByDeviceId($deviceId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('device_id', $qb->createNamedParameter($deviceId, IQueryBuilder::PARAM_INT))
);
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('device_id', $qb->createNamedParameter($deviceId, IQueryBuilder::PARAM_INT))
);
return $this->findEntities($qb);
}
return $this->findEntities($qb);
}
/**
* @param $deviceIds
@ -189,10 +188,10 @@ class DeviceShareMapper extends QBMapper {
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function findById($id) {
public function findById($id) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id))
@ -215,19 +214,19 @@ class DeviceShareMapper extends QBMapper {
return true;
}
/**
* @param $deviceId
* @return bool
*/
public function removeAllByDeviceId($deviceId) {
try {
$entities = $this->findByDeviceId($deviceId);
/**
* @param $deviceId
* @return bool
*/
public function removeAllByDeviceId($deviceId) {
try {
$entities = $this->findByDeviceId($deviceId);
foreach ($entities as $entity) {
$this->delete($entity);
}
} catch (DoesNotExistException) {
return false;
}
return true;
}
} catch (DoesNotExistException) {
return false;
}
return true;
}
}

Просмотреть файл

@ -37,15 +37,15 @@ use OCP\AppFramework\Db\Entity;
* @method string setOwner(string $owner)
*/
class FavoriteShare extends Entity {
public $owner;
public $token;
public $category;
public $allowEdits = false;
public $owner;
public $token;
public $category;
public $allowEdits = false;
public function __construct() {
$this->addType('owner', 'string');
$this->addType('token', 'string');
$this->addType('category', 'string');
$this->addType('allowEdits', 'boolean');
}
public function __construct() {
$this->addType('owner', 'string');
$this->addType('token', 'string');
$this->addType('category', 'string');
$this->addType('allowEdits', 'boolean');
}
}

Просмотреть файл

@ -35,71 +35,70 @@ use OCP\Files\NotFoundException;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
class FavoriteShareMapper extends QBMapper {
/* @var ISecureRandom */
private $secureRandom;
/* @var ISecureRandom */
private $secureRandom;
private $root;
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, IRootFolder $root) {
parent::__construct($db, 'maps_favorite_shares');
public function __construct(IDBConnection $db, ISecureRandom $secureRandom, IRootFolder $root) {
parent::__construct($db, 'maps_favorite_shares');
$this->secureRandom = $secureRandom;
$this->secureRandom = $secureRandom;
$this->root = $root;
}
}
/**
* @param $token
* @return Entity|null
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByToken($token) {
$qb = $this->db->getQueryBuilder();
/**
* @param $token
* @return Entity|null
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByToken($token) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
->from($this->getTableName())
->where(
$qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))
);
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))
);
return $this->findEntity($qb);
}
return $this->findEntity($qb);
}
/**
* @param $owner
* @param $category
* @return Entity
*/
public function create($owner, $category) {
$token = $this->secureRandom->generate(
Constants::TOKEN_LENGTH,
ISecureRandom::CHAR_HUMAN_READABLE
);
/**
* @param $owner
* @param $category
* @return Entity
*/
public function create($owner, $category) {
$token = $this->secureRandom->generate(
Constants::TOKEN_LENGTH,
ISecureRandom::CHAR_HUMAN_READABLE
);
$newShare = new FavoriteShare();
$newShare->setToken($token);
$newShare->setCategory($category);
$newShare->setOwner($owner);
$newShare = new FavoriteShare();
$newShare->setToken($token);
$newShare->setCategory($category);
$newShare->setOwner($owner);
return $this->insert($newShare);
}
return $this->insert($newShare);
}
/**
* @param $owner
* @return array|Entity[]
*/
public function findAllByOwner($owner) {
$qb = $this->db->getQueryBuilder();
/**
* @param $owner
* @return array|Entity[]
*/
public function findAllByOwner($owner) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
->from($this->getTableName())
->where(
$qb->expr()->eq('owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
);
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
return $this->findEntities($qb);
}
/**
* @param $userId
@ -128,39 +127,39 @@ class FavoriteShareMapper extends QBMapper {
* @return mixed
* @throws NotFoundException
*/
public function findAllByFolder($folder, $isCreatable=true) {
public function findAllByFolder($folder, $isCreatable = true) {
try {
$file=$folder->get(".favorite_shares.json");
$file = $folder->get('.favorite_shares.json');
} catch (NotFoundException $e) {
if ($isCreatable) {
$file=$folder->newFile(".favorite_shares.json", $content = '[]');
$file = $folder->newFile('.favorite_shares.json', $content = '[]');
} else {
throw new NotFoundException();
}
}
return json_decode($file->getContent(),true);
return json_decode($file->getContent(), true);
}
/**
* @param $owner
* @param $category
* @return Entity
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByOwnerAndCategory($owner, $category) {
$qb = $this->db->getQueryBuilder();
/**
* @param $owner
* @param $category
* @return Entity
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findByOwnerAndCategory($owner, $category) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('category', $qb->createNamedParameter($category, IQueryBuilder::PARAM_STR))
)->andWhere(
$qb->expr()->eq('owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
);
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('category', $qb->createNamedParameter($category, IQueryBuilder::PARAM_STR))
)->andWhere(
$qb->expr()->eq('owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
);
return $this->findEntity($qb);
}
return $this->findEntity($qb);
}
/**
* @param $userId
@ -193,14 +192,14 @@ class FavoriteShareMapper extends QBMapper {
return $deleted;
}
try {
$file=$folder->get(".favorite_shares.json");
$file = $folder->get('.favorite_shares.json');
} catch (NotFoundException $e) {
$file=$folder->newFile(".favorite_shares.json", $content = '[]');
$file = $folder->newFile('.favorite_shares.json', $content = '[]');
}
$data = json_decode($file->getContent(),true);
$data = json_decode($file->getContent(), true);
foreach ($data as $share) {
$c = $share["category"];
if($c === $category) {
$c = $share['category'];
if ($c === $category) {
$deleted = $share;
} else {
$shares[] = $share;
@ -210,39 +209,39 @@ class FavoriteShareMapper extends QBMapper {
return $deleted;
}
/**
* @param $owner
* @param $category
* @return Entity|null
*/
public function findOrCreateByOwnerAndCategory($owner, $category) {
/* @var Entity */
$entity = null;
/**
* @param $owner
* @param $category
* @return Entity|null
*/
public function findOrCreateByOwnerAndCategory($owner, $category) {
/* @var Entity */
$entity = null;
try {
$entity = $this->findByOwnerAndCategory($owner, $category);
} catch (DoesNotExistException $e) {
$entity = $this->create($owner, $category);
} catch (MultipleObjectsReturnedException $e) {
}
try {
$entity = $this->findByOwnerAndCategory($owner, $category);
} catch (DoesNotExistException $e) {
$entity = $this->create($owner, $category);
} catch (MultipleObjectsReturnedException $e) {
}
return $entity;
}
return $entity;
}
/**
* @param $owner
* @param $category
* @return bool
*/
public function removeByOwnerAndCategory($owner, $category) {
try {
$entity = $this->findByOwnerAndCategory($owner, $category);
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
return false;
}
/**
* @param $owner
* @param $category
* @return bool
*/
public function removeByOwnerAndCategory($owner, $category) {
try {
$entity = $this->findByOwnerAndCategory($owner, $category);
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return false;
}
$this->delete($entity);
$this->delete($entity);
return true;
}
return true;
}
}

Просмотреть файл

@ -16,16 +16,16 @@ use OCP\AppFramework\Db\Entity;
class Geophoto extends Entity {
protected $fileId;
protected $lat;
protected $lng;
protected $dateTaken;
protected $userId;
protected $fileId;
protected $lat;
protected $lng;
protected $dateTaken;
protected $userId;
public function __construct() {
$this->addType('fileId', 'integer');
$this->addType('lat', 'float');
$this->addType('lng', 'float');
$this->addType('dateTaken', 'integer');
}
public function __construct() {
$this->addType('fileId', 'integer');
$this->addType('lat', 'float');
$this->addType('lng', 'float');
$this->addType('dateTaken', 'integer');
}
}

Просмотреть файл

@ -10,19 +10,17 @@
* @copyright Piotr Bator 2017
*/
namespace OCA\Maps\DB;
namespace OCA\Maps\DB;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\AppFramework\Db\QBMapper;
class GeophotoMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'maps_photos');
}
public function __construct(IDBConnection $db) {
parent::__construct($db, 'maps_photos');
}
/**
* @param $id
@ -31,17 +29,17 @@ class GeophotoMapper extends QBMapper {
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function find($id) {
public function find($id) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
);
return $this->findEntity($qb);
}
}
/**
* @param $fileId
@ -51,10 +49,10 @@ class GeophotoMapper extends QBMapper {
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function findByFileIdUserId($fileId, $userId) {
public function findByFileIdUserId($fileId, $userId) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
@ -63,7 +61,7 @@ class GeophotoMapper extends QBMapper {
);
return $this->findEntity($qb);
}
}
/**
* @param $fileId
@ -75,7 +73,7 @@ class GeophotoMapper extends QBMapper {
public function findByFileId($fileId) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_STR))
@ -91,10 +89,10 @@ class GeophotoMapper extends QBMapper {
* @return array|\OCP\AppFramework\Db\Entity[]
* @throws \OCP\DB\Exception
*/
public function findAll($userId, $limit=null, $offset=null) {
public function findAll($userId, $limit = null, $offset = null) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
@ -110,7 +108,7 @@ class GeophotoMapper extends QBMapper {
$qb->setMaxResults($limit);
}
return $this->findEntities($qb);
}
}
/**
* @param $userId
@ -119,10 +117,10 @@ class GeophotoMapper extends QBMapper {
* @return array|\OCP\AppFramework\Db\Entity[]
* @throws \OCP\DB\Exception
*/
public function findAllNonLocalized($userId, $limit=null, $offset=null) {
public function findAllNonLocalized($userId, $limit = null, $offset = null) {
$qb = $this->db->getQueryBuilder();
$qb->select("*")
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
@ -139,14 +137,14 @@ class GeophotoMapper extends QBMapper {
$qb->setMaxResults($limit);
}
return array_reverse($this->findEntities($qb));
}
}
/**
* @param $fileId
* @return int
* @throws \OCP\DB\Exception
*/
public function deleteByFileId($fileId) {
public function deleteByFileId($fileId) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
@ -155,7 +153,7 @@ class GeophotoMapper extends QBMapper {
);
return $qb->executeStatement();
}
}
/**
* @param $fileId
@ -163,7 +161,7 @@ class GeophotoMapper extends QBMapper {
* @return int
* @throws \OCP\DB\Exception
*/
public function deleteByFileIdUserId($fileId, $userId) {
public function deleteByFileIdUserId($fileId, $userId) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
@ -173,14 +171,14 @@ class GeophotoMapper extends QBMapper {
$qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_STR))
);
return $qb->executeStatement();
}
}
/**
* @param $userId
* @return int
* @throws \OCP\DB\Exception
*/
public function deleteAll($userId) {
public function deleteAll($userId) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
@ -188,7 +186,7 @@ class GeophotoMapper extends QBMapper {
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);
return $qb->executeStatement();
}
}
/**
* @param $fileId
@ -197,7 +195,7 @@ class GeophotoMapper extends QBMapper {
* @return int
* @throws \OCP\DB\Exception
*/
public function updateByFileId($fileId, $lat, $lng) {
public function updateByFileId($fileId, $lat, $lng) {
$qb = $this->db->getQueryBuilder();
$qb->update($this->getTableName())
@ -205,7 +203,7 @@ class GeophotoMapper extends QBMapper {
->set('lng', $qb->createNamedParameter($lng))
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)));
return $qb->executeStatement();
}
return $qb->executeStatement();
}
}

Просмотреть файл

@ -14,6 +14,5 @@ namespace OCA\Maps\Helper;
/**
* Class GeoPhotoException
*/
class ExifDataInvalidException extends \Exception
{
class ExifDataInvalidException extends \Exception {
}

Просмотреть файл

@ -14,6 +14,5 @@ namespace OCA\Maps\Helper;
/**
* Class GeoPhotoException
*/
class ExifDataNoLocationException extends \Exception
{
class ExifDataNoLocationException extends \Exception {
}

Просмотреть файл

@ -24,7 +24,7 @@ use lsolesen\pel\PelTiff;
//PHP 7 polyfill
if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle): bool {
return '' === $needle || false !== strpos($haystack, $needle);
return $needle === '' || strpos($haystack, $needle) !== false;
}
}
@ -35,370 +35,355 @@ if (!function_exists('str_contains')) {
* @property-read ?float $lng
* @property-read ?int $dateTaken
*/
class ExifGeoData
extends \stdClass
implements \JsonSerializable
{
/**
* Mime type regex
*/
private const SUPPORTED_MIMETYPES = 'image\/(jpe?g|tiff)';
class ExifGeoData extends \stdClass implements \JsonSerializable {
/**
* Mime type regex
*/
private const SUPPORTED_MIMETYPES = 'image\/(jpe?g|tiff)';
/**
* Exif Latitude attribute names
*/
private const LATITUDE_REF = 'GPSLatitudeRef';
private const LATITUDE = 'GPSLatitude';
/**
* Exif Latitude attribute names
*/
private const LATITUDE_REF = 'GPSLatitudeRef';
private const LATITUDE = 'GPSLatitude';
/**
* Exif Longitude attribute names
*/
private const LONGITUDE_REF = 'GPSLongitudeRef';
private const LONGITUDE = 'GPSLongitude';
/**
* Exif Longitude attribute names
*/
private const LONGITUDE_REF = 'GPSLongitudeRef';
private const LONGITUDE = 'GPSLongitude';
/**
* Exif Timestamp attribute name
*/
private const TIMESTAMP = 'DateTimeOriginal';
/**
* Exif Timestamp attribute name
*/
private const TIMESTAMP = 'DateTimeOriginal';
/**
* Coordinate modulo
* Modulo between degree, minute, second
*/
private const COORDINATE_MODULO = 60;
/**
* Coordinate modulo
* Modulo between degree, minute, second
*/
private const COORDINATE_MODULO = 60;
/**
* Regex to extract date components from exif parameter
*/
private const EXIF_TIME_REGEX = '(?<years>[0-9]{4})\:(?<months>[0-9]{2})\:(?<days>[0-9]{2}) (?<hours>[0-9]{2})\:(?<minutes>[0-9]{2})\:(?<seconds>[0-9]{2})';
/**
* Regex to extract date components from exif parameter
*/
private const EXIF_TIME_REGEX = '(?<years>[0-9]{4})\:(?<months>[0-9]{2})\:(?<days>[0-9]{2}) (?<hours>[0-9]{2})\:(?<minutes>[0-9]{2})\:(?<seconds>[0-9]{2})';
/**
* @var int|null
*/
private $timestamp = null;
/**
* @var int|null
*/
private $timestamp = null;
/**
* @var float|null
*/
private $latitude = null;
/**
* @var float|null
*/
private $latitude = null;
/**
* @var float|null
*/
private $longitude = null;
/**
* @var float|null
*/
private $longitude = null;
/**
* @var bool|null
*/
private $is_valid = null;
/**
* @var bool|null
*/
private $is_valid = null;
/**
* @var ?array
*/
protected $exif_data = null;
/**
* @var ?array
*/
protected $exif_data = null;
/**
* @param string $path
*
* @return array|null
* @throws PelInvalidArgumentException
*/
protected static function get_exif_data_array(string $path) : array{
if( function_exists('exif_read_data') ) {
$data = @exif_read_data($path, null, true);
if ($data && isset($data['EXIF']) && isset($data['EXIF'][self::LATITUDE]) && isset($data['EXIF'][self::LONGITUDE])) {
return $data['EXIF'];
} elseif ($data && isset($data['GPS']) && isset($data['GPS'][self::LATITUDE]) && isset($data['GPS'][self::LONGITUDE])) {
/**
* @param string $path
*
* @return array|null
* @throws PelInvalidArgumentException
*/
protected static function get_exif_data_array(string $path) : array {
if (function_exists('exif_read_data')) {
$data = @exif_read_data($path, null, true);
if ($data && isset($data['EXIF']) && isset($data['EXIF'][self::LATITUDE]) && isset($data['EXIF'][self::LONGITUDE])) {
return $data['EXIF'];
} elseif ($data && isset($data['GPS']) && isset($data['GPS'][self::LATITUDE]) && isset($data['GPS'][self::LONGITUDE])) {
$d = $data['GPS'];
if (!isset($d[self::TIMESTAMP]) && isset($data['EXIF'][self::TIMESTAMP])) {
$d[self::TIMESTAMP] = $data['EXIF'][self::TIMESTAMP];
}
return $d;
}
}
$data = new PelDataWindow(file_get_contents($path));
if (PelJpeg::isValid($data)) {
$pelJpeg = new PelJpeg($data);
}
$data = new PelDataWindow(file_get_contents($path));
if (PelJpeg::isValid($data)) {
$pelJpeg = new PelJpeg($data);
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
return [];
}
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
return [];
}
$pelTiff = $pelExif->getTiff();
} elseif (PelTiff::isValid($data)) {
$pelTiff = new PelTiff($data);
} else {
return [];
}
if (is_null($pelTiff)) {
return [];
}
$pelIfd0 = $pelTiff->getIfd();
if (is_null($pelIfd0)) {
return [];
}
$pelIfdExif = $pelIfd0->getSubIfd(PelIfd::EXIF);
$pelTiff = $pelExif->getTiff();
} elseif (PelTiff::isValid($data)) {
$pelTiff = new PelTiff($data);
} else {
return [];
}
if (is_null($pelTiff)) {
return [];
}
$pelIfd0 = $pelTiff->getIfd();
if (is_null($pelIfd0)) {
return [];
}
$pelIfdExif = $pelIfd0->getSubIfd(PelIfd::EXIF);
if (is_null($pelIfdExif)) {
return [];
}
$pelDateTimeOriginal = $pelIfdExif->getEntry(PelTag::DATE_TIME_ORIGINAL);
if (is_null($pelDateTimeOriginal)) {
return [];
}
$exif = [
# self::TIMESTAMP => $pelDateTimeOriginal->getValue(PelEntryTime::EXIF_STRING) // for old pel 0.9.6 and above
self::TIMESTAMP => (int) $pelDateTimeOriginal->getValue() // for new pel >= 0.9.11
];
$pelIfdGPS = $pelIfd0->getSubIfd(PelIfd::GPS);
if (!is_null($pelIfdGPS) && !is_null($pelIfdGPS->getEntry(PelTag::GPS_LATITUDE )) && !is_null( $pelIfdGPS->getEntry(PelTag::GPS_LONGITUDE))) {
static::readPelCoordinate($pelIfdGPS,
self::LATITUDE, PelTag::GPS_LATITUDE,
self::LATITUDE_REF, PelTag::GPS_LATITUDE_REF,
$exif
);
static::readPelCoordinate($pelIfdGPS,
self::LONGITUDE, PelTag::GPS_LONGITUDE,
self::LONGITUDE_REF, PelTag::GPS_LONGITUDE_REF,
$exif
);
}
Pel::clearExceptions();
return $exif;
}
if (is_null($pelIfdExif)) {
return [];
}
$pelDateTimeOriginal = $pelIfdExif->getEntry(PelTag::DATE_TIME_ORIGINAL);
if (is_null($pelDateTimeOriginal)) {
return [];
}
$exif = [
# self::TIMESTAMP => $pelDateTimeOriginal->getValue(PelEntryTime::EXIF_STRING) // for old pel 0.9.6 and above
self::TIMESTAMP => (int)$pelDateTimeOriginal->getValue() // for new pel >= 0.9.11
];
$pelIfdGPS = $pelIfd0->getSubIfd(PelIfd::GPS);
if (!is_null($pelIfdGPS) && !is_null($pelIfdGPS->getEntry(PelTag::GPS_LATITUDE)) && !is_null($pelIfdGPS->getEntry(PelTag::GPS_LONGITUDE))) {
static::readPelCoordinate($pelIfdGPS,
self::LATITUDE, PelTag::GPS_LATITUDE,
self::LATITUDE_REF, PelTag::GPS_LATITUDE_REF,
$exif
);
static::readPelCoordinate($pelIfdGPS,
self::LONGITUDE, PelTag::GPS_LONGITUDE,
self::LONGITUDE_REF, PelTag::GPS_LONGITUDE_REF,
$exif
);
}
Pel::clearExceptions();
return $exif;
}
/**
* @param PelIfd $pelIfdGPS
* @param $target
* @param $source
* @param $ref_target
* @param $ref_source
* @param array $exif
*/
protected static function readPelCoordinate( PelIfd $pelIfdGPS, $target, $source, $ref_target, $ref_source, array &$exif = [] ) : void{
$coordinate = $pelIfdGPS->getEntry($source)->getValue();
if ((int) $coordinate[0][1] !=0 && (int) $coordinate[1][1] !=0 && (int) $coordinate[2][1] !=0) {
$exif[$ref_target] = $pelIfdGPS->getEntry($ref_source)->getValue();
$exif[$target] = [
0 => (int) $coordinate[0][0]/ (int) $coordinate[0][1],
1 => (int) $coordinate[1][0]/ (int) $coordinate[1][1],
2 => (int) $coordinate[2][0]/ (int) $coordinate[2][1]
];
}
}
/**
* @param PelIfd $pelIfdGPS
* @param $target
* @param $source
* @param $ref_target
* @param $ref_source
* @param array $exif
*/
protected static function readPelCoordinate(PelIfd $pelIfdGPS, $target, $source, $ref_target, $ref_source, array &$exif = []) : void {
$coordinate = $pelIfdGPS->getEntry($source)->getValue();
if ((int)$coordinate[0][1] != 0 && (int)$coordinate[1][1] != 0 && (int)$coordinate[2][1] != 0) {
$exif[$ref_target] = $pelIfdGPS->getEntry($ref_source)->getValue();
$exif[$target] = [
0 => (int)$coordinate[0][0] / (int)$coordinate[0][1],
1 => (int)$coordinate[1][0] / (int)$coordinate[1][1],
2 => (int)$coordinate[2][0] / (int)$coordinate[2][1]
];
}
}
/**
* @param string $path
* @return ExifGeoData
*/
public static function get(string $path) : ?ExifGeoData{
try{
/**
* @param string $path
* @return ExifGeoData
*/
public static function get(string $path) : ?ExifGeoData {
try {
$data = static::get_exif_data_array($path);
}catch(\Throwable $e){
$data = [];
}
return new static($data);
}
} catch (\Throwable $e) {
$data = [];
}
return new static($data);
}
/**
* ExifGeoData constructor.
* @param array $exif_data
*/
private final function __construct(array $exif_data)
{
$this->exif_data = $exif_data;
$this->parse();
}
/**
* ExifGeoData constructor.
* @param array $exif_data
*/
final private function __construct(array $exif_data) {
$this->exif_data = $exif_data;
$this->parse();
}
/**
* @param false $invalidate_zero_iland
* @throws ExifDataInvalidException
* @throws ExifDataNoLocationException
*/
public function validate( $invalidate_zero_iland = false )
{
if (!$this->exif_data) {
throw new ExifDataInvalidException('No exif_data found', 1);
}
if (!is_array($this->exif_data)) {
throw new ExifDataInvalidException('exif_data is not an array', 2);
}
public function validate($invalidate_zero_iland = false) {
if (!$this->exif_data) {
throw new ExifDataInvalidException('No exif_data found', 1);
}
if (!is_array($this->exif_data)) {
throw new ExifDataInvalidException('exif_data is not an array', 2);
}
if (!isset($this->exif_data[self::LATITUDE]) || !isset($this->exif_data[self::LONGITUDE])) {
throw new ExifDataNoLocationException('Latitude and/or Longitude are missing from exif data', 1);
}
if( $invalidate_zero_iland && $this->isZeroIsland() ){
if (!isset($this->exif_data[self::LATITUDE]) || !isset($this->exif_data[self::LONGITUDE])) {
throw new ExifDataNoLocationException('Latitude and/or Longitude are missing from exif data', 1);
}
if ($invalidate_zero_iland && $this->isZeroIsland()) {
$this->latitude = null;
$this->longitude = null;
throw new ExifDataNoLocationException('Zero island is not valid', 2);
}
}
throw new ExifDataNoLocationException('Zero island is not valid', 2);
}
}
/**
* @return bool
*/
public function isValid(): bool
{
if (null === $this->is_valid) {
try {
$this->validate();
$this->is_valid = true;
} catch (\Throwable $e) {
$this->is_valid = false;
}
}
return $this->is_valid;
}
/**
* @return bool
*/
public function isValid(): bool {
if ($this->is_valid === null) {
try {
$this->validate();
$this->is_valid = true;
} catch (\Throwable $e) {
$this->is_valid = false;
}
}
return $this->is_valid;
}
/**
* @return bool
*/
private function parse()
{
if ($this->isValid() && (null === $this->latitude || null === $this->longitude)) {
$this->longitude = $this->geo2float($this->exif_data[self::LONGITUDE]);
if( isset($this->exif_data[self::LONGITUDE_REF]) && 'W' === $this->exif_data[self::LONGITUDE_REF] ){
$this->longitude*=-1;
}
$this->latitude = $this->geo2float($this->exif_data[self::LATITUDE]);
if( isset($this->exif_data[self::LATITUDE_REF]) && 'S' === $this->exif_data[self::LATITUDE_REF] ){
$this->latitude*=-1;
}
}
/**
* @return bool
*/
private function parse() {
if ($this->isValid() && ($this->latitude === null || $this->longitude === null)) {
$this->longitude = $this->geo2float($this->exif_data[self::LONGITUDE]);
if (isset($this->exif_data[self::LONGITUDE_REF]) && $this->exif_data[self::LONGITUDE_REF] === 'W') {
$this->longitude *= -1;
}
$this->latitude = $this->geo2float($this->exif_data[self::LATITUDE]);
if (isset($this->exif_data[self::LATITUDE_REF]) && $this->exif_data[self::LATITUDE_REF] === 'S') {
$this->latitude *= -1;
}
}
// optional
if (isset($this->exif_data[self::TIMESTAMP])) {
$t = $this->exif_data[self::TIMESTAMP];
$this->timestamp = is_string($t) ? $this->string2time($t) : ( is_int($t) ? $t : null );
$this->timestamp = is_string($t) ? $this->string2time($t) : (is_int($t) ? $t : null);
}
}
}
/**
* @param string $timestamp
* @return int
*/
private function string2time(string $timestamp): ?int
{
$result = null;
if (preg_match('#' . self::EXIF_TIME_REGEX . '#ui', $timestamp, $match)) {
$result =
strtotime("{$match['years']}-{$match['months']}-{$match['days']} {$match['hours']}:{$match['minutes']}:{$match['seconds']}");
}
return $result;
}
/**
* @param string $timestamp
* @return int
*/
private function string2time(string $timestamp): ?int {
$result = null;
if (preg_match('#' . self::EXIF_TIME_REGEX . '#ui', $timestamp, $match)) {
$result =
strtotime("{$match['years']}-{$match['months']}-{$match['days']} {$match['hours']}:{$match['minutes']}:{$match['seconds']}");
}
return $result;
}
/**
* @param $geo
* @return float|null
*/
private function geo2float($geo): ?float
{
if (!is_array($geo)) {
$geo = [$geo];
}
$result = .0;
$d = 1.0;
foreach ($geo as $component) {
$result += ($this->string2float($component) * $d);
$d /= self::COORDINATE_MODULO;
}
/**
* @param $geo
* @return float|null
*/
private function geo2float($geo): ?float {
if (!is_array($geo)) {
$geo = [$geo];
}
$result = .0;
$d = 1.0;
foreach ($geo as $component) {
$result += ($this->string2float($component) * $d);
$d /= self::COORDINATE_MODULO;
}
return $result;
}
return $result;
}
/**
* @param string $value
* @return float|null
*/
private function string2float(string $value): ?float
{
$result = null;
$value = trim($value, '/');
if (str_contains($value, '/')) {
$value = array_map('intval', explode('/', $value));
if (0 != $value[1]) {
$result = $value[0] / $value[1];
}
} else {
$result = floatval($value);
}
return $result;
/**
* @param string $value
* @return float|null
*/
private function string2float(string $value): ?float {
$result = null;
$value = trim($value, '/');
if (str_contains($value, '/')) {
$value = array_map('intval', explode('/', $value));
if ($value[1] != 0) {
$result = $value[0] / $value[1];
}
} else {
$result = floatval($value);
}
return $result;
}
}
/**
* @return bool
*/
public function isZeroIsland(): bool
{
return .0 === $this->latitude() && .0 === $this->longitude();
}
/**
* @return bool
*/
public function isZeroIsland(): bool {
return $this->latitude() === .0 && $this->longitude() === .0;
}
/**
* @return float
*/
public function latitude(int $precision = 6): ?float
{
return $this->latitude === null ? null : round($this->latitude,$precision);
}
/**
* @return float
*/
public function latitude(int $precision = 6): ?float {
return $this->latitude === null ? null : round($this->latitude, $precision);
}
/**
* @return float
*/
public function longitude(int $precision = 6): ?float
{
return $this->longitude === null ? null : round($this->longitude,$precision);
}
/**
* @return float
*/
public function longitude(int $precision = 6): ?float {
return $this->longitude === null ? null : round($this->longitude, $precision);
}
/**
* @param string|null $format
* @return string|int|null
*/
public function timestamp(?string $format = 'Y-m-d H:i:s')
{
$result = $this->timestamp;
if ($this->timestamp !== null && $format) {
$result = date($format, $this->timestamp);
}
return $result;
}
/**
* @param string|null $format
* @return string|int|null
*/
public function timestamp(?string $format = 'Y-m-d H:i:s') {
$result = $this->timestamp;
if ($this->timestamp !== null && $format) {
$result = date($format, $this->timestamp);
}
return $result;
}
/**
* If someone wants to have it as a json object
*
* @return array
*/
public function jsonSerialize()
{
return [
'lat'=>$this->lat,
'lng'=>$this->lng,
'dateTaken'=>$this->dateTaken
];
}
/**
* If someone wants to have it as a json object
*
* @return array
*/
public function jsonSerialize() {
return [
'lat' => $this->lat,
'lng' => $this->lng,
'dateTaken' => $this->dateTaken
];
}
/**
* Magic getter function
*
* @param $name
* @return float|int|string|null
*/
public function __get($name){
$value = null;
switch($name){
case 'lat':
$value = $this->latitude();
break;
case 'lng':
$value = $this->longitude();
break;
case 'dateTaken':
$value = $this->timestamp(null);
break;
}
return $value;
}
/**
* Magic getter function
*
* @param $name
* @return float|int|string|null
*/
public function __get($name) {
$value = null;
switch ($name) {
case 'lat':
$value = $this->latitude();
break;
case 'lng':
$value = $this->longitude();
break;
case 'dateTaken':
$value = $this->timestamp(null);
break;
}
return $value;
}
}

Просмотреть файл

@ -30,7 +30,7 @@ namespace OCA\Maps\Helper;
* @return string
*/
function remove_utf8_bom(string $text): string {
$bom = pack('H*','EFBBBF');
$bom = pack('H*', 'EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}

Просмотреть файл

@ -13,111 +13,108 @@
namespace OCA\Maps\Hooks;
use OC\Files\Filesystem;
use OC\Files\View;
use OCP\Files\FileInfo;
use OCP\ILogger;
use OCP\Files\Node;
use OCP\Files\IRootFolder;
use OCP\Lock\ILockingProvider;
use OCP\Util;
use OCP\Share;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\Service\TracksService;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\ILogger;
use OCP\Lock\ILockingProvider;
use OCP\Share;
use OCP\Util;
/**
* Handles files events
*/
class FileHooks {
private $photofilesService;
private $tracksService;
private $photofilesService;
private $tracksService;
private $logger;
private $logger;
private $root;
private $root;
private ILockingProvider $lockingProvider;
public function __construct(IRootFolder $root, PhotofilesService $photofilesService, TracksService $tracksService,
ILogger $logger, $appName, ILockingProvider $lockingProvider) {
$this->photofilesService = $photofilesService;
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->root = $root;
$this->lockingProvider = $lockingProvider;
}
ILogger $logger, $appName, ILockingProvider $lockingProvider) {
$this->photofilesService = $photofilesService;
$this->tracksService = $tracksService;
$this->logger = $logger;
$this->root = $root;
$this->lockingProvider = $lockingProvider;
}
public function register() {
$fileWriteCallback = function(\OCP\Files\Node $node) {
if ($this->isUserNode($node) && $node->getSize() > 0) {
$path = $node->getPath();
if (!$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_SHARED)
and !$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_EXCLUSIVE)
) {
$isPhoto = $this->photofilesService->addByFile($node);
if (!$isPhoto) {
$this->tracksService->safeAddByFile($node);
}
}
}
};
$this->root->listen('\OC\Files', 'postWrite', $fileWriteCallback);
public function register() {
$fileWriteCallback = function (\OCP\Files\Node $node) {
if ($this->isUserNode($node) && $node->getSize() > 0) {
$path = $node->getPath();
if (!$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_SHARED)
and !$this->lockingProvider->isLocked($path, ILockingProvider::LOCK_EXCLUSIVE)
) {
$isPhoto = $this->photofilesService->addByFile($node);
if (!$isPhoto) {
$this->tracksService->safeAddByFile($node);
}
}
}
};
$this->root->listen('\OC\Files', 'postWrite', $fileWriteCallback);
$fileDeletionCallback = function(\OCP\Files\Node $node) {
if ($this->isUserNode($node)) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->photofilesService->deleteByFolder($node);
$this->tracksService->deleteByFolder($node);
} else {
$this->photofilesService->deleteByFile($node);
$this->tracksService->deleteByFile($node);
}
}
};
$this->root->listen('\OC\Files', 'preDelete', $fileDeletionCallback);
$fileDeletionCallback = function (\OCP\Files\Node $node) {
if ($this->isUserNode($node)) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->photofilesService->deleteByFolder($node);
$this->tracksService->deleteByFolder($node);
} else {
$this->photofilesService->deleteByFile($node);
$this->tracksService->deleteByFile($node);
}
}
};
$this->root->listen('\OC\Files', 'preDelete', $fileDeletionCallback);
// this one is triggered when restoring a version of a file
// and NOT when it's created so we can use it for updating coordinates in DB
$this->root->listen('\OC\Files', 'postTouch', function(\OCP\Files\Node $node) {
if ($this->isUserNode($node) and $node->getType() === FileInfo::TYPE_FILE) {
$this->photofilesService->updateByFile($node);
// nothing to update on tracks, metadata will be regenerated when getting content if etag has changed
}
});
// this one is triggered when restoring a version of a file
// and NOT when it's created so we can use it for updating coordinates in DB
$this->root->listen('\OC\Files', 'postTouch', function (\OCP\Files\Node $node) {
if ($this->isUserNode($node) and $node->getType() === FileInfo::TYPE_FILE) {
$this->photofilesService->updateByFile($node);
// nothing to update on tracks, metadata will be regenerated when getting content if etag has changed
}
});
// move file: delete then add it again in DB to be sure it's there for all users with access to target file
$this->root->listen('\OC\Files', 'postRename', function(\OCP\Files\Node $source, \OCP\Files\Node $target) {
if ($this->isUserNode($target)) {
if ($target->getType() === FileInfo::TYPE_FILE) {
// if moved (parents are different) => update DB with access list
if ($source->getParent()->getId() !== $target->getParent()->getId()) {
// move file: delete then add it again in DB to be sure it's there for all users with access to target file
$this->root->listen('\OC\Files', 'postRename', function (\OCP\Files\Node $source, \OCP\Files\Node $target) {
if ($this->isUserNode($target)) {
if ($target->getType() === FileInfo::TYPE_FILE) {
// if moved (parents are different) => update DB with access list
if ($source->getParent()->getId() !== $target->getParent()->getId()) {
// we renamed therefore target and source are identical
$this->photofilesService->deleteByFile($target);
$this->photofilesService->addByFile($target);
// tracks: nothing to do here because we use fileID
}
}
elseif ($target->getType() === FileInfo::TYPE_FOLDER) {
if ($source->getParent()->getId() !== $target->getParent()->getId()) {
$this->photofilesService->deleteByFile($target);
$this->photofilesService->addByFile($target);
// tracks: nothing to do here because we use fileID
}
} elseif ($target->getType() === FileInfo::TYPE_FOLDER) {
if ($source->getParent()->getId() !== $target->getParent()->getId()) {
// we renamed therefore target and source have the same childs.
$this->photofilesService->deleteByFolder($target);
$this->photofilesService->addByFolder($target);
// tracks: nothing to do here because we use fileID
}
}
}
});
$this->photofilesService->deleteByFolder($target);
$this->photofilesService->addByFolder($target);
// tracks: nothing to do here because we use fileID
}
}
}
});
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $this, 'restore');
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $this, 'restore');
// sharing hooks
Util::connectHook(\OCP\Share::class, 'post_shared', $this, 'postShare');
Util::connectHook(\OCP\Share::class, 'post_unshare', $this, 'postUnShare');
Util::connectHook(\OCP\Share::class, 'pre_unshare', $this, 'preUnShare');
}
// sharing hooks
Util::connectHook(\OCP\Share::class, 'post_shared', $this, 'postShare');
Util::connectHook(\OCP\Share::class, 'post_unshare', $this, 'postUnShare');
Util::connectHook(\OCP\Share::class, 'pre_unshare', $this, 'preUnShare');
}
public function postShare($params) {
public function postShare($params) {
if ($params['itemType'] === 'file') {
//$targetFilePath = $params['itemTarget'];
//$sourceUserId = $params['uidOwner'];
@ -127,10 +124,9 @@ class FileHooks {
return;
}
$file = array_shift($files);
$this->photofilesService->addByFile($file,);
$this->photofilesService->addByFile($file, );
$this->tracksService->safeAddByFile($file);
}
else if ($params['itemType'] === 'folder') {
} elseif ($params['itemType'] === 'folder') {
$dirId = $params['fileSource']; // or itemSource
$folders = $this->root->getById($dirId);
if (empty($folders)) {
@ -140,55 +136,55 @@ class FileHooks {
$this->photofilesService->addByFolder($folder);
$this->tracksService->safeAddByFolder($folder);
}
}
}
public function postUnShare($params) {
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'file') {
$targetUserId = $params['shareWith'];
$fileId = $params['fileSource']; // or itemSource
$this->photofilesService->deleteByFileIdUserId($fileId, $targetUserId);
$this->tracksService->safeDeleteByFileIdUserId($fileId, $targetUserId);
}
}
}
public function postUnShare($params) {
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'file') {
$targetUserId = $params['shareWith'];
$fileId = $params['fileSource']; // or itemSource
$this->photofilesService->deleteByFileIdUserId($fileId, $targetUserId);
$this->tracksService->safeDeleteByFileIdUserId($fileId, $targetUserId);
}
}
}
public function preUnShare($params) {
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'folder') {
$targetUserId = $params['shareWith'];
$dirId = $params['fileSource']; // or itemSource
$this->photofilesService->deleteByFolderIdUserId($dirId, $targetUserId);
$this->tracksService->safeDeleteByFolderIdUserId($dirId, $targetUserId);
}
}
}
public function preUnShare($params) {
if ($params['shareType'] === Share::SHARE_TYPE_USER) {
if ($params['itemType'] === 'folder') {
$targetUserId = $params['shareWith'];
$dirId = $params['fileSource']; // or itemSource
$this->photofilesService->deleteByFolderIdUserId($dirId, $targetUserId);
$this->tracksService->safeDeleteByFolderIdUserId($dirId, $targetUserId);
}
}
}
public function restore($params) {
$node = $this->getNodeForPath($params['filePath']);
if ($this->isUserNode($node)) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->photofilesService->addByFolder($node);
$this->tracksService->safeAddByFolder($node);
} else {
$this->photofilesService->addByFile($node);
$this->tracksService->safeAddByFile($node);
}
}
}
public function restore($params) {
$node = $this->getNodeForPath($params['filePath']);
if ($this->isUserNode($node)) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->photofilesService->addByFolder($node);
$this->tracksService->safeAddByFolder($node);
} else {
$this->photofilesService->addByFile($node);
$this->tracksService->safeAddByFile($node);
}
}
}
private function getNodeForPath($path) {
$user = \OC::$server->getUserSession()->getUser();
$fullPath = Filesystem::normalizePath('/' . $user->getUID() . '/files/' . $path);
return $this->root->get($fullPath);
}
private function getNodeForPath($path) {
$user = \OC::$server->getUserSession()->getUser();
$fullPath = Filesystem::normalizePath('/' . $user->getUID() . '/files/' . $path);
return $this->root->get($fullPath);
}
/**
* Ugly Hack, find API way to check if file is added by user.
*/
private function isUserNode(\OCP\Files\Node $node) {
//return strpos($node->getStorage()->getId(), "home::", 0) === 0;
return $node->getStorage()->instanceOfStorage('\OC\Files\Storage\Home');
}
/**
* Ugly Hack, find API way to check if file is added by user.
*/
private function isUserNode(\OCP\Files\Node $node) {
//return strpos($node->getStorage()->getId(), "home::", 0) === 0;
return $node->getStorage()->instanceOfStorage('\OC\Files\Storage\Home');
}
}

Просмотреть файл

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace OCA\Maps\Listener;
use OCP\ILogger;
use OCA\DAV\Events\CardCreatedEvent;
use OCA\Maps\Service\AddressService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
class CardCreatedListener implements IEventListener {
@ -50,7 +50,7 @@ class CardCreatedListener implements IEventListener {
return;
}
$cData = $event->getCardData();
$cUri = $cData["uri"];
$this->addressService->scheduleVCardForLookup($cData["carddata"], $cUri);
$cUri = $cData['uri'];
$this->addressService->scheduleVCardForLookup($cData['carddata'], $cUri);
}
}

Просмотреть файл

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace OCA\Maps\Listener;
use OCP\ILogger;
use OCA\DAV\Events\CardDeletedEvent;
use OCA\Maps\Service\AddressService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
class CardDeletedListener implements IEventListener {
@ -50,7 +50,7 @@ class CardDeletedListener implements IEventListener {
return;
}
$cData = $event->getCardData();
$cUri = $cData["uri"];
$cUri = $cData['uri'];
$this->addressService->deleteDBContactAddresses($cUri);
}
}

Просмотреть файл

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace OCA\Maps\Listener;
use OCP\ILogger;
use OCA\DAV\Events\CardUpdatedEvent;
use OCA\Maps\Service\AddressService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
class CardUpdatedListener implements IEventListener {
@ -50,7 +50,7 @@ class CardUpdatedListener implements IEventListener {
return;
}
$cData = $event->getCardData();
$cUri = $cData["uri"];
$this->addressService->scheduleVCardForLookup($cData["carddata"], $cUri);
$cUri = $cData['uri'];
$this->addressService->scheduleVCardForLookup($cData['carddata'], $cUri);
}
}

Просмотреть файл

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace OCA\Maps\Listener;
use OCP\ILogger;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

Просмотреть файл

@ -23,19 +23,17 @@
namespace OCA\Maps\Migration;
use OCA\Maps\BackgroundJob\LaunchUsersInstallScanJob;
use OCP\BackgroundJob\IJobList;
use OCP\Encryption\IManager;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use \OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\Share;
use \OCA\Maps\BackgroundJob\LaunchUsersInstallScanJob;
use OCP\Migration\IRepairStep;
/**
* Class InstallScan
@ -47,16 +45,16 @@ class InstallScan implements IRepairStep {
/** @var IDBConnection */
private $connection;
/** @var IConfig */
/** @var IConfig */
private $config;
public function __construct(IDBConnection $connection,
IConfig $config,
IUserManager $userManager,
IJobList $jobList,
IManager $encryptionManager
) {
IConfig $config,
IUserManager $userManager,
IJobList $jobList,
IManager $encryptionManager
) {
$this->connection = $connection;
$this->config = $config;
$this->jobList = $jobList;

Просмотреть файл

@ -6,24 +6,20 @@ use OCP\Files\IMimeTypeLoader;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
class RegisterMimeType implements IRepairStep
{
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
class RegisterMimeType implements IRepairStep {
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
protected IMimeTypeLoader $mimeTypeLoader;
public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
public function __construct(IMimeTypeLoader $mimeTypeLoader) {
$this->mimeTypeLoader = $mimeTypeLoader;
}
public function getName()
{
public function getName() {
return 'Register Maps MIME types"';
}
private function registerForExistingFiles()
{
private function registerForExistingFiles() {
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps');
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId);
@ -40,15 +36,14 @@ class RegisterMimeType implements IRepairStep
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId);
}
private function registerForNewFiles()
{
private function registerForNewFiles() {
$mapping = [
'maps' => ['application/x-nextcloud-maps'],
'noindex' => ['application/x-nextcloud-noindex'],
'nomedia' => ['application/x-nextcloud-nomedia'],
'noimage' => ['application/x-nextcloud-noimage'],
'notrack' => ['application/x-nextcloud-maps-notrack'],
];
];
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING;
if (file_exists($mappingFile)) {
@ -61,8 +56,7 @@ class RegisterMimeType implements IRepairStep
file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
public function run(IOutput $output)
{
public function run(IOutput $output) {
$output->info('Registering the mimetype...');
// Register the mime type for existing files

Просмотреть файл

@ -6,24 +6,20 @@ use OCP\Files\IMimeTypeLoader;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
class UnregisterMimeType implements IRepairStep
{
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
class UnregisterMimeType implements IRepairStep {
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
protected IMimeTypeLoader $mimeTypeLoader;
public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
public function __construct(IMimeTypeLoader $mimeTypeLoader) {
$this->mimeTypeLoader = $mimeTypeLoader;
}
public function getName()
{
public function getName() {
return 'Register Maps MIME types"';
}
private function unregisterForExistingFiles()
{
private function unregisterForExistingFiles() {
$mimeTypeId = $this->mimeTypeLoader->getId('application/octet-stream');
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId);
$this->mimeTypeLoader->updateFilecache('noindex', $mimeTypeId);
@ -32,8 +28,7 @@ class UnregisterMimeType implements IRepairStep
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId);
}
private function unregisterForNewFiles()
{
private function unregisterForNewFiles() {
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING;
if (file_exists($mappingFile)) {
@ -51,8 +46,7 @@ class UnregisterMimeType implements IRepairStep
}
}
public function run(IOutput $output)
{
public function run(IOutput $output) {
$output->info('Unregistering the mimetype...');
// Register the mime type for existing files
@ -64,4 +58,3 @@ class UnregisterMimeType implements IRepairStep
$output->info('The mimetype was successfully unregistered.');
}
}

Просмотреть файл

@ -6,8 +6,8 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,8 +6,8 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -14,55 +14,55 @@ use OCP\Migration\SimpleMigrationStep;
*/
class Version000106Date20191118221134 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if (!$schema->hasTable('maps_favorite_shares')) {
$table = $schema->createTable('maps_favorite_shares');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 41,
]);
$table->addColumn('owner', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('category', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('token', 'string', [
'notnull' => true,
'length' => 64,
]);
if (!$schema->hasTable('maps_favorite_shares')) {
$table = $schema->createTable('maps_favorite_shares');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 41,
]);
$table->addColumn('owner', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('category', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('token', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->setPrimaryKey(['id']);
}
$table->setPrimaryKey(['id']);
}
return $schema;
}
return $schema;
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
}

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -6,10 +6,9 @@ namespace OCA\Maps\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use OCP\IDBConnection;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!

Просмотреть файл

@ -12,16 +12,16 @@
namespace OCA\Maps\Service;
use \OCA\Maps\BackgroundJob\LookupMissingGeoJob;
use OCA\Maps\BackgroundJob\LookupMissingGeoJob;
use OCP\BackgroundJob\IJobList;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IAppData;
use OCP\ICacheFactory;
use \OCP\ILogger;
use \OCP\IDBConnection;
use \OCP\BackgroundJob\IJobList;
use \OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IMemcache;
use \Sabre\VObject\Reader;
use \OCP\Files\IAppData;
use OpenLocationCode\OpenLocationCode;
use Sabre\VObject\Reader;
/**
* Class AddressService
@ -39,303 +39,301 @@ use OpenLocationCode\OpenLocationCode;
* @package OCA\Maps\Service
*/
class AddressService {
private $qb;
private $dbconnection;
private $logger;
private $jobList;
private $appData;
private $qb;
private $dbconnection;
private $logger;
private $jobList;
private $appData;
/** @var IMemcache */
private $memcache;
public function __construct(ICacheFactory $cacheFactory, ILogger $logger, IJobList $jobList,
IAppData $appData, IDBConnection $dbconnection) {
$this->dbconnection = $dbconnection;
$this->qb = $dbconnection->getQueryBuilder();
$this->memcache = $cacheFactory->createLocal('maps');
$this->logger = $logger;
$this->jobList = $jobList;
$this->appData = $appData;
}
public function __construct(ICacheFactory $cacheFactory, ILogger $logger, IJobList $jobList,
IAppData $appData, IDBConnection $dbconnection) {
$this->dbconnection = $dbconnection;
$this->qb = $dbconnection->getQueryBuilder();
$this->memcache = $cacheFactory->createLocal('maps');
$this->logger = $logger;
$this->jobList = $jobList;
$this->appData = $appData;
}
// converts the address to geo lat;lon
public function addressToGeo($adr, $uri): string {
$geo = $this->lookupAddress($adr, $uri);
return strval($geo[0]).';'.strval($geo[1]);
}
// converts the address to geo lat;lon
public function addressToGeo($adr, $uri): string {
$geo = $this->lookupAddress($adr, $uri);
return strval($geo[0]).';'.strval($geo[1]);
}
/**
* Safely looks up an adr string
* First: Checks if the adress is known and in the db
* Uses this geo if it was looked up externally
* Look's it up if it was not looked up
* @param $adr
* @param $uri ressource identifier (contact URI for example)
* @return array($lat,$lng,$lookedUp)
*/
public function lookupAddress($adr, $uri): array {
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->select('id', 'lat', 'lng', 'looked_up')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
$lat = null;
$lng = null;
$inDb = false;
while ($row = $req->fetch()) {
if ($row['looked_up']) {
$id = $row['id'];
$lat = $row['lat'];
$lng = $row['lng'];
$lookedUp = false;
$inDb = true;
}
else {
$id = $row['id'];
// if it's in the DB but not yet looked up, we can do it now
// we first check if this address was already looked up
$geo = $this->lookupAddressInternal($adr);
// if not, ask external service
if (!$geo[2]) {
$geo = $this->lookupAddressExternal($adr);
}
$lat = $geo[0];
$lng = $geo[1];
$lookedUp = $geo[2];
$inDb = true;
}
break;
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
// if it's still not in the DB, it means the lookup did not happen yet
// so we can schedule it for later
if (!$inDb) {
$foo = $this->scheduleForLookup($adr, $uri);
$id = $foo[0];
$lat = $foo[1];
$lng = $foo[2];
$lookedUp = $foo[3];
/**
* Safely looks up an adr string
* First: Checks if the adress is known and in the db
* Uses this geo if it was looked up externally
* Look's it up if it was not looked up
* @param $adr
* @param $uri ressource identifier (contact URI for example)
* @return array($lat,$lng,$lookedUp)
*/
public function lookupAddress($adr, $uri): array {
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->select('id', 'lat', 'lng', 'looked_up')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
$lat = null;
$lng = null;
$inDb = false;
while ($row = $req->fetch()) {
if ($row['looked_up']) {
$id = $row['id'];
$lat = $row['lat'];
$lng = $row['lng'];
$lookedUp = false;
$inDb = true;
} else {
$id = $row['id'];
// if it's in the DB but not yet looked up, we can do it now
// we first check if this address was already looked up
$geo = $this->lookupAddressInternal($adr);
// if not, ask external service
if (!$geo[2]) {
$geo = $this->lookupAddressExternal($adr);
}
$lat = $geo[0];
$lng = $geo[1];
$lookedUp = $geo[2];
$inDb = true;
}
break;
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
// if it's still not in the DB, it means the lookup did not happen yet
// so we can schedule it for later
if (!$inDb) {
$foo = $this->scheduleForLookup($adr, $uri);
$id = $foo[0];
$lat = $foo[1];
$lng = $foo[2];
$lookedUp = $foo[3];
}
else {
if ($lookedUp) {
$this->qb->update('maps_address_geo')
->set('lat', $qb->createNamedParameter($lat, IQueryBuilder::PARAM_STR))
->set('lng', $qb->createNamedParameter($lng, IQueryBuilder::PARAM_STR))
->set('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
->set('looked_up', $qb->createNamedParameter($lookedUp, IQueryBuilder::PARAM_BOOL))
->where($this->qb->expr()->eq('id', $this->qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
$qb = $this->qb->resetQueryParts();
}
}
} else {
if ($lookedUp) {
$this->qb->update('maps_address_geo')
->set('lat', $qb->createNamedParameter($lat, IQueryBuilder::PARAM_STR))
->set('lng', $qb->createNamedParameter($lng, IQueryBuilder::PARAM_STR))
->set('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
->set('looked_up', $qb->createNamedParameter($lookedUp, IQueryBuilder::PARAM_BOOL))
->where($this->qb->expr()->eq('id', $this->qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
$qb = $this->qb->resetQueryParts();
}
}
return [$lat, $lng, $lookedUp];
}
return [$lat, $lng, $lookedUp];
}
private function lookupAddressInternal($adr): array {
$res = [null, null, False];
private function lookupAddressInternal($adr): array {
$res = [null, null, false];
if (OpenLocationCode::isFull($adr)) {
$decoded = OpenLocationCode::decode($adr);
$res[0] = $decoded['latitudeCenter'];
$res[1] = $decoded['longitudeCenter'];
$res[2] = True;
return $res;
}
if (OpenLocationCode::isFull($adr)) {
$decoded = OpenLocationCode::decode($adr);
$res[0] = $decoded['latitudeCenter'];
$res[1] = $decoded['longitudeCenter'];
$res[2] = true;
return $res;
}
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->select('lat', 'lng')
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(True, IQueryBuilder::PARAM_BOOL)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
while ($row = $req->fetch()) {
$res[0] = $row['lat'];
$res[1] = $row['lng'];
$res[2] = True;
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
$this->qb->select('lat', 'lng')
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
while ($row = $req->fetch()) {
$res[0] = $row['lat'];
$res[1] = $row['lng'];
$res[2] = true;
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
return $res;
}
return $res;
}
// looks up the address on external provider returns lat, lon, lookupstate
// do lookup only if last one occured more than one second ago
private function lookupAddressExternal($adr): array {
if (time() - intval($this->memcache->get('lastAddressLookup')) >= 1) {
$opts = [
'http' => [
'method' => 'GET',
'user_agent' => 'Nextcloud Maps app',
]
];
$context = stream_context_create($opts);
// looks up the address on external provider returns lat, lon, lookupstate
// do lookup only if last one occured more than one second ago
private function lookupAddressExternal($adr): array {
if (time() - intval($this->memcache->get('lastAddressLookup')) >= 1) {
$opts = [
'http' => [
'method' => 'GET',
'user_agent' => 'Nextcloud Maps app',
]
];
$context = stream_context_create($opts);
// we get rid of "post office box" field
$splitted_adr = explode(';', $adr);
if (count($splitted_adr) > 2) {
array_shift($splitted_adr);
}
// we get rid of "post office box" field
$splitted_adr = explode(';', $adr);
if (count($splitted_adr) > 2) {
array_shift($splitted_adr);
}
// remove blank lines (#706)
$splitted_adr = array_filter(array_map('trim', $splitted_adr));
$query_adr = implode(', ', $splitted_adr);
// remove blank lines (#706)
$splitted_adr = array_filter(array_map('trim', $splitted_adr));
$query_adr = implode(', ', $splitted_adr);
$result_json = @file_get_contents(
'https://nominatim.openstreetmap.org/search.php?q='.urlencode($query_adr).'&format=json',
false,
$context
);
if ($result_json !== false) {
$result = \json_decode($result_json, true);
if (!(key_exists('request_failed', $result) AND $result['request_failed'])) {
$this->logger->debug('External looked up address: ' . $adr . ' with result' . print_r($result, true));
$this->memcache->set('lastAddressLookup', time());
$lat = null;
$lon = null;
foreach ($result as $addr) {
if (key_exists('lat', $addr) AND
key_exists('lon', $addr)
) {
if (is_null($lat) OR
(key_exists('class', $addr) AND
($addr['class'] == "building" OR $addr['class'] == "place"))) {
$lat = $addr['lat'];
$lon = $addr['lon'];
}
}
}
return [$lat, $lon, true];
}
}
$this->logger->debug('Externally looked failed');
}
return [null, null, False];
}
$result_json = @file_get_contents(
'https://nominatim.openstreetmap.org/search.php?q='.urlencode($query_adr).'&format=json',
false,
$context
);
if ($result_json !== false) {
$result = \json_decode($result_json, true);
if (!(key_exists('request_failed', $result) and $result['request_failed'])) {
$this->logger->debug('External looked up address: ' . $adr . ' with result' . print_r($result, true));
$this->memcache->set('lastAddressLookup', time());
$lat = null;
$lon = null;
foreach ($result as $addr) {
if (key_exists('lat', $addr) and
key_exists('lon', $addr)
) {
if (is_null($lat) or
(key_exists('class', $addr) and
($addr['class'] == 'building' or $addr['class'] == 'place'))) {
$lat = $addr['lat'];
$lon = $addr['lon'];
}
}
}
return [$lat, $lon, true];
}
}
$this->logger->debug('Externally looked failed');
}
return [null, null, false];
}
// launch lookup for all addresses of the vCard
public function scheduleVCardForLookup($cardData, $cardUri) {
$vCard = Reader::read($cardData);
// launch lookup for all addresses of the vCard
public function scheduleVCardForLookup($cardData, $cardUri) {
$vCard = Reader::read($cardData);
$this->cleanUpDBContactAddresses($vCard, $cardUri);
$this->cleanUpDBContactAddresses($vCard, $cardUri);
foreach ($vCard->children() as $property) {
if ($property->name === 'ADR') {
$adr = $property->getValue();
if ($adr !== ';;;;;;') {
$this->lookupAddress($property->getValue(), $cardUri);
}
}
}
}
foreach ($vCard->children() as $property) {
if ($property->name === 'ADR') {
$adr = $property->getValue();
if ($adr !== ';;;;;;') {
$this->lookupAddress($property->getValue(), $cardUri);
}
}
}
}
private function cleanUpDBContactAddresses($vCard, $uri) {
$qb = $this->qb;
// get all vcard addresses
$vCardAddresses = [];
foreach ($vCard->children() as $property) {
if ($property->name === 'ADR') {
$adr = $property->getValue();
array_push($vCardAddresses, $adr);
}
}
// check which addresses from DB is not in the vCard anymore
$adrIdToDelete = [];
$this->qb->select('id', 'adr')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
while ($row = $req->fetch()) {
if (!in_array($row['adr'], $vCardAddresses)) {
array_push($adrIdToDelete, $row['id']);
}
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
private function cleanUpDBContactAddresses($vCard, $uri) {
$qb = $this->qb;
// get all vcard addresses
$vCardAddresses = [];
foreach ($vCard->children() as $property) {
if ($property->name === 'ADR') {
$adr = $property->getValue();
array_push($vCardAddresses, $adr);
}
}
// check which addresses from DB is not in the vCard anymore
$adrIdToDelete = [];
$this->qb->select('id', 'adr')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
while ($row = $req->fetch()) {
if (!in_array($row['adr'], $vCardAddresses)) {
array_push($adrIdToDelete, $row['id']);
}
}
$req->closeCursor();
$qb = $this->qb->resetQueryParts();
foreach ($adrIdToDelete as $id) {
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
}
foreach ($adrIdToDelete as $id) {
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
}
public function deleteDBContactAddresses($uri) {
$qb = $this->qb;
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
public function deleteDBContactAddresses($uri) {
$qb = $this->qb;
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
// schedules the address for an external lookup
private function scheduleForLookup($adr, $uri): array {
$geo = $this->lookupAddressInternal($adr);
// if not found internally, ask external service
if (!$geo[2]) {
$geo = $this->lookupAddressExternal($adr);
}
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->insert('maps_address_geo')
->values([
'adr' => $this->qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm' => $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri' => $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat' => $this->qb->createNamedParameter($geo[0], IQueryBuilder::PARAM_STR),
'lng' => $this->qb->createNamedParameter($geo[1], IQueryBuilder::PARAM_STR),
'looked_up' => $this->qb->createNamedParameter($geo[2], IQueryBuilder::PARAM_BOOL),
]);
$req = $this->qb->execute();
$id = $this->qb->getLastInsertId();
$qb = $this->qb->resetQueryParts();
if (!$geo[2]) {
$this->jobList->add(LookupMissingGeoJob::class, []);
}
return [$id, $geo[0], $geo[1], $geo[2]];
}
// schedules the address for an external lookup
private function scheduleForLookup($adr, $uri): array {
$geo = $this->lookupAddressInternal($adr);
// if not found internally, ask external service
if (!$geo[2]) {
$geo = $this->lookupAddressExternal($adr);
}
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->insert('maps_address_geo')
->values([
'adr' => $this->qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm' => $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri' => $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat' => $this->qb->createNamedParameter($geo[0], IQueryBuilder::PARAM_STR),
'lng' => $this->qb->createNamedParameter($geo[1], IQueryBuilder::PARAM_STR),
'looked_up' => $this->qb->createNamedParameter($geo[2], IQueryBuilder::PARAM_BOOL),
]);
$req = $this->qb->execute();
$id = $this->qb->getLastInsertId();
$qb = $this->qb->resetQueryParts();
if (!$geo[2]) {
$this->jobList->add(LookupMissingGeoJob::class, []);
}
return [$id, $geo[0], $geo[1], $geo[2]];
}
// looks up the geo information which have not been looked up
// this is called by the Cron job
public function lookupMissingGeo($max=200):bool {
// stores if all addresses where looked up
$lookedUpAll = true;
$this->qb->select('adr', 'object_uri')
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(False, IQueryBuilder::PARAM_BOOL)))
->setMaxResults($max);
$req = $this->qb->execute();
$result = $req->fetchAll();
$req->closeCursor();
$i = 0;
foreach ($result as $row) {
$i++;
$geo = $this->lookupAddress($row['adr'], $row['object_uri']);
// lookup failed
if (!$geo[2]){
$lookedUpAll = false;
}
\sleep(1);
\usleep(\rand(100,100000));
}
// not all addresses where loaded from database
if ($i === $max){
$lookedUpAll = false;
}
if ($lookedUpAll){
$this->logger->debug('Successfully looked up all addresses during cron job');
} else {
$this->logger->debug('Failed to look up all addresses during cron job');
}
return $lookedUpAll;
}
// looks up the geo information which have not been looked up
// this is called by the Cron job
public function lookupMissingGeo($max = 200):bool {
// stores if all addresses where looked up
$lookedUpAll = true;
$this->qb->select('adr', 'object_uri')
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
->setMaxResults($max);
$req = $this->qb->execute();
$result = $req->fetchAll();
$req->closeCursor();
$i = 0;
foreach ($result as $row) {
$i++;
$geo = $this->lookupAddress($row['adr'], $row['object_uri']);
// lookup failed
if (!$geo[2]) {
$lookedUpAll = false;
}
\sleep(1);
\usleep(\rand(100, 100000));
}
// not all addresses where loaded from database
if ($i === $max) {
$lookedUpAll = false;
}
if ($lookedUpAll) {
$this->logger->debug('Successfully looked up all addresses during cron job');
} else {
$this->logger->debug('Failed to look up all addresses during cron job');
}
return $lookedUpAll;
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -16,77 +16,71 @@ use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\User\NoUserException;
use OCA\Maps\DB\GeophotoMapper;
use OCP\DB\Exception;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\ICacheFactory;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use OCP\Files\Folder;
use OCP\IPreview;
use OCP\ILogger;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\DB\Geophoto;
use OCA\Maps\DB\GeophotoMapper;
use OCA\Maps\Service\TracksService;
use OCA\Maps\Service\DevicesService;
use OCP\IPreview;
class GeophotoService {
private $l10n;
private $root;
private $photoMapper;
private $logger;
private $preview;
private $tracksService;
private $timeorderedPointSets;
private $devicesService;
private $l10n;
private $root;
private $photoMapper;
private $logger;
private $preview;
private $tracksService;
private $timeorderedPointSets;
private $devicesService;
private $cacheFactory;
private $userId;
private \OCP\ICache $photosCache;
private \OCP\ICache $timeOrderedPointSetsCache;
private \OCP\ICache $backgroundJobCache;
public function __construct (ILogger $logger,
IRootFolder $root,
IL10N $l10n,
GeophotoMapper $photoMapper,
IPreview $preview,
TracksService $tracksService,
DevicesService $devicesService,
ICacheFactory $cacheFactory,
$userId) {
$this->root = $root;
$this->l10n = $l10n;
$this->photoMapper = $photoMapper;
$this->logger = $logger;
$this->preview = $preview;
$this->tracksService = $tracksService;
$this->timeorderedPointSets = null;
$this->userId = $userId;
$this->devicesService = $devicesService;
public function __construct(ILogger $logger,
IRootFolder $root,
IL10N $l10n,
GeophotoMapper $photoMapper,
IPreview $preview,
TracksService $tracksService,
DevicesService $devicesService,
ICacheFactory $cacheFactory,
$userId) {
$this->root = $root;
$this->l10n = $l10n;
$this->photoMapper = $photoMapper;
$this->logger = $logger;
$this->preview = $preview;
$this->tracksService = $tracksService;
$this->timeorderedPointSets = null;
$this->userId = $userId;
$this->devicesService = $devicesService;
$this->cacheFactory = $cacheFactory;
$this->photosCache = $this->cacheFactory->createDistributed('maps:photos');
$this->timeOrderedPointSetsCache = $this->cacheFactory->createDistributed('maps:time-ordered-point-sets');
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}
}
/**
* @param string $userId
* @return bool
*/
public function clearCache(string $userId=''): bool {
$a = $this->photosCache->clear($userId);
$b = $this->timeOrderedPointSetsCache->clear($userId);
$c = $this->backgroundJobCache->clear('recentlyAdded:'.$userId);
$d = $this->backgroundJobCache->clear('recentlyUpdated:'.$userId);
return $a and $b and $c and $d;
public function clearCache(string $userId = ''): bool {
$a = $this->photosCache->clear($userId);
$b = $this->timeOrderedPointSetsCache->clear($userId);
$c = $this->backgroundJobCache->clear('recentlyAdded:'.$userId);
$d = $this->backgroundJobCache->clear('recentlyUpdated:'.$userId);
return $a and $b and $c and $d;
}
/**
@ -101,77 +95,77 @@ class GeophotoService {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getAll(string $userId, $folder=null, bool $respectNomediaAndNoimage=true, bool $hideImagesOnCustomMaps=false, bool $hideImagesInMapsFolder=true): array {
$userFolder = $this->getFolderForUser($userId);
if (is_null($folder)) {
$folder = $userFolder;
}
$key = $userId . ':' . $userFolder->getRelativePath($folder->getPath()) . ':' . (string) $respectNomediaAndNoimage . ':' . (string) $hideImagesOnCustomMaps . ':' . (string) $hideImagesInMapsFolder;
$filesById = $this->photosCache->get($key);
if ($filesById === null) {
$ignoredPaths = $respectNomediaAndNoimage ? $this->getIgnoredPaths($userId, $folder, $hideImagesOnCustomMaps) : [];
if ($hideImagesInMapsFolder) {
$ignoredPaths[] = "/Maps";
}
$photoEntities = $this->photoMapper->findAll($userId);
public function getAll(string $userId, $folder = null, bool $respectNomediaAndNoimage = true, bool $hideImagesOnCustomMaps = false, bool $hideImagesInMapsFolder = true): array {
$userFolder = $this->getFolderForUser($userId);
if (is_null($folder)) {
$folder = $userFolder;
}
$key = $userId . ':' . $userFolder->getRelativePath($folder->getPath()) . ':' . (string)$respectNomediaAndNoimage . ':' . (string)$hideImagesOnCustomMaps . ':' . (string)$hideImagesInMapsFolder;
$filesById = $this->photosCache->get($key);
if ($filesById === null) {
$ignoredPaths = $respectNomediaAndNoimage ? $this->getIgnoredPaths($userId, $folder, $hideImagesOnCustomMaps) : [];
if ($hideImagesInMapsFolder) {
$ignoredPaths[] = '/Maps';
}
$photoEntities = $this->photoMapper->findAll($userId);
$filesById = [];
$cache = $folder->getStorage()->getCache();
$previewEnableMimetypes = $this->getPreviewEnabledMimetypes();
foreach ($photoEntities as $photoEntity) {
$cacheEntry = $cache->get($photoEntity->getFileId());
if ($cacheEntry) {
// this path is relative to owner's storage
//$path = $cacheEntry->getPath();
//but we want it relative to current user's storage
$files = $folder->getById($photoEntity->getFileId());
if (empty($files)) {
continue;
}
$file = array_shift($files);
if ($file === null) {
continue;
}
$path = $userFolder->getRelativePath($file->getPath());
$isIgnored = false;
foreach ($ignoredPaths as $ignoredPath) {
if (str_starts_with($path, $ignoredPath)) {
$isIgnored = true;
break;
}
}
if (!$isIgnored) {
$isRoot = $file === $userFolder;
$filesById = [];
$cache = $folder->getStorage()->getCache();
$previewEnableMimetypes = $this->getPreviewEnabledMimetypes();
foreach ($photoEntities as $photoEntity) {
$cacheEntry = $cache->get($photoEntity->getFileId());
if ($cacheEntry) {
// this path is relative to owner's storage
//$path = $cacheEntry->getPath();
//but we want it relative to current user's storage
$files = $folder->getById($photoEntity->getFileId());
if (empty($files)) {
continue;
}
$file = array_shift($files);
if ($file === null) {
continue;
}
$path = $userFolder->getRelativePath($file->getPath());
$isIgnored = false;
foreach ($ignoredPaths as $ignoredPath) {
if (str_starts_with($path, $ignoredPath)) {
$isIgnored = true;
break;
}
}
if (!$isIgnored) {
$isRoot = $file === $userFolder;
$file_object = new \stdClass();
$file_object->fileId = $photoEntity->getFileId();
$file_object->fileid = $file_object->fileId;
$file_object->lat = $photoEntity->getLat();
$file_object->lng = $photoEntity->getLng();
$file_object->dateTaken = $photoEntity->getDateTaken() ?? \time();
$file_object->basename = $isRoot ? '' : $file->getName();
$file_object->filename = $this->normalizePath($path);
$file_object->etag = $cacheEntry->getEtag();
//Not working for NC21 as Viewer requires String representation of permissions
// $file_object->permissions = $file->getPermissions();
$file_object->type = $file->getType();
$file_object->mime = $file->getMimetype();
$file_object->lastmod = $file->getMTime();
$file_object->size = $file->getSize();
$file_object->path = $path;
$file_object->isReadable = $file->isReadable();
$file_object->isUpdateable = $file->isUpdateable();
$file_object->isShareable = $file->isShareable();
$file_object->isDeletable = $file->isDeletable();
$file_object->hasPreview = in_array($cacheEntry->getMimeType(), $previewEnableMimetypes);
$filesById[] = $file_object;
}
}
}
$this->photosCache->set($key, $filesById, 60 * 60 * 24);
}
return $filesById;
}
$file_object = new \stdClass();
$file_object->fileId = $photoEntity->getFileId();
$file_object->fileid = $file_object->fileId;
$file_object->lat = $photoEntity->getLat();
$file_object->lng = $photoEntity->getLng();
$file_object->dateTaken = $photoEntity->getDateTaken() ?? \time();
$file_object->basename = $isRoot ? '' : $file->getName();
$file_object->filename = $this->normalizePath($path);
$file_object->etag = $cacheEntry->getEtag();
//Not working for NC21 as Viewer requires String representation of permissions
// $file_object->permissions = $file->getPermissions();
$file_object->type = $file->getType();
$file_object->mime = $file->getMimetype();
$file_object->lastmod = $file->getMTime();
$file_object->size = $file->getSize();
$file_object->path = $path;
$file_object->isReadable = $file->isReadable();
$file_object->isUpdateable = $file->isUpdateable();
$file_object->isShareable = $file->isShareable();
$file_object->isDeletable = $file->isDeletable();
$file_object->hasPreview = in_array($cacheEntry->getMimeType(), $previewEnableMimetypes);
$filesById[] = $file_object;
}
}
}
$this->photosCache->set($key, $filesById, 60 * 60 * 24);
}
return $filesById;
}
/**
* @param string $userId
@ -188,7 +182,7 @@ class GeophotoService {
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getNonLocalized (string $userId, $folder=null, bool $respectNomediaAndNoimage=true, bool $hideImagesOnCustomMaps=false, bool $hideImagesInMapsFolder=true, string $timezone=null, int $limit=250, int $offset=0): array {
public function getNonLocalized(string $userId, $folder = null, bool $respectNomediaAndNoimage = true, bool $hideImagesOnCustomMaps = false, bool $hideImagesInMapsFolder = true, ?string $timezone = null, int $limit = 250, int $offset = 0): array {
$userFolder = $this->getFolderForUser($userId);
if (is_null($folder)) {
$folder = $userFolder;
@ -196,7 +190,7 @@ class GeophotoService {
$ignoredPaths = $respectNomediaAndNoimage ? $this->getIgnoredPaths($userId, $folder, $hideImagesOnCustomMaps) : [];
if ($hideImagesInMapsFolder) {
$ignoredPaths[] = "/Maps";
$ignoredPaths[] = '/Maps';
}
$this->loadTimeorderedPointSets($userId, $folder, $respectNomediaAndNoimage, $hideImagesOnCustomMaps, $hideImagesInMapsFolder);
$photoEntities = $this->photoMapper->findAllNonLocalized($userId, $limit, $offset);
@ -263,7 +257,7 @@ class GeophotoService {
$file_object->isDeletable = $file->isDeletable();
$file_object->hasPreview = in_array($cacheEntry->getMimeType(), $previewEnableMimetypes);
$file_object->trackOrDeviceId = $key;
if(!array_key_exists($key, $suggestionsBySource)){
if (!array_key_exists($key, $suggestionsBySource)) {
$suggestionsBySource[$key] = [];
}
$suggestionsBySource[$key][] = $file_object;
@ -271,8 +265,8 @@ class GeophotoService {
}
}
}
return $suggestionsBySource;
}
return $suggestionsBySource;
}
/**
* @param $userId
@ -282,9 +276,9 @@ class GeophotoService {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
private function getIgnoredPaths($userId, $folder=null, $hideImagesOnCustomMaps=true){
private function getIgnoredPaths($userId, $folder = null, $hideImagesOnCustomMaps = true) {
$ignoredPaths = [];
$userFolder=$this->getFolderForUser($userId);
$userFolder = $this->getFolderForUser($userId);
if (is_null($folder)) {
$folder = $userFolder;
}
@ -296,48 +290,48 @@ class GeophotoService {
if ($hideImagesOnCustomMaps) {
$ignoreFileMimetypes[] = 'application/x-nextcloud-maps';
}
$func = function(string $i): SearchComparison {
$func = function (string $i): SearchComparison {
return new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $i);
};
$excludedNodes = $folder->search(new SearchQuery(
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, array_map(
$func,
$ignoreFileMimetypes)
),
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, array_map(
$func,
$ignoreFileMimetypes)
),
0,
0,
[]
));
foreach($excludedNodes as $node) {
foreach ($excludedNodes as $node) {
$ignoredPaths[] = $userFolder->getRelativePath($node->getParent()->getPath());
}
return $ignoredPaths;
}
/**
* returns a array of locations for a given date
*
* @param $dateTaken int
* @return array
*/
private function getLocationGuesses(int $dateTaken): array {
$locations = [];
foreach (($this->timeorderedPointSets ?? []) as $key => $timeordedPointSet) {
$location = $this->getLocationFromSequenceOfPoints($dateTaken,$timeordedPointSet);
if (!is_null($location)) {
$locations[$key] = $location;
}
}
return $locations;
/**
* returns a array of locations for a given date
*
* @param $dateTaken int
* @return array
*/
private function getLocationGuesses(int $dateTaken): array {
$locations = [];
foreach (($this->timeorderedPointSets ?? []) as $key => $timeordedPointSet) {
$location = $this->getLocationFromSequenceOfPoints($dateTaken, $timeordedPointSet);
if (!is_null($location)) {
$locations[$key] = $location;
}
}
return $locations;
}
}
/*
* Timeordered Point sets is an Array of Arrays with time => location as key=>value pair, which are orderd by the key.
* This function loads this Arrays from all Track files of the user.
*/
private function loadTimeorderedPointSets(string $userId, $folder=null, bool $respectNomediaAndNoimage=true, bool $hideTracksOnCustomMaps=false, bool $hideTracksInMapsFolder=true): void {
$key = $userId . ':' . (string) $respectNomediaAndNoimage . ':' . (string) $hideTracksOnCustomMaps . ':' . (string) $hideTracksInMapsFolder;
/*
* Timeordered Point sets is an Array of Arrays with time => location as key=>value pair, which are orderd by the key.
* This function loads this Arrays from all Track files of the user.
*/
private function loadTimeorderedPointSets(string $userId, $folder = null, bool $respectNomediaAndNoimage = true, bool $hideTracksOnCustomMaps = false, bool $hideTracksInMapsFolder = true): void {
$key = $userId . ':' . (string)$respectNomediaAndNoimage . ':' . (string)$hideTracksOnCustomMaps . ':' . (string)$hideTracksInMapsFolder;
$this->timeorderedPointSets = $this->timeOrderedPointSetsCache->get($key);
if (is_null($this->timeorderedPointSets)) {
$userFolder = $this->getFolderForUser($userId);
@ -358,41 +352,41 @@ class GeophotoService {
}
$this->timeOrderedPointSetsCache->set($key, $this->timeorderedPointSets);
}
}
}
/*
* A GPX file can contain multiple tracks this function returns all Tracks from a given sting containing GPX encoded information.
* @param $content
* @return array
*/
private function getTracksFromGPX($content): array {
$tracks = [];
$gpx = simplexml_load_string($content);
foreach ($gpx->trk as $trk) {
$tracks[] = $trk;
}
return $tracks;
}
/*
* A GPX file can contain multiple tracks this function returns all Tracks from a given sting containing GPX encoded information.
* @param $content
* @return array
*/
private function getTracksFromGPX($content): array {
$tracks = [];
$gpx = simplexml_load_string($content);
foreach ($gpx->trk as $trk) {
$tracks[] = $trk;
}
return $tracks;
}
/*
* Loads all trackpoints from a given $track SimpleXMLObject. And stores them in a time=>location stuctured array which is sorted by the key.
* @param $track
* @return array
*/
private function getTimeorderdPointsFromTrack($track): array {
$points = [];
foreach ($track->trkseg as $seg) {
foreach ($seg->trkpt as $pt) {
$points[strtotime($pt->time)] = [(string) $pt["lat"],(string) $pt["lon"]];
}
}
foreach ($track->trkpt as $pt) {
$points[strtotime($pt->time)] = [(string) $pt["lat"],(string) $pt["lon"]];
}
/*
* Loads all trackpoints from a given $track SimpleXMLObject. And stores them in a time=>location stuctured array which is sorted by the key.
* @param $track
* @return array
*/
private function getTimeorderdPointsFromTrack($track): array {
$points = [];
foreach ($track->trkseg as $seg) {
foreach ($seg->trkpt as $pt) {
$points[strtotime($pt->time)] = [(string)$pt['lat'],(string)$pt['lon']];
}
}
foreach ($track->trkpt as $pt) {
$points[strtotime($pt->time)] = [(string)$pt['lat'],(string)$pt['lon']];
}
$foo = ksort($points);
return $points;
}
$foo = ksort($points);
return $points;
}
/**
* @param int $timeUTC
@ -404,59 +398,59 @@ class GeophotoService {
}
/**
* @param $dateTaken int timestamp of the picture
* @param $points array sorted by keys timestamp => [lat, lng]
*/
private function getLocationFromSequenceOfPoints(int $dateTaken, array $points): ?array {
$foo = end($points);
$end = key($points);
$foo = reset($points);
$start = key($points);
if ($start > $dateTaken OR $end < $dateTaken) {
return null;
}
$smaller = null;
$bigger = null;
foreach ($points as $time => $locations) {
if ($time < $dateTaken) {
$smaller = $time;
} else {
$bigger = $time;
break;
}
}
if (!is_null($smaller) AND !is_null($bigger)) {
$d = $bigger - $smaller;
$t = ($dateTaken - $smaller) / $d;
$latd = $points[$bigger][0] - $points[$smaller][0];
$lngd = $points[$bigger][1] - $points[$smaller][1];
return [$points[$smaller][0] + $t * $latd, $points[$smaller][1] + $t * $lngd];
} else {
return null;
}
}
/**
* @param $dateTaken int timestamp of the picture
* @param $points array sorted by keys timestamp => [lat, lng]
*/
private function getLocationFromSequenceOfPoints(int $dateTaken, array $points): ?array {
$foo = end($points);
$end = key($points);
$foo = reset($points);
$start = key($points);
if ($start > $dateTaken or $end < $dateTaken) {
return null;
}
$smaller = null;
$bigger = null;
foreach ($points as $time => $locations) {
if ($time < $dateTaken) {
$smaller = $time;
} else {
$bigger = $time;
break;
}
}
if (!is_null($smaller) and !is_null($bigger)) {
$d = $bigger - $smaller;
$t = ($dateTaken - $smaller) / $d;
$latd = $points[$bigger][0] - $points[$smaller][0];
$lngd = $points[$bigger][1] - $points[$smaller][1];
return [$points[$smaller][0] + $t * $latd, $points[$smaller][1] + $t * $lngd];
} else {
return null;
}
}
private function getPreviewEnabledMimetypes(): array {
$enabledMimeTypes = [];
foreach (PhotofilesService::PHOTO_MIME_TYPES as $mimeType) {
if ($this->preview->isMimeSupported($mimeType)) {
$enabledMimeTypes[] = $mimeType;
}
}
return $enabledMimeTypes;
}
private function getPreviewEnabledMimetypes(): array {
$enabledMimeTypes = [];
foreach (PhotofilesService::PHOTO_MIME_TYPES as $mimeType) {
if ($this->preview->isMimeSupported($mimeType)) {
$enabledMimeTypes[] = $mimeType;
}
}
return $enabledMimeTypes;
}
private function normalizePath($path) {
return str_replace("files","", $path);
}
private function normalizePath($path) {
return str_replace('files', '', $path);
}
/**
* @param string $userId the user id
* @return Folder
*/
private function getFolderForUser ($userId) {
/**
* @param string $userId the user id
* @return Folder
*/
private function getFolderForUser($userId) {
return $this->root->getUserFolder($userId);
}
}
}

Просмотреть файл

@ -6,17 +6,15 @@ use OCP\Files\IMimeTypeLoader;
class MimetypeService {
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
protected IMimeTypeLoader $mimeTypeLoader;
public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
public function __construct(IMimeTypeLoader $mimeTypeLoader) {
$this->mimeTypeLoader = $mimeTypeLoader;
}
public function registerForExistingFiles()
{
public function registerForExistingFiles() {
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps');
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId);
@ -33,8 +31,7 @@ class MimetypeService {
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId);
}
public function registerForNewFiles()
{
public function registerForNewFiles() {
$mapping = [
'maps' => ['application/x-nextcloud-maps'],
'noindex' => ['application/x-nextcloud-noindex'],

Просмотреть файл

@ -12,132 +12,118 @@
namespace OCA\Maps\Service;
use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\OCS\Exception;
use OC\User\NoUserException;
use OCP\Files\InvalidPathException;
use OCP\Files\NotPermittedException;
use OCP\Files\Search\ISearchQuery;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\NotFoundException;
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\IL10N;
use OCP\ILogger;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IRootFolder;
use OCP\Files\FileInfo;
use OCP\Share\IManager;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Search\ISearchComparison;
use OCP\ILogger;
class MyMapsService {
private $logger;
private $logger;
private $root;
public function __construct (ILogger $logger, IRootFolder $root) {
$this->logger = $logger;
public function __construct(ILogger $logger, IRootFolder $root) {
$this->logger = $logger;
$this->root = $root;
}
}
public function addMyMap($newName, $userId, $counter=0) {
public function addMyMap($newName, $userId, $counter = 0) {
$userFolder = $this->root->getUserFolder($userId);
if (!$userFolder->nodeExists('/Maps')) {
if (!$userFolder->nodeExists('/Maps')) {
$userFolder->newFolder('Maps');
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
$response = '/Maps is not a directory';
return $response;
}
else if (!$mapsFolder->isCreatable()) {
$response = '/Maps is not writeable';
return $response;
}
}
else {
$response = 'Impossible to create /Maps';
return $response;
}
if ($counter>0) {
$folderName = $newName." ".$counter;
}
else {
$folderName = $newName;
}
}
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($mapsFolder->getType() !== \OCP\Files\FileInfo::TYPE_FOLDER) {
$response = '/Maps is not a directory';
return $response;
} elseif (!$mapsFolder->isCreatable()) {
$response = '/Maps is not writeable';
return $response;
}
} else {
$response = 'Impossible to create /Maps';
return $response;
}
if ($counter > 0) {
$folderName = $newName.' '.$counter;
} else {
$folderName = $newName;
}
if ($mapsFolder->nodeExists($folderName)) {
return $this->addMyMap($newName, $userId,$counter+1);
}
$mapFolder = $mapsFolder->newFolder($folderName);
$mapFolder->newFile(".index.maps","{}");
if ($mapsFolder->nodeExists($folderName)) {
return $this->addMyMap($newName, $userId, $counter + 1);
}
$mapFolder = $mapsFolder->newFolder($folderName);
$mapFolder->newFile('.index.maps', '{}');
$isRoot = $mapFolder->getPath() === $userFolder->getPath();
$MyMap = [
"id"=>$mapFolder->getId(),
"name"=>$folderName,
"color"=>null,
"path"=>$userFolder->getRelativePath($mapFolder->getPath()),
"isShareable"=>$mapFolder->isShareable(),
"isDeletable"=>$mapFolder->isDeletable(),
"isCreatable"=>$mapFolder->isCreatable(),
"isUpdateable"=>$mapFolder->isUpdateable(),
"isReadable"=>$mapFolder->isReadable(),
"fileInfo"=>[
"id" => $mapFolder->getId(),
"name" => "",
"basename" => $isRoot ? '' : $mapFolder->getName(),
"filename" => $userFolder->getRelativePath($mapFolder->getPath()),
"etag" => $mapFolder->getEtag(),
"permissions" => $mapFolder->getPermissions(),
"type" => $mapFolder->getType(),
"mime" => $mapFolder->getMimetype(),
"lastmod" => $mapFolder->getMTime(),
"path"=>$userFolder->getRelativePath($mapFolder->getPath()),
"sharePermissions"=>$mapFolder->getPermissions(),
'id' => $mapFolder->getId(),
'name' => $folderName,
'color' => null,
'path' => $userFolder->getRelativePath($mapFolder->getPath()),
'isShareable' => $mapFolder->isShareable(),
'isDeletable' => $mapFolder->isDeletable(),
'isCreatable' => $mapFolder->isCreatable(),
'isUpdateable' => $mapFolder->isUpdateable(),
'isReadable' => $mapFolder->isReadable(),
'fileInfo' => [
'id' => $mapFolder->getId(),
'name' => '',
'basename' => $isRoot ? '' : $mapFolder->getName(),
'filename' => $userFolder->getRelativePath($mapFolder->getPath()),
'etag' => $mapFolder->getEtag(),
'permissions' => $mapFolder->getPermissions(),
'type' => $mapFolder->getType(),
'mime' => $mapFolder->getMimetype(),
'lastmod' => $mapFolder->getMTime(),
'path' => $userFolder->getRelativePath($mapFolder->getPath()),
'sharePermissions' => $mapFolder->getPermissions(),
]
];
return $MyMap;
}
return $MyMap;
}
private function node2MyMap($node, $userFolder):array{
private function node2MyMap($node, $userFolder):array {
$mapData = json_decode($node->getContent(), true);
if (isset($mapData["name"])) {
$name = $mapData["name"];
if (isset($mapData['name'])) {
$name = $mapData['name'];
} else {
$name = $node->getParent()->getName();
}
$color = null;
if (isset($mapData["color"])) {
$color = $mapData["color"];
if (isset($mapData['color'])) {
$color = $mapData['color'];
}
$parentNode = $node->getParent();
$isRoot = $parentNode->getPath() === $userFolder->getPath();
$MyMap = [
"id" => $parentNode->getId(),
"name" => $name,
"color" => $color,
"path" => $userFolder->getRelativePath($parentNode->getPath()),
"isShareable" => $parentNode->isShareable(),
"isDeletable" => $parentNode->isDeletable(),
"isCreatable" => $parentNode->isCreatable(),
"isUpdateable" => $parentNode->isUpdateable(),
"isReadable" => $parentNode->isReadable(),
"fileInfo" => [
"id" => $parentNode->getId(),
"name" => "",
"basename" => $isRoot ? '' : $parentNode->getName(),
"filename" => $userFolder->getRelativePath($parentNode->getPath()),
"etag" => $parentNode->getEtag(),
"permissions" => $parentNode->getPermissions(),
"type" => $parentNode->getType(),
"mime" => $parentNode->getMimetype(),
"lastmod" => $parentNode->getMTime(),
"path" => $userFolder->getRelativePath($parentNode->getPath()),
"sharePermissions" => $parentNode->getPermissions(),
'id' => $parentNode->getId(),
'name' => $name,
'color' => $color,
'path' => $userFolder->getRelativePath($parentNode->getPath()),
'isShareable' => $parentNode->isShareable(),
'isDeletable' => $parentNode->isDeletable(),
'isCreatable' => $parentNode->isCreatable(),
'isUpdateable' => $parentNode->isUpdateable(),
'isReadable' => $parentNode->isReadable(),
'fileInfo' => [
'id' => $parentNode->getId(),
'name' => '',
'basename' => $isRoot ? '' : $parentNode->getName(),
'filename' => $userFolder->getRelativePath($parentNode->getPath()),
'etag' => $parentNode->getEtag(),
'permissions' => $parentNode->getPermissions(),
'type' => $parentNode->getType(),
'mime' => $parentNode->getMimetype(),
'lastmod' => $parentNode->getMTime(),
'path' => $userFolder->getRelativePath($parentNode->getPath()),
'sharePermissions' => $parentNode->getPermissions(),
]
];
return $MyMap;
@ -149,7 +135,7 @@ class MyMapsService {
* @throws NoUserException
* @throws NotPermittedException
*/
public function getAllMyMaps($userId){
public function getAllMyMaps($userId) {
$userFolder = $this->root->getUserFolder($userId);
$MyMaps = [];
$MyMapsNodes = $userFolder->search(new SearchQuery(
@ -157,65 +143,65 @@ class MyMapsService {
0, 0, []));
foreach ($MyMapsNodes as $node) {
if ($node->getName() === ".index.maps") {
if ($node->getName() === '.index.maps') {
$MyMaps[] = $this->node2MyMap($node, $userFolder);
}
}
return $MyMaps;
}
}
public function updateMyMap($id, $values, $userId) {
public function updateMyMap($id, $values, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$folders = $userFolder->getById($id);
$folder = array_shift($folders);
try {
$file=$folder->get(".index.maps");
} catch (NotFoundException $e) {
$file=$folder->newFile(".index.maps", $content = '{}');
}
$mapData = json_decode($file->getContent(),true);
$renamed = false;
foreach ($values as $key=>$value) {
if ($key === 'newName') {
$key = 'name';
$newName = $value;
$renamed = true;
}
if (is_null($value)) {
unset($mapData[$key]);
} else {
$mapData[$key] = $value;
}
}
$file->putContent(json_encode($mapData,JSON_PRETTY_PRINT));
if ($renamed) {
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($folder->getParent()->getId() === $mapsFolder->getId() ) {
try {
$folder->move($mapsFolder->getPath()."/".$newName);
} catch (Exception $e) {
}
}
}
}
return $mapData;
}
$folders = $userFolder->getById($id);
$folder = array_shift($folders);
try {
$file = $folder->get('.index.maps');
} catch (NotFoundException $e) {
$file = $folder->newFile('.index.maps', $content = '{}');
}
$mapData = json_decode($file->getContent(), true);
$renamed = false;
foreach ($values as $key => $value) {
if ($key === 'newName') {
$key = 'name';
$newName = $value;
$renamed = true;
}
if (is_null($value)) {
unset($mapData[$key]);
} else {
$mapData[$key] = $value;
}
}
$file->putContent(json_encode($mapData, JSON_PRETTY_PRINT));
if ($renamed) {
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($folder->getParent()->getId() === $mapsFolder->getId()) {
try {
$folder->move($mapsFolder->getPath().'/'.$newName);
} catch (Exception $e) {
}
}
}
}
return $mapData;
}
public function deleteMyMap($id, $userId) {
public function deleteMyMap($id, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$folders = $userFolder->getById($id);
$folder = array_shift($folders);
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($folder->getParent()->getId() === $mapsFolder->getId() ) {
try {
$folder->delete();
} catch (Exception $e) {
return 1;
}
} else {
$folders = $userFolder->getById($id);
$folder = array_shift($folders);
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
if ($folder->getParent()->getId() === $mapsFolder->getId()) {
try {
$folder->delete();
} catch (Exception $e) {
return 1;
}
} else {
try {
$file = $folder->get('.index.maps');
$file->delete();
@ -223,14 +209,14 @@ class MyMapsService {
return 1;
}
}
}
try {
$file=$folder->get(".index.maps");
$file->delete();
} catch (NotFoundException $e) {
return 1;
}
return 0;
}
}
try {
$file = $folder->get('.index.maps');
$file->delete();
} catch (NotFoundException $e) {
return 1;
}
return 0;
}
}

Просмотреть файл

@ -12,151 +12,145 @@
namespace OCA\Maps\Service;
use lsolesen\pel\PelEntryTime;
use OCA\Maps\BackgroundJob\AddPhotoJob;
use OCA\Maps\BackgroundJob\UpdatePhotoByFileJob;
use OCA\Maps\DB\Geophoto;
use OCA\Maps\DB\GeophotoMapper;
use OCA\Maps\Helper\ExifDataInvalidException;
use OCA\Maps\Helper\ExifDataNoLocationException;
use OCA\Maps\Helper\ExifGeoData;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\BackgroundJob\IJobList;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\ICacheFactory;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\ILogger;
use OCP\Share\IManager;
use OCP\Files\Mount\IMountPoint;
use \OCP\BackgroundJob\IJobList;
use OCA\Maps\DB\Geophoto;
use OCA\Maps\DB\GeophotoMapper;
use OCA\Maps\BackgroundJob\AddPhotoJob;
use OCA\Maps\BackgroundJob\UpdatePhotoByFileJob;
require_once __DIR__ . '/../../vendor/autoload.php';
use lsolesen\pel\PelDataWindow;
use lsolesen\pel\PelException;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelExif;
use lsolesen\pel\PelTiff;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelEntryAscii;
use lsolesen\pel\PelEntryRational;
use lsolesen\pel\PelExif;
use lsolesen\pel\PelIfd;
use lsolesen\pel\Pel;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelTiff;
class PhotofilesService {
const PHOTO_MIME_TYPES = ['image/jpeg', 'image/tiff'];
public const PHOTO_MIME_TYPES = ['image/jpeg', 'image/tiff'];
private $l10n;
private $root;
private $photoMapper;
private $shareManager;
private $logger;
private $jobList;
private $l10n;
private $root;
private $photoMapper;
private $shareManager;
private $logger;
private $jobList;
private ICacheFactory $cacheFactory;
private \OCP\ICache $photosCache;
private \OCP\ICache $backgroundJobCache;
public function __construct (ILogger $logger,
ICacheFactory $cacheFactory,
IRootFolder $root,
IL10N $l10n,
GeophotoMapper $photoMapper,
IManager $shareManager,
IJobList $jobList) {
$this->root = $root;
$this->l10n = $l10n;
$this->photoMapper = $photoMapper;
$this->shareManager = $shareManager;
$this->logger = $logger;
$this->jobList = $jobList;
public function __construct(ILogger $logger,
ICacheFactory $cacheFactory,
IRootFolder $root,
IL10N $l10n,
GeophotoMapper $photoMapper,
IManager $shareManager,
IJobList $jobList) {
$this->root = $root;
$this->l10n = $l10n;
$this->photoMapper = $photoMapper;
$this->shareManager = $shareManager;
$this->logger = $logger;
$this->jobList = $jobList;
$this->cacheFactory = $cacheFactory;
$this->photosCache = $this->cacheFactory->createDistributed('maps:photos');
$this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs');
}
}
public function rescan($userId, $inBackground=true) {
public function rescan($userId, $inBackground = true) {
$this->photosCache->clear($userId);
$userFolder = $this->root->getUserFolder($userId);
$photos = $this->gatherPhotoFiles($userFolder, true);
$this->photoMapper->deleteAll($userId);
foreach ($photos as $photo) {
$userFolder = $this->root->getUserFolder($userId);
$photos = $this->gatherPhotoFiles($userFolder, true);
$this->photoMapper->deleteAll($userId);
foreach ($photos as $photo) {
if ($inBackground) {
$this->addPhoto($photo, $userId);
} else {
$this->addPhotoNow($photo, $userId);
}
yield $photo->getPath();
}
}
yield $photo->getPath();
}
}
// add the file for its owner and users that have access
// check if it's already in DB before adding
public function addByFile(Node $file) {
$ownerId = $file->getOwner()->getUID();
if ($this->isPhoto($file)) {
$this->addPhoto($file, $ownerId);
// is the file accessible to other users ?
$accesses = $this->shareManager->getAccessList($file);
foreach ($accesses['users'] as $uid) {
if ($uid !== $ownerId) {
$this->addPhoto($file, $uid);
}
}
return true;
}
else {
return false;
}
}
// add the file for its owner and users that have access
// check if it's already in DB before adding
public function addByFile(Node $file) {
$ownerId = $file->getOwner()->getUID();
if ($this->isPhoto($file)) {
$this->addPhoto($file, $ownerId);
// is the file accessible to other users ?
$accesses = $this->shareManager->getAccessList($file);
foreach ($accesses['users'] as $uid) {
if ($uid !== $ownerId) {
$this->addPhoto($file, $uid);
}
}
return true;
} else {
return false;
}
}
public function addByFileIdUserId($fileId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$files = $userFolder->getById($fileId);
if (empty($files)) {
return;
}
$file = array_shift($files);
if ($file !== null and $this->isPhoto($file)) {
$this->addPhoto($file, $userId);
}
}
public function addByFileIdUserId($fileId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$files = $userFolder->getById($fileId);
if (empty($files)) {
return;
}
$file = array_shift($files);
if ($file !== null and $this->isPhoto($file)) {
$this->addPhoto($file, $userId);
}
}
public function addByFolderIdUserId($folderId, $userId) {
$folders = $this->root->getById($folderId);
public function addByFolderIdUserId($folderId, $userId) {
$folders = $this->root->getById($folderId);
if (empty($folders)) {
return;
}
$folder = array_shift($folders);
if ($folder !== null) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach($photos as $photo) {
$this->addPhoto($photo, $userId);
}
}
}
if ($folder !== null) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach ($photos as $photo) {
$this->addPhoto($photo, $userId);
}
}
}
// add all photos of a folder taking care of shared accesses
public function addByFolder($folder) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach($photos as $photo) {
$this->addByFile($photo);
}
}
// add all photos of a folder taking care of shared accesses
public function addByFolder($folder) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach ($photos as $photo) {
$this->addByFile($photo);
}
}
public function updateByFile(Node $file) {
$this->jobList->add(UpdatePhotoByFileJob::class, ['fileId' => $file->getId(), 'userId' => $file->getOwner()->getUID()]);
}
public function updateByFile(Node $file) {
$this->jobList->add(UpdatePhotoByFileJob::class, ['fileId' => $file->getId(), 'userId' => $file->getOwner()->getUID()]);
}
public function updateByFileNow(Node $file) {
if ($this->isPhoto($file)) {
$exif = $this->getExif($file);
if (!is_null($exif)) {
$ownerId = $file->getOwner()->getUID();
// in case there is no entry for this file yet (normally there is because non-localized photos are added)
public function updateByFileNow(Node $file) {
if ($this->isPhoto($file)) {
$exif = $this->getExif($file);
if (!is_null($exif)) {
$ownerId = $file->getOwner()->getUID();
// in case there is no entry for this file yet (normally there is because non-localized photos are added)
try {
$this->photoMapper->findByFileIdUserId($file->getId(), $ownerId);
$this->updatePhoto($file, $exif);
@ -164,69 +158,69 @@ class PhotofilesService {
} catch (DoesNotExistException $exception) {
$this->insertPhoto($file, $ownerId, $exif);
}
}
}
}
}
}
}
public function deleteByFile(Node $file) {
$this->photoMapper->deleteByFileId($file->getId());
}
public function deleteByFile(Node $file) {
$this->photoMapper->deleteByFileId($file->getId());
}
// delete photo only if it's not accessible to user anymore
// it might have been shared multiple times by different users
public function deleteByFileIdUserId($fileId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$files = $userFolder->getById($fileId);
if (!is_array($files) or count($files) === 0) {
$this->photoMapper->deleteByFileIdUserId($fileId, $userId);
// delete photo only if it's not accessible to user anymore
// it might have been shared multiple times by different users
public function deleteByFileIdUserId($fileId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$files = $userFolder->getById($fileId);
if (!is_array($files) or count($files) === 0) {
$this->photoMapper->deleteByFileIdUserId($fileId, $userId);
$this->photosCache->clear($userId);
}
}
}
}
public function deleteByFolder(Node $folder) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach ($photos as $photo) {
$this->photoMapper->deleteByFileId($photo->getId());
}
}
public function deleteByFolder(Node $folder) {
$photos = $this->gatherPhotoFiles($folder, true);
foreach ($photos as $photo) {
$this->photoMapper->deleteByFileId($photo->getId());
}
}
// delete folder photos only if it's not accessible to user anymore
public function deleteByFolderIdUserId($folderId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$folders = $userFolder->getById($folderId);
if (is_array($folders) and count($folders) === 1) {
$folder = array_shift($folders);
$photos = $this->gatherPhotoFiles($folder, true);
foreach($photos as $photo) {
$this->photoMapper->deleteByFileIdUserId($photo->getId(), $userId);
}
// delete folder photos only if it's not accessible to user anymore
public function deleteByFolderIdUserId($folderId, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$folders = $userFolder->getById($folderId);
if (is_array($folders) and count($folders) === 1) {
$folder = array_shift($folders);
$photos = $this->gatherPhotoFiles($folder, true);
foreach ($photos as $photo) {
$this->photoMapper->deleteByFileIdUserId($photo->getId(), $userId);
}
$this->photosCache->clear($userId);
}
}
}
}
/**
* @param $userId
* @return array
*/
public function getBackgroundJobStatus($userId): array {
$add_counter = 0;
$addJobsRunning = False;
$add_counter = 0;
$addJobsRunning = false;
foreach ($this->jobList->getJobsIterator(AddPhotoJob::class, Null, 0) as $job) {
foreach ($this->jobList->getJobsIterator(AddPhotoJob::class, null, 0) as $job) {
if ($job->getArgument()['userId'] === $userId) {
$add_counter += 1;
}
$addJobsRunning = True;
$addJobsRunning = true;
}
$update_counter = 0;
$updateJobsRunning = False;
$updateJobsRunning = false;
foreach ($this->jobList->getJobsIterator(UpdatePhotoByFileJob::class, Null, 0) as $job) {
foreach ($this->jobList->getJobsIterator(UpdatePhotoByFileJob::class, null, 0) as $job) {
if ($job->getArgument()['userId'] === $userId) {
$update_counter += 1;
}
$updateJobsRunning = True;
$updateJobsRunning = true;
}
$recentlyAdded = $this->backgroundJobCache->get('recentlyAdded:'.$userId) ?? 0;
$recentlyUpdated = $this->backgroundJobCache->get('$recentlyUpdated:'.$userId) ?? 0;
@ -240,312 +234,315 @@ class PhotofilesService {
];
}
public function setPhotosFilesCoords($userId, $paths, $lats, $lngs, $directory) {
if ($directory) {
return $this->setDirectoriesCoords($userId, $paths, $lats, $lngs);
}
else {
return $this->setFilesCoords($userId, $paths, $lats, $lngs);
}
}
public function setPhotosFilesCoords($userId, $paths, $lats, $lngs, $directory) {
if ($directory) {
return $this->setDirectoriesCoords($userId, $paths, $lats, $lngs);
} else {
return $this->setFilesCoords($userId, $paths, $lats, $lngs);
}
}
private function setDirectoriesCoords($userId, $paths, $lats, $lngs) {
$lat = $lats[0] ?? 0;
$lng = $lngs[0] ?? 0;
$userFolder = $this->root->getUserFolder($userId);
$done = [];
foreach ($paths as $dirPath) {
$cleanDirPath = str_replace(array('../', '..\\'), '', $dirPath);
if ($userFolder->nodeExists($cleanDirPath)) {
$dir = $userFolder->get($cleanDirPath);
if ($dir->getType() === FileInfo::TYPE_FOLDER) {
$nodes = $dir->getDirectoryListing();
foreach($nodes as $node) {
if ($this->isPhoto($node) && $node->isUpdateable()) {
$photo = $this->photoMapper->findByFileIdUserId($node->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $node->getInternalPath()),
'lat' => $lat,
'lng' => $lng,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->setExifCoords($node, $lat, $lng);
$this->updateByFileNow($node);
}
}
}
}
}
return $done;
}
private function setDirectoriesCoords($userId, $paths, $lats, $lngs) {
$lat = $lats[0] ?? 0;
$lng = $lngs[0] ?? 0;
$userFolder = $this->root->getUserFolder($userId);
$done = [];
foreach ($paths as $dirPath) {
$cleanDirPath = str_replace(['../', '..\\'], '', $dirPath);
if ($userFolder->nodeExists($cleanDirPath)) {
$dir = $userFolder->get($cleanDirPath);
if ($dir->getType() === FileInfo::TYPE_FOLDER) {
$nodes = $dir->getDirectoryListing();
foreach ($nodes as $node) {
if ($this->isPhoto($node) && $node->isUpdateable()) {
$photo = $this->photoMapper->findByFileIdUserId($node->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $node->getInternalPath()),
'lat' => $lat,
'lng' => $lng,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->setExifCoords($node, $lat, $lng);
$this->updateByFileNow($node);
}
}
}
}
}
return $done;
}
private function setFilesCoords($userId, $paths, $lats, $lngs) {
$userFolder = $this->root->getUserFolder($userId);
$done = [];
private function setFilesCoords($userId, $paths, $lats, $lngs) {
$userFolder = $this->root->getUserFolder($userId);
$done = [];
foreach ($paths as $i => $path) {
$cleanpath = str_replace(array('../', '..\\'), '', $path);
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($this->isPhoto($file) && $file->isUpdateable()) {
$lat = (count($lats) > $i) ? $lats[$i] : $lats[0];
$lng = (count($lngs) > $i) ? $lngs[$i] : $lngs[0];
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
'lat' => $lat,
'lng' => $lng,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->setExifCoords($file, $lat, $lng);
$this->updateByFileNow($file);
}
}
}
return $done;
}
foreach ($paths as $i => $path) {
$cleanpath = str_replace(['../', '..\\'], '', $path);
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($this->isPhoto($file) && $file->isUpdateable()) {
$lat = (count($lats) > $i) ? $lats[$i] : $lats[0];
$lng = (count($lngs) > $i) ? $lngs[$i] : $lngs[0];
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
'lat' => $lat,
'lng' => $lng,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->setExifCoords($file, $lat, $lng);
$this->updateByFileNow($file);
}
}
}
return $done;
}
public function resetPhotosFilesCoords($userId, $paths) {
$userFolder = $this->root->getUserFolder($userId);
$done = [];
public function resetPhotosFilesCoords($userId, $paths) {
$userFolder = $this->root->getUserFolder($userId);
$done = [];
foreach ($paths as $i => $path) {
$cleanpath = str_replace(array('../', '..\\'), '', $path);
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($this->isPhoto($file) && $file->isUpdateable()) {
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
'lat' => null,
'lng' => null,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->resetExifCoords($file);
$this->photoMapper->updateByFileId($file->getId(), null, null);
}
}
}
return $done;
}
foreach ($paths as $i => $path) {
$cleanpath = str_replace(['../', '..\\'], '', $path);
if ($userFolder->nodeExists($cleanpath)) {
$file = $userFolder->get($cleanpath);
if ($this->isPhoto($file) && $file->isUpdateable()) {
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
$done[] = [
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
'lat' => null,
'lng' => null,
'oldLat' => $photo ? $photo->getLat() : null,
'oldLng' => $photo ? $photo->getLng() : null,
];
$this->resetExifCoords($file);
$this->photoMapper->updateByFileId($file->getId(), null, null);
}
}
}
return $done;
}
// avoid adding photo if it already exists in the DB
private function addPhoto($photo, $userId) {
$this->jobList->add(AddPhotoJob::class, ['photoId' => $photo->getId(), 'userId' => $userId]);
}
// avoid adding photo if it already exists in the DB
private function addPhoto($photo, $userId) {
$this->jobList->add(AddPhotoJob::class, ['photoId' => $photo->getId(), 'userId' => $userId]);
}
public function addPhotoNow($photo, $userId) {
$exif = $this->getExif($photo);
if (!is_null($exif)) {
// filehooks are triggered several times (2 times for file creation)
// so we need to be sure it's not inserted several times
// by checking if it already exists in DB
// OR by using file_id in primary key
try {
public function addPhotoNow($photo, $userId) {
$exif = $this->getExif($photo);
if (!is_null($exif)) {
// filehooks are triggered several times (2 times for file creation)
// so we need to be sure it's not inserted several times
// by checking if it already exists in DB
// OR by using file_id in primary key
try {
$this->photoMapper->findByFileIdUserId($photo->getId(), $userId);
} catch (DoesNotExistException $exception) {
$this->insertPhoto($photo, $userId, $exif);
}
$this->insertPhoto($photo, $userId, $exif);
}
$this->photosCache->clear($userId);
}
}
}
}
private function insertPhoto($photo, $userId, $exif) {
$photoEntity = new Geophoto();
$photoEntity->setFileId($photo->getId());
$photoEntity->setLat(
is_numeric($exif->lat)&&!is_nan($exif->lat) ? $exif->lat : null
);
$photoEntity->setLng(
is_numeric($exif->lng)&&!is_nan($exif->lng) ? $exif->lng : null
);
$photoEntity->setUserId($userId);
// alternative should be file creation date
$photoEntity->setDateTaken($exif->dateTaken ?? $photo->getMTime());
$this->photoMapper->insert($photoEntity);
private function insertPhoto($photo, $userId, $exif) {
$photoEntity = new Geophoto();
$photoEntity->setFileId($photo->getId());
$photoEntity->setLat(
is_numeric($exif->lat) && !is_nan($exif->lat) ? $exif->lat : null
);
$photoEntity->setLng(
is_numeric($exif->lng) && !is_nan($exif->lng) ? $exif->lng : null
);
$photoEntity->setUserId($userId);
// alternative should be file creation date
$photoEntity->setDateTaken($exif->dateTaken ?? $photo->getMTime());
$this->photoMapper->insert($photoEntity);
$this->photosCache->clear($userId);
}
}
private function updatePhoto($file, $exif) {
$lat = is_numeric($exif->lat)&&!is_nan($exif->lat) ? $exif->lat : null;
$lng = is_numeric($exif->lng)&&!is_nan($exif->lng) ? $exif->lng : null;
$this->photoMapper->updateByFileId($file->getId(), $lat, $lng);
}
private function updatePhoto($file, $exif) {
$lat = is_numeric($exif->lat) && !is_nan($exif->lat) ? $exif->lat : null;
$lng = is_numeric($exif->lng) && !is_nan($exif->lng) ? $exif->lng : null;
$this->photoMapper->updateByFileId($file->getId(), $lat, $lng);
}
private function normalizePath($node) {
return str_replace("files","", $node->getInternalPath());
}
private function normalizePath($node) {
return str_replace('files', '', $node->getInternalPath());
}
public function getPhotosByFolder($userId, $path) {
$userFolder = $this->root->getUserFolder($userId);
$folder = $userFolder->get($path);
return $this->getPhotosListForFolder($folder);
}
public function getPhotosByFolder($userId, $path) {
$userFolder = $this->root->getUserFolder($userId);
$folder = $userFolder->get($path);
return $this->getPhotosListForFolder($folder);
}
private function getPhotosListForFolder($folder) {
$FilesList = $this->gatherPhotoFiles($folder, false);
$notes = [];
foreach($FilesList as $File) {
$file_object = new \stdClass();
$file_object->fileId = $File->getId();
$file_object->path = $this->normalizePath($File);
$notes[] = $file_object;
}
return $notes;
}
private function getPhotosListForFolder($folder) {
$FilesList = $this->gatherPhotoFiles($folder, false);
$notes = [];
foreach ($FilesList as $File) {
$file_object = new \stdClass();
$file_object->fileId = $File->getId();
$file_object->path = $this->normalizePath($File);
$notes[] = $file_object;
}
return $notes;
}
private function gatherPhotoFiles ($folder, $recursive) {
$notes = [];
$nodes = $folder->getDirectoryListing();
foreach ($nodes as $node) {
if ($node->getType() === FileInfo::TYPE_FOLDER AND $recursive) {
// we don't explore external storages for which previews are disabled
if ($node->isMounted()) {
$options = $node->getMountPoint()->getOptions();
if (!(isset($options['previews']) && $options['previews'])) {
continue;
}
}
try {
$notes = array_merge($notes, $this->gatherPhotoFiles($node, $recursive));
} catch (\OCP\Files\StorageNotAvailableException | \Exception $e) {
$msg = "WARNING: Could not access " . $node->getName();
echo($msg . "\n");
$this->logger->error($msg);
}
continue;
}
if ($this->isPhoto($node)) {
$notes[] = $node;
}
}
return $notes;
}
private function gatherPhotoFiles($folder, $recursive) {
$notes = [];
$nodes = $folder->getDirectoryListing();
foreach ($nodes as $node) {
if ($node->getType() === FileInfo::TYPE_FOLDER and $recursive) {
// we don't explore external storages for which previews are disabled
if ($node->isMounted()) {
$options = $node->getMountPoint()->getOptions();
if (!(isset($options['previews']) && $options['previews'])) {
continue;
}
}
try {
$notes = array_merge($notes, $this->gatherPhotoFiles($node, $recursive));
} catch (\OCP\Files\StorageNotAvailableException|\Exception $e) {
$msg = 'WARNING: Could not access ' . $node->getName();
echo($msg . "\n");
$this->logger->error($msg);
}
continue;
}
if ($this->isPhoto($node)) {
$notes[] = $node;
}
}
return $notes;
}
private function isPhoto($file) {
if ($file->getType() !== \OCP\Files\FileInfo::TYPE_FILE) return false;
if (!in_array($file->getMimetype(), self::PHOTO_MIME_TYPES)) return false;
return true;
}
private function isPhoto($file) {
if ($file->getType() !== \OCP\Files\FileInfo::TYPE_FILE) {
return false;
}
if (!in_array($file->getMimetype(), self::PHOTO_MIME_TYPES)) {
return false;
}
return true;
}
/**
* Get exif geo Data object
* returns with null in any validation or Critical errors
*
* @param $file
* @return ExifGeoData|null
*/
private function getExif($file) : ?ExifGeoData {
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
try{
$exif_geo_data = ExifGeoData::get($path);
$exif_geo_data->validate(true);
}catch(ExifDataInvalidException $e){
$exif_geo_data = null;
$this->logger->notice($e->getMessage(), ['code'=>$e->getCode(),'path'=>$path]);
}catch(ExifDataNoLocationException $e){
$this->logger->notice($e->getMessage(), ['code'=>$e->getCode(),'path'=>$path]);
}catch(\Throwable $f){
$exif_geo_data = null;
$this->logger->error($f->getMessage(), ['code'=>$f->getCode(),'path'=>$path]);
}
return $exif_geo_data;
}
/**
* Get exif geo Data object
* returns with null in any validation or Critical errors
*
* @param $file
* @return ExifGeoData|null
*/
private function getExif($file) : ?ExifGeoData {
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
try {
$exif_geo_data = ExifGeoData::get($path);
$exif_geo_data->validate(true);
} catch (ExifDataInvalidException $e) {
$exif_geo_data = null;
$this->logger->notice($e->getMessage(), ['code' => $e->getCode(),'path' => $path]);
} catch (ExifDataNoLocationException $e) {
$this->logger->notice($e->getMessage(), ['code' => $e->getCode(),'path' => $path]);
} catch (\Throwable $f) {
$exif_geo_data = null;
$this->logger->error($f->getMessage(), ['code' => $f->getCode(),'path' => $path]);
}
return $exif_geo_data;
}
private function resetExifCoords($file) {
$data = new PelDataWindow($file->getContent());
$pelJpeg = new PelJpeg($data);
private function resetExifCoords($file) {
$data = new PelDataWindow($file->getContent());
$pelJpeg = new PelJpeg($data);
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
$pelExif = new PelExif();
$pelJpeg->setExif($pelExif);
}
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
$pelExif = new PelExif();
$pelJpeg->setExif($pelExif);
}
$pelTiff = $pelExif->getTiff();
if ($pelTiff === null) {
$pelTiff = new PelTiff();
$pelExif->setTiff($pelTiff);
}
$pelTiff = $pelExif->getTiff();
if ($pelTiff === null) {
$pelTiff = new PelTiff();
$pelExif->setTiff($pelTiff);
}
$pelIfd0 = $pelTiff->getIfd();
if ($pelIfd0 === null) {
$pelIfd0 = new PelIfd(PelIfd::IFD0);
$pelTiff->setIfd($pelIfd0);
}
$pelIfd0 = $pelTiff->getIfd();
if ($pelIfd0 === null) {
$pelIfd0 = new PelIfd(PelIfd::IFD0);
$pelTiff->setIfd($pelIfd0);
}
$pelSubIfdGps = new PelIfd(PelIfd::GPS);
$pelIfd0->addSubIfd($pelSubIfdGps);
$pelSubIfdGps = new PelIfd(PelIfd::GPS);
$pelIfd0->addSubIfd($pelSubIfdGps);
$file->putContent($pelJpeg->getBytes());
}
$file->putContent($pelJpeg->getBytes());
}
private function setExifCoords($file, $lat, $lng) {
$data = new PelDataWindow($file->getContent());
$pelJpeg = new PelJpeg($data);
private function setExifCoords($file, $lat, $lng) {
$data = new PelDataWindow($file->getContent());
$pelJpeg = new PelJpeg($data);
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
$pelExif = new PelExif();
$pelJpeg->setExif($pelExif);
}
$pelExif = $pelJpeg->getExif();
if ($pelExif === null) {
$pelExif = new PelExif();
$pelJpeg->setExif($pelExif);
}
$pelTiff = $pelExif->getTiff();
if ($pelTiff === null) {
$pelTiff = new PelTiff();
$pelExif->setTiff($pelTiff);
}
$pelTiff = $pelExif->getTiff();
if ($pelTiff === null) {
$pelTiff = new PelTiff();
$pelExif->setTiff($pelTiff);
}
$pelIfd0 = $pelTiff->getIfd();
if ($pelIfd0 === null) {
$pelIfd0 = new PelIfd(PelIfd::IFD0);
$pelTiff->setIfd($pelIfd0);
}
$pelIfd0 = $pelTiff->getIfd();
if ($pelIfd0 === null) {
$pelIfd0 = new PelIfd(PelIfd::IFD0);
$pelTiff->setIfd($pelIfd0);
}
$pelSubIfdGps = new PelIfd(PelIfd::GPS);
$pelIfd0->addSubIfd($pelSubIfdGps);
$pelSubIfdGps = new PelIfd(PelIfd::GPS);
$pelIfd0->addSubIfd($pelSubIfdGps);
$this->setGeolocation($pelSubIfdGps, $lat, $lng);
$this->setGeolocation($pelSubIfdGps, $lat, $lng);
$file->putContent($pelJpeg->getBytes());
}
$file->putContent($pelJpeg->getBytes());
}
private function setGeolocation($pelSubIfdGps, $latitudeDegreeDecimal, $longitudeDegreeDecimal) {
$latitudeRef = ($latitudeDegreeDecimal >= 0) ? 'N' : 'S';
$latitudeDegreeMinuteSecond
= $this->degreeDecimalToDegreeMinuteSecond(abs($latitudeDegreeDecimal));
$longitudeRef= ($longitudeDegreeDecimal >= 0) ? 'E' : 'W';
$longitudeDegreeMinuteSecond
= $this->degreeDecimalToDegreeMinuteSecond(abs($longitudeDegreeDecimal));
private function setGeolocation($pelSubIfdGps, $latitudeDegreeDecimal, $longitudeDegreeDecimal) {
$latitudeRef = ($latitudeDegreeDecimal >= 0) ? 'N' : 'S';
$latitudeDegreeMinuteSecond
= $this->degreeDecimalToDegreeMinuteSecond(abs($latitudeDegreeDecimal));
$longitudeRef = ($longitudeDegreeDecimal >= 0) ? 'E' : 'W';
$longitudeDegreeMinuteSecond
= $this->degreeDecimalToDegreeMinuteSecond(abs($longitudeDegreeDecimal));
$pelSubIfdGps->addEntry(new PelEntryAscii(
PelTag::GPS_LATITUDE_REF, $latitudeRef));
$pelSubIfdGps->addEntry(new PelEntryRational(
PelTag::GPS_LATITUDE,
array($latitudeDegreeMinuteSecond['degree'], 1),
array($latitudeDegreeMinuteSecond['minute'], 1),
array(round($latitudeDegreeMinuteSecond['second'] * 1000), 1000)));
$pelSubIfdGps->addEntry(new PelEntryAscii(
PelTag::GPS_LONGITUDE_REF, $longitudeRef));
$pelSubIfdGps->addEntry(new PelEntryRational(
PelTag::GPS_LONGITUDE,
array($longitudeDegreeMinuteSecond['degree'], 1),
array($longitudeDegreeMinuteSecond['minute'], 1),
array(round($longitudeDegreeMinuteSecond['second'] * 1000), 1000)));
}
$pelSubIfdGps->addEntry(new PelEntryAscii(
PelTag::GPS_LATITUDE_REF, $latitudeRef));
$pelSubIfdGps->addEntry(new PelEntryRational(
PelTag::GPS_LATITUDE,
[$latitudeDegreeMinuteSecond['degree'], 1],
[$latitudeDegreeMinuteSecond['minute'], 1],
[round($latitudeDegreeMinuteSecond['second'] * 1000), 1000]));
$pelSubIfdGps->addEntry(new PelEntryAscii(
PelTag::GPS_LONGITUDE_REF, $longitudeRef));
$pelSubIfdGps->addEntry(new PelEntryRational(
PelTag::GPS_LONGITUDE,
[$longitudeDegreeMinuteSecond['degree'], 1],
[$longitudeDegreeMinuteSecond['minute'], 1],
[round($longitudeDegreeMinuteSecond['second'] * 1000), 1000]));
}
private function degreeDecimalToDegreeMinuteSecond($degreeDecimal) {
$degree = floor($degreeDecimal);
$remainder = $degreeDecimal - $degree;
$minute = floor($remainder * 60);
$remainder = ($remainder * 60) - $minute;
$second = $remainder * 60;
return array('degree' => $degree, 'minute' => $minute, 'second' => $second);
}
private function degreeDecimalToDegreeMinuteSecond($degreeDecimal) {
$degree = floor($degreeDecimal);
$remainder = $degreeDecimal - $degree;
$minute = floor($remainder * 60);
$remainder = ($remainder * 60) - $minute;
$second = $remainder * 60;
return ['degree' => $degree, 'minute' => $minute, 'second' => $second];
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -1,80 +1,80 @@
<?php
namespace OCA\Maps\Settings;
use bantu\IniGetWrapper\IniGetWrapper;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\IL10N;
use OCP\IConfig;
use OCP\Settings\ISettings;
use OCP\Util;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Settings\ISettings;
class AdminSettings implements ISettings {
/** @var IniGetWrapper */
private $iniWrapper;
/** @var IniGetWrapper */
private $iniWrapper;
/** @var IRequest */
private $request;
private $config;
private $dataDirPath;
private $urlGenerator;
private $l;
/** @var IRequest */
private $request;
private $config;
private $dataDirPath;
private $urlGenerator;
private $l;
public function __construct(
IniGetWrapper $iniWrapper,
IL10N $l,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator) {
$this->urlGenerator = $urlGenerator;
$this->iniWrapper = $iniWrapper;
$this->request = $request;
$this->l = $l;
$this->config = $config;
}
public function __construct(
IniGetWrapper $iniWrapper,
IL10N $l,
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator) {
$this->urlGenerator = $urlGenerator;
$this->iniWrapper = $iniWrapper;
$this->request = $request;
$this->l = $l;
$this->config = $config;
}
/**
* @return TemplateResponse
*/
public function getForm() {
$keys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
/**
* @return TemplateResponse
*/
public function getForm() {
$keys = [
'osrmCarURL',
'osrmBikeURL',
'osrmFootURL',
'osrmDEMO',
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
$parameters = [];
foreach ($keys as $k) {
$v = $this->config->getAppValue('maps', $k);
$parameters[$k] = $v;
}
'graphhopperURL'
];
$parameters = [];
foreach ($keys as $k) {
$v = $this->config->getAppValue('maps', $k);
$parameters[$k] = $v;
}
return new TemplateResponse('maps', 'adminSettings', $parameters, '');
}
return new TemplateResponse('maps', 'adminSettings', $parameters, '');
}
/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection() {
return 'additional';
}
/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection() {
return 'additional';
}
/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority() {
return 5;
}
/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority() {
return 5;
}
}

Просмотреть файл

@ -4,7 +4,7 @@ script($appId, $appId . '-adminSettings');
style($appId, 'adminSettings');
$osrmDemoChecked = '';
if (!isset($_['osrmDEMO']) || $_['osrmDEMO'] === '1') {
$osrmDemoChecked = 'checked="checked"';
$osrmDemoChecked = 'checked="checked"';
}
?>
@ -16,13 +16,19 @@ if (!isset($_['osrmDEMO']) || $_['osrmDEMO'] === '1') {
<p><?php p($l->t('Leave URL fields empty to disable OSRM routing provider.')); ?></p><br/>
<div id="osrm">
<label for="osrmCarURL"><?php p($l->t('OSRM server URL (car profile)')); ?></label><br/>
<input id="osrmCarURL" type="text" value="<?php if (isset($_['osrmCarURL'])) p($_['osrmCarURL']); ?>"/><br/>
<input id="osrmCarURL" type="text" value="<?php if (isset($_['osrmCarURL'])) {
p($_['osrmCarURL']);
} ?>"/><br/>
<label for="osrmBikeURL"><?php p($l->t('OSRM server URL (bicycle profile)')); ?></label><br/>
<input id="osrmBikeURL" type="text" value="<?php if (isset($_['osrmBikeURL'])) p($_['osrmBikeURL']); ?>"/><br/>
<input id="osrmBikeURL" type="text" value="<?php if (isset($_['osrmBikeURL'])) {
p($_['osrmBikeURL']);
} ?>"/><br/>
<label for="osrmFootURL"><?php p($l->t('OSRM server URL (foot profile)')); ?></label><br/>
<input id="osrmFootURL" type="text" value="<?php if (isset($_['osrmFootURL'])) p($_['osrmFootURL']); ?>"/><br/>
<input id="osrmFootURL" type="text" value="<?php if (isset($_['osrmFootURL'])) {
p($_['osrmFootURL']);
} ?>"/><br/>
<br/>
<input id="osrmDEMO" type="checkbox" class="checkbox" <?php p($osrmDemoChecked); ?>/>
@ -34,9 +40,13 @@ if (!isset($_['osrmDEMO']) || $_['osrmDEMO'] === '1') {
<div id="graphhopper">
<label for="graphhopperURL"><?php p($l->t('GraphHopper server URL (will use main graphhopper server if empty)')); ?></label><br/>
<input id="graphhopperURL" type="text" value="<?php if (isset($_['graphhopperURL'])) p($_['graphhopperURL']); ?>"/><br/>
<input id="graphhopperURL" type="text" value="<?php if (isset($_['graphhopperURL'])) {
p($_['graphhopperURL']);
} ?>"/><br/>
<label for="graphhopperAPIKEY"><?php p($l->t('GraphHopper API key (mandatory if main server used)')); ?></label><br/>
<input id="graphhopperAPIKEY" type="text" value="<?php if (isset($_['graphhopperAPIKEY'])) p($_['graphhopperAPIKEY']); ?>"/>
<input id="graphhopperAPIKEY" type="text" value="<?php if (isset($_['graphhopperAPIKEY'])) {
p($_['graphhopperAPIKEY']);
} ?>"/>
</div><br/>
<h3><a href="https://www.mapbox.com/" title="<?php p($l->t('Mapbox Website')) ?>" target="_blank"><?php p($l->t('Mapbox settings')); ?></a></h3>
@ -44,7 +54,9 @@ if (!isset($_['osrmDEMO']) || $_['osrmDEMO'] === '1') {
<p><?php p($l->t('Leave empty to disable.')); ?></p><br/>
<div id="mapbox">
<label for="mapboxAPIKEY"><?php p($l->t('Mapbox API key')); ?></label><br/>
<input id="mapboxAPIKEY" type="text" value="<?php if (isset($_['mapboxAPIKEY'])) p($_['mapboxAPIKEY']); ?>"/>
<input id="mapboxAPIKEY" type="text" value="<?php if (isset($_['mapboxAPIKEY'])) {
p($_['mapboxAPIKEY']);
} ?>"/>
</div><br/>
<h3><a href="https://maplibre.org/" title="<?php p($l->t('MapLibre Website')) ?>" target="_blank"><?php p($l->t('MapLibre settings')); ?></a></h3>
@ -52,8 +64,12 @@ if (!isset($_['osrmDEMO']) || $_['osrmDEMO'] === '1') {
<p><?php p($l->t('Leave empty to disable.')); ?></p><br/>
<div id="maplibre_street">
<label for="maplibreStreetStyleURL"><?php p($l->t('MapLibre Street style URL')); ?></label><br/>
<input id="maplibreStreetStyleURL" type="text" value="<?php if (isset($_['maplibreStreetStyleURL'])) p($_['maplibreStreetStyleURL']); ?>"/><br/>
<input id="maplibreStreetStyleURL" type="text" value="<?php if (isset($_['maplibreStreetStyleURL'])) {
p($_['maplibreStreetStyleURL']);
} ?>"/><br/>
<label for="maplibreStreetStyleAuth"><?php p($l->t('Basic Authorization if required. Format is "user:password"')); ?></label><br/>
<input id="maplibreStreetStyleAuth" type="text" value="<?php if (isset($_['maplibreStreetStyleAuth'])) p($_['maplibreStreetStyleAuth']); ?>"/>
<input id="maplibreStreetStyleAuth" type="text" value="<?php if (isset($_['maplibreStreetStyleAuth'])) {
p($_['maplibreStreetStyleAuth']);
} ?>"/>
</div><br/>
</div>

Просмотреть файл

@ -1,5 +1,5 @@
<?php
$appId = OCA\Maps\AppInfo\Application::APP_ID;
script($appId, $appId . '-main');
script($appId, $appId . '-report-error-map-action')
?>
script($appId, $appId . '-report-error-map-action');

Просмотреть файл

@ -13,7 +13,6 @@ namespace OCA\Maps\Tests\Integration\Controller;
use OCP\AppFramework\App;
/**
* This test shows how to make a small Integration Test. Query your class
* directly from the container, only pass in mocks if needed and run your tests
@ -21,15 +20,15 @@ use OCP\AppFramework\App;
*/
class AppTest extends \PHPUnit\Framework\TestCase {
private $container;
private $container;
protected function setUp(): void {
$app = new App('maps');
$this->container = $app->getContainer();
}
protected function setUp(): void {
$app = new App('maps');
$this->container = $app->getContainer();
}
public function testAppInstalled() {
$appManager = $this->container->query(\OCP\App\IAppManager::class);
$this->assertTrue($appManager->isInstalled('maps'));
}
public function testAppInstalled() {
$appManager = $this->container->query(\OCP\App\IAppManager::class);
$this->assertTrue($appManager->isInstalled('maps'));
}
}

Просмотреть файл

@ -24,7 +24,6 @@
namespace tests\Integration\Db;
use ChristophWurst\Nextcloud\Testing\DatabaseTransaction;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OC;
@ -33,93 +32,93 @@ use OCA\Maps\DB\FavoriteShareMapper;
use OCP\AppFramework\Db\DoesNotExistException;
class FavoriteShareMapperTest extends TestCase {
use DatabaseTransaction;
use DatabaseTransaction;
/* @var FavoriteShareMapper */
private $mapper;
/* @var FavoriteShareMapper */
private $mapper;
public function setUp(): void {
parent::setUp();
public function setUp(): void {
parent::setUp();
$this->mapper = new FavoriteShareMapper(
OC::$server->query(\OCP\IDBConnection::class),
OC::$server->getSecureRandom(),
OC::$server->getRootFolder()
);
}
$this->mapper = new FavoriteShareMapper(
OC::$server->query(\OCP\IDBConnection::class),
OC::$server->getSecureRandom(),
OC::$server->getRootFolder()
);
}
public function testCreateByOwnerAndTokenIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->create("testUser", "testCategory");
public function testCreateByOwnerAndTokenIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->create('testUser', 'testCategory');
$this->assertIsString($share->getToken());
$this->assertEquals("testUser", $share->getOwner());
$this->assertEquals("testCategory", $share->getCategory());
}
$this->assertIsString($share->getToken());
$this->assertEquals('testUser', $share->getOwner());
$this->assertEquals('testCategory', $share->getCategory());
}
public function testFindByTokenIsSuccessful() {
/* @var FavoriteShare */
$shareExpected = $this->mapper->create("testUser", "testCategory");
public function testFindByTokenIsSuccessful() {
/* @var FavoriteShare */
$shareExpected = $this->mapper->create('testUser', 'testCategory');
/* @var FavoriteShare */
$shareActual = $this->mapper->findByToken($shareExpected->getToken());
/* @var FavoriteShare */
$shareActual = $this->mapper->findByToken($shareExpected->getToken());
$this->assertEquals($shareExpected->getToken(), $shareActual->getToken());
$this->assertEquals($shareExpected->getOwner(), $shareActual->getOwner());
$this->assertEquals($shareExpected->getCategory(), $shareActual->getCategory());
}
$this->assertEquals($shareExpected->getToken(), $shareActual->getToken());
$this->assertEquals($shareExpected->getOwner(), $shareActual->getOwner());
$this->assertEquals($shareExpected->getCategory(), $shareActual->getCategory());
}
public function testFindByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$shareExpected = $this->mapper->create("testUser", "testCategory");
public function testFindByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$shareExpected = $this->mapper->create('testUser', 'testCategory');
/* @var FavoriteShare */
$shareActual = $this->mapper->findByOwnerAndCategory("testUser", "testCategory");
/* @var FavoriteShare */
$shareActual = $this->mapper->findByOwnerAndCategory('testUser', 'testCategory');
$this->assertEquals($shareExpected->getToken(), $shareActual->getToken());
$this->assertEquals($shareExpected->getOwner(), $shareActual->getOwner());
$this->assertEquals($shareExpected->getCategory(), $shareActual->getCategory());
}
$this->assertEquals($shareExpected->getToken(), $shareActual->getToken());
$this->assertEquals($shareExpected->getOwner(), $shareActual->getOwner());
$this->assertEquals($shareExpected->getCategory(), $shareActual->getCategory());
}
public function testFindAllByOwnerIsSuccessfulAndDoesNotContainOtherShares() {
/* @var FavoriteShare */
$share1 = $this->mapper->create("testUser", "testCategory1");
public function testFindAllByOwnerIsSuccessfulAndDoesNotContainOtherShares() {
/* @var FavoriteShare */
$share1 = $this->mapper->create('testUser', 'testCategory1');
/* @var FavoriteShare */
$share2 = $this->mapper->create("testUser", "testCategory2");
/* @var FavoriteShare */
$share2 = $this->mapper->create('testUser', 'testCategory2');
$this->mapper->create("testUser2", "testCategory");
$this->mapper->create('testUser2', 'testCategory');
/* @var array */
$shares = $this->mapper->findAllByOwner("testUser");
/* @var array */
$shares = $this->mapper->findAllByOwner('testUser');
$shareTokens = array_map(function ($share) {
return $share->getToken();
}, $shares);
$shareTokens = array_map(function ($share) {
return $share->getToken();
}, $shares);
$this->assertEquals(2, count($shareTokens));
$this->assertContains($share1->getToken(), $shareTokens);
$this->assertContains($share2->getToken(), $shareTokens);
}
$this->assertEquals(2, count($shareTokens));
$this->assertContains($share1->getToken(), $shareTokens);
$this->assertContains($share2->getToken(), $shareTokens);
}
public function testFindOrCreateByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->findOrCreateByOwnerAndCategory("testUser", "testCategory");
public function testFindOrCreateByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->findOrCreateByOwnerAndCategory('testUser', 'testCategory');
$this->assertIsString($share->getToken());
$this->assertEquals("testUser", $share->getOwner());
$this->assertEquals("testCategory", $share->getCategory());
}
$this->assertIsString($share->getToken());
$this->assertEquals('testUser', $share->getOwner());
$this->assertEquals('testCategory', $share->getCategory());
}
public function testRemoveByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->create("testUser", "testCategory");
public function testRemoveByOwnerAndCategoryIsSuccessful() {
/* @var FavoriteShare */
$share = $this->mapper->create('testUser', 'testCategory');
$this->mapper->removeByOwnerAndCategory($share->getOwner(), $share->getCategory());
$this->mapper->removeByOwnerAndCategory($share->getOwner(), $share->getCategory());
$this->expectException(DoesNotExistException::class);
$this->expectException(DoesNotExistException::class);
$this->mapper->findByOwnerAndCategory($share->getOwner(), $share->getCategory());
}
$this->mapper->findByOwnerAndCategory($share->getOwner(), $share->getCategory());
}
}

Просмотреть файл

@ -11,122 +11,116 @@
namespace OCA\Maps\Controller;
use \OCA\Maps\AppInfo\Application;
use \OCA\Maps\Service\DevicesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Maps\Service\AddressService;
use OCP\Files\IAppData;
use \OCP\IServerContainer;
use \OCP\EventDispatcher\IEventDispatcher;
use \OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCP\IURLGenerator;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\Service\AddressService;
use OCP\IServerContainer;
class ContactsControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $appName;
private $request;
private $contacts;
private $mapFolder;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $contactsController;
private $contactsController2;
private $utilsController;
private $cdBackend;
private $contactsController;
private $contactsController2;
private $utilsController;
private $cdBackend;
private $root;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->app = new Application();
$c = $this->app->getContainer();
protected function setUp(): void {
$this->app = new Application();
$c = $this->app->getContainer();
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$urlGenerator = $c->getServer()->getURLGenerator();
$urlGenerator = $c->getServer()->getURLGenerator();
$this->contactsManager = $c->query(IServerContainer::class)->getContactsManager();
$this->cm = $c->query(ContactsManager::class);
$this->cm->setupContactsProvider($this->contactsManager, 'test', $urlGenerator);
$this->contactsManager = $c->query(IServerContainer::class)->getContactsManager();
$this->cm = $c->query(ContactsManager::class);
$this->cm->setupContactsProvider($this->contactsManager, 'test', $urlGenerator);
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->appData = $this->getMockBuilder('\OCP\Files\IAppData')
->disableOriginalConstructor()
->getMock();
$this->appData = $this->getMockBuilder('\OCP\Files\IAppData')
->disableOriginalConstructor()
->getMock();
$this->addressService = new AddressService(
$c->query(IServerContainer::class)->getMemCacheFactory(),
$c->query(IServerContainer::class)->getLogger(),
$c->query(IServerContainer::class)->getJobList(),
$this->appData,
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)
);
$this->addressService = new AddressService(
$c->query(IServerContainer::class)->getMemCacheFactory(),
$c->query(IServerContainer::class)->getLogger(),
$c->query(IServerContainer::class)->getJobList(),
$this->appData,
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)
);
//$this->userPrincipalBackend = new Principal(
// $c->getServer()->getUserManager(),
// $c->getServer()->getGroupManager(),
// $c->getServer()->getShareManager(),
// \OC::$server->getUserSession(),
// $c->query(IServerContainer::class)->getConfig(),
// \OC::$server->getAppManager()
//);
$this->userPrincipalBackend = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
->disableOriginalConstructor()
->getMock();
//$this->userPrincipalBackend = new Principal(
// $c->getServer()->getUserManager(),
// $c->getServer()->getGroupManager(),
// $c->getServer()->getShareManager(),
// \OC::$server->getUserSession(),
// $c->query(IServerContainer::class)->getConfig(),
// \OC::$server->getAppManager()
//);
$this->userPrincipalBackend = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
->disableOriginalConstructor()
->getMock();
$this->cdBackend = $c->query(IServerContainer::class)->query(CardDavBackend::class);
$this->cdBackend = $c->query(IServerContainer::class)->query(CardDavBackend::class);
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->mapFolder = $this->createMapFolder();
$this->contactsController = new ContactsController(
$this->contactsController = new ContactsController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
@ -138,33 +132,33 @@ class ContactsControllerTest extends \PHPUnit\Framework\TestCase {
$c->query(IServerContainer::class)->getAvatarManager(),
$this->root,
$urlGenerator);
//$this->contactsController = $this->getMockBuilder('OCA\Maps\Controller\ContactsController')
// ->disableOriginalConstructor()
// ->getMock();
//$this->contactsController = $this->getMockBuilder('OCA\Maps\Controller\ContactsController')
// ->disableOriginalConstructor()
// ->getMock();
$this->contactsController2 = new ContactsController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class),
$this->contactsManager,
$this->addressService,
'test2',
$this->cdBackend,
$c->query(IServerContainer::class)->getAvatarManager(),
$this->contactsController2 = new ContactsController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class),
$this->contactsManager,
$this->addressService,
'test2',
$this->cdBackend,
$c->query(IServerContainer::class)->getAvatarManager(),
$this->root,
$urlGenerator
);
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
}
'test'
);
}
private function createMapFolder() {
$userFolder = $this->root->getUserFolder('test');
@ -175,26 +169,26 @@ class ContactsControllerTest extends \PHPUnit\Framework\TestCase {
}
}
public static function tearDownAfterClass(): void {
}
public static function tearDownAfterClass(): void {
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
public function testAddContact() {
$c = $this->container;
//$this->contacts->createOrUpdate()
//var_dump($this->contactsManager->isEnabled());
// TODO understand why this only returns system address book
//var_dump($this->contactsManager->getUserAddressBooks());
public function testAddContact() {
$c = $this->container;
//$this->contacts->createOrUpdate()
//var_dump($this->contactsManager->isEnabled());
// TODO understand why this only returns system address book
//var_dump($this->contactsManager->getUserAddressBooks());
$resp = $this->contactsController->getContacts();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
//var_dump($data);
}
$resp = $this->contactsController->getContacts();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
//var_dump($data);
}
public function testAddContactMyMap() {
$c = $this->container;

Просмотреть файл

@ -11,394 +11,391 @@
namespace OCA\Maps\Controller;
use OCP\Files\IRootFolder;
use \OCP\IServerContainer;
use \OCA\Maps\AppInfo\Application;
use \OCA\Maps\Service\DevicesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\Service\DevicesService;
use OCP\IServerContainer;
class DevicesApiControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $devicesApiController;
private $devicesApiController2;
private $utilsController;
private $devicesApiController;
private $devicesApiController2;
private $utilsController;
private $root;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->devicesApiController = new DevicesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
'test'
);
$this->devicesApiController = new DevicesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
'test'
);
$this->devicesApiController2 = new DevicesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
'test2'
);
$this->devicesApiController2 = new DevicesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
'test2'
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
'test'
);
// delete
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
}
// delete
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
}
public static function tearDownAfterClass(): void {
}
public static function tearDownAfterClass(): void {
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
public function testAddPoints() {
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
public function testAddPoints() {
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
// delete device that does not exist
$resp = $this->devicesApiController->deleteDevice(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such device', $data);
// delete device that does not exist
$resp = $this->devicesApiController->deleteDevice(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such device', $data);
// correct values
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
// correct values
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$_SERVER['HTTP_USER_AGENT'] = 'testBrowser';
$ts = (new \DateTime())->getTimestamp();
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.2, 2.3, null, null, 1001, 100, 5);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId2 = $data['deviceId'];
$pointId2 = $data['pointId'];
// test user agent is correct
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
$d2Found = false;
foreach ($data as $device) {
if ($device['id'] === $deviceId2) {
$this->assertEquals('testBrowser', $device['user_agent']);
$d2Found = true;
}
}
$this->assertEquals(true, $d2Found);
$_SERVER['HTTP_USER_AGENT'] = 'testBrowser';
$ts = (new \DateTime())->getTimestamp();
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.2, 2.3, null, null, 1001, 100, 5);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId2 = $data['deviceId'];
$pointId2 = $data['pointId'];
// test user agent is correct
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
$d2Found = false;
foreach ($data as $device) {
if ($device['id'] === $deviceId2) {
$this->assertEquals('testBrowser', $device['user_agent']);
$d2Found = true;
}
}
$this->assertEquals(true, $d2Found);
// This happens with a request such as /api/1.0/devices?lat=1.1&lng=2.2&timestamp=&user_agent=testDevice&altitude=&battery=&accuracy=
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, '', 'testDevice', '', '', '');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId3 = $data['deviceId'];
$pointId3 = $data['pointId'];
// This happens with a request such as /api/1.0/devices?lat=1.1&lng=2.2&timestamp=&user_agent=testDevice&altitude=&battery=&accuracy=
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, '', 'testDevice', '', '', '');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId3 = $data['deviceId'];
$pointId3 = $data['pointId'];
// test point values
$resp = $this->devicesApiController->getDevicePoints($deviceId2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 1);
$this->assertEquals(true, $data[0]['timestamp'] >= $ts);
// test point values
$resp = $this->devicesApiController->getDevicePoints($deviceId2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 1);
$this->assertEquals(true, $data[0]['timestamp'] >= $ts);
// invalid values
$resp = $this->devicesApiController->addDevicePoint('1.0', 'aaa', 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
// invalid values
$resp = $this->devicesApiController->addDevicePoint('1.0', 'aaa', 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 'aaa', 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
}
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 'aaa', 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
}
public function testEditDevice() {
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
public function testEditDevice() {
$resp = $this->devicesApiController->getDevices('1.0');
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesApiController->deleteDevice($device['id']);
}
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$resp = $this->devicesApiController->addDevicePoint('1.0', 1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$resp = $this->devicesApiController->editDevice($deviceId, '#001122');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('#001122', $data['color']);
$resp = $this->devicesApiController->editDevice($deviceId, '#001122');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('#001122', $data['color']);
$resp = $this->devicesApiController->editDevice(0, '#001122');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesApiController->editDevice(0, '#001122');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesApiController->editDevice($deviceId, '');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
$resp = $this->devicesApiController->editDevice($deviceId, '');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
//public function testImportExportDevices() {
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $resp = $this->devicesApiController->deleteDevice($device['id']);
// }
//public function testImportExportDevices() {
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $resp = $this->devicesApiController->deleteDevice($device['id']);
// }
// $userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// $content1 = file_get_contents('tests/test_files/devicesOk.gpx');
// $userfolder->newFile('devicesOk.gpx')->putContent($content1);
// $userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// $content1 = file_get_contents('tests/test_files/devicesOk.gpx');
// $userfolder->newFile('devicesOk.gpx')->putContent($content1);
// $resp = $this->devicesApiController->importDevices('/devicesOk.gpx');
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $data = $resp->getData();
// $this->assertEquals(2, $data);
// $resp = $this->devicesApiController->importDevices('/devicesOk.gpx');
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $data = $resp->getData();
// $this->assertEquals(2, $data);
// $resp = $this->devicesApiController->importDevices('/doesNotExist.gpx');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('File does not exist', $data);
// $resp = $this->devicesApiController->importDevices('/doesNotExist.gpx');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('File does not exist', $data);
// $resp = $this->devicesApiController->importDevices('/nc.jpg');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $resp = $this->devicesApiController->importDevices('/nc.jpg');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $resp = $this->devicesApiController->importDevices('/Maps');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $resp = $this->devicesApiController->importDevices('/Maps');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// // get ids
// $devices = [];
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $id = $device['id'];
// $devices[$id] = $device;
// }
// // get number of points
// foreach ($devices as $id=>$device) {
// $resp = $this->devicesApiController->getDevicePoints($id);
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $data = $resp->getData();
// $devices[$id]['nbPoints'] = count($data);
// }
// // get ids
// $devices = [];
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $id = $device['id'];
// $devices[$id] = $device;
// }
// // get number of points
// foreach ($devices as $id=>$device) {
// $resp = $this->devicesApiController->getDevicePoints($id);
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $data = $resp->getData();
// $devices[$id]['nbPoints'] = count($data);
// }
// // export and compare
// $ids = array_keys($devices);
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $exportPath = $resp->getData();
// $this->assertEquals(true, $userfolder->nodeExists($exportPath));
// // export and compare
// $ids = array_keys($devices);
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(200, $status);
// $exportPath = $resp->getData();
// $this->assertEquals(true, $userfolder->nodeExists($exportPath));
// // parse xml and compare number of devices and points
// $xmLData = $userfolder->get($exportPath)->getContent();
// $xml = simplexml_load_string($xmLData);
// $trks = $xml->trk;
// // number of devices
// $this->assertEquals(count($ids), count($trks));
// $pointCountExport = [];
// // count exported points per device
// foreach ($trks as $trk) {
// $name = (string)$trk->name[0];
// $pointCountExport[$name] = count($trk->trkseg[0]->trkpt);
// }
// // check that it matches the data in the DB
// foreach ($devices as $id => $device) {
// $this->assertEquals($device['nbPoints'], $pointCountExport[$device['user_agent']]);
// }
// // parse xml and compare number of devices and points
// $xmLData = $userfolder->get($exportPath)->getContent();
// $xml = simplexml_load_string($xmLData);
// $trks = $xml->trk;
// // number of devices
// $this->assertEquals(count($ids), count($trks));
// $pointCountExport = [];
// // count exported points per device
// foreach ($trks as $trk) {
// $name = (string)$trk->name[0];
// $pointCountExport[$name] = count($trk->trkseg[0]->trkpt);
// }
// // check that it matches the data in the DB
// foreach ($devices as $id => $device) {
// $this->assertEquals($device['nbPoints'], $pointCountExport[$device['user_agent']]);
// }
// // export error
// $resp = $this->devicesApiController->exportDevices(null, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('No device to export', $data);
// // export error
// $resp = $this->devicesApiController->exportDevices(null, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('No device to export', $data);
// $userfolder->get('/Maps')->delete();
// $userfolder->newFile('Maps')->putContent('dummy content');
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('/Maps is not a directory', $data);
// $userfolder->get('/Maps')->delete();
// $userfolder->get('/Maps')->delete();
// $userfolder->newFile('Maps')->putContent('dummy content');
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('/Maps is not a directory', $data);
// $userfolder->get('/Maps')->delete();
// // delete all points
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $resp = $this->devicesApiController->deleteDevice($device['id']);
// }
// // delete all points
// $resp = $this->devicesApiController->getDevices('1.0');
// $data = $resp->getData();
// foreach ($data as $device) {
// $resp = $this->devicesApiController->deleteDevice($device['id']);
// }
// // and then try to export
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('Nothing to export', $data);
//}
// // and then try to export
// $resp = $this->devicesApiController->exportDevices($ids, null, null, true);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('Nothing to export', $data);
//}
public function testEditDevices() {
$this->assertEquals(true, 1==1);
//// valid edition
//$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
//$favId = $resp->getData()['id'];
public function testEditDevices() {
$this->assertEquals(true, 1 == 1);
//// valid edition
//$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
//$favId = $resp->getData()['id'];
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals($favId, $data['id']);
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals($favId, $data['id']);
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['id'] === $favId) {
// $seen = true;
// $this->assertEquals('aa', $fav['name']);
// $this->assertEquals(3.2, $fav['lat']);
// $this->assertEquals(4.2, $fav['lng']);
// $this->assertEquals('cat2', $fav['category']);
// $this->assertEquals('comment', $fav['comment']);
// $this->assertEquals('ext', $fav['extensions']);
// }
//}
//$this->assertEquals(true, $seen);
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['id'] === $favId) {
// $seen = true;
// $this->assertEquals('aa', $fav['name']);
// $this->assertEquals(3.2, $fav['lat']);
// $this->assertEquals(4.2, $fav['lng']);
// $this->assertEquals('cat2', $fav['category']);
// $this->assertEquals('comment', $fav['comment']);
// $this->assertEquals('ext', $fav['extensions']);
// }
//}
//$this->assertEquals(true, $seen);
//// invalid edition
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('invalid values', $data);
//// invalid edition
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('invalid values', $data);
//$resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('No such favorite', $data);
//$resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('No such favorite', $data);
//// rename category
//$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
//$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
//// rename category
//$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
//$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
//$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals('RENAMED', $data);
//// check if renaming worked
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['name'] === 'b') {
// $seen = true;
// $this->assertEquals('cat1RENAMED', $fav['category']);
// }
//}
//$this->assertEquals(true, $seen);
}
//$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals('RENAMED', $data);
//// check if renaming worked
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['name'] === 'b') {
// $seen = true;
// $this->assertEquals('cat1RENAMED', $fav['category']);
// }
//}
//$this->assertEquals(true, $seen);
}
}

Просмотреть файл

@ -11,423 +11,421 @@
namespace OCA\Maps\Controller;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\DB\DeviceShareMapper;
use OCA\Maps\Service\DevicesService;
use OCP\Files\IRootFolder;
use \OCP\IServerContainer;
use \OCA\Maps\AppInfo\Application;
use \OCA\Maps\Service\DevicesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IServerContainer;
class DevicesControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $devicesController;
private $devicesController2;
private $utilsController;
private $devicesController;
private $devicesController2;
private $utilsController;
private $root;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->devicesController = new DevicesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
$this->devicesController = new DevicesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
$c->query(DeviceShareMapper::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(IRootFolder::class),
'test'
);
'test'
);
$this->devicesController2 = new DevicesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
$this->devicesController2 = new DevicesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(DevicesService::class),
$c->query(DeviceShareMapper::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(IRootFolder::class),
'test2'
);
'test2'
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
'test'
);
// delete
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
}
// delete
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
}
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
public function testAddPoints() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
public function testAddPoints() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
// delete device that does not exist
$resp = $this->devicesController->deleteDevice(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such device', $data);
// delete device that does not exist
$resp = $this->devicesController->deleteDevice(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such device', $data);
// correct values
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
// correct values
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$_SERVER['HTTP_USER_AGENT'] = 'testBrowser';
$ts = (new \DateTime())->getTimestamp();
$resp = $this->devicesController->addDevicePoint(1.2, 2.3, null, null, 1001, 100, 5);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId2 = $data['deviceId'];
$pointId2 = $data['pointId'];
// test user agent is correct
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
$d2Found = false;
foreach ($data as $device) {
if ($device['id'] === $deviceId2) {
$this->assertEquals('testBrowser', $device['user_agent']);
$d2Found = true;
}
}
$this->assertEquals(true, $d2Found);
$_SERVER['HTTP_USER_AGENT'] = 'testBrowser';
$ts = (new \DateTime())->getTimestamp();
$resp = $this->devicesController->addDevicePoint(1.2, 2.3, null, null, 1001, 100, 5);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId2 = $data['deviceId'];
$pointId2 = $data['pointId'];
// test user agent is correct
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
$d2Found = false;
foreach ($data as $device) {
if ($device['id'] === $deviceId2) {
$this->assertEquals('testBrowser', $device['user_agent']);
$d2Found = true;
}
}
$this->assertEquals(true, $d2Found);
// test point values
$resp = $this->devicesController->getDevicePoints($deviceId2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 1);
$this->assertEquals(true, $data[0]['timestamp'] >= $ts);
// test point values
$resp = $this->devicesController->getDevicePoints($deviceId2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 1);
$this->assertEquals(true, $data[0]['timestamp'] >= $ts);
// test missing values
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12346, 'testDevice', null, null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
// test missing values
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12346, 'testDevice', null, null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12347, 'testDevice', '', '', '');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12347, 'testDevice', '', '', '');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12348, 'testDevice', 'a', 'b', 'c');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12348, 'testDevice', 'a', 'b', 'c');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$resp = $this->devicesController->getDevicePoints($deviceId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 4);
$resp = $this->devicesController->getDevicePoints($deviceId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, count($data) === 4);
// invalid values
$resp = $this->devicesController->addDevicePoint('aaa', 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
// invalid values
$resp = $this->devicesController->addDevicePoint('aaa', 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
$resp = $this->devicesController->addDevicePoint(1.1, 'aaa', 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
}
$resp = $this->devicesController->addDevicePoint(1.1, 'aaa', 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
}
public function testEditDevice() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
public function testEditDevice() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$resp = $this->devicesController->addDevicePoint(1.1, 2.2, 12345, 'testDevice', 1000, 99, 50);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$deviceId = $data['deviceId'];
$pointId = $data['pointId'];
$resp = $this->devicesController->editDevice($deviceId, '#001122', 'editedDevice');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('#001122', $data['color']);
$this->assertEquals('editedDevice', $data['user_agent']);
$resp = $this->devicesController->editDevice($deviceId, '#001122', 'editedDevice');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('#001122', $data['color']);
$this->assertEquals('editedDevice', $data['user_agent']);
$resp = $this->devicesController->editDevice(0, '#001122', 'editedDevice');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesController->editDevice(0, '#001122', 'editedDevice');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesController->editDevice($deviceId, '', '');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
$resp = $this->devicesController->editDevice($deviceId, '', '');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
public function testImportExportDevices() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
public function testImportExportDevices() {
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$content1 = file_get_contents('tests/test_files/devicesOk.gpx');
$userfolder->newFile('devicesOk.gpx')->putContent($content1);
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$content1 = file_get_contents('tests/test_files/devicesOk.gpx');
$userfolder->newFile('devicesOk.gpx')->putContent($content1);
$resp = $this->devicesController->importDevices('/devicesOk.gpx');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, $data);
$resp = $this->devicesController->importDevices('/devicesOk.gpx');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, $data);
$resp = $this->devicesController->importDevices('/doesNotExist.gpx');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File does not exist', $data);
$resp = $this->devicesController->importDevices('/doesNotExist.gpx');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File does not exist', $data);
$resp = $this->devicesController->importDevices('/nc.jpg');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesController->importDevices('/nc.jpg');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesController->importDevices('/Maps');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->devicesController->importDevices('/Maps');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
// get ids
$devices = [];
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$id = $device['id'];
$devices[$id] = $device;
}
// get number of points
foreach ($devices as $id=>$device) {
$resp = $this->devicesController->getDevicePoints($id);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$devices[$id]['nbPoints'] = count($data);
}
// get ids
$devices = [];
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$id = $device['id'];
$devices[$id] = $device;
}
// get number of points
foreach ($devices as $id => $device) {
$resp = $this->devicesController->getDevicePoints($id);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$devices[$id]['nbPoints'] = count($data);
}
// export and compare
$ids = array_keys($devices);
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$exportPath = $resp->getData();
$this->assertEquals(true, $userfolder->nodeExists($exportPath));
// export and compare
$ids = array_keys($devices);
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$exportPath = $resp->getData();
$this->assertEquals(true, $userfolder->nodeExists($exportPath));
// parse xml and compare number of devices and points
$xmLData = $userfolder->get($exportPath)->getContent();
$xml = simplexml_load_string($xmLData);
$trks = $xml->trk;
// number of devices
$this->assertEquals(count($ids), count($trks));
$pointCountExport = [];
// count exported points per device
foreach ($trks as $trk) {
$name = (string)$trk->name[0];
$pointCountExport[$name] = count($trk->trkseg[0]->trkpt);
}
// check that it matches the data in the DB
foreach ($devices as $id => $device) {
$this->assertEquals($device['nbPoints'], $pointCountExport[$device['user_agent']]);
}
// parse xml and compare number of devices and points
$xmLData = $userfolder->get($exportPath)->getContent();
$xml = simplexml_load_string($xmLData);
$trks = $xml->trk;
// number of devices
$this->assertEquals(count($ids), count($trks));
$pointCountExport = [];
// count exported points per device
foreach ($trks as $trk) {
$name = (string)$trk->name[0];
$pointCountExport[$name] = count($trk->trkseg[0]->trkpt);
}
// check that it matches the data in the DB
foreach ($devices as $id => $device) {
$this->assertEquals($device['nbPoints'], $pointCountExport[$device['user_agent']]);
}
// export error
$resp = $this->devicesController->exportDevices(null, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No device to export', $data);
// export error
$resp = $this->devicesController->exportDevices(null, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No device to export', $data);
$userfolder->get('/Maps')->delete();
$userfolder->newFile('Maps')->putContent('dummy content');
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('/Maps is not a directory', $data);
$userfolder->get('/Maps')->delete();
$userfolder->get('/Maps')->delete();
$userfolder->newFile('Maps')->putContent('dummy content');
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('/Maps is not a directory', $data);
$userfolder->get('/Maps')->delete();
// delete all points
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
// delete all points
$resp = $this->devicesController->getDevices();
$data = $resp->getData();
foreach ($data as $device) {
$resp = $this->devicesController->deleteDevice($device['id']);
}
// and then try to export
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
}
// and then try to export
$resp = $this->devicesController->exportDevices($ids, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
}
public function testEditDevices() {
$this->assertEquals(true, 1==1);
//// valid edition
//$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
//$favId = $resp->getData()['id'];
public function testEditDevices() {
$this->assertEquals(true, 1 == 1);
//// valid edition
//$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
//$favId = $resp->getData()['id'];
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals($favId, $data['id']);
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals($favId, $data['id']);
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['id'] === $favId) {
// $seen = true;
// $this->assertEquals('aa', $fav['name']);
// $this->assertEquals(3.2, $fav['lat']);
// $this->assertEquals(4.2, $fav['lng']);
// $this->assertEquals('cat2', $fav['category']);
// $this->assertEquals('comment', $fav['comment']);
// $this->assertEquals('ext', $fav['extensions']);
// }
//}
//$this->assertEquals(true, $seen);
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['id'] === $favId) {
// $seen = true;
// $this->assertEquals('aa', $fav['name']);
// $this->assertEquals(3.2, $fav['lat']);
// $this->assertEquals(4.2, $fav['lng']);
// $this->assertEquals('cat2', $fav['category']);
// $this->assertEquals('comment', $fav['comment']);
// $this->assertEquals('ext', $fav['extensions']);
// }
//}
//$this->assertEquals(true, $seen);
//// invalid edition
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('invalid values', $data);
//// invalid edition
//$resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$status = $resp->getStatus();
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('invalid values', $data);
//$resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('No such favorite', $data);
//$resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
//$this->assertEquals(400, $status);
//$data = $resp->getData();
//$this->assertEquals('No such favorite', $data);
//// rename category
//$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
//$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
//// rename category
//$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
//$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
//$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals('RENAMED', $data);
//// check if renaming worked
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['name'] === 'b') {
// $seen = true;
// $this->assertEquals('cat1RENAMED', $fav['category']);
// }
//}
//$this->assertEquals(true, $seen);
}
//$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals('RENAMED', $data);
//// check if renaming worked
//$resp = $this->favoritesController->getFavorites();
//$favs = $resp->getData();
//$seen = false;
//foreach ($favs as $fav) {
// if ($fav['name'] === 'b') {
// $seen = true;
// $this->assertEquals('cat1RENAMED', $fav['category']);
// }
//}
//$this->assertEquals(true, $seen);
}
}

Просмотреть файл

@ -11,242 +11,233 @@
namespace OCA\Maps\Controller;
use \OCP\IServerContainer;
use \OCA\Maps\AppInfo\Application;
use \OCA\Maps\Service\FavoritesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\Service\FavoritesService;
use OCP\IServerContainer;
class FavoritesApiControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
class FavoritesApiControllerTest extends \PHPUnit\Framework\TestCase
{
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $root;
private $container;
private $config;
private $app;
private $root;
private $favoritesApiController;
private $favoritesApiController2;
private $utilsController;
private $favoritesApiController;
private $favoritesApiController2;
private $utilsController;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void
{
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function setUp(): void
{
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->favoritesApiController = new FavoritesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
'test'
);
$this->favoritesApiController = new FavoritesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
'test'
);
$this->favoritesApiController2 = new FavoritesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
'test2'
);
$this->favoritesApiController2 = new FavoritesApiController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
'test2'
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
}
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
}
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
public static function tearDownAfterClass(): void
{
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
$resp = $this->favoritesApiController->getFavorites('1.0');
$data = $resp->getData();
foreach ($data as $fav) {
$resp = $this->favoritesApiController->deleteFavorite($fav['id']);
}
}
protected function tearDown(): void
{
// in case there was a failure and something was not deleted
$resp = $this->favoritesApiController->getFavorites('1.0');
$data = $resp->getData();
foreach ($data as $fav) {
$resp = $this->favoritesApiController->deleteFavorite($fav['id']);
}
}
public function testAddFavorites() {
// correct values
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('one', $data['name']);
$id1 = $data['id'];
public function testAddFavorites()
{
// correct values
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('one', $data['name']);
$id1 = $data['id'];
$resp = $this->favoritesApiController->addFavorite('1.0', '', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('', $data['name']);
$id2 = $data['id'];
$resp = $this->favoritesApiController->addFavorite('1.0', '', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('', $data['name']);
$id2 = $data['id'];
// Invalid values
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 'lat', 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
// Invalid values
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 'lat', 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 3.1, 'lon', '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->favoritesApiController->addFavorite('1.0', 'one', 3.1, 'lon', '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
// get favorites
$resp = $this->favoritesApiController->getFavorites('1.0');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, count($data));
// get favorites
$resp = $this->favoritesApiController->getFavorites('1.0');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, count($data));
//// get favorites using etag
//$etag = $resp->getEtag();
//var_dump($etag);
//$this->request->setHeader('If-None-Match', '"'.$etag.'"');
//$resp = $this->favoritesApiController->getFavorites('1.0');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals(2, count($data));
//// get favorites using etag
//$etag = $resp->getEtag();
//var_dump($etag);
//$this->request->setHeader('If-None-Match', '"'.$etag.'"');
//$resp = $this->favoritesApiController->getFavorites('1.0');
//$status = $resp->getStatus();
//$this->assertEquals(200, $status);
//$data = $resp->getData();
//$this->assertEquals(2, count($data));
// delete created favorites
$resp = $this->favoritesApiController->deleteFavorite($id1);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
// delete created favorites
$resp = $this->favoritesApiController->deleteFavorite($id1);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
$resp = $this->favoritesApiController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
$resp = $this->favoritesApiController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
// delete something that does not exist
$resp = $this->favoritesApiController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
// delete something that does not exist
$resp = $this->favoritesApiController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
public function testEditFavorites() {
// valid edition
$resp = $this->favoritesApiController->addFavorite('1.0', 'a', 3.1, 4.1, 'cat1', null, null);
$favId = $resp->getData()['id'];
public function testEditFavorites()
{
// valid edition
$resp = $this->favoritesApiController->addFavorite('1.0', 'a', 3.1, 4.1, 'cat1', null, null);
$favId = $resp->getData()['id'];
$resp = $this->favoritesApiController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals($favId, $data['id']);
$resp = $this->favoritesApiController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals($favId, $data['id']);
$resp = $this->favoritesApiController->getFavorites('1.0');
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['id'] === $favId) {
$seen = true;
$this->assertEquals('aa', $fav['name']);
$this->assertEquals(3.2, $fav['lat']);
$this->assertEquals(4.2, $fav['lng']);
$this->assertEquals('cat2', $fav['category']);
$this->assertEquals('comment', $fav['comment']);
$this->assertEquals('ext', $fav['extensions']);
}
}
$this->assertEquals(true, $seen);
$resp = $this->favoritesApiController->getFavorites('1.0');
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['id'] === $favId) {
$seen = true;
$this->assertEquals('aa', $fav['name']);
$this->assertEquals(3.2, $fav['lat']);
$this->assertEquals(4.2, $fav['lng']);
$this->assertEquals('cat2', $fav['category']);
$this->assertEquals('comment', $fav['comment']);
$this->assertEquals('ext', $fav['extensions']);
}
}
$this->assertEquals(true, $seen);
// invalid edition
$resp = $this->favoritesApiController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
// invalid edition
$resp = $this->favoritesApiController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid values', $data);
$resp = $this->favoritesApiController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such favorite', $data);
}
$resp = $this->favoritesApiController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such favorite', $data);
}
}

Просмотреть файл

@ -13,215 +13,208 @@
namespace OCA\Maps\Controller;
use \OCA\Maps\AppInfo\Application;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\DB\FavoriteShareMapper;
use \OCA\Maps\Service\FavoritesService;
use OCA\Maps\Service\FavoritesService;
use OCP\AppFramework\Http;
use \OCP\IServerContainer;
use OCP\IServerContainer;
class FavoritesControllerTest extends \PHPUnit\Framework\TestCase
{
private $appName;
private $request;
private $contacts;
class FavoritesControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $pageController;
private $pageController2;
private $utilsController;
private $root;
private $pageController;
private $pageController2;
private $utilsController;
private $root;
/* @var FavoritesController */
private $favoritesController;
/* @var FavoritesController */
private $favoritesController;
/* @var FavoritesController */
private $favoritesController2;
/* @var FavoritesController */
private $favoritesController2;
public static function setUpBeforeClass(): void
{
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void
{
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
$this->favoritesController = new FavoritesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(FavoriteShareMapper::class),
'test'
);
$this->favoritesController2 = new FavoritesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(FavoriteShareMapper::class),
'test2'
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
$this->mapFolder = $this->createMapFolder();
}
private function createMapFolder() {
$userFolder = $this->root->getUserFolder('test');
if ($userFolder->nodeExists('Map')) {
return $userFolder->get('Map')->delete();
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
return $userFolder->newFolder('Map');
}
public static function tearDownAfterClass(): void
{
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function tearDown(): void
{
// in case there was a failure and something was not deleted
$resp = $this->favoritesController->getFavorites();
$data = $resp->getData();
foreach ($data as $fav) {
$resp = $this->favoritesController->deleteFavorite($fav['id']);
}
}
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->root = $c->query(IServerContainer::class)->getRootFolder();
public function testAddFavorites()
{
// correct values
$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('one', $data['name']);
$id1 = $data['id'];
$this->favoritesController = new FavoritesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(FavoriteShareMapper::class),
'test'
);
$resp = $this->favoritesController->addFavorite('', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('', $data['name']);
$id2 = $data['id'];
$this->favoritesController2 = new FavoritesController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(FavoritesService::class),
$c->query(IServerContainer::class)->getDateTimeZone(),
$c->query(FavoriteShareMapper::class),
'test2'
);
// invalid values
/* ToDo: Probably test for type error
$resp = $this->favoritesController->addFavorite('one', 'lat', 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->root,
'test'
);
$this->mapFolder = $this->createMapFolder();
}
$resp = $this->favoritesController->addFavorite('one', 3.1, 'lon', '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);*/
private function createMapFolder() {
$userFolder = $this->root->getUserFolder('test');
if ($userFolder->nodeExists('Map')) {
return $userFolder->get('Map')->delete();
}
return $userFolder->newFolder('Map');
}
// get favorites
$resp = $this->favoritesController->getFavorites();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, count($data));
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
// delete created favorites
$resp = $this->favoritesController->deleteFavorite($id1);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
protected function tearDown(): void {
// in case there was a failure and something was not deleted
$resp = $this->favoritesController->getFavorites();
$data = $resp->getData();
foreach ($data as $fav) {
$resp = $this->favoritesController->deleteFavorite($fav['id']);
}
}
$resp = $this->favoritesController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
public function testAddFavorites() {
// correct values
$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('one', $data['name']);
$id1 = $data['id'];
// delete something that does not exist
$resp = $this->favoritesController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
$resp = $this->favoritesController->addFavorite('', 3.1, 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('', $data['name']);
$id2 = $data['id'];
public function testAddFavoritesMyMap()
{
// invalid values
/* ToDo: Probably test for type error
$resp = $this->favoritesController->addFavorite('one', 'lat', 4.2, '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$resp = $this->favoritesController->addFavorite('one', 3.1, 'lon', '', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);*/
// get favorites
$resp = $this->favoritesController->getFavorites();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(2, count($data));
// delete created favorites
$resp = $this->favoritesController->deleteFavorite($id1);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
$resp = $this->favoritesController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('DELETED', $data);
// delete something that does not exist
$resp = $this->favoritesController->deleteFavorite($id2);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
}
public function testAddFavoritesMyMap() {
$myMapId = $this->mapFolder->getId();
// correct values
$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, '', null, null, $myMapId);
@ -281,169 +274,166 @@ class FavoritesControllerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(400, $status);
}
public function testImportExportFavorites()
{
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$content1 = file_get_contents('tests/test_files/favoritesOk.gpx');
$newFile = $userfolder->newFile('favoritesOk.gpx');
$newFile->putContent($content1);
public function testImportExportFavorites() {
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$content1 = file_get_contents('tests/test_files/favoritesOk.gpx');
$newFile = $userfolder->newFile('favoritesOk.gpx');
$newFile->putContent($content1);
$resp = $this->favoritesController->importFavorites('/favoritesOk.gpx');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(27, $data['nbImported']);
$resp = $this->favoritesController->importFavorites('/favoritesOk.gpx');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(27, $data['nbImported']);
// get favorites
$resp = $this->favoritesController->getFavorites();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(27, count($data));
$nbFavorites = count($data);
$categoryCount = [];
foreach ($data as $fav) {
$categoryCount[$fav['category']] = isset($categoryCount[$fav['category']]) ? ($categoryCount[$fav['category']] + 1) : 1;
}
$categories = array_keys($categoryCount);
// get favorites
$resp = $this->favoritesController->getFavorites();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(27, count($data));
$nbFavorites = count($data);
$categoryCount = [];
foreach ($data as $fav) {
$categoryCount[$fav['category']] = isset($categoryCount[$fav['category']]) ? ($categoryCount[$fav['category']] + 1) : 1;
}
$categories = array_keys($categoryCount);
// import errors
$userfolder->newFile('dummy.pdf')->putContent('dummy content');
// import errors
$userfolder->newFile('dummy.pdf')->putContent('dummy content');
$resp = $this->favoritesController->importFavorites('/dummy.gpx');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File does not exist', $data);
$resp = $this->favoritesController->importFavorites('/dummy.gpx');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File does not exist', $data);
$resp = $this->favoritesController->importFavorites('/dummy.pdf');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid file extension', $data);
$resp = $this->favoritesController->importFavorites('/dummy.pdf');
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Invalid file extension', $data);
// export and compare
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$exportPath = $resp->getData();
$this->assertEquals(true, $userfolder->nodeExists($exportPath));
// export and compare
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$exportPath = $resp->getData();
$this->assertEquals(true, $userfolder->nodeExists($exportPath));
// parse xml and compare number of favorite for each category
$xmLData = $userfolder->get($exportPath)->getContent();
$xml = simplexml_load_string($xmLData);
$wpts = $xml->wpt;
$this->assertEquals($nbFavorites, count($wpts));
$categoryCountExport = [];
foreach ($wpts as $wpt) {
$cat = (string)$wpt->type[0];
$categoryCountExport[$cat] = isset($categoryCountExport[$cat]) ? ($categoryCountExport[$cat] + 1) : 1;
}
foreach ($categoryCount as $cat => $nb) {
$this->assertEquals($categoryCountExport[$cat], $nb);
}
// parse xml and compare number of favorite for each category
$xmLData = $userfolder->get($exportPath)->getContent();
$xml = simplexml_load_string($xmLData);
$wpts = $xml->wpt;
$this->assertEquals($nbFavorites, count($wpts));
$categoryCountExport = [];
foreach ($wpts as $wpt) {
$cat = (string)$wpt->type[0];
$categoryCountExport[$cat] = isset($categoryCountExport[$cat]) ? ($categoryCountExport[$cat] + 1) : 1;
}
foreach ($categoryCount as $cat => $nb) {
$this->assertEquals($categoryCountExport[$cat], $nb);
}
// export error
$resp = $this->favoritesController->exportFavorites(null, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
// export error
$resp = $this->favoritesController->exportFavorites(null, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
$userfolder->get('/Maps')->delete();
$userfolder->newFile('Maps')->putContent('dummy content');
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('/Maps is not a directory', $data);
$userfolder->get('/Maps')->delete();
$userfolder->get('/Maps')->delete();
$userfolder->newFile('Maps')->putContent('dummy content');
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('/Maps is not a directory', $data);
$userfolder->get('/Maps')->delete();
// delete all favorites
$resp = $this->favoritesController->getFavorites();
$data = $resp->getData();
$favIds = [];
foreach ($data as $fav) {
array_push($favIds, $fav['id']);
}
$resp = $this->favoritesController->deleteFavorites($favIds);
// delete all favorites
$resp = $this->favoritesController->getFavorites();
$data = $resp->getData();
$favIds = [];
foreach ($data as $fav) {
array_push($favIds, $fav['id']);
}
$resp = $this->favoritesController->deleteFavorites($favIds);
// and then try to export
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
}
// and then try to export
$resp = $this->favoritesController->exportFavorites($categories, null, null, true);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('Nothing to export', $data);
}
public function testEditFavorites()
{
// valid edition
$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
$favId = $resp->getData()['id'];
public function testEditFavorites() {
// valid edition
$resp = $this->favoritesController->addFavorite('a', 3.1, 4.1, 'cat1', null, null);
$favId = $resp->getData()['id'];
$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals($favId, $data['id']);
$resp = $this->favoritesController->editFavorite($favId, 'aa', 3.2, 4.2, 'cat2', 'comment', 'ext');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals($favId, $data['id']);
$resp = $this->favoritesController->getFavorites();
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['id'] === $favId) {
$seen = true;
$this->assertEquals('aa', $fav['name']);
$this->assertEquals(3.2, $fav['lat']);
$this->assertEquals(4.2, $fav['lng']);
$this->assertEquals('cat2', $fav['category']);
$this->assertEquals('comment', $fav['comment']);
$this->assertEquals('ext', $fav['extensions']);
}
}
$this->assertEquals(true, $seen);
$resp = $this->favoritesController->getFavorites();
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['id'] === $favId) {
$seen = true;
$this->assertEquals('aa', $fav['name']);
$this->assertEquals(3.2, $fav['lat']);
$this->assertEquals(4.2, $fav['lng']);
$this->assertEquals('cat2', $fav['category']);
$this->assertEquals('comment', $fav['comment']);
$this->assertEquals('ext', $fav['extensions']);
}
}
$this->assertEquals(true, $seen);
// invalid edition
//ToDo: Probably test for type errors
//
// $resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('Invalid values', $data);
//
// $resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('No such favorite', $data);
// invalid edition
//ToDo: Probably test for type errors
//
// $resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('Invalid values', $data);
//
// $resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext');
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('No such favorite', $data);
// rename category
$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
// rename category
$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null);
$resp = $this->favoritesController->addFavorite('one', 3.1, 4.2, 'cat2', null, null);
$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('RENAMED', $data);
// check if renaming worked
$resp = $this->favoritesController->getFavorites();
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['name'] === 'b') {
$seen = true;
$this->assertEquals('cat1RENAMED', $fav['category']);
}
}
$this->assertEquals(true, $seen);
}
$resp = $this->favoritesController->renameCategories(['cat1'], 'cat1RENAMED');
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('RENAMED', $data);
// check if renaming worked
$resp = $this->favoritesController->getFavorites();
$favs = $resp->getData();
$seen = false;
foreach ($favs as $fav) {
if ($fav['name'] === 'b') {
$seen = true;
$this->assertEquals('cat1RENAMED', $fav['category']);
}
}
$this->assertEquals(true, $seen);
}
public function testEditFavoritesMyMap()
{
public function testEditFavoritesMyMap() {
$this->mapFolder = $this->createMapFolder();
$myMapId = $this->mapFolder->getId();
// valid edition
@ -473,16 +463,16 @@ class FavoritesControllerTest extends \PHPUnit\Framework\TestCase
// invalid edition
//todo: check for type error
// $resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext', $myMapId);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('invalid values', $data);
//
// $resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext', $myMapId);
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('no such favorite', $data);
// $resp = $this->favoritesController->editFavorite($favId, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext', $myMapId);
// $status = $resp->getStatus();
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('invalid values', $data);
//
// $resp = $this->favoritesController->editFavorite(-1, 'aa', 'invalid lat', 4.2, 'cat2', 'comment', 'ext', $myMapId);
// $this->assertEquals(400, $status);
// $data = $resp->getData();
// $this->assertEquals('no such favorite', $data);
// rename category
$resp = $this->favoritesController->addFavorite('b', 3.1, 4.2, 'cat1', null, null, $myMapId);
@ -506,110 +496,110 @@ class FavoritesControllerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(true, $seen);
}
public function testShareUnShareCategory() {
$categoryName = 'test3458565';
public function testShareUnShareCategory() {
$categoryName = 'test3458565';
$id = $this->favoritesController
->addFavorite('Test', 0, 0, $categoryName, "", null)
->getData()['id'];
$id = $this->favoritesController
->addFavorite('Test', 0, 0, $categoryName, '', null)
->getData()['id'];
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$this->favoritesController->deleteFavorite($id);
$this->favoritesController->deleteFavorite($id);
$this->assertEquals(Http::STATUS_OK, $response1->getStatus());
$this->assertEquals(Http::STATUS_OK, $response2->getStatus());
$this->assertEquals(Http::STATUS_OK, $response1->getStatus());
$this->assertEquals(Http::STATUS_OK, $response2->getStatus());
$this->assertIsString($response1->getData()->getToken());
$this->assertTrue($response2->getData()['did_exist']);
}
$this->assertIsString($response1->getData()->getToken());
$this->assertTrue($response2->getData()['did_exist']);
}
public function testShareUnShareCategoryNotAuthorized() {
$categoryName = 'test3458565';
public function testShareUnShareCategoryNotAuthorized() {
$categoryName = 'test3458565';
$id = $this->favoritesController2
->addFavorite("Test2", 0, 0, $categoryName, "", null)
->getData()['id'];
$id = $this->favoritesController2
->addFavorite('Test2', 0, 0, $categoryName, '', null)
->getData()['id'];
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$this->favoritesController->deleteFavorite($id);
$this->favoritesController->deleteFavorite($id);
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response1->getStatus());
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response2->getStatus());
}
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response1->getStatus());
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response2->getStatus());
}
public function testShareUnShareNonExistentCategory() {
$categoryName = 'non_existent';
public function testShareUnShareNonExistentCategory() {
$categoryName = 'non_existent';
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$response1 = $this->favoritesController->shareCategory($categoryName);
$response2 = $this->favoritesController->unShareCategory($categoryName);
$this->favoritesController->unShareCategory($categoryName);
$this->favoritesController->unShareCategory($categoryName);
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response1->getStatus());
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response2->getStatus());
}
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response1->getStatus());
$this->assertEquals(Http::STATUS_BAD_REQUEST, $response2->getStatus());
}
public function testGetSharedCategories() {
$categoryNames = ['test345456', 'test2345465', 'test65765'];
$ids = [];
public function testGetSharedCategories() {
$categoryNames = ['test345456', 'test2345465', 'test65765'];
$ids = [];
foreach ($categoryNames as $categoryName) {
array_push(
$ids,
$this->favoritesController
->addFavorite("Test", 0, 0, $categoryName, "", null)
->getData()['id']
);
$this->favoritesController->shareCategory($categoryName);
}
foreach ($categoryNames as $categoryName) {
array_push(
$ids,
$this->favoritesController
->addFavorite('Test', 0, 0, $categoryName, '', null)
->getData()['id']
);
$this->favoritesController->shareCategory($categoryName);
}
$categories = $this->favoritesController->getSharedCategories();
$categories = $this->favoritesController->getSharedCategories();
$this->assertIsArray($categories->getData());
$this->assertIsArray($categories->getData());
$mappedCategories = array_map(function ($el) {
return $el->getCategory();
}, $categories->getData());
$mappedCategories = array_map(function ($el) {
return $el->getCategory();
}, $categories->getData());
foreach ($categoryNames as $categoryName) {
$this->assertContains($categoryName, $mappedCategories);
}
foreach ($categoryNames as $categoryName) {
$this->assertContains($categoryName, $mappedCategories);
}
foreach ($categoryNames as $categoryName) {
$this->favoritesController->unShareCategory($categoryName);
}
foreach ($categoryNames as $categoryName) {
$this->favoritesController->unShareCategory($categoryName);
}
foreach ($ids as $id) {
$this->favoritesController->deleteFavorite($id);
}
}
foreach ($ids as $id) {
$this->favoritesController->deleteFavorite($id);
}
}
public function testFavoriteShareIsRenamedCorrectly() {
$categoryName = 'test03059035';
$newCategoryName = 'test097876';
public function testFavoriteShareIsRenamedCorrectly() {
$categoryName = 'test03059035';
$newCategoryName = 'test097876';
$id = $this->favoritesController
->addFavorite("Test", 0, 0, $categoryName, "", null)
->getData()['id'];
$id = $this->favoritesController
->addFavorite('Test', 0, 0, $categoryName, '', null)
->getData()['id'];
$this->favoritesController->shareCategory($categoryName);
$this->favoritesController->shareCategory($categoryName);
$this->favoritesController->renameCategories([$categoryName], $newCategoryName);
$this->favoritesController->renameCategories([$categoryName], $newCategoryName);
$shares = $this->favoritesController->getSharedCategories()->getData();
$shares = $this->favoritesController->getSharedCategories()->getData();
$shareNames = array_map(function($el) {
return $el->getCategory();
}, $shares);
$shareNames = array_map(function ($el) {
return $el->getCategory();
}, $shares);
$this->favoritesController->deleteFavorite($id);
$this->favoritesController->unShareCategory($newCategoryName);
$this->favoritesController->deleteFavorite($id);
$this->favoritesController->unShareCategory($newCategoryName);
$this->assertContains($newCategoryName, $shareNames);
$this->assertNotContains($categoryName, $shareNames);
}
$this->assertContains($newCategoryName, $shareNames);
$this->assertNotContains($categoryName, $shareNames);
}
}

Просмотреть файл

@ -11,53 +11,52 @@
namespace OCA\Maps\Controller;
use \OCA\Maps\AppInfo\Application;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\EventDispatcher\IEventDispatcher;
use \OCP\IServerContainer;
use OCA\Maps\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IServerContainer;
class PageControllerTest extends \PHPUnit\Framework\TestCase {
private $controller;
private $userId = 'john';
private $config;
private $controller;
private $userId = 'john';
private $config;
private $eventDispatcher;
private $app;
private $container;
private $app;
private $container;
protected function setUp(): void {
$request = $this->getMockBuilder('OCP\IRequest')->getMock();
$initialStateService = $this->getMockBuilder('OCP\IInitialStateService')->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
protected function setUp(): void {
$request = $this->getMockBuilder('OCP\IRequest')->getMock();
$initialStateService = $this->getMockBuilder('OCP\IInitialStateService')->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->eventDispatcher = $c->query(IServerContainer::class)->query(IEventDispatcher::class);
$this->oldGHValue = $this->config->getAppValue('maps', 'graphhopperURL');
$this->config->setAppValue('maps', 'graphhopperURL', 'https://graphhopper.com:8080');
$this->oldGHValue = $this->config->getAppValue('maps', 'graphhopperURL');
$this->config->setAppValue('maps', 'graphhopperURL', 'https://graphhopper.com:8080');
$this->controller = new PageController(
'maps', $request, $this->eventDispatcher, $this->config, $initialStateService, $this->userId
);
}
$this->controller = new PageController(
'maps', $request, $this->eventDispatcher, $this->config, $initialStateService, $this->userId
);
}
protected function tearDown(): void {
$this->config->setAppValue('maps', 'graphhopperURL', $this->oldGHValue);
}
protected function tearDown(): void {
$this->config->setAppValue('maps', 'graphhopperURL', $this->oldGHValue);
}
public function testIndex() {
$result = $this->controller->index();
public function testIndex() {
$result = $this->controller->index();
$this->assertEquals('main', $result->getTemplateName());
$this->assertTrue($result instanceof TemplateResponse);
}
$this->assertEquals('main', $result->getTemplateName());
$this->assertTrue($result instanceof TemplateResponse);
}
public function testOpenGeoLink() {
$result = $this->controller->openGeoLink('geo:1.1,2.2');
public function testOpenGeoLink() {
$result = $this->controller->openGeoLink('geo:1.1,2.2');
$this->assertEquals('main', $result->getTemplateName());
$this->assertTrue($result instanceof TemplateResponse);
}
$this->assertEquals('main', $result->getTemplateName());
$this->assertTrue($result instanceof TemplateResponse);
}
}

Просмотреть файл

@ -11,207 +11,203 @@
namespace OCA\Maps\Controller;
use \OCA\Maps\AppInfo\Application;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\DB\GeophotoMapper;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\Service\TracksService;
use OCA\Maps\Service\DevicesService;
use OCA\Maps\DB\GeophotoMapper;
use OCP\AppFramework\Http\TemplateResponse;
use \OCP\IServerContainer;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IServerContainer;
class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $photosController;
private $photosController2;
private $utilsController;
private $photosController;
private $photosController2;
private $utilsController;
private $photoFileService;
private $photoFileService;
private $GeoPhotosService;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->rootFolder = $c->query(IServerContainer::class)->getRootFolder();
$this->rootFolder = $c->query(IServerContainer::class)->getRootFolder();
$this->GeoPhotosService = $c->query(GeoPhotoService::class);
$this->photoFileService = new PhotoFilesService(
$c->query(IServerContainer::class)->getLogger(),
$this->photoFileService = new PhotoFilesService(
$c->query(IServerContainer::class)->getLogger(),
$c->query(IServerContainer::class)->getMemCacheFactory(),
$this->rootFolder,
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(GeophotoMapper::class),
$c->query(IServerContainer::class)->getShareManager(),
$c->query(\OCP\BackgroundJob\IJobList::class)
);
$this->rootFolder,
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(GeophotoMapper::class),
$c->query(IServerContainer::class)->getShareManager(),
$c->query(\OCP\BackgroundJob\IJobList::class)
);
$this->photosController = new PhotosController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$this->GeoPhotosService,
$this->photoFileService,
$this->photosController = new PhotosController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$this->GeoPhotosService,
$this->photoFileService,
$this->rootFolder,
'test'
);
);
$this->photosController2 = new PhotosController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$c->query(GeoPhotoService::class),
$this->photoFileService,
$this->photosController2 = new PhotosController(
$this->appName,
$c->query(IServerContainer::class)->getLogger(),
$this->request,
$c->query(GeoPhotoService::class),
$this->photoFileService,
$this->rootFolder,
'test2'
);
'test2'
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->rootFolder,
'test'
);
'test'
);
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// delete files
if ($userfolder->nodeExists('nc.jpg')) {
$file = $userfolder->get('nc.jpg');
$file->delete();
}
if ($userfolder->nodeExists('nut.jpg')) {
$file = $userfolder->get('nut.jpg');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_photos')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// delete files
if ($userfolder->nodeExists('nc.jpg')) {
$file = $userfolder->get('nc.jpg');
$file->delete();
}
if ($userfolder->nodeExists('nut.jpg')) {
$file = $userfolder->get('nut.jpg');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_photos')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
protected function tearDown(): void {
// in case there was a failure and something was not deleted
}
public function testAddGetPhotos() {
$c = $this->app->getContainer();
public function testAddGetPhotos() {
$c = $this->app->getContainer();
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$filename = 'tests/test_files/nc.jpg';
$handle = fopen($filename, 'rb');
$content1 = fread($handle, filesize($filename));
fclose($handle);
$file = $userfolder->newFile('nc.jpgg');
$fp = $file->fopen('wb');
fwrite($fp, $content1);
fclose($fp);
$file->touch();
// rename
$file = $userfolder->get('nc.jpgg');
$file->move($userfolder->getPath().'/nc.jpg');
$file = $userfolder->get('nc.jpg');
$file->touch();
$filename = 'tests/test_files/nc.jpg';
$handle = fopen($filename, 'rb');
$content1 = fread($handle, filesize($filename));
fclose($handle);
$file = $userfolder->newFile('nc.jpgg');
$fp = $file->fopen('wb');
fwrite($fp, $content1);
fclose($fp);
$file->touch();
// rename
$file = $userfolder->get('nc.jpgg');
$file->move($userfolder->getPath().'/nc.jpg');
$file = $userfolder->get('nc.jpg');
$file->touch();
$this->photoFileService->addPhotoNow($file, 'test');
$filename = 'tests/test_files/nut.jpg';
$handle = fopen($filename, 'rb');
$content1 = fread($handle, filesize($filename));
fclose($handle);
$file = $userfolder->newFile('nut.jpgg');
$fp = $file->fopen('wb');
fwrite($fp, $content1);
fclose($fp);
$file->touch();
// rename
$file = $userfolder->get('nut.jpgg');
$file->move($userfolder->getPath().'/nut.jpg');
$file = $userfolder->get('nut.jpg');
$file->touch();
$filename = 'tests/test_files/nut.jpg';
$handle = fopen($filename, 'rb');
$content1 = fread($handle, filesize($filename));
fclose($handle);
$file = $userfolder->newFile('nut.jpgg');
$fp = $file->fopen('wb');
fwrite($fp, $content1);
fclose($fp);
$file->touch();
// rename
$file = $userfolder->get('nut.jpgg');
$file->move($userfolder->getPath().'/nut.jpg');
$file = $userfolder->get('nut.jpg');
$file->touch();
// following section is not valid anymore
// TODO fix photo scan (or make it really better) and then adjust tests ;-)
$this->photoFileService->addPhotoNow($file, 'test');
// following section is not valid anymore
// TODO fix photo scan (or make it really better) and then adjust tests ;-)
$this->photoFileService->addPhotoNow($file, 'test');
$this->GeoPhotosService->clearCache();
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
$this->assertEquals('/nc.jpg', $data[0]->path);
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
$this->assertEquals('/nc.jpg', $data[0]->path);
//Test .nomedia respected
$this->GeoPhotosService->clearCache();
@ -243,13 +239,13 @@ class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals(0, count($data));
$file->delete();
// non localized without track
// non localized without track
$this->GeoPhotosService->clearCache();
$resp = $this->photosController->getNonLocalizedPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(0, $data);
$resp = $this->photosController->getNonLocalizedPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(0, $data);
// with track
$filename = 'tests/test_files/testFile1_locationNut.gpx';
@ -273,7 +269,7 @@ class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
$this->assertStringStartsWith('track:', array_key_first($data));
$dataForTrack = array_shift($data);
$this->assertCount(1, $dataForTrack);
$this->assertEquals('/nut.jpg', $dataForTrack[0]->path);
$this->assertEquals('/nut.jpg', $dataForTrack[0]->path);
//Test .nomedia respected
$this->GeoPhotosService->clearCache();
@ -315,19 +311,19 @@ class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals(0, count($data));
$file->delete();
// place photos
$resp = $this->photosController->placePhotos(['/nut.jpg'], [1.2345], [9.8765]);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
// place photos
$resp = $this->photosController->placePhotos(['/nut.jpg'], [1.2345], [9.8765]);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
$this->GeoPhotosService->clearCache();
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(2, $data);
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(2, $data);
$this->GeoPhotosService->clearCache();
$resp = $this->photosController->getNonLocalizedPhotos();
@ -336,19 +332,19 @@ class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
$data = $resp->getData();
$this->assertCount(0, $data);
// reset coords
$resp = $this->photosController->resetPhotosCoords(['/nut.jpg']);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
// reset coords
$resp = $this->photosController->resetPhotosCoords(['/nut.jpg']);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
$this->GeoPhotosService->clearCache();
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
$resp = $this->photosController->getPhotos();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertCount(1, $data);
//Test usage of cache adding photo
$resp = $this->photosController->placePhotos(['/nut.jpg'], [1.2345], [9.8765]);
@ -371,6 +367,6 @@ class PhotosControllerTest extends \PHPUnit\Framework\TestCase {
$data = $resp->getData();
$this->assertCount(0, $data);
}
}
}

Просмотреть файл

@ -25,104 +25,98 @@ namespace OCA\Maps\Controller;
use OC;
use OC\AppFramework\Http;
use \OCA\Maps\AppInfo\Application;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\DB\FavoriteShareMapper;
use OCA\Maps\Service\FavoritesService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\IAppContainer;
use \OCP\IServerContainer;
use OCP\IServerContainer;
use PHPUnit\Framework\TestCase;
class PublicFavoritePageControllerTest extends TestCase {
/* @var PublicFavoritePageController */
private $publicPageController;
class PublicFavoritePageControllerTest extends TestCase
{
/* @var PublicFavoritePageController */
private $publicPageController;
private $config;
private $config;
/* @var Application */
private $app;
/* @var Application */
private $app;
/* @var IAppContainer */
private $container;
/* @var IAppContainer */
private $container;
/* @var FavoritesService */
private $favoritesService;
/* @var FavoritesService */
private $favoritesService;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
protected function setUp(): void {
// Begin transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->beginTransaction();
protected function setUp(): void
{
// Begin transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->beginTransaction();
$this->app = new Application();
$this->app = new Application();
$this->container = $this->app->getContainer();
$container = $this->container;
$this->container = $this->app->getContainer();
$container = $this->container;
$appName = $container->query('AppName');
$appName = $container->query('AppName');
$this->favoritesService = new FavoritesService(
$container->query(IServerContainer::class)->getLogger(),
$container->query(IServerContainer::class)->getL10N($appName),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(\OCP\IDBConnection::class)
);
$this->favoritesService = new FavoritesService(
$container->query(IServerContainer::class)->getLogger(),
$container->query(IServerContainer::class)->getL10N($appName),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(\OCP\IDBConnection::class)
);
$this->favoriteShareMapper = new FavoriteShareMapper(
$container->query(\OCP\IDBConnection::class),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(IServerContainer::class)->getRootFolder()
);
$this->favoriteShareMapper = new FavoriteShareMapper(
$container->query(\OCP\IDBConnection::class),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(IServerContainer::class)->getRootFolder()
);
$requestMock = $this->getMockBuilder('OCP\IRequest')->getMock();
$sessionMock = $this->getMockBuilder('OCP\ISession')->getMock();
$requestMock = $this->getMockBuilder('OCP\IRequest')->getMock();
$sessionMock = $this->getMockBuilder('OCP\ISession')->getMock();
$this->config = $container->query(IServerContainer::class)->getConfig();
$this->config = $container->query(IServerContainer::class)->getConfig();
$this->publicPageController = new PublicFavoritePageController(
$appName,
$requestMock,
$sessionMock,
$this->config,
$container->query(\OCP\ILogger::class),
$this->favoriteShareMapper
);
}
$this->publicPageController = new PublicFavoritePageController(
$appName,
$requestMock,
$sessionMock,
$this->config,
$container->query(\OCP\ILogger::class),
$this->favoriteShareMapper
);
}
protected function tearDown(): void {
// Rollback transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->rollBack();
}
protected function tearDown(): void
{
// Rollback transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->rollBack();
}
public function testSharedFavoritesCategory() {
$categoryName = 'test908780';
$testUserName = 'test';
public function testSharedFavoritesCategory()
{
$categoryName = 'test908780';
$testUserName = 'test';
$this->favoritesService
->addFavoriteToDB($testUserName, 'Test', 0, 0, $categoryName, '', null);
$share = $this->favoriteShareMapper->create($testUserName, $categoryName);
$this->favoritesService
->addFavoriteToDB($testUserName, "Test", 0, 0, $categoryName, "", null);
$share = $this->favoriteShareMapper->create($testUserName, $categoryName);
$result = $this->publicPageController->sharedFavoritesCategory($share->getToken());
$result = $this->publicPageController->sharedFavoritesCategory($share->getToken());
// Assertions
$this->assertTrue($result instanceof TemplateResponse);
$this->assertEquals('public/favorites_index', $result->getTemplateName());
}
// Assertions
$this->assertTrue($result instanceof TemplateResponse);
$this->assertEquals('public/favorites_index', $result->getTemplateName());
}
public function testAccessRestrictionsForSharedFavoritesCategory() {
$result = $this->publicPageController->sharedFavoritesCategory('test8348985');
public function testAccessRestrictionsForSharedFavoritesCategory()
{
$result = $this->publicPageController->sharedFavoritesCategory('test8348985');
$this->assertTrue($result instanceof DataResponse);
$this->assertEquals(Http::STATUS_NOT_FOUND, $result->getStatus());
}
$this->assertTrue($result instanceof DataResponse);
$this->assertEquals(Http::STATUS_NOT_FOUND, $result->getStatus());
}
}

Просмотреть файл

@ -25,101 +25,98 @@ namespace OCA\Maps\Controller;
use OC;
use OC\AppFramework\Http;
use \OCP\IServerContainer;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\DB\FavoriteShare;
use OCA\Maps\DB\FavoriteShareMapper;
use OCA\Maps\Service\FavoritesService;
use OCP\IServerContainer;
use PHPUnit\Framework\TestCase;
class PublicFavoritesApiControllerTest extends TestCase {
/* @var PublicFavoritesApiController */
private $publicFavoritesApiController;
class PublicFavoritesApiControllerTest extends TestCase
{
/* @var PublicFavoritesApiController */
private $publicFavoritesApiController;
private $config;
private $config;
/* @var FavoritesService */
private $favoritesService;
/* @var FavoritesService */
private $favoritesService;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
/* @var FavoriteShareMapper */
private $favoriteShareMapper;
protected function setUp(): void {
// Begin transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->beginTransaction();
protected function setUp(): void {
// Begin transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->beginTransaction();
$container = (new Application())->getContainer();
$container = (new Application())->getContainer();
$appName = $container->query('AppName');
$appName = $container->query('AppName');
$requestMock = $this->getMockBuilder('OCP\IRequest')->getMock();
$sessionMock = $this->getMockBuilder('OCP\ISession')->getMock();
$requestMock = $this->getMockBuilder('OCP\IRequest')->getMock();
$sessionMock = $this->getMockBuilder('OCP\ISession')->getMock();
$this->config = $container->query(IServerContainer::class)->getConfig();
$this->config = $container->query(IServerContainer::class)->getConfig();
$this->favoritesService = new FavoritesService(
$container->query(IServerContainer::class)->getLogger(),
$container->query(IServerContainer::class)->getL10N($appName),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(\OCP\IDBConnection::class)
);
$this->favoritesService = new FavoritesService(
$container->query(IServerContainer::class)->getLogger(),
$container->query(IServerContainer::class)->getL10N($appName),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(\OCP\IDBConnection::class)
);
$this->favoriteShareMapper = new FavoriteShareMapper(
$container->query(\OCP\IDBConnection::class),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(IserverContainer::class)->getRootFolder()
);
$this->favoriteShareMapper = new FavoriteShareMapper(
$container->query(\OCP\IDBConnection::class),
$container->query(IServerContainer::class)->getSecureRandom(),
$container->query(IserverContainer::class)->getRootFolder()
);
$this->publicFavoritesApiController = new PublicFavoritesApiController(
$appName,
$requestMock,
$sessionMock,
$this->favoritesService,
$this->favoriteShareMapper
);
}
$this->publicFavoritesApiController = new PublicFavoritesApiController(
$appName,
$requestMock,
$sessionMock,
$this->favoritesService,
$this->favoriteShareMapper
);
}
protected function tearDown(): void {
// Rollback transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->rollBack();
}
protected function tearDown(): void
{
// Rollback transaction
$db = OC::$server->query(\OCP\IDBConnection::class);
$db->rollBack();
}
public function testGetFavorites() {
$testUser = 'test099897';
$categoryName = 'test89774590';
public function testGetFavorites() {
$testUser = 'test099897';
$categoryName = 'test89774590';
$this->favoritesService
->addFavoriteToDB($testUser, 'Test1', 0, 0, $categoryName, '', null);
$this->favoritesService
->addFavoriteToDB($testUser, "Test1", 0, 0, $categoryName, "", null);
/* @var FavoriteShare */
$share = $this->favoriteShareMapper->create($testUser, $categoryName);
/* @var FavoriteShare */
$share = $this->favoriteShareMapper->create($testUser, $categoryName);
// Mock token sent by request
$this->publicFavoritesApiController->setToken($share->getToken());
// Mock token sent by request
$this->publicFavoritesApiController->setToken($share->getToken());
$response = $this->publicFavoritesApiController->getFavorites();
$response = $this->publicFavoritesApiController->getFavorites();
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
$data = $response->getData();
$data = $response->getData();
$this->assertIsArray($data);
$this->assertArrayHasKey('share', $data);
$this->assertArrayHasKey('favorites', $data);
$this->assertIsArray($data);
$this->assertArrayHasKey('share', $data);
$this->assertArrayHasKey('favorites', $data);
$this->assertEquals($testUser, $data['share']->getOwner());
$this->assertEquals($categoryName, $data['share']->getCategory());
$this->assertEquals($share->getToken(), $data['share']->getToken());
$this->assertEquals($testUser, $data['share']->getOwner());
$this->assertEquals($categoryName, $data['share']->getCategory());
$this->assertEquals($share->getToken(), $data['share']->getToken());
$this->assertEquals(1, count($data['favorites']));
$this->assertEquals(1, count($data['favorites']));
$el = $data['favorites'][0];
$this->assertEquals('Test1', $el['name']);
$this->assertEquals($categoryName, $el['category']);
}
$el = $data['favorites'][0];
$this->assertEquals('Test1', $el['name']);
$this->assertEquals($categoryName, $el['category']);
}
}

Просмотреть файл

@ -11,285 +11,279 @@
namespace OCA\Maps\Controller;
use \OCA\Maps\AppInfo\Application;
use OCP\DB\QueryBuilder\IQueryBuilder;
use \OCP\IServerContainer;
use OCA\Maps\Service\GeophotoService;
use OCA\Maps\Service\PhotofilesService;
use OCA\Maps\AppInfo\Application;
use OCA\Maps\Service\TracksService;
use OCA\Maps\Service\DevicesService;
use OCA\Maps\DB\GeophotoMapper;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IServerContainer;
class TracksControllerTest extends \PHPUnit\Framework\TestCase {
private $appName;
private $request;
private $contacts;
private $appName;
private $request;
private $contacts;
private $container;
private $config;
private $app;
private $container;
private $config;
private $app;
private $tracksController;
private $tracksController2;
private $utilsController;
private $tracksController;
private $tracksController2;
private $utilsController;
private $tracksService;
private $tracksService;
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
public static function setUpBeforeClass(): void {
$app = new Application();
$c = $app->getContainer();
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
$user = $c->getServer()->getUserManager()->get('test');
$user2 = $c->getServer()->getUserManager()->get('test2');
$user3 = $c->getServer()->getUserManager()->get('test3');
$group = $c->getServer()->getGroupManager()->get('group1test');
$group2 = $c->getServer()->getGroupManager()->get('group2test');
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
// CREATE DUMMY USERS
if ($user === null) {
$u1 = $c->getServer()->getUserManager()->createUser('test', 'tatotitoTUTU');
$u1->setEMailAddress('toto@toto.net');
}
if ($user2 === null) {
$u2 = $c->getServer()->getUserManager()->createUser('test2', 'plopinoulala000');
}
if ($user2 === null) {
$u3 = $c->getServer()->getUserManager()->createUser('test3', 'yeyeahPASSPASS');
}
if ($group === null) {
$c->getServer()->getGroupManager()->createGroup('group1test');
$u1 = $c->getServer()->getUserManager()->get('test');
$c->getServer()->getGroupManager()->get('group1test')->addUser($u1);
}
if ($group2 === null) {
$c->getServer()->getGroupManager()->createGroup('group2test');
$u2 = $c->getServer()->getUserManager()->get('test2');
$c->getServer()->getGroupManager()->get('group2test')->addUser($u2);
}
}
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
protected function setUp(): void {
$this->appName = 'maps';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->contacts = $this->getMockBuilder('OCP\Contacts\IManager')
->disableOriginalConstructor()
->getMock();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->app = new Application();
$this->container = $this->app->getContainer();
$c = $this->container;
$this->config = $c->query(IServerContainer::class)->getConfig();
$this->rootFolder = $c->query(IServerContainer::class)->getRootFolder();
$this->rootFolder = $c->query(IServerContainer::class)->getRootFolder();
$this->tracksService = new TracksService(
$c->query(IServerContainer::class)->getLogger(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$this->rootFolder,
$c->query(IServerContainer::class)->getShareManager(),
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)
);
$this->tracksController = new TracksController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->query(IServerContainer::class)->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(TracksService::class),
'test',
);
$this->tracksController2 = new TracksController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->query(IServerContainer::class)->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(TracksService::class),
'test2',
);
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->tracksService = new TracksService(
$c->query(IServerContainer::class)->getLogger(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$this->rootFolder,
'test'
);
$c->query(IServerContainer::class)->getShareManager(),
$c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)
);
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$this->tracksController = new TracksController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->query(IServerContainer::class)->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(TracksService::class),
'test',
);
// delete first
if ($userfolder->nodeExists('testFile1.gpx')) {
//echo "DELETE\n";
$file = $userfolder->get('testFile1.gpx');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_tracks')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
$this->tracksController2 = new TracksController(
$this->appName,
$this->request,
$c->query(IServerContainer::class),
$c->query(IServerContainer::class)->getConfig(),
$c->query(IServerContainer::class)->getShareManager(),
$c->getServer()->getAppManager(),
$c->getServer()->getUserManager(),
$c->getServer()->getGroupManager(),
$c->query(IServerContainer::class)->getL10N($c->query('AppName')),
$c->query(IServerContainer::class)->getLogger(),
$c->query(TracksService::class),
'test2',
);
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
$this->utilsController = new UtilsController(
$this->appName,
$this->request,
$c->query(IServerContainer::class)->getConfig(),
$c->getServer()->getAppManager(),
$this->rootFolder,
'test'
);
protected function tearDown(): void {
// in case there was a failure and something was not deleted
$c = $this->app->getContainer();
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// delete files
if ($userfolder->nodeExists('testFile1.gpx')) {
$file = $userfolder->get('testFile1.gpx');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_tracks')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
// delete first
if ($userfolder->nodeExists('testFile1.gpx')) {
//echo "DELETE\n";
$file = $userfolder->get('testFile1.gpx');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_tracks')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
public function testAddGetTracks() {
$c = $this->app->getContainer();
public static function tearDownAfterClass(): void {
//$app = new Application();
//$c = $app->getContainer();
//$user = $c->getServer()->getUserManager()->get('test');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test2');
//$user->delete();
//$user = $c->getServer()->getUserManager()->get('test3');
//$user->delete();
//$c->getServer()->getGroupManager()->get('group1test')->delete();
//$c->getServer()->getGroupManager()->get('group2test')->delete();
}
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
protected function tearDown(): void {
// in case there was a failure and something was not deleted
$c = $this->app->getContainer();
$filename = 'tests/test_files/testFile1.gpx';
$content1 = file_get_contents($filename);
$file = $userfolder->newFile('testFile1.gpxx');
$file->putContent($content1);
//$file->touch();
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
// delete files
if ($userfolder->nodeExists('testFile1.gpx')) {
$file = $userfolder->get('testFile1.gpx');
$file->delete();
}
// delete db
$qb = $c->query(IServerContainer::class)->query(\OCP\IDBConnection::class)->getQueryBuilder();
$qb->delete('maps_tracks')
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter('test', IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $qb->resetQueryParts();
}
$file = $userfolder->get('testFile1.gpxx');
$file->move($userfolder->getPath().'/testFile1.gpx');
//echo 'I MOVE TO '.$userfolder->getPath().'/testFile1.gpx'."\n";
$file = $userfolder->get('testFile1.gpx');
$file->touch();
public function testAddGetTracks() {
$c = $this->app->getContainer();
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
$userfolder = $this->container->query(IServerContainer::class)->getUserFolder('test');
foreach ($this->tracksService->rescan('test') as $path) {
//echo $path."\n";
}
$filename = 'tests/test_files/testFile1.gpx';
$content1 = file_get_contents($filename);
$file = $userfolder->newFile('testFile1.gpxx');
$file->putContent($content1);
//$file->touch();
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
//var_dump($data);
$trackId = null;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
$trackId = $v['id'];
$this->assertEquals(true, $v['color'] === null);
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
$file = $userfolder->get('testFile1.gpxx');
$file->move($userfolder->getPath().'/testFile1.gpx');
//echo 'I MOVE TO '.$userfolder->getPath().'/testFile1.gpx'."\n";
$file = $userfolder->get('testFile1.gpx');
$file->touch();
// track content
$resp = $this->tracksController->getTrackFileContent($trackId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, $content1 === $data['content']);
$meta = $data['metadata'];
$this->assertEquals(true, strlen($meta) > 0);
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
// to get stored metadata
$resp = $this->tracksController->getTrackFileContent($trackId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, $content1 === $data['content']);
$this->assertEquals(true, $meta === $data['metadata']);
foreach ($this->tracksService->rescan('test') as $path) {
//echo $path."\n";
}
// file that does not exist
$resp = $this->tracksController->getTrackFileContent(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File not found', $data);
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
//var_dump($data);
$trackId = null;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
$trackId = $v['id'];
$this->assertEquals(true, $v['color'] === null);
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
// edit track
$resp = $this->tracksController->editTrack($trackId, '#002244', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('EDITED', $data);
// track content
$resp = $this->tracksController->getTrackFileContent($trackId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, $content1 === $data['content']);
$meta = $data['metadata'];
$this->assertEquals(true, strlen($meta) > 0);
// check new color
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
$this->assertEquals(true, $v['color'] === '#002244');
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
// to get stored metadata
$resp = $this->tracksController->getTrackFileContent($trackId);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals(true, $content1 === $data['content']);
$this->assertEquals(true, $meta === $data['metadata']);
// edit track that does not exist
$resp = $this->tracksController->editTrack(0, '#002244', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such track', $data);
}
// file that does not exist
$resp = $this->tracksController->getTrackFileContent(0);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('File not found', $data);
// edit track
$resp = $this->tracksController->editTrack($trackId, '#002244', null, null);
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$this->assertEquals('EDITED', $data);
// check new color
$resp = $this->tracksController->getTracks();
$status = $resp->getStatus();
$this->assertEquals(200, $status);
$data = $resp->getData();
$foundTestFile = false;
foreach ($data as $k => $v) {
if ($v['file_path'] === '/testFile1.gpx') {
$foundTestFile = true;
$this->assertEquals(true, $v['color'] === '#002244');
break;
}
}
$this->assertEquals(true, count($data) > 0);
$this->assertEquals(true, $foundTestFile);
// edit track that does not exist
$resp = $this->tracksController->editTrack(0, '#002244', null, null);
$status = $resp->getStatus();
$this->assertEquals(400, $status);
$data = $resp->getData();
$this->assertEquals('No such track', $data);
}
}

Просмотреть файл

@ -3,45 +3,43 @@
namespace OCA\Maps\Helper;
use PHPUnit\Framework\TestCase;
use OCA\Maps\Helper\ExifGeoData;
use OCA\Maps\Helper\ExifDataNoLocationException;
class ExifGeoDataTest extends TestCase {
public function imageWithDateAndLocationProvider(): array {
return [
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation1.JPG", 1311984000 + 7200, 47.071717, 10.339557],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation2.JPG", 1312156800 + 7200, 46.862350, 10.916452],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation3.JPG", 1312070400 + 7200, 47.069058, 10.329370],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation4.JPG", 1312070400 + 7200, 47.059160, 10.312354],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation5.JPG", 1568101093 + 7200, 47.357735, 11.177585],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation6.JPG", 1577630208 + 3600, 50.083045, 9.986018],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation7.jpg", 1568999599 + 7200, 49.420833, 11.114444],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation8.JPG", 1501431401 + 7200, 45.306983, 10.700902],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation9.JPG", 1302998400 + 7200, 52.363055, 4.903418],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation10.JPG", 1501238375 + 7200, 46.388742, 11.266598],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation11.JPG", 1501567361 + 7200, 44.827830, 10.956387],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation12.JPG", 1501591333 + 7200, 44.528283, 11.262207],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation13.jpg", 1640083235 + 3600, 54.359561, 10.017325],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation14.jpg", 1559327910 + 7200, 52.976844, 12.988281],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation15.jpg", 1559332394 + 7200, 52.983697, 12.935217],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation16.jpeg", 1593458542 + 7200, 62.733947, 6.779617],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation17.jpeg", 1593458620 + 7200, 62.733769, 6.777794],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation18.jpeg", 1596136867 + 7200, 54.350891, 9.903506],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation19.jpeg", 1596136833 + 7200, 54.350894, 9.903505],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation20.jpeg", 1592913150 + 7200, 61.351753, 6.519107],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation21.jpg", 1653565075 + 7200, 48.704331, 8.418475],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation22.jpeg", 1593890841 + 7200, 62.735419, 7.155311],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation23.jpeg", 1592904886 + 7200, 61.217086, 6.558886],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation24.jpeg", 1592677991 + 7200, 60.427481, 6.548446],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation25.jpeg", 1592650395 + 7200, 59.860523, 6.696346],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation26.jpeg", 1592770386 + 7200, 60.594022, 6.581317],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation27.jpeg", 1592654095 + 7200, 60.033561, 6.563068],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation28.jpg", 1595326357 + 7200, 59.852992, 6.714458],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation29.jpg", 1594918175 + 7200, 57.595925, 9.976864],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation30.jpg", 1595418724 + 7200, 60.669492, 6.807386],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation31.jpg", 1594934141 + 7200, 57.801164, 8.314269],
["tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation32.jpeg", 1595629060 + 7200, 59.598981, 9.677297],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation1.JPG', 1311984000 + 7200, 47.071717, 10.339557],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation2.JPG', 1312156800 + 7200, 46.862350, 10.916452],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation3.JPG', 1312070400 + 7200, 47.069058, 10.329370],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation4.JPG', 1312070400 + 7200, 47.059160, 10.312354],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation5.JPG', 1568101093 + 7200, 47.357735, 11.177585],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation6.JPG', 1577630208 + 3600, 50.083045, 9.986018],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation7.jpg', 1568999599 + 7200, 49.420833, 11.114444],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation8.JPG', 1501431401 + 7200, 45.306983, 10.700902],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation9.JPG', 1302998400 + 7200, 52.363055, 4.903418],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation10.JPG', 1501238375 + 7200, 46.388742, 11.266598],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation11.JPG', 1501567361 + 7200, 44.827830, 10.956387],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation12.JPG', 1501591333 + 7200, 44.528283, 11.262207],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation13.jpg', 1640083235 + 3600, 54.359561, 10.017325],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation14.jpg', 1559327910 + 7200, 52.976844, 12.988281],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation15.jpg', 1559332394 + 7200, 52.983697, 12.935217],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation16.jpeg', 1593458542 + 7200, 62.733947, 6.779617],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation17.jpeg', 1593458620 + 7200, 62.733769, 6.777794],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation18.jpeg', 1596136867 + 7200, 54.350891, 9.903506],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation19.jpeg', 1596136833 + 7200, 54.350894, 9.903505],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation20.jpeg', 1592913150 + 7200, 61.351753, 6.519107],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation21.jpg', 1653565075 + 7200, 48.704331, 8.418475],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation22.jpeg', 1593890841 + 7200, 62.735419, 7.155311],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation23.jpeg', 1592904886 + 7200, 61.217086, 6.558886],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation24.jpeg', 1592677991 + 7200, 60.427481, 6.548446],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation25.jpeg', 1592650395 + 7200, 59.860523, 6.696346],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation26.jpeg', 1592770386 + 7200, 60.594022, 6.581317],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation27.jpeg', 1592654095 + 7200, 60.033561, 6.563068],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation28.jpg', 1595326357 + 7200, 59.852992, 6.714458],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation29.jpg', 1594918175 + 7200, 57.595925, 9.976864],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation30.jpg', 1595418724 + 7200, 60.669492, 6.807386],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation31.jpg', 1594934141 + 7200, 57.801164, 8.314269],
['tests/test_files/Photos/WithDateAndLocation/imageWithDateAndLocation32.jpeg', 1595629060 + 7200, 59.598981, 9.677297],
];
}
/**
@ -52,13 +50,13 @@ class ExifGeoDataTest extends TestCase {
$exif_geo_data->validate(true);
$this->assertEquals($date, $exif_geo_data->dateTaken);
//This is the same upto ~55cm
$this->assertEqualsWithDelta($lat, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta($lat, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta($lng, $exif_geo_data->lng, 0.000005);
}
public function imageWithZeroIslandProvider(): array {
return [
["tests/test_files/Photos/ZeroIsland/imageZeroIsland1.JPG", 1653829180 + 7200],
['tests/test_files/Photos/ZeroIsland/imageZeroIsland1.JPG', 1653829180 + 7200],
];
}
/**
@ -66,7 +64,7 @@ class ExifGeoDataTest extends TestCase {
*/
public function testImagesWithZeroIslandException(string $path, int $date) {
$this->expectException(ExifDataNoLocationException::class);
$this->expectExceptionMessage("Zero island is not valid");
$this->expectExceptionMessage('Zero island is not valid');
$exif_geo_data = ExifGeoData::get($path);
$exif_geo_data->validate(true);
}
@ -78,11 +76,11 @@ class ExifGeoDataTest extends TestCase {
$exif_geo_data = ExifGeoData::get($path);
try {
$exif_geo_data->validate(true);
$this->assertEquals(true,false);
$this->assertEquals(true, false);
} catch (ExifDataNoLocationException $e) {
$this->assertEquals($date, $exif_geo_data->dateTaken);
//This is the same upto ~55cm
$this->assertEqualsWithDelta(null, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta(null, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta(null, $exif_geo_data->lng, 0.000005);
}
}
@ -95,7 +93,7 @@ class ExifGeoDataTest extends TestCase {
$exif_geo_data->validate(false);
$this->assertEquals($date, $exif_geo_data->dateTaken);
//This is the same upto ~55cm
$this->assertEqualsWithDelta(0.0, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta(0.0, $exif_geo_data->lat, 0.000005);
$this->assertEqualsWithDelta(0.0, $exif_geo_data->lng, 0.000005);
}
}