From 0642e844a44c9db390185abbb56530c8a7edcabe Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 12:59:27 +0200 Subject: [PATCH 1/6] Media: large file upload --- migrations/54-60/new-features.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index ea380ce9..01d29689 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -11,3 +11,35 @@ sidebar_position: 1 All the new features that have been added to this version. Any changes in best practice. +## Filesystem adapter changes + +### Improving large files handling + +Starting from 6.0 Media manager introduces new class `TmpFileUpload` that represent temporary file, +which is a reference to temporary file (that was just uploaded), without keeping whole file in the memory. +This allows to upload large files without violation PHP max_memory limits. + +Note: API calls still handle base64 encoded images, which still uses PHP memory directly. + +It is recommended to update existing Filesystem Adapter: +```php +// Before: +public function updateFile(string $name, string $path, $data){ + ... + File::write($dstPath, $data); + ... +} + +// After: +public function updateFile(string $name, string $path, $data){ + ... + if ($data instanceof TmpFileUpload) { + File::upload($data->getUri(), $localPath); + } else { + File::write($dstPath, $data); + } + ... +} +``` + +PR: https://github.com/joomla/joomla-cms/pull/44848 From b95ee7a0a3bdcc8a430317e3a2340e04674efad1 Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 13:03:13 +0200 Subject: [PATCH 2/6] Media: large file upload --- migrations/54-60/new-features.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index 01d29689..df714df8 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -19,7 +19,7 @@ Starting from 6.0 Media manager introduces new class `TmpFileUpload` that repres which is a reference to temporary file (that was just uploaded), without keeping whole file in the memory. This allows to upload large files without violation PHP max_memory limits. -Note: API calls still handle base64 encoded images, which still uses PHP memory directly. +Note: Media API calls still handle base64 encoded images, which still uses PHP memory directly. It is recommended to update existing Filesystem Adapter: ```php @@ -40,6 +40,17 @@ public function updateFile(string $name, string $path, $data){ } ... } + +// Or: +public function updateFile(string $name, string $path, $data){ + ... + if ($data instanceof TmpFileUpload) { + $data = fopen($data->getUri(), 'r'); + } + + File::write($dstPath, $data); + ... +} ``` PR: https://github.com/joomla/joomla-cms/pull/44848 From 555b89a9eb298285dac21c7a4dfead17627a59d2 Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 13:03:24 +0200 Subject: [PATCH 3/6] Media: large file upload --- migrations/54-60/new-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index df714df8..27c2ca4d 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -11,7 +11,7 @@ sidebar_position: 1 All the new features that have been added to this version. Any changes in best practice. -## Filesystem adapter changes +## Filesystem adapter ### Improving large files handling From b373fddc2918bd79cad9c63be0880220c4fc37f8 Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 13:37:32 +0200 Subject: [PATCH 4/6] Media: large file upload --- migrations/54-60/new-features.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index 27c2ca4d..4424fbf2 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -24,14 +24,14 @@ Note: Media API calls still handle base64 encoded images, which still uses PHP m It is recommended to update existing Filesystem Adapter: ```php // Before: -public function updateFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data){ ... File::write($dstPath, $data); ... } // After: -public function updateFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data){ ... if ($data instanceof TmpFileUpload) { File::upload($data->getUri(), $localPath); @@ -42,7 +42,7 @@ public function updateFile(string $name, string $path, $data){ } // Or: -public function updateFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data){ ... if ($data instanceof TmpFileUpload) { $data = fopen($data->getUri(), 'r'); @@ -52,5 +52,6 @@ public function updateFile(string $name, string $path, $data){ ... } ``` +The same for `updateFile()` method. PR: https://github.com/joomla/joomla-cms/pull/44848 From 43d6dee175b80e714573bfb98e27f428a715260e Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 13:38:33 +0200 Subject: [PATCH 5/6] Media: large file upload --- migrations/54-60/new-features.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index 4424fbf2..a57e426f 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -24,14 +24,16 @@ Note: Media API calls still handle base64 encoded images, which still uses PHP m It is recommended to update existing Filesystem Adapter: ```php // Before: -public function createFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data): string +{ ... File::write($dstPath, $data); ... } // After: -public function createFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data): string +{ ... if ($data instanceof TmpFileUpload) { File::upload($data->getUri(), $localPath); @@ -42,7 +44,8 @@ public function createFile(string $name, string $path, $data){ } // Or: -public function createFile(string $name, string $path, $data){ +public function createFile(string $name, string $path, $data): string +{ ... if ($data instanceof TmpFileUpload) { $data = fopen($data->getUri(), 'r'); From 1dc2939d7eef648b32a83b5a9b35364f8e7a2462 Mon Sep 17 00:00:00 2001 From: Fedik Date: Mon, 10 Mar 2025 13:39:03 +0200 Subject: [PATCH 6/6] Media: large file upload --- migrations/54-60/new-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/54-60/new-features.md b/migrations/54-60/new-features.md index a57e426f..8ba3c1a4 100644 --- a/migrations/54-60/new-features.md +++ b/migrations/54-60/new-features.md @@ -36,7 +36,7 @@ public function createFile(string $name, string $path, $data): string { ... if ($data instanceof TmpFileUpload) { - File::upload($data->getUri(), $localPath); + File::upload($data->getUri(), $dstPath); } else { File::write($dstPath, $data); }