mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-13 00:31:07 +03:30
fix media
This commit is contained in:
@@ -7,37 +7,40 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
class MediaHelper
|
class MediaHelper
|
||||||
{
|
{
|
||||||
public static function moveMediaTo($model) {
|
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)
|
|
||||||
{
|
{
|
||||||
if (is_null($batchId)) {
|
try {
|
||||||
$batchId = request()->input('file_batch_id');
|
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();
|
||||||
|
}
|
||||||
|
} catch ( \Exception $exception ) {
|
||||||
|
dd( $exception );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function storeMediaFor( Model $model , string $batchId = null )
|
||||||
|
{
|
||||||
|
if ( is_null( $batchId ) ) {
|
||||||
|
$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();
|
||||||
@@ -46,23 +49,24 @@ class MediaHelper
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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([
|
request()->validate( [
|
||||||
'tinymce_batch_id' => 'nullable|string',
|
'tinymce_batch_id' => 'nullable|string' ,
|
||||||
]);
|
] );
|
||||||
|
|
||||||
if ($fakeModel = FakeModel::where('batch_id', request('tinymce_batch_id'))->first()) {
|
if ( $fakeModel = FakeModel::where( 'batch_id' , request( 'tinymce_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') {
|
if ( substr( $media->mime_type , 0 , 5 ) == 'image' ) {
|
||||||
$media->assign($model, 'tinymce_images');
|
$media->assign( $model , 'tinymce_images' );
|
||||||
} else {
|
} else {
|
||||||
$media->assign($model, 'temp_medias');
|
$media->assign( $model , 'temp_medias' );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$fakeModel->delete();
|
$fakeModel->delete();
|
||||||
@@ -70,15 +74,16 @@ class MediaHelper
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function loadMedia($model, ...$collections) {
|
public static function loadMedia( $model , ...$collections )
|
||||||
$model->loadMissing(['media' => function($query) use ($collections) {
|
{
|
||||||
$query->select('*');
|
$model->loadMissing( [ 'media' => function ( $query ) use ( $collections ) {
|
||||||
if (! empty($collections)) {
|
$query->select( '*' );
|
||||||
$query->whereIn('collection_name', $collections);
|
if ( ! empty( $collections ) ) {
|
||||||
|
$query->whereIn( 'collection_name' , $collections );
|
||||||
}
|
}
|
||||||
}]);
|
} ] );
|
||||||
if ($model->media) {
|
if ( $model->media ) {
|
||||||
foreach ($model->media as $media) {
|
foreach ( $model->media as $media ) {
|
||||||
$media->address = $media->getUrl();
|
$media->address = $media->getUrl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user