diff options
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index aff0534a..17b53a01 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -43,50 +43,59 @@ class EntryRestController extends WallabagRestController | |||
43 | 43 | ||
44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | 44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
45 | 45 | ||
46 | $urls = $request->query->get('urls', []); | ||
47 | $hashedUrls = $request->query->get('hashed_urls', []); | 46 | $hashedUrls = $request->query->get('hashed_urls', []); |
47 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
48 | if (!empty($hashedUrl)) { | ||
49 | $hashedUrls[] = $hashedUrl; | ||
50 | } | ||
48 | 51 | ||
49 | // handle multiple urls first | 52 | $urls = $request->query->get('urls', []); |
50 | if (!empty($hashedUrls)) { | 53 | $url = $request->query->get('url', ''); |
51 | $results = []; | 54 | if (!empty($url)) { |
52 | foreach ($hashedUrls as $hashedUrl) { | 55 | $urls[] = $url; |
53 | $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); | 56 | } |
54 | |||
55 | $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); | ||
56 | } | ||
57 | 57 | ||
58 | return $this->sendResponse($results); | 58 | $urlHashMap = []; |
59 | foreach($urls as $urlToHash) { | ||
60 | $urlHash = hash('sha1', $urlToHash); // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls | ||
61 | $hashedUrls[] = $urlHash; | ||
62 | $urlHashMap[$urlHash] = $urlToHash; | ||
59 | } | 63 | } |
60 | 64 | ||
61 | // @deprecated, to be remove in 3.0 | 65 | if (empty($hashedUrls)) { |
62 | if (!empty($urls)) { | 66 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); |
63 | $results = []; | 67 | } |
64 | foreach ($urls as $url) { | ||
65 | $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
66 | 68 | ||
67 | $results[$url] = $this->returnExistInformation($res, $returnId); | 69 | $results = []; |
68 | } | 70 | foreach ($hashedUrls as $hashedUrlToSearch) { |
71 | $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); | ||
69 | 72 | ||
70 | return $this->sendResponse($results); | 73 | $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); |
71 | } | 74 | } |
72 | 75 | ||
73 | // let's see if it is a simple url? | 76 | $results = $this->replaceUrlHashes($results, $urlHashMap); |
74 | $url = $request->query->get('url', ''); | ||
75 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
76 | 77 | ||
77 | if (empty($url) && empty($hashedUrl)) { | 78 | if (!empty($url) || !empty($hashedUrl)) { |
78 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | 79 | $hu = array_keys($results)[0]; |
80 | return $this->sendResponse(['exists' => $results[$hu]]); | ||
79 | } | 81 | } |
82 | return $this->sendResponse($results); | ||
83 | } | ||
80 | 84 | ||
81 | $method = 'findByUrlAndUserId'; | 85 | /** |
82 | if (!empty($hashedUrl)) { | 86 | * Replace the hashedUrl keys in $results with the unhashed URL from the |
83 | $method = 'findByHashedUrlAndUserId'; | 87 | * request, as recorded in $urlHashMap. |
84 | $url = $hashedUrl; | 88 | */ |
89 | private function replaceUrlHashes(array $results, array $urlHashMap) { | ||
90 | $newResults = []; | ||
91 | foreach($results as $hash => $res) { | ||
92 | if (isset($urlHashMap[$hash])) { | ||
93 | $newResults[$urlHashMap[$hash]] = $res; | ||
94 | } else { | ||
95 | $newResults[$hash] = $res; | ||
96 | } | ||
85 | } | 97 | } |
86 | 98 | return $newResults; | |
87 | $res = $repo->$method($url, $this->getUser()->getId()); | ||
88 | |||
89 | return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); | ||
90 | } | 99 | } |
91 | 100 | ||
92 | /** | 101 | /** |