Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho authored and github-actions[bot] committed Sep 22, 2023
1 parent 929a066 commit 02636ca
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 49 deletions.
5 changes: 3 additions & 2 deletions src/FilePond/BeforeStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ abstract class BeforeStore
{
protected readonly FilePondUploadedFile $filePondUploadFile;

final public function setFilePondUploadFile (FilePondUploadedFile $filePondUploadFile): self
final public function setFilePondUploadFile(FilePondUploadedFile $filePondUploadFile): self
{
$this->filePondUploadFile = $filePondUploadFile;

return $this;
}

/**
* handle files modifications and return it's content string
*/
abstract public function handle(): string;
}
}
2 changes: 1 addition & 1 deletion src/FilePond/FilePond.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravuewind\FilePond;


use Illuminate\Support\Collection;

class FilePond
Expand Down Expand Up @@ -31,6 +30,7 @@ public function getUpload(string|array|Collection $serverId, bool $alwaysCollect
return FilePondUploadedFile::createFromServerId($this->factory, $this->factory->getServerId($serverId));
})
->ensure(FilePondUploadedFile::class);

return $collection->count() > 1 || $alwaysCollection ? $collection : $collection->first();
}
}
9 changes: 2 additions & 7 deletions src/FilePond/FilePondFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@

class FilePondFactory
{

public function createFolderId(): string
{
return Str::random('32');
}

/**
* @param string $folderId
* @param int $fileSize
* @return \Laravuewind\FilePond\ServerId
* @throws \Exception
*/
public function createServerId(string $folderId, int $fileSize): ServerId
Expand All @@ -43,9 +39,10 @@ public function diskName(): string
{
$defaultDisk = config('filesystems.default');
$disk = config('laravuewind.filepond.disk') ?? $defaultDisk;
if (!isset(config('filesystems.disks')[$disk])) {
if (! isset(config('filesystems.disks')[$disk])) {
throw new Exception("Disk [$disk] not found in filesystems config");
}

return $disk;
}

Expand All @@ -71,8 +68,6 @@ public function getServerId(string $encrypted): ServerId
}

/**
* @param \Laravuewind\FilePond\ServerId $serverId
* @return bool
* @throws \Exception
*/
public function removeUpload(ServerId $serverId): bool
Expand Down
28 changes: 20 additions & 8 deletions src/FilePond/FilePondUploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace Laravuewind\FilePond;

use const UPLOAD_ERR_OK;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use const UPLOAD_ERR_OK;

class FilePondUploadedFile extends UploadedFile
{
public const EXTENDED_FILENAME_POSTFIX = "extended_file.tmp";
public const EXTENDED_FILENAME_POSTFIX = 'extended_file.tmp';

protected ?BeforeStore $beforeStore = null;

protected static Collection $registeredShutdowns;

protected static bool $removeUploadFileOnShutdownAfterStore = true;

protected function __construct(
Expand All @@ -34,23 +36,27 @@ protected function afterStore(false|string $result): false|string
return false;
}
$this->setRemoveUploadFIleOnShutdown();

return $result;
}

public function beforeStore(BeforeStore $beforeStore): self
{
$this->beforeStore = $beforeStore;

return $this;
}

protected function callBeforeStore(): FilePondUploadedFile|null
protected function callBeforeStore(): ?FilePondUploadedFile
{
if ($this->beforeStore) {
$content = $this->beforeStore
->setFilePondUploadFile($this)
->handle();

return $this->createExtendedFilePondUploadedFile($content);
}

return null;
}

Expand Down Expand Up @@ -87,22 +93,25 @@ public static function createFromServerId(

public function isValid(): bool
{
$isOk = UPLOAD_ERR_OK === $this->getError();
$isOk = $this->getError() === UPLOAD_ERR_OK;
$pathBegin = $this->factory->disk()->path($this->serverId->getFolderPath());

return $isOk && str_starts_with($this->getPathname(), $pathBegin);
}

public function store($path = '', $options = []): false|string
{
$result = $this->callBeforeStore()
?->store($path, $options) ?? parent::store($path, $options);

return $this->afterStore($result);
}

public function storeAs($path, $name = null, $options = []): false|string
{
$result = $this->callBeforeStore()
?->storeAs($path, $name, $options) ?? parent::storeAs($path, $name, $options);

return $this->afterStore($result);
}

Expand All @@ -123,24 +132,27 @@ public function storeMany(array|Collection $items, string|array $options = []):
$extendedOptions = $item->options() ?? $options;
if ($item->name() && ! $extendedFile->storeAs($item->path(), $item->name(), $extendedOptions)) {
$itsOk = false;
} elseif ( ! $item->name() && ! $extendedFile->store($item->path(), $extendedOptions)) {
} elseif (! $item->name() && ! $extendedFile->store($item->path(), $extendedOptions)) {
$itsOk = false;
}
}

return $this->afterStore($itsOk);
}

