Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
force use of temp extension
Browse files Browse the repository at this point in the history
force use of temp extension to prevent mime conversion error with intervention image.
  • Loading branch information
bayareawebpro committed Jul 1, 2021
1 parent 3234325 commit a6aa752
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 29 deletions.
6 changes: 6 additions & 0 deletions config/nova-ckeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
*/
'max_width' => 1024,
'max_height' => 768,

/**
* Maximum characters before trimmed.
* @docs https://stackoverflow.com/questions/6870824/what-is-the-maximum-length-of-a-filename-in-s3
*/
'max_filename' => 250,
];
1 change: 1 addition & 0 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function register()

$this->publishes([
__DIR__.'/../stubs/views' => resource_path('views/ckeditor'),
__DIR__.'/../stubs/migrations' => resource_path('database/migrations'),
__DIR__.'/../stubs/models/Page.stub' => app_path('Models/Page.php'),
__DIR__.'/../stubs/models/Media.stub' => app_path('Models/Media.php'),
__DIR__.'/../stubs/resources/Page.stub' => app_path('Nova/Page.php'),
Expand Down
95 changes: 66 additions & 29 deletions src/MediaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace BayAreaWebPro\NovaFieldCkEditor;

use Throwable;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;

use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;

class MediaStorage
Expand All @@ -23,7 +25,7 @@ class MediaStorage
* MediaStorage constructor.
* @param string $disk
*/
public function __construct($disk = 'media')
public function __construct(string $disk = 'media')
{
$this->disk = $disk;
}
Expand All @@ -33,16 +35,16 @@ public function __construct($disk = 'media')
* @param string $disk
* @return static
*/
public static function make($disk = 'media'): self
public static function make(string $disk = 'media'): self
{
return app('ckeditor-media-storage', compact('disk'));
}

/**
* Save a new media file from the Nova request.
* @param Request $request
* @throws Throwable
* @return array
* @throws Throwable
*/
public function __invoke(Request $request)
{
Expand All @@ -52,12 +54,11 @@ public function __invoke(Request $request)
/**
* Handle the File Upload
* @param UploadedFile $file
* @throws Throwable
* @return array
* @throws Throwable
*/
public function handleUpload(UploadedFile $file): array
{

$attributes = $this->resize($file);

$file->storePubliclyAs('', $attributes['file'], [
Expand All @@ -72,8 +73,8 @@ public function handleUpload(UploadedFile $file): array
/**
* Perform Resize & Conversion Operations.
* @param UploadedFile $file
* @throws Throwable
* @return array
* @throws Throwable
*/
protected function resize(UploadedFile $file): array
{
Expand All @@ -82,43 +83,29 @@ protected function resize(UploadedFile $file): array
$maxWidth = config('nova-ckeditor.max_width', 1024);
$maxHeight = config('nova-ckeditor.max_height', 768);

// Hash Original Data.
$hash = md5_file($file->getRealPath());
$hash = $this->hashFileContents($file);
$name = $this->makeTargetFilename($file);
$filePath = $this->makeTargetFilePath($name);

// Make new filename.
$name = sprintf(
"%s.{$file->guessExtension()}",
Str::slug(Str::limit(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME), 120, ''))
);

// Resize the image.
$image = Image::make($file->getRealPath());
if ($image->width() > $maxWidth || $image->height() > $maxHeight) {
$image->resize($maxWidth, $maxHeight, function (Constraint $constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}

$image->save($file->getRealPath(), config('nova-ckeditor.max_quality', 75));
$image = $this->resizeImage($file, $maxWidth, $maxHeight)->save($filePath, config('nova-ckeditor.max_quality', 75));

return [
'hash' => $hash,
'file' => $name,
'mime' => $image->mime(),
'width' => $image->width(),
'mime' => $image->mime(),
'width' => $image->width(),
'height' => $image->height(),
'size' => $this->optimize($file->getRealPath()),
'size' => $this->optimize($filePath),
];
}

/**
* Perform Optimization Operations.
* @param string $tempPath
* @throws Throwable
* @return int
* @throws Throwable
*/
public function optimize(string $tempPath):int
public function optimize(string $tempPath): int
{
ImageOptimizer::optimize($tempPath);
return filesize($tempPath);
Expand Down Expand Up @@ -147,4 +134,54 @@ public function url(string $file)
{
return Storage::disk($this->disk)->url($file);
}

/**
* @param UploadedFile $file
* @return false|string
*/
protected function hashFileContents(UploadedFile $file): string
{
return md5_file($file->getRealPath());
}

/**
* @param UploadedFile $file
* @return string
*/
protected function makeTargetFilename(UploadedFile $file): string
{
return sprintf(
"%s.%s",
Str::slug(Str::limit(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME), config('nova-ckeditor.max_filename_characters', 250), '')),
$file->guessExtension()
);
}

/**
* @param UploadedFile $file
* @param $maxWidth
* @param $maxHeight
* @return \Intervention\Image\Image
*/
protected function resizeImage(UploadedFile $file, $maxWidth, $maxHeight): \Intervention\Image\Image
{
$image = Image::make($file->getRealPath());
if ($image->width() > $maxWidth || $image->height() > $maxHeight) {
$image->resize($maxWidth, $maxHeight, function (Constraint $constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
return $image;
}

/**
* Make target file path.
* @param string $name
* @return string
*/
protected function makeTargetFilePath(string $name): string
{
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name;
}
}
37 changes: 37 additions & 0 deletions stubs/migrations/2021_01_01_000000_create_media_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMediaTable extends Migration
{
/**
* Run the migrations.
* @return void
*/
public function up()
{
Schema::create('media', function (Blueprint $table) {
$table->id();
$table->bigIncrements('id')->index();
$table->string('file')->index();
$table->string('mime')->index();
$table->string('hash')->index();
$table->string('disk')->index();
$table->unsignedInteger('width')->index();
$table->unsignedInteger('height')->index();
$table->unsignedInteger('size')->index();
$table->timestamps();
});
}

/**
* Reverse the migrations.
* @return void
*/
public function down()
{
Schema::drop('media');
}
}
37 changes: 37 additions & 0 deletions stubs/migrations/2021_01_01_000000_create_pages_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePagesTable extends Migration
{
/**
* Run the migrations.
* @return void
*/
public function up()
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('media_id')->nullable()->index();
$table->string('title',255)->index();
$table->string('slug',255)->index();
$table->text('excerpt')->nullable();
$table->mediumText('content')->nullable();
$table->string('meta_title',160)->nullable();
$table->string('meta_description',160)->nullable();
$table->string('meta_robots')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
* @return void
*/
public function down()
{
Schema::drop('pages');
}
}

0 comments on commit a6aa752

Please sign in to comment.