public async Task<bool> downloadFileAsync(CommonDescriptor cd) { OneDriveCommunicationParser odcp = new OneDriveCommunicationParser(); WindowsDownloadManager wdm = new WindowsDownloadManager(); var _oneDriveClient = InitializeAPI.oneDriveClient; //_oneDriveClient.AuthenticateAsync(); var fileId = cd.FileID; string extension = odcp.getExtension(cd.FileType); try { var contentStream = await _oneDriveClient.Drive.Items[fileId].Content.Request().GetAsync(); wdm.downloadFile((MemoryStream)contentStream, cd.FileName + extension); } catch (Exception e) { Console.WriteLine(e); return false; } return true; }
public async Task <bool> downloadFileAsync(CommonDescriptor cd) { var _googleDriveService = InitializeAPI.googleDriveService; //TODO: THE MIMETYPE THIS IS CAN'T BE THE SAME MIMETYPE AS WHAT IT WAS SAVED. It needs to be an export type. //https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents string extension, mimeType = ""; //figure out the mimetype by using the extension in the file name. GoogleDriveCommunicationParser gdcp = new GoogleDriveCommunicationParser(); if (cd.FileName.IndexOf('.') != -1) { //has an extension. extension = cd.FileName.Substring(cd.FileName.IndexOf('.')); //gets the extension. } else { //get a 'default' extension based on the format, convert to a real extension extension = cd.FileType; extension = gdcp.convertExtension(extension); if (extension == null) { //still can't find a good extension //not able to be downloaded, cancel download //TODO: ban the user from pressing download on these kinds of files? return(false); } } mimeType = gdcp.getMimeType(extension); if (mimeType == null) { //user cancelled giving us a new extension, cancel the download return(false); } var request = _googleDriveService.Files.Get(cd.FileID); var stream = new MemoryStream(); WindowsDownloadManager wdm = new WindowsDownloadManager(); request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); break; } case DownloadStatus.Completed: { Console.WriteLine("Download complete."); break; } case DownloadStatus.Failed: { Console.WriteLine("Download failed."); break; } } }; try { IDownloadProgress x = await request.DownloadAsync(stream); wdm.downloadFile(stream, cd.FileName); } catch (Exception e) { Console.WriteLine(e); return(false); } return(true); }