forked from microsoftgraph/aspnet-snippets-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesController.cs
297 lines (252 loc) · 12.2 KB
/
FilesController.cs
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/
using Microsoft.Graph;
using Microsoft_Graph_ASPNET_Snippets.Helpers;
using Microsoft_Graph_ASPNET_Snippets.Models;
using Resources;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace Microsoft_Graph_ASPNET_Snippets.Controllers
{
[Authorize]
public class FilesController : Controller
{
FilesService filesService = new FilesService();
public ActionResult Index()
{
return View("Files");
}
// Get the drive items in the root directory of the current user's default drive.
public async Task<ActionResult> GetMyFilesAndFolders()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the files and folders in the current user's drive.
results.Items = await filesService.GetMyFilesAndFolders(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Get the items that are shared with the current user.
public async Task<ActionResult> GetSharedWithMe()
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the shared items.
results.Items = await filesService.GetSharedWithMe(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Get the current user's default drive.
public async Task<ActionResult> GetMyDrive()
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the current user's default drive.
results.Items = await filesService.GetMyDrive(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Create a text file in the current user's root directory.
public async Task<ActionResult> CreateFile()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Add the file.
results.Items = await filesService.CreateFile(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Create a folder in the current user's root directory.
public async Task<ActionResult> CreateFolder()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Add the folder.
results.Items = await filesService.CreateFolder(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Upload a BMP file to the current user's root directory.
public async Task<ActionResult> UploadLargeFile()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Add the file.
results.Items = await filesService.UploadLargeFile(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Get a file or folder (metadata) in the current user's drive.
public async Task<ActionResult> GetFileOrFolderMetadata(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the file or folder object.
results.Items = await filesService.GetFileOrFolderMetadata(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Download the content of an existing file.
// This snippet returns the length of the file stream and some file metadata properties.
public async Task<ActionResult> DownloadFile(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Download the file.
results.Items = await filesService.DownloadFile(graphClient, id);
// Handle selected item is not supported.
foreach (var item in results.Items)
{
if (item.Properties.ContainsKey(Resource.File_ChooseFile)) results.Selectable = false;
}
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Update the metadata of a file or folder.
// This snippet updates the item's name.
// To move an item, point the ParentReference.Id or ParentReference.Path property to the target destination.
public async Task<ActionResult> UpdateFileOrFolderMetadata(string id, string name)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Update the item.
results.Items = await filesService.UpdateFileOrFolderMetadata(graphClient, id, name);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Update the content of a file in the user's root directory.
// This snippet replaces the text content of a .txt file.
public async Task<ActionResult> UpdateFileContent(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the file. Make sure it's a .txt file (for the purposes of this snippet).
results.Items = await filesService.UpdateFileContent(graphClient, id);
// Handle selected item is not supported.
foreach (var item in results.Items)
{
if (item.Properties.ContainsKey(Resource.File_ChooseTextFile)) results.Selectable = false;
}
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Delete a file in the user's root directory.
public async Task<ActionResult> DeleteFileOrFolder(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Delete the item.
results.Items = await filesService.DeleteFileOrFolder(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
// Get a sharing link for a file in the current user's drive.
public async Task<ActionResult> GetSharingLink(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
results.Items = await filesService.GetSharingLink(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Files", results);
}
}
}