Skip to content

Commit 2756e5d

Browse files
committed
fixed merge conflict
2 parents c2229b4 + c307a04 commit 2756e5d

File tree

3 files changed

+307
-9
lines changed

3 files changed

+307
-9
lines changed

changelog.md

-1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,3 @@ Dropbox::files()->move($fromPath, $toPath, $autoRename = false, $allowOwnershipT
9999
## Version 3.0.4
100100

101101
added support for Laravel 9
102-

readme.md

+273-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,279 @@
55

66
A Laravel package for working with Dropbox v2 API.
77

8-
# Documentation and install instructions
9-
[https://dcblog.dev/docs/laravel-dropbox](https://dcblog.dev/docs/laravel-dropbox)
8+
Dropbox API documentation can be found at:
9+
https://www.dropbox.com/developers/documentation/http/documentation
10+
11+
## Application Register
12+
To use Dropbox API an application needs creating at https://www.dropbox.com/developers/apps
13+
14+
Create a new application, select either Dropbox API or Dropbox Business API
15+
Next select the type of access needed either the app folder (useful for isolating to a single folder), or full Dropbox.
16+
17+
Next copy and paste the APP Key and App Secret into your .env file:
18+
19+
```
20+
DROPBOX_CLIENT_ID=
21+
DROPBOX_SECRET_ID=
22+
```
23+
24+
Now enter your desired redirect URL. This is the URL your application will use to connect to Dropbox API.
25+
26+
A common URL is https://domain.com/dropbox/connect
27+
28+
## Install
29+
30+
Via Composer
31+
32+
```
33+
composer require dcblogdev/laravel-dropbox
34+
```
35+
36+
## Config
37+
38+
You can publish the config file with:
39+
40+
```
41+
php artisan vendor:publish --provider="Dcblogdev\Dropbox\DropboxServiceProvider" --tag="config"
42+
```
43+
44+
When published, the config/dropbox.php config file contains, make sure to publish this file and change the scopes to match the scopes of your Dropbox app, inside Dropbox app console.
45+
46+
```php
47+
<?php
48+
49+
return [
50+
51+
/*
52+
* set the client id
53+
*/
54+
'clientId' => env('DROPBOX_CLIENT_ID'),
55+
56+
/*
57+
* set the client secret
58+
*/
59+
'clientSecret' => env('DROPBOX_SECRET_ID'),
60+
61+
/*
62+
* Set the url to trigger the oauth process this url should call return Dropbox::connect();
63+
*/
64+
'redirectUri' => env('DROPBOX_OAUTH_URL'),
65+
66+
/*
67+
* Set the url to redirecto once authenticated;
68+
*/
69+
'landingUri' => env('DROPBOX_LANDING_URL', '/'),
70+
71+
/**
72+
* Set access token, when set will bypass the oauth2 process
73+
*/
74+
'accessToken' => env('DROPBOX_ACCESS_TOKEN', ''),
75+
76+
/**
77+
* Set access type, options are offline and online
78+
* Offline - will return a short-lived access_token and a long-lived refresh_token that can be used to request a new short-lived access token as long as a user's approval remains valid.
79+
*
80+
* Online - will return a short-lived access_token
81+
*/
82+
'accessType' => env('DROPBOX_ACCESS_TYPE', 'offline'),
83+
84+
/*
85+
set the scopes to be used
86+
*/
87+
'scopes' => 'account_info.read files.metadata.write files.metadata.read files.content.write files.content.read',
88+
];
89+
```
90+
91+
## Migration
92+
You can publish the migration with:
93+
94+
php artisan vendor:publish --provider="Dcblogdev\Dropbox\DropboxServiceProvider" --tag="migrations"
95+
After the migration has been published you can create the tokens tables by running the migration:
96+
97+
```
98+
php artisan migrate
99+
```
100+
101+
.ENV Configuration
102+
Ensure you've set the following in your .env file:
103+
104+
```
105+
DROPBOX_CLIENT_ID=
106+
DROPBOX_SECRET_ID=
107+
DROPBOX_OAUTH_URL=https://domain.com/dropbox/connect
108+
DROPBOX_LANDING_URL=https://domain.com/dropbox
109+
DROPBOX_ACCESS_TYPE=offline
110+
```
111+
112+
Bypass Oauth2
113+
You can bypass the oauth2 process by generating an access token in your dropbox app and entering it on the .env file:
114+
115+
```
116+
DROPBOX_ACCESS_TOKEN=
117+
```
118+
119+
## Usage
120+
Note this package expects a user to be logged in.
121+
122+
Note: these examples assume the authentication is using the oauth2 and not setting the access token in the .env directly.
123+
124+
If setting the access code directly don't rely on Dropbox::getAccessToken()
125+
126+
A routes example:
127+
128+
```php
129+
Route::group(['middleware' => ['web', 'auth']], function(){
130+
Route::get('dropbox', function(){
131+
132+
if (! Dropbox::isConnected()) {
133+
return redirect(env('DROPBOX_OAUTH_URL'));
134+
} else {
135+
//display your details
136+
return Dropbox::post('users/get_current_account');
137+
}
138+
139+
});
140+
141+
Route::get('dropbox/connect', function(){
142+
return Dropbox::connect();
143+
});
144+
145+
Route::get('dropbox/disconnect', function(){
146+
return Dropbox::disconnect('app/dropbox');
147+
});
148+
149+
});
150+
```
151+
152+
Or using a middleware route, if the user does not have a graph token then automatically redirect to get authenticated:
153+
154+
```php
155+
Route::group(['middleware' => ['web', 'DropboxAuthenticated']], function(){
156+
Route::get('dropbox', function(){
157+
return Dropbox::post('users/get_current_account');
158+
});
159+
});
160+
161+
Route::get('dropbox/connect', function(){
162+
return Dropbox::connect();
163+
});
164+
165+
Route::get('dropbox/disconnect', function(){
166+
return Dropbox::disconnect('app/dropbox');
167+
});
168+
```
169+
170+
Once authenticated you can call Dropbox:: with the following verbs:
171+
172+
```php
173+
Dropbox::get($endpoint, $array = [], $headers = [], $useToken = true)
174+
Dropbox::post($endpoint, $array = [], $headers = [], $useToken = true)
175+
Dropbox::put($endpoint, $array = [], $headers = [], $useToken = true)
176+
Dropbox::patch($endpoint, $array = [], $headers = [], $useToken = true)
177+
Dropbox::delete($endpoint, $array = [], $headers = [], $useToken = true)
178+
```
179+
180+
The $array is not always required, its requirement is determined from the endpoint being called, see the API documentation for more details.
181+
182+
The $headers are optional when used can pass in additional headers.
183+
184+
The $useToken is optional when set to true will use the authorisation header, defaults to true.
185+
186+
These expect the API endpoints to be passed, the URL https://api.dropboxapi.com/2/ is provided, only endpoints after this should be used ie:
187+
188+
```php
189+
Dropbox::post('users/get_current_account')
190+
```
191+
192+
## Middleware
193+
To restrict access to routes only to authenticated users there is a middleware route called DropboxAuthenticated
194+
195+
Add DropboxAuthenticated to routes to ensure the user is authenticated:
196+
197+
```php
198+
Route::group(['middleware' => ['web', 'DropboxAuthenticated'], function()
199+
```
200+
201+
To access the token model reference this ORM model:
202+
203+
```php
204+
use Dcblogdev\Dropbox\Models\DropboxToken;
205+
```
206+
207+
## Files
208+
209+
This package provides a clean way of working with files.
210+
211+
To work with files first call ->files() followed by a method.
212+
213+
Import Namespace
214+
215+
```php
216+
use Dcblogdev\Dropbox\Facades\Dropbox;
217+
```
218+
219+
List Content
220+
221+
list files and folders of a given path
222+
223+
```php
224+
Dropbox::files()->listContents($path = '')
225+
```
226+
227+
List Content Continue
228+
229+
Using a cursor from the previous listContents call to paginate over the next set of folders/files.
230+
231+
```php
232+
Dropbox::files()->listContentsContinue($cursor = '')
233+
```
234+
235+
Delete folder/file
236+
Pass the path to the file/folder, When delting a folder all child items will be deleted.
237+
238+
```php
239+
Dropbox::files()->delete($path)
240+
```
241+
242+
Create Folder
243+
Pass the path to the folder to be created.
244+
245+
```php
246+
Dropbox::files()->createFolder($path)
247+
```
248+
249+
Search Files
250+
Each word will used to search for files.
251+
252+
```php
253+
Dropbox::files()->search($query)
254+
```
255+
256+
Upload File
257+
Upload files to Dropbox by passing the folder path followed by the filename. Note this method supports uploads up to 150MB only.
258+
259+
```php
260+
Dropbox::files()->upload($path, $file)
261+
```
262+
263+
Download File
264+
Download file from Dropbox by passing the folder path including the file.
265+
266+
```php
267+
Dropbox::files()->download($path)
268+
```
269+
270+
Move Folder/File
271+
Move accepts 4 params:
272+
273+
$fromPath - provide the path for the existing folder/file
274+
$toPath - provide the new path for the existing golder/file must start with a /
275+
$autoRename - If there's a conflict, have the Dropbox server try to autorename the file to avoid the conflict. The default for this field is false.
276+
$allowOwnershipTransfer - Allow moves by owner even if it would result in an ownership transfer for the content being moved. This does not apply to copies. The default for this field is false.
277+
278+
```php
279+
Dropbox::files()->move($fromPath, $toPath, $autoRename = false, $allowOwnershipTransfer = false);
280+
```
10281

11282
## Change log
12283

src/Resources/Files.php

+34-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function upload($path, $uploadPath, $mode = 'add')
108108
}
109109
}
110110

