Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image handling backend #705

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
/**
* Class GeoPhotoException
*/
class ExifDataException extends \Exception
class ExifDataInvalidException extends \Exception
{
}
19 changes: 19 additions & 0 deletions lib/Helper/ExifDataNoLocationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Nextcloud - maps
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Gergely Kovács 2021
* @copyright Gergely Kovács 2021
*/

namespace OCA\Maps\Helper;

/**
* Class GeoPhotoException
*/
class ExifDataNoLocationException extends \Exception
{
}
25 changes: 13 additions & 12 deletions lib/Helper/ExifGeoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ExifGeoData
*/
protected static function get_exif_data_array(string $path) : array{
if( function_exists('exif_read_data') ) {
$data = @exif_read_data($path, null, true);
$data = @exif_read_data($path, null, true)['EXIF'];
if ($data && isset($data[self::LATITUDE]) && isset($data[self::LONGITUDE])) {
return $data;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ protected static function get_exif_data_array(string $path) : array{
}
$exif = [
# self::TIMESTAMP => $pelDateTimeOriginal->getValue(PelEntryTime::EXIF_STRING) // for old pel 0.9.6 and above
self::TIMESTAMP => $pelDateTimeOriginal->getValue() // for new pel >= 0.9.11
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))) {
Expand Down Expand Up @@ -206,17 +206,17 @@ private final function __construct(array $exif_data)
public function validate( $invalidate_zero_iland = false )
{
if (!$this->exif_data) {
throw new ExifDataException('No exif_data found', 1);
throw new ExifDataInvalidException('No exif_data found', 1);
}
if (!is_array($this->exif_data)) {
throw new ExifDataException('exif_data is not an array', 2);
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 ExifDataException('Latitude and/or Longitude are missing from exif data', 3);
throw new ExifDataNoLocationException('Latitude and/or Longitude are missing from exif data', 1);
}
if( $invalidate_zero_iland && $this->isZeroIsland() ){
throw new ExifDataException('Zero island is not valid', 4);
throw new ExifDataNoLocationException('Zero island is not valid', 2);
}
}

Expand All @@ -229,7 +229,7 @@ public function isValid(): bool
try {
$this->validate();
$this->is_valid = true;
} catch (\Throwable $e) {
} catch (\Throwable $e) {
$this->is_valid = false;
}
}
Expand All @@ -241,7 +241,7 @@ public function isValid(): bool
*/
private function parse()
{
if ($this->isValid() && null === $this->latitude && null === $this->longitude && null === $this->timestamp) {
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;
Expand All @@ -250,11 +250,12 @@ private function parse()
if( isset($this->exif_data[self::LATITUDE_REF]) && 'S' === $this->exif_data[self::LATITUDE_REF] ){
$this->latitude*=-1;
}
// optional
if (isset($this->exif_data[self::TIMESTAMP])) {
$this->timestamp = $this->string2time($this->exif_data[self::TIMESTAMP]);
}
}
// 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;
}
}

/**
Expand Down
24 changes: 17 additions & 7 deletions lib/Service/GeophotoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,34 @@ public function getNonLocalizedFromDB ($userId) {
continue;
}
$file = array_shift($files);
if ($file === null) {
continue;
}
$path = preg_replace('/^\/'.$userId.'\//', '', $file->getPath());
if ($file === null) {
continue;
}
$path = $userFolder->getRelativePath( $file->getPath());
$isRoot = $file === $userFolder;

$date = $photoEntity->getDateTaken() ?? \time();
$locations = $this->getLocationGuesses($date);
foreach ($locations as $location) {
$file_object = new \stdClass();
$file_object->fileId = $photoEntity->getFileId();
$file_object->fileid = $file_object->fileId;
$file_object->path = $this->normalizePath($path);
$file_object->hasPreview = in_array($cacheEntry->getMimeType(), $previewEnableMimetypes);
$file_object->lat = $location[0];
$file_object->lng = $location[1];
$file_object->dateTaken = $date;
$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->hasPreview = in_array($cacheEntry->getMimeType(), $previewEnableMimetypes);
$filesById[] = $file_object;
}

Expand All @@ -169,9 +182,6 @@ private function getLocationGuesses($dateTaken) {
$locations[] = $location;
}
}
if (count($locations) === 0) {
$locations[] = [null, null];
}
return $locations;

}
Expand Down
11 changes: 7 additions & 4 deletions lib/Service/PhotofilesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace OCA\Maps\Service;

use lsolesen\pel\PelEntryTime;
use OCA\Maps\Helper\ExifDataException;
use OCA\Maps\Helper\ExifDataInvalidException;
use OCA\Maps\Helper\ExifDataNoLocationException;
use OCA\Maps\Helper\ExifGeoData;
use OCP\Files\FileInfo;
use OCP\IL10N;
Expand Down Expand Up @@ -187,7 +188,7 @@ public function deleteByFolderIdUserId($folderId, $userId) {
}

public function setPhotosFilesCoords($userId, $paths, $lats, $lngs, $directory) {
if ($directory === 'true') {
if ($directory) {
return $this->setDirectoriesCoords($userId, $paths, $lats, $lngs);
}
else {
Expand Down Expand Up @@ -365,10 +366,12 @@ private function getExif($file) : ?ExifGeoData {
try{
$exif_geo_data = ExifGeoData::get($path);
$exif_geo_data->validate(true);
}catch(ExifDataException $e){
}catch(ExifDataInvalidException $e){
$exif_geo_data = null;
$this->logger->notice($e->getMessage(), ['code'=>$e->getCode(),'path'=>$path]);
}catch(\Throwable $f){
}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]);
}
Expand Down