Skip to content

Commit

Permalink
feat(admin): the video path attribute is no longer required in the up…
Browse files Browse the repository at this point in the history
…load action (#639)

* feat: the video path is not required

* refactor: get resolution by arr laravel class

* test: removed the resolution attribute on tests
  • Loading branch information
Kyrch authored Mar 26, 2024
1 parent 559db07 commit f50dd32
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
9 changes: 1 addition & 8 deletions app/Actions/Storage/Wiki/Video/UploadVideoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,10 @@ protected function getOrCreateVideo(): Video
Video::ATTRIBUTE_FILENAME => File::name($this->file->getClientOriginalName()),
Video::ATTRIBUTE_MIMETYPE => $this->file->getMimeType(),
Video::ATTRIBUTE_PATH => $path,
Video::ATTRIBUTE_RESOLUTION => intval(Arr::get(SubmissionRule::$ffprobeData['streams'][0], 'height')),
Video::ATTRIBUTE_SIZE => $this->file->getSize(),
];

if (Arr::has($this->attributes, Video::ATTRIBUTE_RESOLUTION)) {
if (!empty($this->attributes[Video::ATTRIBUTE_RESOLUTION])) {
$attributes[Video::ATTRIBUTE_RESOLUTION] = Arr::get($this->attributes, Video::ATTRIBUTE_RESOLUTION);
} else {
$ffprobeData = SubmissionRule::$ffprobeData;
$attributes[Video::ATTRIBUTE_RESOLUTION] = intval($ffprobeData['streams'][0]['height']);
}
}
if (Arr::has($this->attributes, Video::ATTRIBUTE_NC)) {
$attributes[Video::ATTRIBUTE_NC] = Arr::get($this->attributes, Video::ATTRIBUTE_NC);
}
Expand Down
8 changes: 6 additions & 2 deletions app/Nova/Actions/Storage/Base/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public function fields(NovaRequest $request): array
->help(__('nova.actions.storage.upload.fields.file.help')),

Text::make(__('nova.actions.storage.upload.fields.path.name'), 'path')
->required()
->rules(['required', 'string', 'doesnt_start_with:/', 'doesnt_end_with:/', new StorageDirectoryExistsRule($fs)])
->rules(fn ($request) => [
'doesnt_start_with:/',
'doesnt_end_with:/',
empty($request->input('path')) ? '' : 'string',
empty($request->input('path')) ? '' : new StorageDirectoryExistsRule($fs),
])
->help(__('nova.actions.storage.upload.fields.path.help')),
];
}
Expand Down
27 changes: 18 additions & 9 deletions app/Nova/Actions/Storage/Wiki/Video/UploadVideoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use App\Actions\Storage\Wiki\Video\UploadVideoAction as UploadVideo;
use App\Constants\Config\VideoConstants;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Enums\Models\Wiki\VideoOverlap;
use App\Enums\Models\Wiki\VideoSource;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Video;
use App\Nova\Actions\Storage\Base\UploadAction;
Expand All @@ -33,6 +35,7 @@
use App\Rules\Wiki\Submission\Video\VideoPixelFormatStreamRule;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Config;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\File as FileRule;
Expand Down Expand Up @@ -81,13 +84,6 @@ public function fields(NovaRequest $request): array
Hidden::make(__('nova.resources.singularLabel.anime_theme_entry'), AnimeThemeEntry::ATTRIBUTE_ID)
->default(fn () => $parent instanceof AnimeThemeEntry ? $parent->getKey() : null),

Number::make(__('nova.fields.video.resolution.name'), Video::ATTRIBUTE_RESOLUTION)
->min(360)
->max(1080)
->nullable()
->rules(['nullable', 'integer'])
->help(__('nova.fields.video.resolution.help')),

Boolean::make(__('nova.fields.video.nc.name'), Video::ATTRIBUTE_NC)
->nullable()
->rules(['nullable', 'boolean'])
Expand Down Expand Up @@ -141,7 +137,7 @@ public function fields(NovaRequest $request): array
*/
protected function action(ActionFields $fields, Collection $models): UploadVideo
{
/** @var string $path */
/** @var ?string $path */
$path = $fields->get('path');

/** @var UploadedFile $file */
Expand All @@ -153,8 +149,21 @@ protected function action(ActionFields $fields, Collection $models): UploadVideo
/** @var UploadedFile|null $script */
$script = $fields->get('script');

if ($path === null) {
/** @var Anime|null $anime */
$anime = $entry->animetheme->anime;
if ($anime instanceof Anime) {
$year = $anime->year;
$path = $year >= 2000 ?
Str::of(strval($year))
->append('/')
->append(AnimeSeason::tryFrom($anime->season->value)->localize())
->__toString()
: floor($year % 100 / 10) . '0s';
}
}

$attributes = [
Video::ATTRIBUTE_RESOLUTION => $fields->get(Video::ATTRIBUTE_RESOLUTION),
Video::ATTRIBUTE_NC => $fields->get(Video::ATTRIBUTE_NC),
Video::ATTRIBUTE_SUBBED => $fields->get(Video::ATTRIBUTE_SUBBED),
Video::ATTRIBUTE_LYRICS => $fields->get(Video::ATTRIBUTE_LYRICS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public function testSetsAttributes(): void
$source = Arr::random(VideoSource::cases());

$attributes = [
Video::ATTRIBUTE_RESOLUTION => $this->faker->numberBetween(360, 1080),
Video::ATTRIBUTE_NC => $this->faker->boolean(),
Video::ATTRIBUTE_SUBBED => $this->faker->boolean(),
Video::ATTRIBUTE_LYRICS => $this->faker->boolean(),
Expand Down

0 comments on commit f50dd32

Please sign in to comment.