-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[4.2] Local adapter thumbnails #36552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
dgrammatiko
wants to merge
61
commits into
joomla:4.2-dev
from
dgrammatiko:4.1-devlocal-adapter-thumbs
Closed
Changes from 16 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
47af679
thumbs up
dgrammatiko a91a482
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko 32e830b
CS
dgrammatiko 61b7c09
Fix windows FS
dgrammatiko ab78242
Deal with the thumbs on delete/save
dgrammatiko 4e7595e
oopsy
dgrammatiko 6fa87d4
cs
dgrammatiko 4d809d3
Cs
dgrammatiko 419f3c4
go
dgrammatiko 86b12aa
meh
dgrammatiko f625e4b
meh
dgrammatiko 0c7c523
meh
dgrammatiko c301e4b
Expose the thumbs option
dgrammatiko 071ebc0
Use the media/cache folder
dgrammatiko e582a28
Merge branch '4.1-dev' into 4.1-devlocal-adapter-thumbs
dgrammatiko ab64d9f
Update plugins/filesystem/local/local.xml
dgrammatiko 92b88c3
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko 8059d5a
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko 97efd55
fixit
dgrammatiko fd45914
also the args here
dgrammatiko 7f33dbb
Merge branch '4.1-dev' into 4.1-devlocal-adapter-thumbs
dgrammatiko 666be48
CS
dgrammatiko d863939
Merge branch '4.1-devlocal-adapter-thumbs' of github.com:dgrammatiko/…
dgrammatiko 7e80d89
Failing tests
dgrammatiko d3fd272
fix the thumbs
dgrammatiko 6e5027d
Merge pull request #9 from dgrammatiko/patch-12
dgrammatiko fd82dfa
Fix the thumbs
dgrammatiko 4fba529
oops
dgrammatiko d3998c3
CS
dgrammatiko b5c3f4f
oops the paths are wrong
dgrammatiko f5806e8
Update local.php
dgrammatiko 78f7971
Update libraries/src/Image/Image.php
dgrammatiko 7e73794
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko ecf9a11
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko 63bccbb
Only images can have thumbs
dgrammatiko b1221c1
this is faster
dgrammatiko 785dc5a
Maybe
dgrammatiko 090bb6a
nope
dgrammatiko 3500626
Update libraries/src/Image/Image.php
dgrammatiko b499429
Keep the old Image createThumbs fn and introduce a new one
dgrammatiko 906c04d
Update libraries/src/Image/Image.php
dgrammatiko 900119b
Merge branch '4.1-dev' into 4.1-devlocal-adapter-thumbs
dgrammatiko f2a4313
Merge branch '4.1-dev' into 4.1-devlocal-adapter-thumbs
dgrammatiko cd114cc
Update Image.php
dgrammatiko b3df15d
Update Image.php
dgrammatiko cf2824b
maybe
dgrammatiko 30aa1a0
Node fn names don't work on PHP. Well...
dgrammatiko 8e6e915
ihniwid
dgrammatiko 250c5c9
nope
dgrammatiko 87517b7
Make a note so others don't have to waste hours debugging this
dgrammatiko af9df1b
don't
dgrammatiko eec4a93
CS
dgrammatiko a69a231
paths again
dgrammatiko e1f768b
maybe
dgrammatiko d54e485
maybe not
dgrammatiko a349fce
mute failed test
dgrammatiko beb1a40
maybe realpath is not the right approach here
dgrammatiko da45a4a
and this
dgrammatiko 43c068b
Update plugins/filesystem/local/src/Adapter/LocalAdapter.php
dgrammatiko 4de5af0
simplified
dgrammatiko 77eddd5
Merge branch '4.2-dev' into 4.1-devlocal-adapter-thumbs
richard67 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,15 +52,25 @@ class LocalAdapter implements AdapterInterface | |
| */ | ||
| private $filePath = null; | ||
|
|
||
| /** | ||
| * Should the adapter create a thumbnail for the image? | ||
| * | ||
| * @var boolean | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| private $thumbs = false; | ||
|
|
||
| /** | ||
| * The absolute root path in the local file system. | ||
| * | ||
| * @param string $rootPath The root path | ||
| * @param string $filePath The file path of media folder | ||
| * @param string $rootPath The root path | ||
| * @param string $filePath The file path of media folder | ||
| * @param boolean $thumbs The thumbs option | ||
| * | ||
| * @since 4.0.0 | ||
| */ | ||
| public function __construct(string $rootPath, string $filePath) | ||
| public function __construct(string $rootPath, string $filePath, bool $thumbs = false) | ||
| { | ||
| if (!file_exists($rootPath)) | ||
| { | ||
|
|
@@ -69,6 +79,17 @@ public function __construct(string $rootPath, string $filePath) | |
|
|
||
| $this->rootPath = Path::clean(realpath($rootPath), '/'); | ||
| $this->filePath = $filePath; | ||
| $this->thumbs = $thumbs; | ||
|
|
||
| if ($this->thumbs) | ||
| { | ||
| $dir = JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath; | ||
|
|
||
| if (!is_dir($dir)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need it only when you writing a thumbs, it does not need in constructor |
||
| { | ||
| mkdir($dir, 0755, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -224,14 +245,30 @@ public function createFolder(string $name, string $path): string | |
| */ | ||
| public function createFile(string $name, string $path, $data): string | ||
| { | ||
| $name = $this->getSafeName($name); | ||
|
|
||
| $name = $this->getSafeName($name); | ||
| $localPath = $this->getLocalPath($path . '/' . $name); | ||
|
|
||
| $this->checkContent($localPath, $data); | ||
|
|
||
| File::write($localPath, $data); | ||
|
|
||
| if ($this->thumbs) | ||
| { | ||
| $thumbPath = str_replace( | ||
| $this->rootPath, | ||
| JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath, | ||
| $localPath | ||
| ); | ||
|
|
||
| if (!is_dir(\dirname($thumbPath))) | ||
| { | ||
| mkdir(\dirname($thumbPath), 0755, true); | ||
dgrammatiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Create the thumbnail | ||
| (new Image($localPath))->resize(300, 300, true)->toFile($thumbPath); | ||
| } | ||
|
|
||
| return $name; | ||
| } | ||
|
|
||
|
|
@@ -259,6 +296,23 @@ public function updateFile(string $name, string $path, $data) | |
| $this->checkContent($localPath, $data); | ||
|
|
||
| File::write($localPath, $data); | ||
|
|
||
| if ($this->thumbs) | ||
| { | ||
| $thumbPath = str_replace( | ||
| $this->rootPath, | ||
| JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath, | ||
| $localPath | ||
| ); | ||
|
|
||
| if (!is_dir(\dirname($thumbPath))) | ||
| { | ||
| mkdir(\dirname($thumbPath), 0755, true); | ||
| } | ||
dgrammatiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Create the thumbnail | ||
| (new Image($localPath))->resize(300, 300, true)->toFile($thumbPath); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -271,9 +325,14 @@ public function updateFile(string $name, string $path, $data) | |
| * @since 4.0.0 | ||
| * @throws \Exception | ||
| */ | ||
| public function delete(string $path) | ||
| public function delete(string $path): void | ||
| { | ||
| $localPath = $this->getLocalPath($path); | ||
| $thumbPath = str_replace( | ||
| $this->rootPath, | ||
| JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath, | ||
| $localPath | ||
| ); | ||
|
|
||
| if (is_file($localPath)) | ||
| { | ||
|
|
@@ -282,6 +341,11 @@ public function delete(string $path) | |
| throw new FileNotFoundException; | ||
| } | ||
|
|
||
| if ($this->thumbs && is_file($thumbPath)) | ||
| { | ||
| File::delete($thumbPath); | ||
| } | ||
|
|
||
| $success = File::delete($localPath); | ||
| } | ||
| else | ||
|
|
@@ -291,6 +355,11 @@ public function delete(string $path) | |
| throw new FileNotFoundException; | ||
| } | ||
|
|
||
| if ($this->thumbs && is_dir($thumbPath)) | ||
| { | ||
| Folder::delete($thumbPath); | ||
| } | ||
|
|
||
| $success = Folder::delete($localPath); | ||
| } | ||
|
|
||
|
|
@@ -358,8 +427,7 @@ private function getPathInformation(string $path): \stdClass | |
| $obj->width = $props->width; | ||
| $obj->height = $props->height; | ||
|
|
||
| // Todo : Change this path to an actual thumbnail path | ||
| $obj->thumb_path = $this->getUrl($obj->path); | ||
| $obj->thumb_path = $this->thumbs ? $this->getThumb($path) : $this->getUrl($obj->path); | ||
| } | ||
| catch (UnparsableImageException $e) | ||
| { | ||
|
|
@@ -869,4 +937,53 @@ private function getLocalPath(string $path): string | |
| throw new InvalidPathException($e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path for the thumbnail of the given image. | ||
| * If the thumbnail does not exist, it will be created. | ||
| * | ||
| * @param string $path The path of the image | ||
| * | ||
| * @return string | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| private function getThumb($path): string | ||
dgrammatiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $dir = \dirname( | ||
dgrammatiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| str_replace( | ||
| $this->rootPath, | ||
| JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath, | ||
| $path | ||
| ) | ||
| ); | ||
|
|
||
| if (!is_dir($dir)) | ||
| { | ||
| mkdir($dir, 0755, true); | ||
| } | ||
|
|
||
| $thumbPath = str_replace( | ||
| $this->rootPath, | ||
| JPATH_ROOT . '/media/cache/com_media/thumbs/' . $this->filePath, | ||
| $path | ||
| ); | ||
| $thumbURL = Uri::root() . $this->getEncodedPath( | ||
dgrammatiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| str_replace( | ||
| $this->rootPath, | ||
| 'media/cache/com_media/thumbs/' . $this->filePath, | ||
| $path | ||
| ) | ||
| ); | ||
|
|
||
| if (file_exists($thumbPath)) | ||
| { | ||
| return $thumbURL; | ||
| } | ||
|
|
||
| // Create the thumbnail | ||
| (new Image($path))->resize(300, 300, true)->toFile($thumbPath); | ||
|
|
||
| return $thumbURL; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be
JPATH_ROOT . '/media/cache/plg_filesystem_local/thumbs/' . $this->filePath;?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I prefer the
com_media. That folder could be used for other things later, like responsive images, cached external adapters JSON responses, etc..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize we are not in the plugin but the adapter. So I would pass the cache path into the adapter as it should not be hardcoded, because I fear it can lead to conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The var
$this->filePathis likeimages\...for the adapterimages. For another adapter, eg:testthe path would betest/...so it should be safe. Also, this functionality is only tightly coupled to the local adapter. But maybe I'm missing somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, meant only the filename, so all good.