Skip to content

Commit

Permalink
Merge pull request #919 from nextcloud/register-mimetypes
Browse files Browse the repository at this point in the history
Register mimetypes
  • Loading branch information
tacruc authored Jan 7, 2023
2 parents 48f1767 + f56f57d commit cdf5eba
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 103 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future plans:
## User Documentation
### My Maps
Custom maps are stored by default in the "/Maps" folder. This folder can be found in the
files app. Other folders turned into map by placing a ".maps" file into it.
files app. Other folders turned into map by placing a ".index.maps" file into it.
Content can therefore be added via:
- Webdav (Desktop and Mobile clients)
- Files app
Expand Down Expand Up @@ -56,7 +56,7 @@ They can be added from files
or from maps
![](screenshots/addPhotosFromMap.gif)
Existing photo albums can be viewed
on the map by placing a ".maps" file into it.
on the map by placing a ".index.maps" file into it.
![](screenshots/photoAlbumOnMap.gif)

Scanning photos take time. Therefore photos are scanned in the background.
Expand Down
7 changes: 7 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@
<repair-steps>
<install>
<step>OCA\Maps\Migration\InstallScan</step>
<step>OCA\Maps\Migration\RegisterMimeType</step>
</install>
<post-migrate>
<step>OCA\Maps\Migration\RegisterMimeType</step>
</post-migrate>
<uninstall>
<step>OCA\Maps\Migration\UnregisterMimeType</step>
</uninstall>
</repair-steps>
<commands>
<command>OCA\Maps\Command\RescanPhotos</command>
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
['name' => 'photos#getNonLocalizedPhotosFromDb', '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'],

// contacts
['name' => 'contacts#getContacts', 'url' => '/contacts', 'verb' => 'GET'],
Expand Down
73 changes: 73 additions & 0 deletions img/reload.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions lib/Controller/PhotosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,17 @@ public function resetPhotosCoords($paths, $myMapId=null): DataResponse {
return new DataResponse($result);
}

/**
* @NoAdminRequired
* @return DataResponse
*/
public function clearCache(): DataResponse {
$result = $this->geophotoService->clearCache();
if ($result) {
return new DataResponse("Cache cleared");
} else {
return new DataResponse("Failed to clear Cache", 400);
}
}

}
10 changes: 5 additions & 5 deletions lib/Controller/UtilsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function saveOptionValue($options, $myMapId=null): DataResponse {
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file=$folder->get(".maps");
$file=$folder->get(".index.maps");
} catch (NotFoundException $e) {
$file=$folder->newFile(".maps", $content = "{}");
$file=$folder->newFile(".index.maps", $content = "{}");
}
try {
$ov = json_decode($file->getContent(),true, 512);
Expand Down Expand Up @@ -127,13 +127,13 @@ public function getOptionsValues($myMapId=null): DataResponse {
$folders = $userFolder->getById($myMapId);
$folder = array_shift($folders);
try {
$file=$folder->get(".maps");
$file=$folder->get(".index.maps");
} catch (NotFoundException $e) {
$file=$folder->newFile(".maps", $content = "{}");
$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 .maps file
//We can delete the map by deleting the folder or the .index.maps file
$ov['isDeletable'] = $folder->isDeletable() || $file->isDeletable();
// Maps content can be read mostly from the folder
$ov['isReadable'] = $folder->isReadable();
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/LoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(Event $event): void {
return;
}

Util::addScript('maps', 'filetypes');
Util::addScript('maps', 'maps-filetypes');
Util::addStyle('maps', 'filetypes');
}
}
76 changes: 76 additions & 0 deletions lib/Migration/RegisterMimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace OCA\Maps\Migration;

use OCP\Files\IMimeTypeLoader;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class RegisterMimeType implements IRepairStep
{
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';

protected IMimeTypeLoader $mimeTypeLoader;

public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
$this->mimeTypeLoader = $mimeTypeLoader;
}

public function getName()
{
return 'Register Maps MIME types"';
}

private function registerForExistingFiles()
{
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps');
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId);

$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-noindex');
$this->mimeTypeLoader->updateFilecache('noindex', $mimeTypeId);

$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-nomedia');
$this->mimeTypeLoader->updateFilecache('nomedia', $mimeTypeId);

$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-noimage');
$this->mimeTypeLoader->updateFilecache('noimage', $mimeTypeId);

$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps-notrack');
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId);
}

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)) {
$existingMapping = json_decode(file_get_contents($mappingFile), true);
if (json_last_error() === JSON_ERROR_NONE && is_array($existingMapping)) {
$mapping = array_merge($existingMapping, $mapping);
}
}

