Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Assets updates! (#1903)
Browse files Browse the repository at this point in the history
* Return asset url as well

* Fix param check in asset retrieval

* Add cache-control header
  • Loading branch information
rijkvanzanten authored May 18, 2020
1 parent 22cf2e2 commit 0f07bd6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/core/Directus/Services/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,25 @@ public function getAsset($fileHashId, array $params = [], $asStream = false)

$file = $result->current()->toArray();

// Get original image
if (count($params) == 0) {
// Get thumbnail
if (
isset($params['key']) ||
isset($params['w']) ||
isset($params['h']) ||
isset($params['f']) ||
isset($params['q'])
) {
$this->fileName = $file['filename_disk'];
$this->fileNameDownload = $file['filename_download'];

try {
return $this->getThumbnail($params, $asStream);
} catch (Exception $e) {
throw new UnprocessableEntityException(sprintf($e->getMessage()));
}
}
// Get original file
else {
$lastModified = $this->filesystem->getAdapter()->getTimestamp($file['filename_disk']);
$lastModified = new DateTimeUtils(date('c', $lastModified));

Expand All @@ -116,15 +133,6 @@ public function getAsset($fileHashId, array $params = [], $asStream = false)
$result['filename_download'] = $file['filename_download'];

return $result;
} else {

$this->fileName = $file['filename_disk'];
$this->fileNameDownload = $file['filename_download'];
try {
return $this->getThumbnail($params, $asStream);
} catch (Exception $e) {
throw new UnprocessableEntityException(sprintf($e->getMessage()));
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/endpoints/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Directus\Application\Http\Response;
use Directus\Services\AssetService;
use Slim\Http\Stream;
use function Directus\get_directus_setting;

class Assets extends Route
{
Expand All @@ -26,6 +27,12 @@ public function __invoke(Request $request, Response $response)
$response->setHeader('Content-Disposition', 'filename="' . $asset['filename_download'] . '"');
$response->setHeader('Last-Modified', $asset['last_modified']);

$ttl = get_directus_setting('thumbnail_cache_ttl');

if ($ttl) {
$response->setHeader('Cache-Control', 'max-age=' . $ttl . ', public');
}

return $response->withBody(new Stream($asset['file']));
} else {
return $response->withStatus(404);
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,24 @@ function append_storage_information(array $rows)
$rows = [$rows];
}

$assetURLNaming = get_directus_setting('asset_url_naming');
$projectName = get_api_project_from_request();
$basepath = get_base_path();

foreach ($rows as &$row) {
$data = [];
$ext = pathinfo($row['filename_disk'], PATHINFO_EXTENSION);

$fileAlias = $assetURLNaming == "private_hash" ? $row['private_hash'] : $row['filename_download'];

if ($proxyDownloads) {
$data['url'] = get_proxy_path($row['filename_disk']);
$data['full_url'] = get_url($data['url']);
} else {
if (isset($row['private_hash'])) {
$data['url'] = $data['full_url'] = $fileRootUrl . '/' . $row['filename_disk'];
$data['asset_url'] = $basepath . $projectName . '/assets/' . $fileAlias;

// Add Full url
if (!$hasFileRootUrlHost) {
$data['full_url'] = get_url($data['url']);
Expand Down

0 comments on commit 0f07bd6

Please sign in to comment.