-
Notifications
You must be signed in to change notification settings - Fork 13
/
google_drive_search.dart
203 lines (177 loc) · 5.52 KB
/
google_drive_search.dart
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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_samples/show_dialog.dart';
import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:http/http.dart' as http;
class GoogleDriveSearch extends StatefulWidget {
@override
_GoogleDriveSearch createState() => _GoogleDriveSearch();
}
const _folderType = "application/vnd.google-apps.folder";
class _GoogleDriveSearch extends State<GoogleDriveSearch> {
final googleSignIn = GoogleSignIn.standard(scopes: [
drive.DriveApi.driveAppdataScope,
// drive.DriveApi.driveFileScope,
drive.DriveApi.driveReadonlyScope
]);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("Google Drive Search"),
),
body: _createBody(context),
),
);
}
Widget _createBody(BuildContext context) {
return Column(
children: [
Center(child: _createButton("All file list", _allFileList)),
Center(child: _createButton("Root list", _root)),
Center(child: _createButton("Text files", _txt)),
Center(child: _createButton("Files in temp1 folder", _filesInFolder)),
Center(
child: _createButton("Files owned by other", _filesOwnedByOther)),
Center(child: _createButton("Shared files", _sharedFiles)),
],
);
}
Future<drive.FileList> _allFileList(drive.DriveApi driveApi) {
return driveApi.files.list(
spaces: 'drive',
);
}
Future<drive.FileList> _root(drive.DriveApi driveApi) {
return driveApi.files.list(
spaces: 'drive',
q: "'root' in parents",
);
}
Future<drive.FileList> _txt(drive.DriveApi driveApi) {
return driveApi.files.list(
spaces: 'drive',
q: "name contains '.txt'",
);
}
Future<drive.FileList> _filesInFolder(drive.DriveApi driveApi) async {
final folderId = await _getFolderId(driveApi, "temp1");
return driveApi.files.list(
spaces: 'drive',
q: "'$folderId' in parents",
);
}
Future<drive.FileList> _filesOwnedByOther(drive.DriveApi driveApi) async {
return driveApi.files.list(
spaces: 'drive',
q: "not 'me' in owners",
);
}
Future<drive.FileList> _sharedFiles(drive.DriveApi driveApi) async {
final list = await driveApi.files.list(
spaces: 'drive',
q: "'me' in owners",
$fields: "files(name, mimeType, shared)",
);
list.files?.removeWhere((element) => element.shared == false);
return list;
}
Future<drive.DriveApi?> _getDriveApi() async {
final googleUser = await googleSignIn.signIn();
if (googleUser != null) {
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final UserCredential loginUser =
await FirebaseAuth.instance.signInWithCredential(credential);
assert(loginUser.user?.uid == FirebaseAuth.instance.currentUser?.uid);
}
final headers = await googleUser?.authHeaders;
if (headers == null) {
await showMessage(context, "Sign-in first", "Error");
return null;
}
final client = GoogleAuthClient(headers);
final driveApi = drive.DriveApi(client);
return driveApi;
}
Future<String?> _getFolderId(
drive.DriveApi driveApi,
String folderName,
) async {
try {
final found = await driveApi.files.list(
q: "mimeType = '$_folderType' and name = '$folderName'",
$fields: "files(id)",
);
final files = found.files;
if (files == null) {
return null;
}
if (files.isNotEmpty) {
return files.first.id;
}
} catch (e) {
print(e);
}
return null;
}
Widget _createButton(String title, Function(drive.DriveApi driveApi) query) {
return ElevatedButton(
onPressed: () async {
final driveApi = await _getDriveApi();
if (driveApi == null) {
return;
}
final fileList = await query(driveApi);
final files = fileList.files;
if (files == null) {
return showMessage(context, "Data not found", "");
}
await _showList(files);
},
child: Text(title),
);
}
Future<void> _showList(List<drive.File> files) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("List"),
content: Container(
width: MediaQuery.of(context).size.height - 50,
height: MediaQuery.of(context).size.height,
child: ListView.builder(
itemCount: files.length,
itemBuilder: (context, index) {
final isFolder = files[index].mimeType == _folderType;
return ListTile(
leading: Icon(isFolder
? Icons.folder
: Icons.insert_drive_file_outlined),
title: Text(files[index].name ?? ""),
);
},
),
),
);
},
);
}
}
class GoogleAuthClient extends http.BaseClient {
final Map<String, String> _headers;
final _client = new http.Client();
GoogleAuthClient(this._headers);
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers.addAll(_headers);
return _client.send(request);
}
}