forked from ivanvermeyen/laravel-google-drive-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.php
75 lines (58 loc) · 2.27 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
<?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('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('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('create-dir', function() {
Storage::cloud()->makeDirectory('Test Dir');
return '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';
});