updates
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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']));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
50
app/Console/Commands/MigrateUploads.php
Normal file
50
app/Console/Commands/MigrateUploads.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\StoreUploadedFileJob;
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\Media;
|
||||
use File;
|
||||
|
||||
class MigrateUploads extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'uploads:migrate';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Migrate the uploads folder to the CDN';
|
||||
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$files = File::allFiles(public_path('uploads'));
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = pathinfo($file, PATHINFO_BASENAME);
|
||||
|
||||
// echo $filename . "\n";
|
||||
$medium = Media::where('name', $filename)->first();
|
||||
|
||||
if ($medium !== null) {
|
||||
$medium->update(['status' => 'Processing media']);
|
||||
StoreUploadedFileJob::dispatch($medium, $file)->onQueue('media');
|
||||
} else {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -35,7 +35,10 @@ class MediaController extends ApiController
|
||||
$collection,
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]
|
||||
]
|
||||
],
|
||||
function ($options) {
|
||||
return $options['total'] === 0;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user