Skip to content

Commit

Permalink
stream file while uploading fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
classic-o committed Oct 23, 2021
1 parent fd06d89 commit b1b0b9e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### [1.0.8] - 2021-10-23
#### Fixed
- stream file while uploading fixes

### [1.0.7] - 2021-05-31
#### Fixed
- translation
Expand Down
5 changes: 3 additions & 2 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ static function upload($path, $folder = null)
{
try {
$base = basename(parse_url(($path), PHP_URL_PATH));
$content = file_get_contents($path);
Storage::disk('local')->put('nml_temp/' . $base, $content);
$stream = fopen($path, 'r');
Storage::disk('local')->writeStream('nml_temp/' . $base, $stream);
fclose($stream);

$file = new UploadedFile(storage_path('app/nml_temp/' . $base), $base);
if ( !$file ) throw new \Exception(__('The file was not downloaded for unknown reasons'), 0);
Expand Down
27 changes: 16 additions & 11 deletions src/Core/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ClassicO\NovaMediaLibrary\Core;

use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Str;

class Upload {
Expand All @@ -19,6 +20,7 @@ class Upload {

private $config;
private $file;
private $stream;
private $extension;
private $bytes = 0;

Expand Down Expand Up @@ -50,11 +52,12 @@ function setType()

function setWH()
{
list($width, $height) = getimagesize($this->file);

if ( $width and $height ) {
$this->options['wh'] = [$width, $height];
}
if ($this->options['mime'] == 'image') {
[$width, $height] = getimagesize($this->file);
if ($width and $height) {
$this->options['wh'] = [$width, $height];
}
}
}

function setFolder($folder = null)
Expand Down Expand Up @@ -108,7 +111,7 @@ function save()
if (
Helper::storage()->put(
Helper::folder($this->folder . $this->name),
$this->file,
$this->stream,
Helper::visibility($this->private)
)
) {
Expand All @@ -131,13 +134,13 @@ function save()
private function byDefault()
{
$this->bytes = $this->file->getSize();
$this->file = file_get_contents($this->file);
$this->stream = Utils::streamFor(fopen($this->file, 'r+'));
}

private function byResize()
{
try {
list($width, $height) = getimagesize($this->file);
[$width, $height] = getimagesize($this->file);
if (
!is_numeric($width) or !is_numeric($height) or
!$this->resize['upWH'] and
Expand All @@ -157,10 +160,12 @@ private function byResize()
$data = $image->resize($this->resize['width'], $this->resize['height'], function ($constraint) {
if ( !$this->resize['width'] or !$this->resize['height'] ) $constraint->aspectRatio();
if ( true !== $this->resize['upSize'] ) $constraint->upsize();
})->stream(null, data_get($this->config, 'resize.quality'))->__toString();
});

$stream = $data->stream(null, data_get($this->config, 'resize.quality'));

$this->bytes = strlen($data);
$this->file = $data;
$this->bytes = $data->filesize();
$this->stream = $stream;
} catch (\Exception $e) {
$this->noResize();
}
Expand Down

0 comments on commit b1b0b9e

Please sign in to comment.