Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Apr 23, 2023
1 parent 0796876 commit 8774e15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions about.page
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ if (is_file($file = '.\path\to\image.png')) {
}
~~~

### Image Folder

A special folder to store image files will be created automatically by this extension located at `.\lot\image`. You can
store important image files exclusively there. These files will not be directly accessible using the original address as
the files in the `.\lot\asset` folder. You need to use the proxy link feature to be able to access these files. A
built-in image proxy link feature is available and it allows public access by default:

~~~ .txt
http://127.0.0.1/image/autumn.jpg → http://127.0.0.1/lot/image/autumn.jpg
http://127.0.0.1/image/a/b/c/autumn.jpg → http://127.0.0.1/lot/image/a/b/c/autumn.jpg
~~~

You can add hooks to the route to restrict access to certain image files based on certain conditions:

~~~ .php
Hook::set('route.image', function ($content, $path) {
// Disable public image access stored in `.\lot\image\me` folder
if (str_starts_with($path, '/me/')) {
status(403);
type('text/plain');
return 'Access denied.';
}
// Disable public image access stored in `.\lot\image\us` folder unless the user is logged-in
if (str_starts_with($path, '/us/') && !Is::user()) {
status(403);
type('text/plain');
return 'Access denied.';
}
return $content;
});
~~~

### Page Image Data

This extension also add `image` and `images` property to the current `$page` object. The `image` property will contain
Expand Down
6 changes: 6 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function image(...$lot) {

namespace x\image {
function route($content, $path) {
if (null !== $content) {
return $content;
}
$age = 60 * 60 * 24 * 365; // Cache output for 1 year
\status(200, [
'cache-control' => 'max-age=' . $age . ', private',
Expand All @@ -33,6 +36,9 @@ function route($content, $path) {
if (0 === \strpos($path, $route . '/') && \is_file($file = \LOT . \D . 'image' . \D . \substr($path, \strlen($route) + 1))) {
\Hook::set('route.image', __NAMESPACE__ . "\\route", 100);
\Hook::set('route', function ($content, $path, $query, $hash) use ($file, $route) {
if (null !== $content) {
return $content;
}
if (false !== \strpos(',apng,avif,bmp,gif,jpeg,jpg,png,svg,webp,xbm,xpm,', ',' . \pathinfo($file, \PATHINFO_EXTENSION) . ',')) {
return \Hook::fire('route.image', [$content, \substr($path, \strlen($route) + 1), $query, $hash]);
}
Expand Down

0 comments on commit 8774e15

Please sign in to comment.