Skip to content

Commit

Permalink
Fix upload on smart albums (#2218)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 20, 2024
1 parent e6fd0f2 commit b6ee379
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 206 deletions.
37 changes: 0 additions & 37 deletions app/Livewire/Components/Forms/Add/ImportFromServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace App\Livewire\Components\Forms\Add;

use App\Actions\Import\FromServer;
use App\Contracts\Livewire\Params;
use App\Contracts\Models\AbstractAlbum;
use App\Livewire\Forms\ImportFromServerForm;
use App\Livewire\Traits\InteractWithModal;
use App\Policies\AlbumPolicy;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Livewire\Component;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* This defines the Import From Server modals.
Expand All @@ -24,14 +19,6 @@ class ImportFromServer extends Component
*/
use InteractWithModal;

public ImportFromServerForm $form;
private FromServer $fromServer;

public function boot(): void
{
$this->fromServer = resolve(FromServer::class);
}

/**
* We load the parameters.
*
Expand All @@ -42,8 +29,6 @@ public function boot(): void
public function mount(array $params = ['parentID' => null]): void
{
Gate::authorize(AlbumPolicy::CAN_IMPORT_FROM_SERVER, AbstractAlbum::class);

$this->form->init($params[Params::PARENT_ID] ?? null);
}

/**
Expand All @@ -65,26 +50,4 @@ public function close(): void
{
$this->closeModal();
}

/**
* Hook the submit button.
*
* @return StreamedResponse
*/
public function submit(): StreamedResponse
{
Gate::authorize(AlbumPolicy::CAN_IMPORT_FROM_SERVER, AbstractAlbum::class);

// Empty error bag
$this->resetErrorBag();

$this->form->prepare();
$this->form->validate();

/** @var int $userId */
$userId = Auth::id();

// Validate
return $this->fromServer->do($this->form->paths, $this->form->getAlbum(), $this->form->getImportMode(), $userId);
}
}
6 changes: 6 additions & 0 deletions app/Livewire/Components/Forms/Add/ImportFromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Actions\Import\FromUrl;
use App\Contracts\Livewire\Params;
use App\Contracts\Models\AbstractAlbum;
use App\Enum\SmartAlbumType;
use App\Exceptions\MassImportException;
use App\Livewire\Components\Pages\Gallery\Album as PageGalleryAlbum;
use App\Livewire\Forms\ImportFromUrlForm;
Expand Down Expand Up @@ -48,6 +49,11 @@ public function mount(array $params = ['parentID' => null]): void
{
$albumId = $params[Params::PARENT_ID] ?? null;

// remove smart albums => if we are in one: upload to unsorted (i.e. albumId = null)
if (SmartAlbumType::tryFrom($albumId) !== null) {
$albumId = null;
}

/** @var Album $album */
$album = $albumId === null ? null : Album::query()->findOrFail($albumId);

Expand Down
7 changes: 7 additions & 0 deletions app/Livewire/Components/Forms/Add/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Livewire\Params;
use App\Contracts\Models\AbstractAlbum;
use App\Enum\Livewire\FileStatus;
use App\Enum\SmartAlbumType;
use App\Exceptions\PhotoSkippedException;
use App\Facades\Helpers;
use App\Image\Files\NativeLocalFile;
Expand Down Expand Up @@ -53,6 +54,12 @@ class Upload extends Component
public function mount(array $params = ['parentID' => null]): void
{
$this->albumId = $params[Params::PARENT_ID] ?? null;

// remove smart albums => if we are in one: upload to unsorted (i.e. albumId = null)
if (SmartAlbumType::tryFrom($this->albumId) !== null) {
$this->albumId = null;
}

$album = $this->albumId === null ? null : Album::findOrFail($this->albumId);
Gate::authorize(AlbumPolicy::CAN_UPLOAD, [AbstractAlbum::class, $album]);

Expand Down
126 changes: 0 additions & 126 deletions app/Livewire/Forms/ImportFromServerForm.php

This file was deleted.

43 changes: 0 additions & 43 deletions tests/Livewire/Forms/ImportFromServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use App\Livewire\Components\Forms\Add\ImportFromServer;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Tests\Feature\Constants\TestConstants;
use Tests\Livewire\Base\BaseLivewireTest;

class ImportFromServerTest extends BaseLivewireTest
Expand All @@ -37,48 +36,6 @@ public function testLoggedInNoRights(): void
->assertForbidden();
}

public function testLoggedInAndError(): void
{
Livewire::actingAs($this->admin)->test(ImportFromServer::class)
->assertViewIs('livewire.forms.add.import-from-server')
->assertOk()
->set('form.path', '')
->call('submit')
->assertOk()
->assertHasErrors('form.paths.0');
}

// public function testLoggedInWithDeleteImported() : void {
// copy(base_path(TestConstants::SAMPLE_FILE_NIGHT_IMAGE), static::importPath('night.jpg'));

// Livewire::actingAs($this->admin)->test(ImportFromServer::class)
// ->assertViewIs('livewire.forms.add.import-from-server')
// ->assertOk()
// ->set('form.path', static::importPath())
// ->assertSet('form.delete_imported', false)
// ->call('submit')
// ->assertOk()
// ->assertHasNoErrors('form.paths.0');

// $this->assertEquals(true, file_exists(static::importPath('night.jpg')));
// }

// public function testLoggedInWithoutDeleteImported() : void {
// copy(base_path(TestConstants::SAMPLE_FILE_NIGHT_IMAGE), static::importPath('night.jpg'));

// Livewire::actingAs($this->admin)->test(ImportFromServer::class)
// ->assertViewIs('livewire.forms.add.import-from-server')
// ->assertOk()
// ->set('form.path', static::importPath())
// ->set('form.delete_imported', true)
// ->assertSet('form.delete_imported', true)
// ->call('submit')
// ->assertOk()
// ->assertHasNoErrors('form.paths.0');

// $this->assertEquals(false, file_exists(static::importPath('night.jpg')));
// }

public function testLoggedInClose(): void
{
Livewire::actingAs($this->admin)->test(ImportFromServer::class)
Expand Down

0 comments on commit b6ee379

Please sign in to comment.