This commit is contained in:
mahdi purHosseini
2023-12-11 10:43:30 +03:30
parent eeb68e0438
commit 92c5d633f7
2 changed files with 74 additions and 81 deletions
+7 -13
View File
@@ -40,30 +40,24 @@ class Item extends Model implements HasMedia
public function registerMediaCollections(): void public function registerMediaCollections(): void
{ {
$this->addMediaCollection( 'main_image' ) $this->addMediaCollection('files')
->useFallbackUrl( config( 'media-library.wm-cdn' ) . '/images/placeholder/blog-placeholder.jpg' ) ->useFallbackUrl('/files/default_pastry.jpg')
->useFallbackPath( public_path( '/images/placeholder/blog-placeholder.jpg' ) ) ->useFallbackPath(public_path('/files/default_pastry.jpg'));
->singleFile();
} }
public function registerMediaConversions( Media $media = null ) 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') $this->addMediaConversion('thumbnail')
->width( 400 ) ->width(480)
->height(640)
->nonQueued() ->nonQueued()
->performOnCollections( 'images' ); ->performOnCollections('files');
} }
protected static function getValidCollections(): array protected static function getValidCollections(): array
{ {
return [ return [
'images' , 'files'
]; ];
} }
} }
+61 -62
View File
@@ -7,7 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class MediaHelper class MediaHelper
{ {
public static function moveMediaTo($model) { public static function moveMediaTo( $model )
{
request()->validate( [ request()->validate( [
'file_batch_id' => 'nullable|string' , 'file_batch_id' => 'nullable|string' ,
@@ -15,72 +16,70 @@ class MediaHelper
if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'file_batch_id' ) )->first() ) { if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'file_batch_id' ) )->first() ) {
foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) { foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) {
if(substr($media->mime_type, 0, 5) == 'image') {
$media->move($model, 'images');
} else {
$media->move( $model , 'files' ); $media->move( $model , 'files' );
} }
}
$fakeModel->delete(); $fakeModel->delete();
} }
} }
public static function storeMediaFor(Model $model, string $batchId = null) // public static function storeMediaFor( Model $model , string $batchId = null )
{ // {
if (is_null($batchId)) { // if ( is_null( $batchId ) ) {
$batchId = request()->input('file_batch_id'); // $batchId = request()->input( 'file_batch_id' );
} // }
//
$modelName = self::getNamespaceMap($model); // $modelName = self::getNamespaceMap( $model );
//
if ($fakeModel = FakeModel::where('batch_id', $batchId)->where('model_name', $modelName)->first()) { // if ( $fakeModel = FakeModel::where( 'batch_id' , $batchId )->where( 'model_name' , $modelName )->first() ) {
//
foreach($fakeModel->media()->get() as $media) { // foreach ( $fakeModel->media()->get() as $media ) {
$media->move($model, $media->collection_name); // $media->move( $model , $media->collection_name );
} // }
//
$fakeModel->delete(); // $fakeModel->delete();
//
} // }
//
} // }
//
private static function getNamespaceMap(Model $model) // private static function getNamespaceMap( Model $model )
{ // {
return $model->getMorphClass(); // return $model->getMorphClass();
} // }
//
public static function moveTinyMCEMediaTo($model) { // public static function moveTinyMCEMediaTo( $model )
// {
request()->validate([ //
'tinymce_batch_id' => 'nullable|string', // 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 ( $fakeModel = FakeModel::where( 'batch_id' , request( 'tinymce_batch_id' ) )->first() ) {
if(substr($media->mime_type, 0, 5) == 'image') { // foreach ( $fakeModel->getMedia( 'temp_medias' ) as $media ) {
$media->assign($model, 'tinymce_images'); // if ( substr( $media->mime_type , 0 , 5 ) == 'image' ) {
} else { // $media->assign( $model , 'tinymce_images' );
$media->assign($model, 'temp_medias'); // } else {
} // $media->assign( $model , 'temp_medias' );
} // }
$fakeModel->delete(); // }
} // $fakeModel->delete();
// }
} //
// }
public static function loadMedia($model, ...$collections) { //
$model->loadMissing(['media' => function($query) use ($collections) { // public static function loadMedia( $model , ...$collections )
$query->select('*'); // {
if (! empty($collections)) { // $model->loadMissing( [ 'media' => function ( $query ) use ( $collections ) {
$query->whereIn('collection_name', $collections); // $query->select( '*' );
} // if ( ! empty( $collections ) ) {
}]); // $query->whereIn( 'collection_name' , $collections );
if ($model->media) { // }
foreach ($model->media as $media) { // } ] );
$media->address = $media->getUrl(); // if ( $model->media ) {
} // foreach ( $model->media as $media ) {
} // $media->address = $media->getUrl();
} // }
// }
// }
} }