]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
8 | use Shaarli\Render\TemplatePage; | |
9 | use Slim\Http\Request; | |
10 | use Slim\Http\Response; | |
11 | ||
12 | /** | |
13 | * Class ToolsController | |
14 | * | |
15 | * Slim controller used to handle thumbnails update. | |
16 | */ | |
17 | class ThumbnailsController extends ShaarliAdminController | |
18 | { | |
19 | /** | |
20 | * GET /admin/thumbnails - Display thumbnails update page | |
21 | */ | |
22 | public function index(Request $request, Response $response): Response | |
23 | { | |
24 | $ids = []; | |
25 | foreach ($this->container->bookmarkService->search() as $bookmark) { | |
26 | // A note or not HTTP(S) | |
27 | if ($bookmark->isNote() || !startsWith(strtolower($bookmark->getUrl()), 'http')) { | |
28 | continue; | |
29 | } | |
30 | ||
31 | $ids[] = $bookmark->getId(); | |
32 | } | |
33 | ||
34 | $this->assignView('ids', $ids); | |
35 | $this->assignView( | |
36 | 'pagetitle', | |
37 | t('Thumbnails update') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
38 | ); | |
39 | ||
40 | return $response->write($this->render(TemplatePage::THUMBNAILS)); | |
41 | } | |
42 | ||
43 | /** | |
44 | * PATCH /admin/shaare/{id}/thumbnail-update - Route for AJAX calls | |
45 | */ | |
46 | public function ajaxUpdate(Request $request, Response $response, array $args): Response | |
47 | { | |
48 | $id = $args['id'] ?? null; | |
49 | ||
50 | if (false === ctype_digit($id)) { | |
51 | return $response->withStatus(400); | |
52 | } | |
53 | ||
54 | try { | |
55 | $bookmark = $this->container->bookmarkService->get($id); | |
56 | } catch (BookmarkNotFoundException $e) { | |
57 | return $response->withStatus(404); | |
58 | } | |
59 | ||
60 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
61 | $this->container->bookmarkService->set($bookmark); | |
62 | ||
63 | return $response->withJson($this->container->formatterFactory->getFormatter('raw')->format($bookmark)); | |
64 | } | |
65 | } |