Add a setting to retrieve bookmark metadata asynchrounously
- There is a new standalone script (metadata.js) which requests a new controller to get bookmark metadata and fill the form async - This feature is enabled with the new setting: general.enable_async_metadata (enabled by default) - general.retrieve_description is now enabled by default - A small rotating loader animation has a been added to bookmark inputs when metadata is being retrieved (default template) - Custom JS htmlentities has been removed and mathiasbynens/he library is used instead Fixes #1563
This commit is contained in:
parent
f34554c6c2
commit
4cf3564d28
19 changed files with 447 additions and 75 deletions
application/http
68
application/http/MetadataRetriever.php
Normal file
68
application/http/MetadataRetriever.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shaarli\Http;
|
||||
|
||||
use Shaarli\Config\ConfigManager;
|
||||
|
||||
/**
|
||||
* HTTP Tool used to extract metadata from external URL (title, description, etc.).
|
||||
*/
|
||||
class MetadataRetriever
|
||||
{
|
||||
/** @var ConfigManager */
|
||||
protected $conf;
|
||||
|
||||
/** @var HttpAccess */
|
||||
protected $httpAccess;
|
||||
|
||||
public function __construct(ConfigManager $conf, HttpAccess $httpAccess)
|
||||
{
|
||||
$this->conf = $conf;
|
||||
$this->httpAccess = $httpAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve metadata for given URL.
|
||||
*
|
||||
* @return array [
|
||||
* 'title' => <remote title>,
|
||||
* 'description' => <remote description>,
|
||||
* 'tags' => <remote keywords>,
|
||||
* ]
|
||||
*/
|
||||
public function retrieve(string $url): array
|
||||
{
|
||||
$charset = null;
|
||||
$title = null;
|
||||
$description = null;
|
||||
$tags = null;
|
||||
$retrieveDescription = $this->conf->get('general.retrieve_description');
|
||||
|
||||
// Short timeout to keep the application responsive
|
||||
// The callback will fill $charset and $title with data from the downloaded page.
|
||||
$this->httpAccess->getHttpResponse(
|
||||
$url,
|
||||
$this->conf->get('general.download_timeout', 30),
|
||||
$this->conf->get('general.download_max_size', 4194304),
|
||||
$this->httpAccess->getCurlDownloadCallback(
|
||||
$charset,
|
||||
$title,
|
||||
$description,
|
||||
$tags,
|
||||
$retrieveDescription
|
||||
)
|
||||
);
|
||||
|
||||
if (!empty($title) && strtolower($charset) !== 'utf-8') {
|
||||
$title = mb_convert_encoding($title, 'utf-8', $charset);
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'tags' => $tags,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue