Ejemplo n.º 1
0
        public async Task <Stream> GetOneDriveContentsAsStreamAsync(OneDriveAttribute attr)
        {
            Stream response;
            // How to download from OneDrive: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_downloadcontent
            // GET https://graph.microsoft.com/v1.0/me/drive/root:/test1/hi.txt:/content HTTP/1.1
            bool isShare = attr.Path.StartsWith("https://");

            if (isShare)
            {
                // TODO: Move this to GraphServiceClient

                // Download via a Share URL
                var shareToken = UrlToSharingToken(attr.Path);
                response = await _client.GetOneDriveContentStreamFromShareAsync(shareToken);
            }
            else
            {
                try
                {
                    // Retrieve stream of OneDrive item
                    response = await _client.GetOneDriveContentStreamAsync(attr.Path);
                } catch
                {
                    //File does not exist, so create new memory stream
                    response = new MemoryStream();
                }
            }

            return(ConvertStream(response, attr));
        }