media job model
This commit is contained in:
135
app/Conductors/MediaJobConductor.php
Normal file
135
app/Conductors/MediaJobConductor.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Conductors;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class MediaJobConductor extends Conductor
|
||||
{
|
||||
/**
|
||||
* The Model Class
|
||||
* @var string
|
||||
*/
|
||||
protected $class = \App\Models\MediaJob::class;
|
||||
|
||||
/**
|
||||
* The default sorting field
|
||||
* @var string
|
||||
*/
|
||||
protected $sort = 'created_at';
|
||||
|
||||
/**
|
||||
* The included fields
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $includes = ['user'];
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of model fields visible to the current user.
|
||||
*
|
||||
* @param Model $model The model in question.
|
||||
* @return array The array of field names.
|
||||
*/
|
||||
public function fields(Model $model): array
|
||||
{
|
||||
$fields = parent::fields($model);
|
||||
|
||||
/** @var \App\Models\User */
|
||||
$user = auth()->user();
|
||||
if ($user === null || $user->hasPermission('admin/media') === false) {
|
||||
$fields = arrayRemoveItem($fields, ['permission', 'storage']);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a scope query on the collection before anything else.
|
||||
*
|
||||
* @param Builder $builder The builder in use.
|
||||
* @return void
|
||||
*/
|
||||
public function scope(Builder $builder): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
if ($user === null) {
|
||||
$builder->where('permission', '');
|
||||
} else {
|
||||
$builder->where('permission', '')->orWhereIn('permission', $user->permissions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the current model is visible.
|
||||
*
|
||||
* @param Model $model The model.
|
||||
* @return boolean Allow model to be visible.
|
||||
*/
|
||||
public static function viewable(Model $model): bool
|
||||
{
|
||||
if ($model->permission !== '') {
|
||||
/** @var \App\Models\User */
|
||||
$user = auth()->user();
|
||||
if ($user === null || $user->hasPermission($model->permission) === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the current model is creatable.
|
||||
*
|
||||
* @return boolean Allow creating model.
|
||||
*/
|
||||
public static function creatable(): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return ($user !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the current model is updatable.
|
||||
*
|
||||
* @param Model $model The model.
|
||||
* @return boolean Allow updating model.
|
||||
*/
|
||||
public static function updatable(Model $model): bool
|
||||
{
|
||||
/** @var \App\Models\User */
|
||||
$user = auth()->user();
|
||||
return ($user !== null && (strcasecmp($model->user_id, $user->id) === 0 ||
|
||||
$user->hasPermission('admin/media') === true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the current model is destroyable.
|
||||
*
|
||||
* @param Model $model The model.
|
||||
* @return boolean Allow deleting model.
|
||||
*/
|
||||
public static function destroyable(Model $model): bool
|
||||
{
|
||||
/** @var \App\Models\User */
|
||||
$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): array
|
||||
{
|
||||
unset($data['user_id']);
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
10
app/Http/Controllers/Api/MediaJobController.php
Normal file
10
app/Http/Controllers/Api/MediaJobController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MediaJobController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -860,7 +860,7 @@ class Media extends Model
|
||||
$video = $ffmpeg->open($filePath);
|
||||
$frame = $video->frame(TimeCode::fromSeconds(5));
|
||||
$frame->save($tempImagePath);
|
||||
} catch(\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e);
|
||||
}
|
||||
|
||||
|
||||
35
app/Models/MediaJob.php
Normal file
35
app/Models/MediaJob.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\Uuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class MediaJob extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use Uuids;
|
||||
|
||||
|
||||
/**
|
||||
* Return the job owner
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the media item
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function media(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Media::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('media_jobs', function (Blueprint $table) {
|
||||
$table->uuid()->primary();
|
||||
$table->timestamps();
|
||||
$table->uuid('user_id');
|
||||
$table->uuid('media_id'); // Add a foreign key for the media model
|
||||
$table->string('status');
|
||||
$table->string('status_text');
|
||||
$table->text('data');
|
||||
$table->integer('progress')->default(0); // Add a column for job progress
|
||||
|
||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||
$table->foreign('media_id')->references('id')->on('media')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('media_jobs');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user