Skip to content
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

Add optional destination folder and get file content #7

Merged
merged 3 commits into from
May 15, 2021
Merged
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
14 changes: 13 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ Added support for refresh tokens, now when a token is about to expire and there

## Version 3.0.1

Improved download method, request a path, it will be downloaded.
Improved download method, request a path, it will be downloaded.

## Version 3.1.0

Add an optional destination folder for the downloaded files in **download** method.
```php
public function download($path, $destFolder = '')
```

Add a function **getContentsFile** to get the content of a file without having to download.
```php
public function getContentsFile($path)
```
40 changes: 34 additions & 6 deletions src/Resources/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function upload($path, $uploadPath, $mode = 'add')
}
}

public function download($path)
public function download($path, $destFolder = '')
{
$path = $this->forceStartingSlash($path);

Expand All @@ -126,15 +126,43 @@ public function download($path)

$header = json_decode($response->getHeader('Dropbox-Api-Result')[0], true);
$body = $response->getBody()->getContents();
$folder = 'dropbox-temp';

if (! is_dir($folder)) {
mkdir($folder);
if (empty($destFolder)){
$destFolder = 'dropbox-temp';

if (! is_dir($destFolder)) {
mkdir($destFolder);
}
}

file_put_contents($folder.$header['name'], $body);
file_put_contents($destFolder.$header['name'], $body);

return response()->download($destFolder.$header['name'], $header['name'])->deleteFileAfterSend();

} catch (ClientException $e) {
throw new Exception($e->getResponse()->getBody()->getContents());
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function getContentsFile($path)
{
$path = $this->forceStartingSlash($path);

try {
$client = new Client;

$response = $client->post("https://content.dropboxapi.com/2/files/download", [
'headers' => [
'Authorization' => 'Bearer ' . $this->getAccessToken(),
'Dropbox-API-Arg' => json_encode([
'path' => $path
])
]
]);

return response()->download($folder.$header['name'], $header['name'])->deleteFileAfterSend();
return $response->getBody()->getContents();

} catch (ClientException $e) {
throw new Exception($e->getResponse()->getBody()->getContents());
Expand Down