2626use OCP \Files \Folder ;
2727use OCP \Files \IRootFolder ;
2828use OCP \Files \Node ;
29+ use OCP \Files \NotFoundException ;
2930use OCP \Files \StorageNotAvailableException ;
3031use OCP \ICache ;
3132use OCP \ICacheFactory ;
@@ -248,39 +249,58 @@ public function getBackgroundJobStatus(string $userId): array {
248249 ];
249250 }
250251
251- public function setPhotosFilesCoords (string $ userId , $ paths , $ lats , $ lngs , $ directory ): array {
252+ /**
253+ * @param list<string> $paths
254+ * @param list<float> $lats
255+ * @param list<float> $lngs
256+ * @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
257+ */
258+ public function setPhotosFilesCoords (string $ userId , $ paths , $ lats , $ lngs , bool $ directory ): array {
252259 if ($ directory ) {
253260 return $ this ->setDirectoriesCoords ($ userId , $ paths , $ lats , $ lngs );
254261 }
255262
256263 return $ this ->setFilesCoords ($ userId , $ paths , $ lats , $ lngs );
257264 }
258265
266+ /**
267+ * @param list<string> $paths
268+ * @param list<float> $lats
269+ * @param list<float> $lngs
270+ * @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
271+ */
259272 private function setDirectoriesCoords (string $ userId , $ paths , $ lats , $ lngs ): array {
260273 $ lat = $ lats [0 ] ?? 0 ;
261274 $ lng = $ lngs [0 ] ?? 0 ;
262275 $ userFolder = $ this ->root ->getUserFolder ($ userId );
263276 $ done = [];
264277 foreach ($ paths as $ dirPath ) {
265278 $ cleanDirPath = str_replace (['../ ' , '.. \\' ], '' , $ dirPath );
266- if ( $ userFolder -> nodeExists ( $ cleanDirPath )) {
279+ try {
267280 $ dir = $ userFolder ->get ($ cleanDirPath );
268- if ($ dir instanceof Folder) {
269- $ nodes = $ dir ->getDirectoryListing ();
270- foreach ($ nodes as $ node ) {
271- if ($ this ->isPhoto ($ node ) && $ node ->isUpdateable ()) {
272- $ photo = $ this ->photoMapper ->findByFileIdUserId ($ node ->getId (), $ userId );
273- $ done [] = [
274- 'path ' => preg_replace ('/^files/ ' , '' , (string )$ node ->getInternalPath ()),
275- 'lat ' => $ lat ,
276- 'lng ' => $ lng ,
277- 'oldLat ' => $ photo ? $ photo ->getLat () : null ,
278- 'oldLng ' => $ photo ? $ photo ->getLng () : null ,
279- ];
280- $ this ->setExifCoords ($ node , $ lat , $ lng );
281- $ this ->updateByFileNow ($ node );
282- }
281+ } catch (NotFoundException ) {
282+ continue ;
283+ }
284+ if (!$ dir instanceof Folder) {
285+ continue ;
286+ }
287+ $ nodes = $ dir ->getDirectoryListing ();
288+ foreach ($ nodes as $ node ) {
289+ if ($ this ->isPhoto ($ node ) && $ node ->isUpdateable ()) {
290+ try {
291+ $ photo = $ this ->photoMapper ->findByFileIdUserId ($ node ->getId (), $ userId );
292+ } catch (DoesNotExistException ) {
293+ $ photo = null ;
283294 }
295+ $ done [] = [
296+ 'path ' => preg_replace ('/^files/ ' , '' , (string )$ node ->getInternalPath ()),
297+ 'lat ' => $ lat ,
298+ 'lng ' => $ lng ,
299+ 'oldLat ' => $ photo ?->getLat(),
300+ 'oldLng ' => $ photo ?->getLng(),
301+ ];
302+ $ this ->setExifCoords ($ node , $ lat , $ lng );
303+ $ this ->updateByFileNow ($ node );
284304 }
285305 }
286306 }
@@ -289,43 +309,49 @@ private function setDirectoriesCoords(string $userId, $paths, $lats, $lngs): arr
289309 }
290310
291311 /**
292- * @return array{path: (array | string | null), lat: mixed, lng: mixed, oldLat: mixed, oldLng: mixed}[]
312+ * @param list<string> $paths
313+ * @param list<float> $lats
314+ * @param list<float> $lngs
315+ * @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
293316 */
294- private function setFilesCoords (string $ userId , $ paths , array $ lats , array $ lngs ): array {
317+ private function setFilesCoords (string $ userId , array $ paths , array $ lats , array $ lngs ): array {
295318 $ userFolder = $ this ->root ->getUserFolder ($ userId );
296319 $ done = [];
297320
298321 foreach ($ paths as $ i => $ path ) {
299- $ cleanpath = str_replace (['../ ' , '.. \\' ], '' , $ path );
300- if ($ userFolder ->nodeExists ($ cleanpath )) {
301- $ file = $ userFolder ->get ($ cleanpath );
302- if ($ file instanceof File && $ this ->isPhoto ($ file ) && $ file ->isUpdateable ()) {
303- $ lat = (count ($ lats ) > $ i ) ? $ lats [$ i ] : $ lats [0 ];
304- $ lng = (count ($ lngs ) > $ i ) ? $ lngs [$ i ] : $ lngs [0 ];
305- try {
306- $ photo = $ this ->photoMapper ->findByFileIdUserId ($ file ->getId (), $ userId );
307- } catch (DoesNotExistException ) {
308- $ photo = null ;
309- }
322+ $ cleanPath = str_replace (['../ ' , '.. \\' ], '' , $ path );
323+ try {
324+ $ file = $ userFolder ->get ($ cleanPath );
325+ } catch (NotFoundException ) {
326+ continue ;
327+ }
310328
311- $ done [] = [
312- 'path ' => preg_replace ('/^files/ ' , '' , $ file ->getInternalPath ()),
313- 'lat ' => $ lat ,
314- 'lng ' => $ lng ,
315- 'oldLat ' => $ photo ? $ photo ->getLat () : null ,
316- 'oldLng ' => $ photo ? $ photo ->getLng () : null ,
317- ];
318- $ this ->setExifCoords ($ file , $ lat , $ lng );
319- $ this ->updateByFileNow ($ file );
329+ if ($ file instanceof File && $ this ->isPhoto ($ file ) && $ file ->isUpdateable ()) {
330+ $ lat = (count ($ lats ) > $ i ) ? $ lats [$ i ] : $ lats [0 ];
331+ $ lng = (count ($ lngs ) > $ i ) ? $ lngs [$ i ] : $ lngs [0 ];
332+ try {
333+ $ photo = $ this ->photoMapper ->findByFileIdUserId ($ file ->getId (), $ userId );
334+ } catch (DoesNotExistException ) {
335+ $ photo = null ;
320336 }
337+
338+ $ done [] = [
339+ 'path ' => preg_replace ('/^files/ ' , '' , $ file ->getInternalPath ()),
340+ 'lat ' => $ lat ,
341+ 'lng ' => $ lng ,
342+ 'oldLat ' => $ photo ?->getLat(),
343+ 'oldLng ' => $ photo ?->getLng(),
344+ ];
345+ $ this ->setExifCoords ($ file , $ lat , $ lng );
346+ $ this ->updateByFileNow ($ file );
321347 }
322348 }
323349
324350 return $ done ;
325351 }
326352
327353 /**
328- * @return array{path: (array | string | null), lat: null, lng: null, oldLat: mixed , oldLng: mixed}[]
354+ * @return list< array{path: (array | string | null), lat: null, lng: null, oldLat: ?float , oldLng: ?float}>
329355 */
330356 public function resetPhotosFilesCoords ($ userId , $ paths ): array {
331357 $ userFolder = $ this ->root ->getUserFolder ($ userId );
0 commit comments