Skip to content

Commit

Permalink
Merge pull request #16 from jobtech-dev/develop
Browse files Browse the repository at this point in the history
Chunk refactor
  • Loading branch information
ilgala committed Sep 1, 2020
2 parents 2c5fa0f + ecd940e commit a9240d4
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 164 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Every major / minor version release will be documented in the changelog.

## v1.1.1 - 2020-09-01

Better documentation, fixed issue with remote files mapped into a chunk object

## v1.0.0 - 2020-08-31

Laravel Chunky first release. Main features:
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,26 @@ Chunky::deleteChunk('chunks-folder');
The package include a method that, given the chunks folder, will return a sorted collection. Each item contains the relative chunk's path and index.

```php
$chunks = Chunky::chunk('chunks-folder-name');
$chunks = Chunky::chunks('chunks-folder-name');

foreach($chunks as $chunk) {
print_r($chunk);
print_r($chunk->toArray());
}

// [
// 'index' => 0,
// 'path' => '/path/to/0_chunk.ext'
// 'path' => '/path/to/chunks-folder-name/0_chunk.ext',
// [...]
// ],
// [
// 'index' => 1,
// 'path' => '/path/to/1_chunk.ext'
// 'path' => '/path/to/chunks-folder-name/1_chunk.ext',
// [...]
// ],
// [
// 'index' => 2,
// 'path' => '/path/to/2_chunk.ext'
// 'path' => '/path/to/chunks-folder-name/2_chunk.ext',
// [...]
// ],
// ...
```
Expand Down
226 changes: 133 additions & 93 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Responsable;
Expand All @@ -12,12 +13,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Jobtech\LaravelChunky\Exceptions\ChunkyException;
use Jobtech\LaravelChunky\Http\Resources\ChunkResource;
use Symfony\Component\HttpFoundation\File\File;

