diff --git a/app/Conductors/Conductor.php b/app/Conductors/Conductor.php index cf1d790..d6798da 100644 --- a/app/Conductors/Conductor.php +++ b/app/Conductors/Conductor.php @@ -259,14 +259,14 @@ class Conductor // Transform and Includes $includes = $conductor->includes; - if(count($limitFields) > 0) { + if (count($limitFields) > 0) { $includes = array_intersect($limitFields, $conductor->includes); } $conductor->collection = $conductor->collection->map(function ($model) use ($conductor, $includes, $limitFields) { $conductor->applyIncludes($model, $includes); - if(count($limitFields) > 0) { + if (count($limitFields) > 0) { $model->setAppends(array_intersect($model->getAppends(), $limitFields)); } @@ -316,10 +316,10 @@ class Conductor $requestFields = $request->input('fields'); if ($requestFields !== null) { $requestFields = explode(',', $requestFields); - if(in_array($key, $requestFields) === false) { - foreach($requestFields as $field) { - if(strpos($field, $key . '.') === 0) { - $fields[] = substr($field, strlen($key) + 1); + if (in_array($key, $requestFields) === false) { + foreach ($requestFields as $field) { + if (strpos($field, $key . '.') === 0) { + $fields[] = substr($field, (strlen($key) + 1)); } } } @@ -332,8 +332,8 @@ class Conductor /** * Run the conductor on a Model with the data stored in a Request. * - * @param mixed $fields The fields to show. - * @param Model|null $model The model. + * @param mixed $fields The fields to show. + * @param Model|null $model The model. * @return array The processed and transformed model data. */ final public static function model(mixed $fields, mixed $model) @@ -349,21 +349,21 @@ class Conductor // Limit fields $limitFields = $modelFields; - if($fields instanceof Request) { + if ($fields instanceof Request) { if ($fields !== null && $fields->has('fields') === true) { $requestFields = $fields->input('fields'); if ($requestFields !== null) { $limitFields = array_intersect(explode(',', $requestFields), $modelFields); } } - } else if(is_array($fields) && count($fields) > 0) { + } elseif (is_array($fields) && count($fields) > 0) { $limitFields = array_intersect($fields, $modelFields); } if (empty($limitFields) === false) { $modelAppends = $model->getAppends(); - foreach(array_diff($modelFields, $limitFields) as $attribute) { + foreach (array_diff($modelFields, $limitFields) as $attribute) { $key = array_search($attribute, $modelAppends); if ($key !== false) { unset($modelAppends[$key]); @@ -476,8 +476,8 @@ class Conductor /** * Paginate the conductor collection. * - * @param integer $page The current page to return. - * @param integer $limit The limit of items to include or use default. + * @param integer $page The current page to return. + * @param integer $limit The limit of items to include or use default. * @param integer $offset Offset the page count after this count of rows. * @return void */ @@ -687,8 +687,8 @@ class Conductor /** * Return an array of model fields visible to the current user. * - * @param Model $model The model in question. - * @param bool $includes Include the includes in the result. + * @param Model $model The model in question. + * @param boolean $includes Include the includes in the result. * @return array The array of field names. */ public function fields(Model $model) @@ -727,7 +727,7 @@ class Conductor $result[$key] = $this->$transformFunction($value); } } - + $result = $this->transformFinal($result); return $result; } @@ -743,7 +743,7 @@ class Conductor $result = $model->toArray(); $fields = $this->fields($model); - + if (is_array($fields) === true) { $result = array_intersect_key($result, array_flip($fields)); } diff --git a/app/Conductors/MediaConductor.php b/app/Conductors/MediaConductor.php index 4f9e869..1571e39 100644 --- a/app/Conductors/MediaConductor.php +++ b/app/Conductors/MediaConductor.php @@ -4,6 +4,7 @@ namespace App\Conductors; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Foundation\Auth\User; class MediaConductor extends Conductor { @@ -19,6 +20,13 @@ class MediaConductor extends Conductor */ protected $sort = 'created_at'; + /** + * The included fields + * + * @var string[] + */ + protected $includes = ['user']; + /** * Return an array of model fields visible to the current user. @@ -106,4 +114,27 @@ class MediaConductor extends Conductor $user = auth()->user(); return ($user !== null && ($model->user_id === $user->id || $user->hasPermission('admin/media') === true)); } + + /** + * Transform the final model data + * + * @param array $data The model data to transform. + * @return array The transformed model. + */ + public function transformFinal(array $data) + { + unset($data['user_id']); + return $data; + } + + /** + * Include User Field. + * + * @param Model $model Them model. + * @return mixed The model result. + */ + public function includeUser(Model $model) + { + return UserConductor::includeModel(request(), 'user', User::find($model['user_id'])); + } } diff --git a/app/Conductors/PostConductor.php b/app/Conductors/PostConductor.php index 5a5ed8a..4862dc4 100644 --- a/app/Conductors/PostConductor.php +++ b/app/Conductors/PostConductor.php @@ -29,11 +29,12 @@ class PostConductor extends Conductor /** * The included fields - * + * * @var string[] */ protected $includes = ['attachments', 'user']; + /** * Run a scope query on the collection before anything else. * @@ -105,7 +106,7 @@ class PostConductor extends Conductor /** * Transform the final model data * - * @param Array $data The model data to transform. + * @param array $data The model data to transform. * @return array The transformed model. */ public function transformFinal(array $data) @@ -116,7 +117,7 @@ class PostConductor extends Conductor /** * Include Attachments Field. - * + * * @param Model $model Them model. * @return mixed The model result. */ @@ -129,7 +130,7 @@ class PostConductor extends Conductor /** * Include User Field. - * + * * @param Model $model Them model. * @return mixed The model result. */ @@ -140,7 +141,7 @@ class PostConductor extends Conductor /** * Transform the Hero field. - * + * * @param mixed $value The current value. * @return array The new value. */ diff --git a/app/Console/Commands/MigrateUploads.php b/app/Console/Commands/MigrateUploads.php new file mode 100644 index 0000000..b41f288 --- /dev/null +++ b/app/Console/Commands/MigrateUploads.php @@ -0,0 +1,50 @@ +first(); + + if ($medium !== null) { + $medium->update(['status' => 'Processing media']); + StoreUploadedFileJob::dispatch($medium, $file)->onQueue('media'); + } else { + unlink($file); + } + } + } +} diff --git a/app/Http/Controllers/Api/ApiController.php b/app/Http/Controllers/Api/ApiController.php index aba301e..f34909b 100644 --- a/app/Http/Controllers/Api/ApiController.php +++ b/app/Http/Controllers/Api/ApiController.php @@ -137,6 +137,7 @@ class ApiController extends Controller protected function respondAsResource( mixed $data, array $options = [], + $validationFn = null ) { $isCollection = $options['isCollection'] ?? false; $appendData = $options['appendData'] ?? null; @@ -144,7 +145,14 @@ class ApiController extends Controller $respondCode = ($options['respondCode'] ?? HttpResponseCodes::HTTP_OK); if ($data === null || ($data instanceof Collection && $data->count() === 0)) { - return $this->respondNotFound(); + $validationData = []; + if (array_key_exists('appendData', $options) === true) { + $validationData = $options['appendData']; + } + + if ($validationFn === null || $validationFn($validationData) === true) { + return $this->respondNotFound(); + } } if (is_null($resourceName) === true || empty($resourceName) === true) { diff --git a/app/Http/Controllers/Api/MediaController.php b/app/Http/Controllers/Api/MediaController.php index 66f9f90..992e3de 100644 --- a/app/Http/Controllers/Api/MediaController.php +++ b/app/Http/Controllers/Api/MediaController.php @@ -35,7 +35,10 @@ class MediaController extends ApiController $collection, ['isCollection' => true, 'appendData' => ['total' => $total] - ] + ], + function ($options) { + return $options['total'] === 0; + } ); } diff --git a/database/migrations/2023_04_05_222458_update_media_table.php b/database/migrations/2023_04_05_222458_update_media_table.php index 43d21a6..0ceb46c 100644 --- a/database/migrations/2023_04_05_222458_update_media_table.php +++ b/database/migrations/2023_04_05_222458_update_media_table.php @@ -14,6 +14,9 @@ return new class extends Migration */ public function up() { + DB::table('media')->whereNull('mime')->update(['mime' => '']); + DB::table('media')->whereNull('permission')->update(['permission' => '']); + Schema::table('media', function (Blueprint $table) { $table->string('storage')->default("cdn"); $table->string('description')->default(""); @@ -21,18 +24,11 @@ return new class extends Migration $table->string('dimensions')->default(""); $table->text('variants'); - // Update null 'mime' values to empty strings - DB::table('media')->whereNull('mime')->update(['mime' => '']); - - // // Update null 'permission' values to empty strings - DB::table('media')->whereNull('permission')->update(['permission' => '']); - $table->bigInteger('size')->default(0)->change(); $table->string('permission')->default("")->nullable(false)->change(); $table->string('mime')->default("")->nullable(false)->change(); - // $table->renameColumn('mime', 'mime_type'); - $table->string('mime_type'); + $table->renameColumn('mime', 'mime_type'); }); } diff --git a/resources/js/components/SMAttachments.vue b/resources/js/components/SMAttachments.vue index 51c83e1..fcdb962 100644 --- a/resources/js/components/SMAttachments.vue +++ b/resources/js/components/SMAttachments.vue @@ -9,8 +9,8 @@
Enter your new password below
-Enter your new password below
+ + +