forked from ivanvermeyen/laravel-google-drive-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.php
169 lines (133 loc) · 5.56 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
<?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-files', function() {
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents('/', $recursive));
//return $contents->where('type', 'dir'); // directories
return $contents->where('type', 'file')->mapWithKeys(function($file) {
return [$file->path() => pathinfo($file->path(),PATHINFO_BASENAME)];
});
});
Route::get('list-team-drives', function () {
$service = Storage::cloud()->getAdapter()->getService();
$teamDrives = collect($service->teamdrives->listTeamdrives()->getTeamDrives());
return $teamDrives->mapWithKeys(function($drive) {
return [$drive->id => $drive->name];
});
});
Route::get('get', function() {
// there can be duplicate file names!
$filename = 'test.txt';
$rawData = Storage::cloud()->get($filename); // raw content
$file = Storage::cloud()->getAdapter()->getMetadata($filename); // array with file info
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+'));
$file = Storage::cloud()->getAdapter()->getMetadata($filename); // array with file info
// Store the file locally...
//$readStream = Storage::cloud()->getDriver()->readStream($filename);
//$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($filename);
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/Sub Dir');
return 'Sub Directory was created in Google Drive';
});
Route::get('put-in-dir', function() {
Storage::cloud()->put('Test Dir/test.txt', 'Hello World');
return 'File was created in the sub directory in Google Drive';
});
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 directory contents...
$files = collect(Storage::cloud()->listContents($folder, false));
return $files->mapWithKeys(function($file) {
return [$file->path() => pathinfo($file->path(),PATHINFO_BASENAME)];
});
});
Route::get('delete', function() {
$path = 'Test Dir/test.txt';
Storage::cloud()->put($path, 'Hello World');
Storage::cloud()->delete($path);
return 'File was deleted from Google Drive';
});
Route::get('delete-dir', function() {
$directoryName = 'Test Dir';
// First we need to create a directory to delete
Storage::cloud()->makeDirectory($directoryName);
Storage::cloud()->deleteDirectory($directoryName);
return 'Directory was deleted from Google Drive';
});
Route::get('rename-dir', function() {
$directoryName = 'test';
Storage::cloud()->makeDirectory($directoryName);
Storage::cloud()->move($directoryName, 'new-test');
return 'Directory was renamed in Google Drive';
});
Route::get('share', function() {
$filename = 'test.txt';
// Store a demo file with public permission
Storage::cloud()->put($filename, 'Hello World', 'public');
return Storage::cloud()->url($filename);
});
Route::get('export/{filename}', function ($filename) {
$service = Storage::cloud()->getAdapter()->getService();
$file = Storage::cloud()->getAdapter()->getMetadata($filename);
$mimeType = 'application/pdf';
$export = $service->files->export($file->extraMetadata()['id'], $mimeType);
return response($export->getBody(), 200, [
'Content-Type' => $mimeType,
'Content-disposition' => 'attachment; filename='.$filename.'.pdf',
]);
});