Skip to content

add support stream upload #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"imagine/imagine": "^0.7",
"myclabs/php-enum": "^1.4",
"psr/container": "^1.0",
"symfony/http-foundation": "^3.0|^4.0",
"symfony/http-foundation": "^3.0|^4.0|^5.0",
"ext-curl": "*"
},
"require-dev": {
Expand Down
11 changes: 11 additions & 0 deletions src/Contracts/Storage/StreamableFileInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Czim\FileHandling\Contracts\Storage;


interface StreamableFileInterface
{

public function stream($function);

}
10 changes: 9 additions & 1 deletion src/Storage/File/ProcessableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
namespace Czim\FileHandling\Storage\File;

use Czim\FileHandling\Contracts\Storage\ProcessableFileInterface;
use Czim\FileHandling\Contracts\Storage\StreamableFileInterface;
use Czim\FileHandling\Exceptions\StorableFileCouldNotBeDeletedException;
use RuntimeException;
use SplFileInfo;

class ProcessableFile extends AbstractStorableFile implements ProcessableFileInterface
class ProcessableFile extends AbstractStorableFile implements ProcessableFileInterface,StreamableFileInterface
{

/**
Expand Down Expand Up @@ -107,4 +108,11 @@ public function path()
return $this->file->getRealPath();
}

public function stream($function)
{
$stream = fopen($this->file->getRealPath(), 'r+');
$output = $function($stream);
fclose($stream);
return $output;
}
}
18 changes: 14 additions & 4 deletions src/Storage/File/SplFileInfoStorableFile.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace Czim\FileHandling\Storage\File;

use Czim\FileHandling\Contracts\Storage\StreamableFileInterface;
use Czim\FileHandling\Exceptions\StorableFileCouldNotBeDeletedException;
use RuntimeException;
use SplFileInfo;
use UnexpectedValueException;

class SplFileInfoStorableFile extends AbstractStorableFile
class SplFileInfoStorableFile extends AbstractStorableFile implements StreamableFileInterface
{

/**
Expand All @@ -19,11 +21,12 @@ class SplFileInfoStorableFile extends AbstractStorableFile
* Initializes the storable file with mixed data.
*
* @param mixed $data
*
* @return $this
*/
public function setData($data)
{
if ( ! ($data instanceof SplFileInfo)) {
if (!($data instanceof SplFileInfo)) {
throw new UnexpectedValueException('Expected SplFileInfo instance');
}

Expand All @@ -39,7 +42,7 @@ public function setData($data)
*/
protected function setDerivedFileProperties()
{
if ( ! $this->file || ! file_exists($this->file->getRealPath())) {
if (!$this->file || !file_exists($this->file->getRealPath())) {
throw new RuntimeException("Local file not found at '{$this->file->getPath()}'");
}

Expand Down Expand Up @@ -85,7 +88,7 @@ public function delete()
);
}

if ( ! $success) {
if (!$success) {
// @codeCoverageIgnoreStart
throw new StorableFileCouldNotBeDeletedException("Failed to unlink '{$this->path()}'");
// @codeCoverageIgnoreEnd
Expand All @@ -100,4 +103,11 @@ public function path()
return $this->file->getRealPath();
}

public function stream($function)
{
$stream = fopen($this->file->getRealPath(), 'r+');
$output = $function($stream);
fclose($stream);
return $output;
}
}
39 changes: 29 additions & 10 deletions src/Storage/Laravel/LaravelStorage.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Czim\FileHandling\Storage\Laravel;

use Czim\FileHandling\Contracts\Storage\StorableFileInterface;
use Czim\FileHandling\Contracts\Storage\StorageInterface;
use Czim\FileHandling\Contracts\Storage\StoredFileInterface;
use Czim\FileHandling\Contracts\Storage\StreamableFileInterface;
use Czim\FileHandling\Exceptions\FileStorageException;
use Czim\FileHandling\Storage\File\DecoratorStoredFile;
use Czim\FileHandling\Storage\File\RawStorableFile;
Expand Down Expand Up @@ -32,24 +34,26 @@ class LaravelStorage implements StorageInterface
protected $baseUrl;

/**
* @param Filesystem $filesystem
* @param bool $isLocal
* @param Filesystem $filesystem
* @param bool $isLocal
* @param null|string $baseUrl
*/
public function __construct(
Filesystem $filesystem,
$isLocal = true,
$baseUrl = null
) {
)
{
$this->filesystem = $filesystem;
$this->isLocal = $isLocal;
$this->baseUrl = trim($baseUrl ?: '', '/');
$this->isLocal = $isLocal;
$this->baseUrl = trim($baseUrl ?: '', '/');
}

/**
* Returns whether a stored file exists.
*
* @param string $path
*
* @return bool
*/
public function exists($path)
Expand All @@ -61,6 +65,7 @@ public function exists($path)
* Returns a public URL to the stored file.
*
* @param string $path
*
* @return string
*/
public function url($path)
Expand All @@ -74,6 +79,7 @@ public function url($path)
* Note that the mimetype is not filled in here. Tackle this manually if it is required.
*
* @param string $path
*
* @return StoredFileInterface
*/
public function get($path)
Expand All @@ -93,14 +99,25 @@ public function get($path)
* Stores a file.
*
* @param StorableFileInterface $file mixed content to store
* @param string $path where the file should be stored, including the filename
* @param string $path where the file should be stored, including the filename
*
* @return StoredFileInterface
* @throws FileStorageException
*/
public function store(StorableFileInterface $file, $path)
{
if ( ! $this->filesystem->put($path, $file->content())) {
throw new FileStorageException("Failed to store '{$file->name()}' to '{$path}'");
if ($file instanceof StreamableFileInterface) {
$file->stream(function ($stream) use ($file, $path) {
if ($this->filesystem->exists($path)) {
$this->filesystem->delete($path);
}
if (!$this->filesystem->writeStream($path, $stream))
throw new FileStorageException("Failed to store streamable '{$file->name()}' to '{$path}'");
});
} else {
if (!$this->filesystem->put($path, $file->content())) {
throw new FileStorageException("Failed to store '{$file->name()}' to '{$path}'");
}
}

$stored = new DecoratorStoredFile($file);
Expand All @@ -113,6 +130,7 @@ public function store(StorableFileInterface $file, $path)
* Deletes a stored media file.
*
* @param string $path
*
* @return bool
*/
public function delete($path)
Expand All @@ -122,11 +140,12 @@ public function delete($path)

/**
* @param string $path
*
* @return string
*/
protected function prefixBaseUrl($path)
{
return $this->baseUrl . '/' . ltrim($path, '/');
}

}
28 changes: 13 additions & 15 deletions src/Support/Download/UrlDownloader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Czim\FileHandling\Support\Download;

use Czim\FileHandling\Contracts\Support\MimeTypeHelperInterface;
Expand Down Expand Up @@ -28,6 +29,7 @@ public function __construct(MimeTypeHelperInterface $mimeTypeHelper)
* Downloads from a URL and returns locally stored temporary file.
*
* @param string $url
*
* @return string
* @throws CouldNotRetrieveRemoteFileException
*/
Expand Down Expand Up @@ -59,6 +61,7 @@ public function download($url)
*
* @param string $url
* @param string $localPath
*
* @throws CouldNotRetrieveRemoteFileException
* @codeCoverageIgnore
*/
Expand All @@ -67,13 +70,19 @@ protected function downloadToTempLocalPath($url, $localPath)
$curlError = 'unknown error';

try {
$fileStream = fopen($localPath, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $fileStream);


$rawFile = curl_exec($ch);

fclose($fileStream);

if ($rawFile === false) {
$curlError = curl_error($ch);
}
Expand All @@ -93,25 +102,12 @@ protected function downloadToTempLocalPath($url, $localPath)
"curl_exec failed while downloading '{$url}': " . $curlError
);
}

try {
if (false === file_put_contents($localPath, $rawFile)) {
throw new CouldNotRetrieveRemoteFileException('file_put_contents call failed');
}

} catch (Exception $e) {

throw new CouldNotRetrieveRemoteFileException(
'file_put_contents call threw an exception',
$e->getCode(),
$e
);
}
}

/**
* @param string $path
* @param string $name
*
* @return string
* @throws CouldNotRetrieveRemoteFileException
*/
Expand Down Expand Up @@ -146,6 +142,7 @@ protected function makeLocalTemporaryPath()
*
* @param string $path
* @param string $newName
*
* @return string
* @throws CouldNotRetrieveRemoteFileException
*/
Expand All @@ -163,7 +160,7 @@ protected function renameFile($path, $newName)
}

// @codeCoverageIgnoreStart
if ( ! $success) {
if (!$success) {
throw new CouldNotRetrieveRemoteFileException("Failed to rename '{$path}' to '{$newName}'");
}
// @codeCoverageIgnoreEnd
Expand All @@ -175,6 +172,7 @@ protected function renameFile($path, $newName)
* Normalizes URL for safe cURL use.
*
* @param string $url
*
* @return string
*/
protected function normalizeUrl($url)
Expand Down