public function storePublicly($path = '', $options = []): false|string
{
$result = $this->callBeforeStore()
?->storePublicly($path, $options) ?? parent::storePublicly($path, $options);

return $this->afterStore($result);
}

public function storePubliclyAs($path, $name = null, $options = []): false|string
{
$result = $this->callBeforeStore()
?->storePubliclyAs($path, $name, $options) ?? parent::storePubliclyAs($path, $name, $options);

return $this->afterStore($result);
}

Expand All @@ -156,10 +168,10 @@ protected function setRemoveUploadFIleOnShutdown(): void
self::$registeredShutdowns = collect();
}
$folderId = $this->serverId->folderId;
if ( ! self::$registeredShutdowns->has($folderId)) {
if (! self::$registeredShutdowns->has($folderId)) {
self::$registeredShutdowns->put($folderId, true);
register_shutdown_function(fn() => $this->factory->removeUpload($this->serverId));
register_shutdown_function(fn () => $this->factory->removeUpload($this->serverId));
}
}
}
}
}
3 changes: 1 addition & 2 deletions src/FilePond/GarbageCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class GarbageCollector
{

protected int $deleted = 0;

public function __invoke(): void
Expand Down Expand Up @@ -47,4 +46,4 @@ protected function collect(): void
}

}
}
}
4 changes: 2 additions & 2 deletions src/FilePond/HasFilePondUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait HasFilePondUpload
protected function setFilePondInput(string $input): void
{
$serverId = $this->input($input);
if (!is_array($serverId) && !is_string($serverId)) {
if (! is_array($serverId) && ! is_string($serverId)) {
return;
} elseif (is_array($serverId)) {
$files = [];
Expand All @@ -26,4 +26,4 @@ protected function setFilePondInput(string $input): void
$this->merge([$input => FilePond::getUpload($serverId)]);
}
}
}
}
16 changes: 8 additions & 8 deletions src/FilePond/ServerId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

readonly class ServerId
{

public string $folderId;

public int $size;
Expand All @@ -23,20 +22,20 @@
*/
private function __construct(
FilePondFactory $factory,
?string $folderId = null,
?int $size = null,
?string $encrypted = null
string $folderId = null,
int $size = null,
string $encrypted = null
) {
$this->factory = $factory;
if ( ! empty($folderId) && ! empty($size)) {
if (! empty($folderId) && ! empty($size)) {
$this->folderId = $folderId;
$this->size = $size;
$data = [
'folder' => $this->folderId,
'size' => $this->size,
];
$this->encrypted = Crypt::encryptString(json_encode($data));
} elseif ( ! empty($encrypted)) {
} elseif (! empty($encrypted)) {
$this->encrypted = $encrypted;
$data = json_decode(Crypt::decryptString($encrypted), true);
if ($data === null || empty($data['folder']) || empty($data['size'])) {
Expand Down Expand Up @@ -72,7 +71,8 @@ public static function create(FilePondFactory $factory, string $folderId, int $s
public function getFilePath(): string
{
$files = collect($this->factory->disk()->files($this->getFolderPath()))
->filter(fn(string $path) => ! str_ends_with($path, FilePondUploadedFile::EXTENDED_FILENAME_POSTFIX));
->filter(fn (string $path) => ! str_ends_with($path, FilePondUploadedFile::EXTENDED_FILENAME_POSTFIX));

return match ($files->count()) {
0 => throw new Exception(sprintf(
'The upload "%s" file does not exist or has been already removed',
Expand All @@ -87,4 +87,4 @@ public function getFolderPath(): string
{
return $this->basePath.DIRECTORY_SEPARATOR.$this->folderId.DIRECTORY_SEPARATOR;
}
}
}
13 changes: 8 additions & 5 deletions src/FilePond/StoreManyItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

abstract class StoreManyItem
{

protected readonly FilePondUploadedFile $filePondUploadFile;

private string|null $name = null;
private ?string $name = null;

private string|array|null $options = null;

Expand All @@ -16,7 +15,7 @@ abstract class StoreManyItem
*/
abstract public function handle(): string;

final public function name(): string|null
final public function name(): ?string
{
return $this->name;
}
Expand All @@ -31,20 +30,24 @@ final public function options(): string|array|null
*/
abstract public function path(): string;

final public function setFilePondUploadFile (FilePondUploadedFile $filePondUploadFile): self
final public function setFilePondUploadFile(FilePondUploadedFile $filePondUploadFile): self
{
$this->filePondUploadFile = $filePondUploadFile;

return $this;
}

final public function withName(string $name): self
{
$this->name = $name;

return $this;
}

final public function withOptions(string|array $options): self
{
$this->options = $options;

return $this;
}
}
}
Loading

0 comments on commit 02636ca

Please sign in to comment.