/**
* @mixin \Symfony\Component\HttpFoundation\File\File
*/
class Chunk implements Arrayable, Jsonable, Responsable
{
use ForwardsCalls;
Expand All @@ -28,52 +27,98 @@ class Chunk implements Arrayable, Jsonable, Responsable
/** @var int */
private $index;

/** @var \Symfony\Component\HttpFoundation\File\File */
private $file;
/** @var \Symfony\Component\HttpFoundation\File\File|string */
private $path;

/** @var string|null */
private $disk;

/** @var bool */
private $last;

public function __construct(int $index, File $file, $last = false)
public function __construct(int $index, $path, $disk = null, $last = false)
{
$this->index = $index;
$this->file = $file;
$this->path = $path;
$this->disk = $disk;
$this->last = $last;
}

private function sanitizeName(int $index, string $file_name, string $extension)
private function sanitizeName(int $index)
{
return $index.'_'.Str::slug($file_name).'.'.$extension;
return $index.'_'.Str::slug($this->getName()).'.'.$this->guessExtension();
}

/**
* Retrieve the chunk original file upload.
* Retrieve the chunk index.
*
* @return \Symfony\Component\HttpFoundation\File\File|null
* @return int
*/
public function getFile(): ?File
public function getIndex(): int
{
return $this->file;
return $this->index;
}

/**
* Retrieve the chunk original file upload.
* Retrieve the chunk file path.
*
* @param \Symfony\Component\HttpFoundation\File\File $file
* @return \Symfony\Component\HttpFoundation\File\File|string
*/
public function setFile(File $file)
public function getPath(): string
{
if ($this->path instanceof File) {
return $this->path->getRealPath();
}

return $this->path;
}

public function getFilename($suffix = null): string
{
$this->file = $file;
if ($this->path instanceof UploadedFile) {
return basename($this->path->getClientOriginalName(), $suffix);
} elseif ($this->path instanceof File) {
return $this->path->getBasename($suffix);
}

return basename($this->path, $suffix);
}

public function getName(): string
{
return pathinfo(
$this->getFilename($this->guessExtension()),
PATHINFO_FILENAME
);
}

public function guessExtension()
{
if ($this->path instanceof File) {
return $this->path->guessExtension();
}

return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
}

/**
* Retrieve the chunk index.
* Retrieve the chunk file disk.
*
* @return int
* @return string|null
*/
public function getIndex(): int
public function getDisk(): ?string
{
return $this->index;
return $this->disk;
}

/**
* Set the chunk file disk.
*
* @param string|null $disk
*/
public function setDisk($disk = null)
{
$this->disk = $disk;
}

/**
Expand All @@ -93,70 +138,77 @@ public function setLast(bool $last): void
}

/**
* Store the chunk into filesystem.
* Retrive file contents.
*
* @param string $folder
* @param array $options
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getFile()
{
if ($this->path instanceof File) {
return $this->path;
}

$this->filesystem()
->get($this->path);
}

/**
* Retrieve the chunk's filesystem and disk.
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return Chunk
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function storeIn(string $folder, $options = []): Chunk
public function filesystem(): Filesystem
{
// Get extension
$extension = $this->guessExtension();
return Container::getInstance()->make(FilesystemFactory::class)
->disk($this->getDisk());
}

// Check file instance and retrieve file name
if ($this->file instanceof UploadedFile) {
$file_name = str_replace(
$extension,
'',
$this->file->getClientOriginalName()
);
} else {
$file_name = $this->file->getBasename(
$extension
);
}
/**
* If this method is called, when a chunk is turned to array, the file path and real path
* will be omitted.
*
* @return $this
*/
public function hideFileInfo()
{
$this->show_file_info = false;

// Sanitize name
$chunk_name = $this->sanitizeName(
$this->index,
$file_name,
$extension
);
return $this;
}

// Store file
$disk = Arr::pull($options, 'disk');
$path = Container::getInstance()->make(FilesystemFactory::class)
->disk($disk)
->putFileAs(
$folder,
$this->file,
$chunk_name,
$options
);
/**
* If this method is called, when a chunk is turned to array, the file path and real path
* will be included.
*
* @return $this
*/
public function showFileInfo()
{
$this->show_file_info = true;

return new static($this->getIndex(), new File($path, false));
return $this;
}

/**
* {@inheritdoc}
*/
public function toArray(): array
{
$extension = $this->file->guessExtension();
$extension = $this->guessExtension();

$data = [
'name' => $this->getBasename($extension),
'name' => $this->getName(),
'extension' => $extension,
'index' => $this->getIndex(),
'last' => $this->isLast(),
];

if ($this->show_file_info) {
$data['file'] = $this->getRealPath();
$data['file'] = $this->getFilename();
$data['path'] = $this->getPath();
}

Expand All @@ -177,7 +229,7 @@ public function toJson($options = 0): string
public function toResponse($request)
{
if ($request->wantsJson()) {
return $this->toResource($request);
return $this->toResource();
}

return new Response(
Expand All @@ -197,13 +249,27 @@ public function toResource()
return new $resource($this);
}

public function __call($method, $parameters)
/**
* Store the chunk into filesystem.
*
* @param string $folder
* @param array $options
*
* @return Chunk
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function store(string $folder, $options = []): Chunk
{
if (! method_exists($this, $method)) {
return $this->forwardCallTo($this->file, $method, $parameters);
if (! $this->path instanceof File) {
throw new ChunkyException('Path must be a file');
}

return $this->{$method}(...$parameters);
$chunk_name = $this->sanitizeName($this->index);

$path = $this->filesystem()
->putFileAs($folder, $this->path, $chunk_name, $options);

return new static($this->getIndex(), $path, $this->getDisk(), $this->isLast());
}

/**
Expand All @@ -220,34 +286,8 @@ public function __call($method, $parameters)
*/
public static function storeFrom(File $file, string $folder, int $index, $options = [])
{
$chunk = new static($index, $file);
$chunk = new static($index, $file, Arr::pull($options, 'disk'));

return $chunk->storeIn($folder, $options);
}

/**
* If this method is called, when a chunk is turned to array, the file path and real path
* will be omitted.
*
* @return $this
*/
public function hideFileInfo()
{
$this->show_file_info = false;

return $this;
}

/**
* If this method is called, when a chunk is turned to array, the file path and real path
* will be included.
*
* @return $this
*/
public function showFileInfo()
{
$this->show_file_info = true;

return $this;
return $chunk->store($folder, $options);
}
}

0 comments on commit a9240d4

Please sign in to comment.