111-
public function download($path)
111+
public function download($path, $destFolder = '')
112112
{
113113
$path = $this->forceStartingSlash($path);
114114

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

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

131-
if (! is_dir($folder)) {
132-
mkdir($folder);
130+
if (empty($destFolder)){
131+
$destFolder = 'dropbox-temp';
132+
133+
if (! is_dir($destFolder)) {
134+
mkdir($destFolder);
135+
}
133136
}
134137

135-
file_put_contents($folder.$header['name'], $body);
138+
file_put_contents($destFolder.$header['name'], $body);
139+
140+
return response()->download($destFolder.$header['name'], $header['name'])->deleteFileAfterSend();
141+
142+
} catch (ClientException $e) {
143+
throw new Exception($e->getResponse()->getBody()->getContents());
144+
} catch (Exception $e) {
145+
throw new Exception($e->getMessage());
146+
}
147+
}
148+
149+
public function getContentsFile($path)
150+
{
151+
$path = $this->forceStartingSlash($path);
152+
153+
try {
154+
$client = new Client;
155+
156+
$response = $client->post("https://content.dropboxapi.com/2/files/download", [
157+
'headers' => [
158+
'Authorization' => 'Bearer ' . $this->getAccessToken(),
159+
'Dropbox-API-Arg' => json_encode([
160+
'path' => $path
161+
])
162+
]
163+
]);
136164

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

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

0 commit comments

Comments
 (0)