From 18a9eb43c307d5820db1ff9e4582e6e91caf2d55 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Wed, 24 Apr 2019 17:50:06 +0200 Subject: [PATCH] photo context menu: reset coordinates Signed-off-by: Julien Veyssier --- appinfo/routes.php | 1 + js/photosController.js | 44 ++++++++++++++++++++++++++ lib/Controller/PhotosController.php | 8 +++++ lib/Service/PhotofilesService.php | 49 ++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index e9f7b536..e4948c65 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -22,6 +22,7 @@ return [ ['name' => 'photos#getPhotosFromDb', 'url' => '/photos', 'verb' => 'GET'], ['name' => 'photos#getNonLocalizedPhotosFromDb', 'url' => '/photos/nonlocalized', 'verb' => 'GET'], ['name' => 'photos#placePhotos', 'url' => '/photos', 'verb' => 'POST'], + ['name' => 'photos#resetPhotosCoords', 'url' => '/photos', 'verb' => 'DELETE'], // contacts ['name' => 'contacts#getContacts', 'url' => '/contacts', 'verb' => 'GET'], diff --git a/js/photosController.js b/js/photosController.js index ddb2758f..a8a7a508 100644 --- a/js/photosController.js +++ b/js/photosController.js @@ -60,6 +60,12 @@ PhotosController.prototype = { that.enterMovePhotoMode(); that.map.closePopup(); }); + $('body').on('click', '.resetphoto', function(e) { + var ul = $(this).parent().parent(); + var filePath = ul.attr('filepath'); + that.resetPhotoCoords([filePath]); + that.map.closePopup(); + }); }, updateMyFirstLastDates: function() { @@ -423,5 +429,43 @@ PhotosController.prototype = { }); }, + resetPhotoCoords: function(paths) { + var that = this; + $('#navigation-photos').addClass('icon-loading-small'); + $('.leaflet-container').css('cursor', 'wait'); + var req = { + paths: paths + }; + var url = OC.generateUrl('/apps/maps/photos'); + $.ajax({ + type: 'DELETE', + url: url, + data: req, + async: true + }).done(function (response) { + OC.Notification.showTemporary(t('maps', '{nb} photos reset', {nb: response})); + if (response > 0) { + that.photosDataLoaded = false; + for (var i=0; i < that.photoMarkers.length; i++) { + that.photoLayer.removeLayer(that.photoMarkers[i]); + } + that.photoMarkers = []; + that.photoMarkersOldest = null; + that.photoMarkersNewest = null; + that.photoMarkersFirstVisible = 0; + that.photoMarkersLastVisible = -1; + that.timeFilterBegin = 0; + that.timeFilterEnd = Date.now(); + + that.showLayer(); + } + }).always(function (response) { + $('#navigation-photos').removeClass('icon-loading-small'); + $('.leaflet-container').css('cursor', 'grab'); + }).fail(function(response) { + OC.Notification.showTemporary(t('maps', 'Failed to reset photos coordinates') + ': ' + response.responseText); + }); + }, + }; diff --git a/lib/Controller/PhotosController.php b/lib/Controller/PhotosController.php index 5e72faea..bb009df8 100644 --- a/lib/Controller/PhotosController.php +++ b/lib/Controller/PhotosController.php @@ -62,4 +62,12 @@ class PhotosController extends Controller { return new DataResponse($result); } + /** + * @NoAdminRequired + */ + public function resetPhotosCoords($paths) { + $result = $this->photofilesService->resetPhotosFilesCoords($this->userId, $paths); + return new DataResponse($result); + } + } diff --git a/lib/Service/PhotofilesService.php b/lib/Service/PhotofilesService.php index c7c2f2c7..ff55ae9e 100644 --- a/lib/Service/PhotofilesService.php +++ b/lib/Service/PhotofilesService.php @@ -236,6 +236,24 @@ class PhotofilesService { return $nbDone; } + public function resetPhotosFilesCoords($userId, $paths) { + $userFolder = $this->root->getUserFolder($userId); + $nbDone = 0; + + foreach ($paths as $i => $path) { + $cleanpath = str_replace(array('../', '..\\'), '', $path); + if ($userFolder->nodeExists($cleanpath)) { + $file = $userFolder->get($cleanpath); + if ($this->isPhoto($file) and $file->isUpdateable()) { + $this->resetExifCoords($file); + $this->photoMapper->updateByFileId($file->getId(), null, null); + $nbDone++; + } + } + } + return $nbDone; + } + private function addPhoto($photo, $userId) { $exif = $this->getExif($photo); if (!is_null($exif)) { @@ -387,9 +405,38 @@ class PhotofilesService { } } - private function setExifCoords($file, $lat, $lng) { + private function resetExifCoords($file) { $path = $file->getStorage()->getLocalFile($file->getInternalPath()); + $data = new PelDataWindow($file->getContent()); + $pelJpeg = new PelJpeg($data); + + $pelExif = $pelJpeg->getExif(); + if ($pelExif == null) { + $pelExif = new PelExif(); + $pelJpeg->setExif($pelExif); + } + + $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); + } + + $pelSubIfdGps = new PelIfd(PelIfd::GPS); + $pelIfd0->addSubIfd($pelSubIfdGps); + + $file->putContent($pelJpeg->getBytes()); + } + + private function setExifCoords($file, $lat, $lng) { + $path = $file->getStorage()->getLocalFile($file->getInternalPath()); $data = new PelDataWindow($file->getContent()); $pelJpeg = new PelJpeg($data);