Skip to content

Commit

Permalink
change urls and add callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ydombrovsky committed Oct 5, 2022
1 parent 21c73aa commit 3a60136
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function sendFiles(CreateFileConfig $config): ResponseInterface
try {
foreach ($config->getFiles() as $file) {
$dropmeFilesDto = $this->api->create($file->getSize(), $config->getPeriod(), $dropmeFilesDto);
$this->api->uploadFile($file, $dropmeFilesDto);
$this->api->uploadFile($file, $dropmeFilesDto, $config->getCallback());
$this->api->save($config->getPeriod(), $dropmeFilesDto);
}
if (null === $dropmeFilesDto) {
Expand Down
15 changes: 15 additions & 0 deletions src/CreateFileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class CreateFileConfig

protected $period = PeriodTypes::DAYS_3;

/**
* @var ?callable
*/
protected $callback;

public function isNeedPassword(): bool
{
return $this->needPassword;
Expand Down Expand Up @@ -50,4 +55,14 @@ public function getFiles(): array
{
return $this->files;
}

public function getCallback(): ?callable
{
return $this->callback;
}

public function setCallback(?callable $callback): void
{
$this->callback = $callback;
}
}
13 changes: 8 additions & 5 deletions src/DropmefilesAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function create(int $size, int $period = PeriodTypes::DAYS_3, ?Dropmefile
} else {
$dto = new DropmefilesDto();
}
$response = $this->httpClient->request('POST', self::HOST . '/s3/upload/create', [
$response = $this->httpClient->request('POST', self::HOST . '/s5/upload/create', [
RequestOptions::HEADERS => array_merge($this->getBaseHeaders(), [
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
]),
Expand All @@ -79,7 +79,7 @@ public function create(int $size, int $period = PeriodTypes::DAYS_3, ?Dropmefile
*/
public function password(DropmefilesDto $dto): bool
{
$response = $this->httpClient->request('POST', self::HOST . '/s3/upload/password', [
$response = $this->httpClient->request('POST', self::HOST . '/s5/upload/password', [
RequestOptions::HEADERS => array_merge($this->getBaseHeaders(), [
'Content-Type' => 'application/x-www-form-urlencoded',
]),
Expand All @@ -102,7 +102,7 @@ public function password(DropmefilesDto $dto): bool
* @throws DropmefilesException
* @throws GuzzleException
*/
public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto): bool
public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto, ?callable $callback): bool
{
$res = [];
$fileDto = new DropmefilesFileDto(
Expand All @@ -118,6 +118,9 @@ public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto): bool
$chunkSize = 1 === $chunks ? $fileInfo->getSize() : self::CHUNK_SIZE;
$sendFileSize = 0;
for ($chunk = 0; $chunk < $chunks; ++$chunk) {
if ($callback !== null) {
$callback(['chunks'=> $chunks,'chunk' => $chunk]);
}
$content = fread($fileStream, $chunkSize);
$this->sendChunk(
$content,
Expand Down Expand Up @@ -146,7 +149,7 @@ public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto): bool
*/
public function save(int $period, DropmefilesDto $dto): bool
{
$response = $this->httpClient->request('POST', self::HOST . '/s3/upload/save', [
$response = $this->httpClient->request('POST', self::HOST . '/s5/upload/save', [
RequestOptions::HEADERS => array_merge($this->getBaseHeaders(), [
'Content-Type' => 'application/x-www-form-urlencoded',
]),
Expand Down Expand Up @@ -220,7 +223,7 @@ private function sendChunk(
try {
$response = $this->httpClient->request(
'POST',
$this::HOST . '/s3/uploadch?name=' . urlencode($fileInfo->getFilename()) . '&chunk=' . $chunk .
$this::HOST . '/s5/uploadrmbl?name=' . urlencode($fileInfo->getFilename()) . '&chunk=' . $chunk .
'&chunks=' . $chunks . '&updir=' . $uid,
[
RequestOptions::HEADERS => array_merge($this->getBaseHeaders(), [
Expand Down
2 changes: 1 addition & 1 deletion src/DropmefilesAPIInteface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface DropmefilesAPIInteface
{
public function create(int $size, int $period, ?DropmefilesDto $dto = null): DropmefilesDto;

public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto): bool;
public function uploadFile(SplFileInfo $fileInfo, DropmefilesDto $dto, ?callable $callback): bool;

public function save(int $period, DropmefilesDto $dto): bool;

Expand Down

0 comments on commit 3a60136

Please sign in to comment.