private async Task <MarkdownFileModel> GetFileHandlerModelV2Async(FileHandlerActivationParameters input, bool allowInteractiveLogin = true) { if (input == null) { return(MarkdownFileModel.GetErrorModel(new FileHandlerActivationParameters(), "No activation parameters were provided.")); } // Retrieve an access token so we can make API calls string accessToken = null; try { accessToken = await AuthHelper.GetUserAccessTokenSilentAsync(input.ResourceId, allowInteractiveLogin); } catch (Exception ex) { return(MarkdownFileModel.GetErrorModel(input, ex)); } // Get file content FileData results = null; try { UriBuilder downloadUrlBuilder = new UriBuilder(input.ItemUrls.First()); downloadUrlBuilder.Path += "/content"; results = await HttpHelper.Default.DownloadFileAsync(downloadUrlBuilder.ToString(), accessToken); } catch (Exception ex) { return(MarkdownFileModel.GetErrorModel(input, ex)); } return(MarkdownFileModel.GetWriteableModel(input, results.Filename, results.Content)); }
private async Task <MarkdownFileModel> GetFileHandlerModelV2Async(FileHandlerActivationParameters input, bool allowInteractiveLogin = true) { if (input == null) { return(MarkdownFileModel.GetErrorModel(new FileHandlerActivationParameters(), "No activation parameters were provided.")); } // Retrieve an access token so we can make API calls string accessToken = null; try { accessToken = await AuthHelper.GetUserAccessTokenSilentAsync(input.ResourceId, allowInteractiveLogin); } catch (Exception ex) { return(MarkdownFileModel.GetErrorModel(input, ex)); } // Check user's permissions HttpClient client = new HttpClient(); client.BaseAddress = new Uri(ConfigurationManager.AppSettings["sc:ApiServer"]); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Api-Key", ConfigurationManager.AppSettings["sc:ApiKey"]); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/v2/office365/isAllowed"); request.Content = new StringContent("{\"email\":\"" + input.UserId + "\"}", Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(request); if (response.StatusCode != HttpStatusCode.OK) { return(MarkdownFileModel.GetErrorModel(input, "Access Denied")); } // Get file content FileData results = null; string filepath = ""; try { UriBuilder downloadUrlBuilder = new UriBuilder(input.ItemUrls.First()); downloadUrlBuilder.Path += "/content"; results = await HttpHelper.Default.DownloadFileAsync(downloadUrlBuilder.ToString(), accessToken); // download file filepath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".docx"); Stream stream = await HttpHelper.Default.GetStreamContentForUrlAsync(downloadUrlBuilder.ToString(), accessToken); FileStream file = System.IO.File.Create(filepath); stream.CopyTo(file); file.Close(); stream.Close(); } catch (Exception ex) { return(MarkdownFileModel.GetErrorModel(input, ex)); } return(MarkdownFileModel.GetWriteableModel(input, results.Filename, results.Content, filepath)); }