Example #1
0
        public async Task <string> UploadSheet(HttpContextBase httpContextBase, string path, string saveName, bool examSheet = false)
        {
            // This whole process is returning a strange exception that doesn't seem to actually be doing anything.
            // File is uploaded properly without corruption and application continues normally.
            // Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

            string token = await GetAccessToken(httpContextBase);

            if (string.IsNullOrEmpty(token))
            {
                return(null);
            }

            GraphServiceClient client = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    (requestMessage) =>
            {
                requestMessage.Headers.Authorization =
                    new AuthenticationHeaderValue("Bearer", token);

                return(Task.FromResult(0));
            }));

            byte[] data = System.IO.File.ReadAllBytes(path);
            // Writeable stream from byte array for drive upload
            Stream stream = new MemoryStream(data);

            DBManager db = new DBManager();

            string dir = "";

            if (examSheet)
            {
                dir = db.GetUserExamDirectory(user);
            }
            else
            {
                dir = db.GetUserUploadDirectory(user);
            }

            Microsoft.Graph.DriveItem file = client.Me.Drive.Root.ItemWithPath(dir + saveName).Content.Request().PutAsync <DriveItem>(stream).Result;
            return(file.WebUrl);
        }
Example #2
0
        public async Task <IDriveItemChildrenCollectionPage> GetDriveItems(HttpContextBase httpContextBase)
        {
            // This whole process is returning a strange exception that doesn't seem to actually be doing anything.
            // File is uploaded properly without corruption and application continues normally.
            // Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

            string token = await GetAccessToken(httpContextBase);

            if (string.IsNullOrEmpty(token))
            {
                return(null);
            }

            GraphServiceClient client = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    (requestMessage) =>
            {
                requestMessage.Headers.Authorization =
                    new AuthenticationHeaderValue("Bearer", token);

                return(Task.FromResult(0));
            }));

            DBManager db  = new DBManager();
            string    dir = db.GetUserUploadDirectory(user);

            //Get all items in drive
            IDriveItemChildrenCollectionPage items = null;

            try
            {
                items = await client.Me.Drive.Root.ItemWithPath(dir).Children.Request().GetAsync();
            }
            catch (Exception e) { }
            return(items);
        }