file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}

public function run(IOutput $output)
{
$output->info('Registering the mimetype...');

// Register the mime type for existing files
$this->registerForExistingFiles();

// Register the mime type for new files
$this->registerForNewFiles();

$output->info('The mimetype was successfully registered.');
}
}
67 changes: 67 additions & 0 deletions lib/Migration/UnregisterMimType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace OCA\Maps\Migration;

use OCP\Files\IMimeTypeLoader;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class UnregisterMimeType implements IRepairStep
{
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';

protected IMimeTypeLoader $mimeTypeLoader;

public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
$this->mimeTypeLoader = $mimeTypeLoader;
}

public function getName()
{
return 'Register Maps MIME types"';
}

private function unregisterForExistingFiles()
{
$mimeTypeId = $this->mimeTypeLoader->getId('application/octet-stream');
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId);
$this->mimeTypeLoader->updateFilecache('noindex', $mimeTypeId);
$this->mimeTypeLoader->updateFilecache('nomedia', $mimeTypeId);
$this->mimeTypeLoader->updateFilecache('noimage', $mimeTypeId);
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId);
}

private function unregisterForNewFiles()
{
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING;

if (file_exists($mappingFile)) {
$mapping = json_decode(file_get_contents($mappingFile), true);
if (json_last_error() === JSON_ERROR_NONE && is_array($mapping)) {
unset($mapping['maps']);
unset($mapping['noindex']);
unset($mapping['nomedia']);
unset($mapping['noimage']);
unset($mapping['notrack']);
} else {
$mapping = [];
}
file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}

public function run(IOutput $output)
{
$output->info('Unregistering the mimetype...');

// Register the mime type for existing files
$this->unregisterForExistingFiles();

// Register the mime type for new files
$this->unregisterForNewFiles();

$output->info('The mimetype was successfully unregistered.');
}
}

26 changes: 12 additions & 14 deletions lib/Service/GeophotoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ public function __construct (ILogger $logger,

/**
* @param string $userId
* @return void
* @return bool
*/
public function clearCache(string $userId=''): void {
$this->photosCache->clear($userId);
$this->nonLocalizedPhotosCache->clear($userId);
public function clearCache(string $userId=''): bool {
$a = $this->photosCache->clear($userId);
$b = $this->nonLocalizedPhotosCache->clear($userId);
return $a and $b;
}

/**
Expand Down Expand Up @@ -258,25 +259,22 @@ private function getIgnoredPaths($userId, $folder=null, $hideImagesOnCustomMaps=
if (is_null($folder)) {
$folder = $userFolder;
}
$ignoreMarkerFiles = [
'.nomedia',
'.noimage',
'.noindex',
$ignoreFileMimetypes = [
'application/x-nextcloud-noindex',
'application/x-nextcloud-nomedia',
'application/x-nextcloud-noimage',
];
if ($hideImagesOnCustomMaps) {
$ignoreMarkerFiles[] = '.maps';
$ignoreFileMimetypes[] = 'application/x-nextcloud-maps';
}
$func = function(string $i): SearchComparison {
return new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', $i);
return new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $i);
};
$excludedNodes = $folder->search(new SearchQuery(
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'application/octet-stream'),
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, array_map(
$func,
$ignoreMarkerFiles)
$ignoreFileMimetypes)
),
]),
0,
0,
[]
Expand Down
Loading

0 comments on commit cdf5eba

Please sign in to comment.