X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FApiBundle%2FController%2FEntryRestController.php;h=e9e1aca3b2c2d6ea2f08453cc615db659aee1b33;hb=08f29ae7b6af585f3af1dd2f8b01854fb0985668;hp=c5bf1df819e4183a53eb2de8fc8acb86b8eaf8b7;hpb=1f00547836d1e832b31368413dccb32586206321;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index c5bf1df8..e9e1aca3 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -199,10 +199,19 @@ class EntryRestController extends WallabagRestController $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); if (false === $entry) { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( - new Entry($this->getUser()), - $url - ); + $entry = new Entry($this->getUser()); + try { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + $entry, + $url + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + $entry->setUrl($url); + } } if (!is_null($title)) { @@ -285,6 +294,51 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + /** + * Reload an entry. + * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). + * + * @ApiDoc( + * requirements={ + * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} + * } + * ) + * + * @return JsonResponse + */ + public function patchEntriesReloadAction(Entry $entry) + { + $this->validateAuthentication(); + $this->validateUserAccess($entry->getUser()->getId()); + + try { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + + return new JsonResponse([], 304); + } + + // if refreshing entry failed, don't save it + if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { + return new JsonResponse([], 304); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + + $json = $this->get('serializer')->serialize($entry, 'json'); + + return (new JsonResponse())->setJson($json); + } + /** * Delete **permanently** an entry. *