From 92c5d633f78749953de2bcc93fcaf071af852fac Mon Sep 17 00:00:00 2001 From: mahdi purHosseini Date: Mon, 11 Dec 2023 10:43:30 +0330 Subject: [PATCH] testing --- app/Item.php | 22 ++--- app/Services/Media/MediaHelper.php | 133 ++++++++++++++--------------- 2 files changed, 74 insertions(+), 81 deletions(-) diff --git a/app/Item.php b/app/Item.php index f490a9ff..a8fbd526 100644 --- a/app/Item.php +++ b/app/Item.php @@ -40,30 +40,24 @@ class Item extends Model implements HasMedia public function registerMediaCollections(): void { - $this->addMediaCollection( 'main_image' ) - ->useFallbackUrl( config( 'media-library.wm-cdn' ) . '/images/placeholder/blog-placeholder.jpg' ) - ->useFallbackPath( public_path( '/images/placeholder/blog-placeholder.jpg' ) ) - ->singleFile(); + $this->addMediaCollection('files') + ->useFallbackUrl('/files/default_pastry.jpg') + ->useFallbackPath(public_path('/files/default_pastry.jpg')); } public function registerMediaConversions( Media $media = null ) { - $crop = $media->getCustomProperty( 'crop' ); - $conversion = $this->addMediaConversion( 'thumbnail' ); - $conversion = ! $crop ? $conversion : $conversion->manualCrop( $crop[ 'width' ] , $crop[ 'height' ] , $crop[ 'left' ] , $crop[ 'top' ] ); - $conversion->nonQueued() - ->performOnCollections( 'main_image' ); - - $this->addMediaConversion( 'thumbnail' ) - ->width( 400 ) + $this->addMediaConversion('thumbnail') + ->width(480) + ->height(640) ->nonQueued() - ->performOnCollections( 'images' ); + ->performOnCollections('files'); } protected static function getValidCollections(): array { return [ - 'images' , + 'files' ]; } } diff --git a/app/Services/Media/MediaHelper.php b/app/Services/Media/MediaHelper.php index 44f45887..92d8267b 100644 --- a/app/Services/Media/MediaHelper.php +++ b/app/Services/Media/MediaHelper.php @@ -7,80 +7,79 @@ use Illuminate\Database\Eloquent\Model; class MediaHelper { - public static function moveMediaTo($model) { - - request()->validate([ - 'file_batch_id' => 'nullable|string', - ]); - - if ($fakeModel = FakeModel::where('batch_id', request('file_batch_id'))->first()) { - foreach($fakeModel->getMedia('temp_medias') as $media) { - if(substr($media->mime_type, 0, 5) == 'image') { - $media->move($model, 'images'); - } else { - $media->move($model, 'files'); - } - } - $fakeModel->delete(); - } - - } - - public static function storeMediaFor(Model $model, string $batchId = null) + public static function moveMediaTo( $model ) { - if (is_null($batchId)) { - $batchId = request()->input('file_batch_id'); - } - $modelName = self::getNamespaceMap($model); + request()->validate( [ + 'file_batch_id' => 'nullable|string' , + ] ); - if ($fakeModel = FakeModel::where('batch_id', $batchId)->where('model_name', $modelName)->first()) { - - foreach($fakeModel->media()->get() as $media) { - $media->move($model, $media->collection_name); - } - - $fakeModel->delete(); - - } - - } - - private static function getNamespaceMap(Model $model) - { - return $model->getMorphClass(); - } - - public static function moveTinyMCEMediaTo($model) { - - request()->validate([ - 'tinymce_batch_id' => 'nullable|string', - ]); - - if ($fakeModel = FakeModel::where('batch_id', request('tinymce_batch_id'))->first()) { - foreach($fakeModel->getMedia('temp_medias') as $media) { - if(substr($media->mime_type, 0, 5) == 'image') { - $media->assign($model, 'tinymce_images'); - } else { - $media->assign($model, 'temp_medias'); - } + if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'file_batch_id' ) )->first() ) { + foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) { + $media->move( $model , 'files' ); } $fakeModel->delete(); } } - public static function loadMedia($model, ...$collections) { - $model->loadMissing(['media' => function($query) use ($collections) { - $query->select('*'); - if (! empty($collections)) { - $query->whereIn('collection_name', $collections); - } - }]); - if ($model->media) { - foreach ($model->media as $media) { - $media->address = $media->getUrl(); - } - } - } +// public static function storeMediaFor( Model $model , string $batchId = null ) +// { +// if ( is_null( $batchId ) ) { +// $batchId = request()->input( 'file_batch_id' ); +// } +// +// $modelName = self::getNamespaceMap( $model ); +// +// if ( $fakeModel = FakeModel::where( 'batch_id' , $batchId )->where( 'model_name' , $modelName )->first() ) { +// +// foreach ( $fakeModel->media()->get() as $media ) { +// $media->move( $model , $media->collection_name ); +// } +// +// $fakeModel->delete(); +// +// } +// +// } +// +// private static function getNamespaceMap( Model $model ) +// { +// return $model->getMorphClass(); +// } +// +// public static function moveTinyMCEMediaTo( $model ) +// { +// +// request()->validate( [ +// 'tinymce_batch_id' => 'nullable|string' , +// ] ); +// +// if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'tinymce_batch_id' ) )->first() ) { +// foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) { +// if ( substr( $media->mime_type , 0 , 5 ) == 'image' ) { +// $media->assign( $model , 'tinymce_images' ); +// } else { +// $media->assign( $model , 'temp_medias' ); +// } +// } +// $fakeModel->delete(); +// } +// +// } +// +// public static function loadMedia( $model , ...$collections ) +// { +// $model->loadMissing( [ 'media' => function ( $query ) use ( $collections ) { +// $query->select( '*' ); +// if ( ! empty( $collections ) ) { +// $query->whereIn( 'collection_name' , $collections ); +// } +// } ] ); +// if ( $model->media ) { +// foreach ( $model->media as $media ) { +// $media->address = $media->getUrl(); +// } +// } +// } }