Skip to content

Commit

Permalink
поддержка события 'progress' для метода 'upload'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arhitector committed Aug 22, 2016
1 parent f3d62d4 commit cfd6cda
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Disk/Resource/Closed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*/
namespace Arhitector\Yandex\Disk\Resource;


use Arhitector\Yandex\Client\Container;
use Arhitector\Yandex\Client\Exception\NotFoundException;
use Arhitector\Yandex\Client\Stream\Progress;
use Arhitector\Yandex\Disk;
use Arhitector\Yandex\Disk\AbstractResource;
use Arhitector\Yandex\Disk\Exception\AlreadyExistsException;
use League\Event\Event;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Request;
use Zend\Diactoros\Stream;
Expand Down Expand Up @@ -165,7 +166,7 @@ public function getProperties()
* @param mixed $meta строка либо массив значений
* @param mixed $value NULL чтобы удалить определённую метаинформаию когда $meta строка
*
* @return \Arhitector\Yandex\Disk
* @return Closed
* @throws \LengthException
*/
public function set($meta, $value = null)
Expand Down Expand Up @@ -581,7 +582,8 @@ public function upload($file_path, $overwrite = false, $disable_redirects = fals
$response = $this->client->send(new Request($this->uri->withPath($this->uri->getPath().'resources/upload')
->withQuery(http_build_query([
'url' => $file_path,
'path' => $this->getPath()
'path' => $this->getPath(),
'disable_redirects' => (int) $disable_redirects
], null, '&')), 'POST'));
}
catch (AlreadyExistsException $exc)
Expand Down Expand Up @@ -632,8 +634,19 @@ public function upload($file_path, $overwrite = false, $disable_redirects = fals
// $this->client->setRetries = 1
throw new \RuntimeException('Не возможно загрузить локальный файл - не получено разрешение.');
}

if ($this->getEmitter()->hasListeners('progress'))
{
$stream = new Progress($file_path, 'rb');
$stream->addListener('progress', function (Event $event, $percent) {
$this->emit('progress', $percent);
});
}
else
{
$stream = new Stream($file_path, 'rb');
}

$stream = new Stream($file_path, 'rb');
$response = $this->client->send((new Request($access_upload['href'], 'PUT', $stream)));
$this->emit('uploaded', $this, $this->client);
$this->client->emit('uploaded', $this, $this->client);
Expand Down Expand Up @@ -669,4 +682,4 @@ protected function createDocViewerUrl()
return false;
}

}
}

0 comments on commit cfd6cda

Please sign in to comment.