This repository was archived by the owner on Dec 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFileController.cs
More file actions
148 lines (122 loc) · 4.68 KB
/
FileController.cs
File metadata and controls
148 lines (122 loc) · 4.68 KB
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
using O365_APIs_Start_ASPNET_MVC.Helpers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;
using model = O365_APIs_Start_ASPNET_MVC.Models;
using System.Linq;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OpenIdConnect;
using System.Web;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace O365_APIs_Start_ASPNET_MVC.Controllers
{
[Authorize]
[HandleError(ExceptionType = typeof(AdalException))]
public class FileController : Controller
{
private FileOperations _fileOperations = new FileOperations();
private static bool _O365ServiceOperationFailed = false;
//Returns the user's my files
//Implements Office 365-side paging
// GET: /File/
public async Task<ActionResult> Index()
{
ViewBag.O365ServiceOperationFailed = _O365ServiceOperationFailed;
if (_O365ServiceOperationFailed)
{
_O365ServiceOperationFailed = false;
}
List<model.FileObject> myFiles = new List<model.FileObject>();
try
{
myFiles = await _fileOperations.GetMyFilesAsync();
}
catch (AdalException e)
{
if (e.ErrorCode == AdalError.FailedToAcquireTokenSilently)
{
//This exception is thrown when either you have a stale access token, or you attempted to access a resource that you don't have permissions to access.
throw e;
}
}
return View(myFiles);
}
//
// GET: /Files/Create - This GET operation returns the Create view (Create.cshtml)
[HttpGet]
public ActionResult Create()
{
return View();
}
//
// POST: /Files/Create - This POST operation creates the text file and returns the default files list view (Index.cshtml)
[HttpPost]
public async Task<ActionResult> Create(FormCollection collection)
{
_O365ServiceOperationFailed = false;
String newEventID = "";
try
{
newEventID = await _fileOperations.CreateNewTextFileAsync();
}
catch (Exception)
{
_O365ServiceOperationFailed = true;
}
return RedirectToAction("Index", new { newid = newEventID });
}
//GET: /Files/Edit - This GET operation gets a handle on the selected file and returns the text file contents to the edit view (Edit.cshtml)
[HttpGet]
public async Task<ActionResult> Edit(string id)
{
var files = await _fileOperations.GetMyFilesAsync();
var fileToEdit = files.Where(f => f.ID == id).SingleOrDefault();
if (fileToEdit != null)
{
var results = await _fileOperations.ReadTextFileAsync(id);
fileToEdit.FileText = results[0].ToString();
}
return View(fileToEdit);
}
// POST: /Files/Edit - This POST operation gets a handle on the selected file and saves/updates the text file contents to the file in O365
[HttpPost]
[ValidateInput(false)]
public async Task<ActionResult> Edit(string id, string fileText)
{
_O365ServiceOperationFailed = false;
try
{
await _fileOperations.UpdateTextFileAsync(id, fileText);
}
catch (Exception)
{
_O365ServiceOperationFailed = true;
}
return RedirectToAction("Index", new {changedid = id });
}
// GET: /Files/Delete - This GET operation gets a handle on the selected file or folder marked for deletion and returns the delete view (Delete.cshtml)
[HttpGet]
public async Task<ActionResult> Delete(string id)
{
var files = await _fileOperations.GetMyFilesAsync();
var fileToDelete = files.Where(f => f.ID == id).SingleOrDefault();
return View(fileToDelete);
}
// POST: /Files/Delete - This POST operation gets a handle on the selected file or folder and deletes the it in O365
[HttpPost]
public async Task<ActionResult> Delete(string id, FormCollection collection)
{
_O365ServiceOperationFailed = false;
try
{
await _fileOperations.DeleteFileOrFolderAsync(id);
}
catch (Exception)
{
_O365ServiceOperationFailed = true;
}
return RedirectToAction("Index");
}
}
}