Skip to content

Commit

Permalink
add upload function
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Apr 26, 2024
1 parent c9b6c19 commit a80e973
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Cone\Root\Models;

use Closure;
use Cone\Root\Database\Factories\MediumFactory;
use Cone\Root\Interfaces\Models\Medium as Contract;
use Cone\Root\Interfaces\Models\User;
use Cone\Root\Jobs\MoveFile;
use Cone\Root\Jobs\PerformConversions;
use Cone\Root\Support\Facades\Conversion;
use Cone\Root\Traits\Filterable;
use Cone\Root\Traits\InteractsWithProxy;
Expand All @@ -15,7 +18,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
Expand Down Expand Up @@ -117,11 +120,27 @@ protected static function newFactory(): Factory
}

/**
* Make a new medium instance from the file.
* Upload the given file.
*/
public static function fromFile(File $file, array $attributes = []): static
public static function upload(UploadedFile $file, ?Closure $callback = null): static
{
return static::fromPath($file->getPathname(), $attributes);
$medium = static::fromPath($file->getPathname(), [
'file_name' => $file->getClientOriginalName(),
'name' => pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME),
]);

if (is_callable($callback)) {
call_user_func_array($callback, [$medium, $file]);
}

$medium->save();

$path = Storage::disk('local')->path(Storage::disk('local')->putFile('root-tmp', $file));

MoveFile::withChain($medium->convertible() ? [new PerformConversions($medium)] : [])
->dispatch($medium, $path, false);

return $medium;
}

/**
Expand Down

0 comments on commit a80e973

Please sign in to comment.