diff --git a/app/Helpers/NextCloudHelper.php b/app/Helpers/NextCloudHelper.php index b51fb34..55109fb 100644 --- a/app/Helpers/NextCloudHelper.php +++ b/app/Helpers/NextCloudHelper.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Str; use Illuminate\Support\Facades\Storage; +use Spatie\Image\Image; class NextCloudHelper { @@ -63,12 +64,11 @@ class NextCloudHelper { $baseUrl = env('NEXT_CLOUD_URL'); $username = env('NEXT_CLOUD_USERNAME'); - $password= env('NEXT_CLOUD_PASSWORD'); + $password = env('NEXT_CLOUD_PASSWORD'); // Ensure the folder exists (or create it) $folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath); - // Check if folder creation was successful or if it already exists if (!$folderResponse['success']) { return [ 'success' => false, @@ -81,12 +81,37 @@ class NextCloudHelper $url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; try { - // If fileObject is a stream, we can use it directly - // If it's raw content, ensure it's being properly passed + // Check if the file is an image + if (Str::endsWith($fileName, ['.jpeg', '.png', '.jpg', '.gif', '.svg', '.heic', '.heif'])) { + // Save the uploaded file or raw content to a temporary path + $tempPath = tempnam(sys_get_temp_dir(), 'image_') . '.' . pathinfo($fileName, PATHINFO_EXTENSION); + + if (is_string($fileObject)) { + // If fileObject is raw content, write it to the temp file + file_put_contents($tempPath, $fileObject); + } elseif ($fileObject instanceof \Illuminate\Http\UploadedFile) { + // If fileObject is an UploadedFile, move it to the temp file + $fileObject->move(dirname($tempPath), basename($tempPath)); + } + + // Optimize the image + Image::load($tempPath) + ->optimize() + ->quality(50) + ->save(); + + // Replace fileObject with the optimized file content + $fileObject = file_get_contents($tempPath); + + // Remove the temporary file + unlink($tempPath); + } + + // Upload the file $response = $client->request('PUT', $url, [ 'auth' => [$username, $password], - 'body' => $fileObject, // File content or stream - 'verify' => false // Disable SSL verification (only for testing purposes) + 'body' => $fileObject, + 'verify' => false // Disable SSL verification (only for testing purposes) ]); return [ diff --git a/composer.json b/composer.json index ea137cc..7f35a49 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "nino/laravel-nextcloud-fs": "^2.0", "phpoffice/phpspreadsheet": "^3.5", "singlequote/laravel-webdav": "^1.1", + "spatie/image": "^3.7", "spatie/laravel-html": "^3.11", - "spatie/laravel-image-optimizer": "^1.8", "spatie/laravel-menu": "^4.2", "spatie/laravel-permission": "^6.10", "yajra/laravel-datatables-oracle": "11.0" diff --git a/composer.lock b/composer.lock index d2b7929..b58983c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "21b0b8595f584053b4b3e6a9267a2a47", + "content-hash": "868009e8371e3394bb5e17f99038a6b8", "packages": [ { "name": "barryvdh/laravel-dompdf", @@ -4856,6 +4856,79 @@ }, "time": "2024-06-07T12:41:19+00:00" }, + { + "name": "spatie/image", + "version": "3.7.4", + "source": { + "type": "git", + "url": "https://github.com/spatie/image.git", + "reference": "d72d1ae07f91a3c1230e064acd4fd8c334ab237b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/image/zipball/d72d1ae07f91a3c1230e064acd4fd8c334ab237b", + "reference": "d72d1ae07f91a3c1230e064acd4fd8c334ab237b", + "shasum": "" + }, + "require": { + "ext-exif": "*", + "ext-json": "*", + "ext-mbstring": "*", + "php": "^8.2", + "spatie/image-optimizer": "^1.7.5", + "spatie/temporary-directory": "^2.2", + "symfony/process": "^6.4|^7.0" + }, + "require-dev": { + "ext-gd": "*", + "ext-imagick": "*", + "laravel/sail": "^1.34", + "pestphp/pest": "^2.28", + "phpstan/phpstan": "^1.10.50", + "spatie/pest-plugin-snapshots": "^2.1", + "spatie/pixelmatch-php": "^1.0", + "spatie/ray": "^1.40.1", + "symfony/var-dumper": "^6.4|7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Image\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Manipulate images with an expressive API", + "homepage": "https://github.com/spatie/image", + "keywords": [ + "image", + "spatie" + ], + "support": { + "source": "https://github.com/spatie/image/tree/3.7.4" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-10-07T09:03:34+00:00" + }, { "name": "spatie/image-optimizer", "version": "1.8.0", @@ -4989,74 +5062,6 @@ ], "time": "2024-10-18T14:37:21+00:00" }, - { - "name": "spatie/laravel-image-optimizer", - "version": "1.8.0", - "source": { - "type": "git", - "url": "https://github.com/spatie/laravel-image-optimizer.git", - "reference": "024752cba691fee3cd1800000b6aa3da3b8b2474" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-image-optimizer/zipball/024752cba691fee3cd1800000b6aa3da3b8b2474", - "reference": "024752cba691fee3cd1800000b6aa3da3b8b2474", - "shasum": "" - }, - "require": { - "laravel/framework": "^8.0|^9.0|^10.0|^11.0", - "php": "^8.0", - "spatie/image-optimizer": "^1.2.0" - }, - "require-dev": { - "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0", - "phpunit/phpunit": "^9.4|^10.5" - }, - "type": "library", - "extra": { - "laravel": { - "aliases": { - "ImageOptimizer": "Spatie\\LaravelImageOptimizer\\Facades\\ImageOptimizer" - }, - "providers": [ - "Spatie\\LaravelImageOptimizer\\ImageOptimizerServiceProvider" - ] - } - }, - "autoload": { - "psr-4": { - "Spatie\\LaravelImageOptimizer\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], - "description": "Optimize images in your Laravel app", - "homepage": "https://github.com/spatie/laravel-image-optimizer", - "keywords": [ - "laravel-image-optimizer", - "spatie" - ], - "support": { - "source": "https://github.com/spatie/laravel-image-optimizer/tree/1.8.0" - }, - "funding": [ - { - "url": "https://spatie.be/open-source/support-us", - "type": "custom" - } - ], - "time": "2024-02-29T10:55:08+00:00" - }, { "name": "spatie/laravel-menu", "version": "4.2.0", @@ -5328,6 +5333,67 @@ ], "time": "2023-04-12T10:02:12+00:00" }, + { + "name": "spatie/temporary-directory", + "version": "2.2.1", + "source": { + "type": "git", + "url": "https://github.com/spatie/temporary-directory.git", + "reference": "76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/temporary-directory/zipball/76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a", + "reference": "76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\TemporaryDirectory\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Easily create, use and destroy temporary directories", + "homepage": "https://github.com/spatie/temporary-directory", + "keywords": [ + "php", + "spatie", + "temporary-directory" + ], + "support": { + "issues": "https://github.com/spatie/temporary-directory/issues", + "source": "https://github.com/spatie/temporary-directory/tree/2.2.1" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2023-12-25T11:46:58+00:00" + }, { "name": "spatie/url", "version": "2.4.0",