From: Vincent Vanwaelscappel Date: Fri, 1 Dec 2023 11:57:51 +0000 (+0100) Subject: wip #6542 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=c811c885f650edbd2a8fada7f3185f46a28a4ca4;p=cubist_cms-back.git wip #6542 @0.25 --- diff --git a/src/app/Media/Util.php b/src/app/Media/Util.php index 7bef5d7..0249b11 100644 --- a/src/app/Media/Util.php +++ b/src/app/Media/Util.php @@ -2,10 +2,48 @@ namespace Cubist\Backpack\Media; +use Cubist\Backpack\Magic\Fields\Files; +use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; +use Cubist\Util\CommandLine\Rsync; +use Illuminate\Support\Facades\DB; + class Util { - public static function copyMedia($models, $connection) + /** + * @param $models CubistMagicAbstractModel[] + * @param $connection string + * @return void + */ + public static function copyMedia($models, $old_db, $old_root, $new_root) { + $old_root = \Cubist\Util\Files\Files::mkdir($old_root); + $new_root = \Cubist\Util\Files\Files::mkdir($new_root); + + foreach ($models as $model) { + $type = get_class($model); + $id = $model->id; + foreach ($model->getFields() as $field) { + if ($field instanceof Files) { + $collection = $model->getAttribute($field->getName()); + $mediaCollection = $table = DB::table($old_db . '.media')->where('collection_name', $collection) + ->where('model_id', $id) + ->where('model_type', $type) + ->get(); + + foreach ($mediaCollection as $media) { + $from = \Cubist\Util\Files\Files::mkdir($old_root . $media->id); + + $insert = (array)$media; + unset($insert['id']); + + $newId = DB::table('media')->insertGetId($insert); + $to = \Cubist\Util\Files\Files::mkdir($new_root . $newId); + + (new Rsync($from, $to))->execute(); + } + } + } + } } }