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();
+ }
+ }
+ }
+ }
}
}