-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
web.php
304 lines (231 loc) · 9.89 KB
/
web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('put', function() {
Storage::cloud()->put('test.txt', 'Hello World');
return 'File was saved to Google Drive';
});
Route::get('put-existing', function() {
$filename = 'laravel.png';
$filePath = public_path($filename);
$fileData = File::get($filePath);
Storage::cloud()->put($filename, $fileData);
return 'File was saved to Google Drive';
});
Route::get('list', function() {
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
//return $contents->where('type', '=', 'dir'); // directories
return $contents->where('type', '=', 'file'); // files
});
Route::get('list-folder-contents', function() {
// The human readable folder name to get the contents of...
// For simplicity, this folder is assumed to exist in the root directory.
$folder = 'Test Dir';
// Get root directory contents...
$contents = collect(Storage::cloud()->listContents('/', false));
// Find the folder you are looking for...
$dir = $contents->where('type', '=', 'dir')
->where('filename', '=', $folder)
->first(); // There could be duplicate directory names!
if ( ! $dir) {
return 'No such folder!';
}
// Get the files inside the folder...
$files = collect(Storage::cloud()->listContents($dir['path'], false))
->where('type', '=', 'file');
return $files->mapWithKeys(function($file) {
$filename = $file['filename'].'.'.$file['extension'];
$path = $file['path'];
// Use the path to download each file via a generated link..
// Storage::cloud()->get($file['path']);
return [$filename => $path];
});
});
Route::get('get', function() {
$filename = 'test.txt';
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
//return $file; // array with file info
$rawData = Storage::cloud()->get($file['path']);
return response($rawData, 200)
->header('ContentType', $file['mimetype'])
->header('Content-Disposition', "attachment; filename='$filename'");
});
Route::get('put-get-stream', function() {
// Use a stream to upload and download larger files
// to avoid exceeding PHP's memory limit.
// Thanks to @Arman8852's comment:
// https://github.com/ivanvermeyen/laravel-google-drive-demo/issues/4#issuecomment-331625531
// And this excellent explanation from Freek Van der Herten:
// https://murze.be/2015/07/upload-large-files-to-s3-using-laravel-5/
// Assume this is a large file...
$filename = 'laravel.png';
$filePath = public_path($filename);
// Upload using a stream...
Storage::cloud()->put($filename, fopen($filePath, 'r+'));
// Get file listing...
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
// Get file details...
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
//return $file; // array with file info
// Store the file locally...
//$readStream = Storage::cloud()->getDriver()->readStream($file['path']);
//$targetFile = storage_path("downloaded-{$filename}");
//file_put_contents($targetFile, stream_get_contents($readStream), FILE_APPEND);
// Stream the file to the browser...
$readStream = Storage::cloud()->getDriver()->readStream($file['path']);
return response()->stream(function () use ($readStream) {
fpassthru($readStream);
}, 200, [
'Content-Type' => $file['mimetype'],
//'Content-disposition' => 'attachment; filename="'.$filename.'"', // force download?
]);
});
Route::get('create-dir', function() {
Storage::cloud()->makeDirectory('Test Dir');
return 'Directory was created in Google Drive';
});
Route::get('create-sub-dir', function() {
// Create parent dir
Storage::cloud()->makeDirectory('Test Dir');
// Find parent dir for reference
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$dir = $contents->where('type', '=', 'dir')
->where('filename', '=', 'Test Dir')
->first(); // There could be duplicate directory names!
if ( ! $dir) {
return 'Directory does not exist!';
}
// Create sub dir
Storage::cloud()->makeDirectory($dir['path'].'/Sub Dir');
return 'Sub Directory was created in Google Drive';
});
Route::get('put-in-dir', function() {
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$dir = $contents->where('type', '=', 'dir')
->where('filename', '=', 'Test Dir')
->first(); // There could be duplicate directory names!
if ( ! $dir) {
return 'Directory does not exist!';
}
Storage::cloud()->put($dir['path'].'/test.txt', 'Hello World');
return 'File was created in the sub directory in Google Drive';
});
Route::get('newest', function() {
$filename = 'test.txt';
Storage::cloud()->put($filename, \Carbon\Carbon::now()->toDateTimeString());
$dir = '/';
$recursive = false; // Get subdirectories also?
$file = collect(Storage::cloud()->listContents($dir, $recursive))
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->sortBy('timestamp')
->last();
return Storage::cloud()->get($file['path']);
});
Route::get('delete', function() {
$filename = 'test.txt';
// First we need to create a file to delete
Storage::cloud()->makeDirectory('Test Dir');
// Now find that file and use its ID (path) to delete it
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
Storage::cloud()->delete($file['path']);
return 'File was deleted from Google Drive';
});
Route::get('delete-dir', function() {
$directoryName = 'test';
// First we need to create a directory to delete
Storage::cloud()->makeDirectory($directoryName);
// Now find that directory and use its ID (path) to delete it
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$directory = $contents
->where('type', '=', 'dir')
->where('filename', '=', $directoryName)
->first(); // there can be duplicate file names!
Storage::cloud()->deleteDirectory($directory['path']);
return 'Directory was deleted from Google Drive';
});
Route::get('rename-dir', function() {
$directoryName = 'test';
// First we need to create a directory to rename
Storage::cloud()->makeDirectory($directoryName);
// Now find that directory and use its ID (path) to rename it
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$directory = $contents
->where('type', '=', 'dir')
->where('filename', '=', $directoryName)
->first(); // there can be duplicate file names!
Storage::cloud()->move($directory['path'], 'new-test');
return 'Directory was renamed in Google Drive';
});
Route::get('share', function() {
$filename = 'test.txt';
// Store a demo file
Storage::cloud()->put($filename, 'Hello World');
// Get the file to find the ID
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
// Change permissions
// - https://developers.google.com/drive/v3/web/about-permissions
// - https://developers.google.com/drive/v3/reference/permissions
$service = Storage::cloud()->getAdapter()->getService();
$permission = new \Google_Service_Drive_Permission();
$permission->setRole('reader');
$permission->setType('anyone');
$permission->setAllowFileDiscovery(false);
$permissions = $service->permissions->create($file['basename'], $permission);
return Storage::cloud()->url($file['path']);
});
Route::get('export/{basename}', function ($basename) {
$service = Storage::cloud()->getAdapter()->getService();
$mimeType = 'application/pdf';
$export = $service->files->export($basename, $mimeType);
return response($export->getBody(), 200, $export->getHeaders());
});