private async void GetSkyDriveSessionStatus() { try { IsSkyDriveButtonEnabled = false; _skyDriveSessionStatus = await SkyDriveHelper.GetSessionStatusAsync(); switch (_skyDriveSessionStatus) { case LiveConnectSessionStatus.Connected: SkyDriveStatusText = AppResources.AccountConnectedStatusText; break; case LiveConnectSessionStatus.NotConnected: SkyDriveStatusText = AppResources.AccountDisconnectedStatusText; break; case LiveConnectSessionStatus.Unknown: SkyDriveStatusText = AppResources.AccountUnknownStatusText; break; } } catch (LiveAuthException) { SkyDriveStatusText = AppResources.AccountErrorStatusText; } finally { IsSkyDriveButtonEnabled = true; } }
private async void SkyDriveLogin() { try { _skyDriveSessionStatus = await SkyDriveHelper.LoginAsync(); switch (_skyDriveSessionStatus) { case LiveConnectSessionStatus.Connected: SkyDriveStatusText = AppResources.AccountConnectedStatusText; break; case LiveConnectSessionStatus.NotConnected: SkyDriveStatusText = AppResources.AccountDisconnectedStatusText; break; case LiveConnectSessionStatus.Unknown: SkyDriveStatusText = AppResources.AccountUnknownStatusText; break; } } catch (LiveAuthException) { SkyDriveStatusText = AppResources.AccountErrorStatusText; } }
/// <summary> /// Make container/directory (depending on platform). /// </summary> /// <param name="container"></param> public void MakeContainer(string containerName) { var url = baseUrl + "/" + containerName; url = url.Replace(SkyDriveHelper.OneDrivePrefix, ""); var targetDirectory = SkyDriveHelper.CreateFolder(url); }
private async Task <string> UploadToSkyDriveAsync(bool getLink) { _cts = new CancellationTokenSource(); _cts.Token.Register(OnUploadCanceled); _sdProgress = new Progress <LiveOperationProgress>(OnSkyDriveUploadProgress); _cts.Token.ThrowIfCancellationRequested(); var status = await SkyDriveHelper.GetSessionStatusAsync(); if (status != LiveConnectSessionStatus.Connected) { throw new InvalidOperationException("Not connected."); } var fileId = await SkyDriveHelper.UploadFileAsync(_memo.AudioFile, GetUploadFileName(), _cts.Token, _sdProgress); if (getLink && !string.IsNullOrWhiteSpace(fileId)) { var link = await SkyDriveHelper.GetLinkAsync(fileId, _cts.Token); return(link); } return(null); }
static UrlType GetUrlType(string url) { UrlType urlType = UrlType.Local; if (AzureHelper.MatchHandler(url)) { urlType = UrlType.Azure; } else if (AzureHelper.MatchFileHandler(url)) { urlType = UrlType.AzureFile; } else if (S3Helper.MatchHandler(url)) { urlType = UrlType.S3; } else if (SkyDriveHelper.MatchHandler(url)) { urlType = UrlType.SkyDrive; } else if (SharepointHelper.MatchHandler(url)) { urlType = UrlType.Sharepoint; } else if (DropboxHelper.MatchHandler(url)) { urlType = UrlType.Dropbox; } else { urlType = UrlType.Local; // local filesystem. } return(urlType); }
/// <summary> /// Read blob. /// </summary> /// <param name="containerName"></param> /// <param name="blobName"></param> /// <param name="cacheFilePath"></param> /// <returns></returns> public Blob ReadBlob(string containerName, string blobName, string cacheFilePath = "") { Blob blob = new Blob(); StringBuilder requestUriFile; var url = baseUrl + "/" + containerName + "/" + blobName; if (url.IndexOf(SkyDriveHelper.OneDrivePrefix) != -1) { url = url.Replace(SkyDriveHelper.OneDrivePrefix, ""); var skydriveFileEntry = SkyDriveHelper.GetSkyDriveEntryByFileNameAndDirectory(url); requestUriFile = new StringBuilder(skydriveFileEntry.Source); var sp = url.Split('/'); blobName = sp[sp.Length - 1]; } else { // get blob name from url then remove it. // ugly ugly hack. var sp = url.Split('='); blobName = sp.Last(); var lastIndex = url.LastIndexOf("&"); requestUriFile = new StringBuilder(url.Substring(0, lastIndex)); // manipulated url to include blobName="real blob name". So parse end off url. // ugly hack... but needed a way for compatibility with other handlers. } requestUriFile.AppendFormat("?access_token={0}", accessToken); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUriFile.ToString()); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var s = response.GetResponseStream(); // get stream to store. using (var stream = CommonHelper.GetStream(cacheFilePath)) { byte[] data = new byte[32768]; int bytesRead = 0; do { bytesRead = s.Read(data, 0, data.Length); stream.Write(data, 0, bytesRead); }while (bytesRead > 0); if (!blob.BlobSavedToFile) { var ms = stream as MemoryStream; blob.Data = ms.ToArray(); } } blob.Name = blobName; blob.BlobOriginType = UrlType.SkyDrive; return(blob); }
private string GetSkyDriveDirectoryId(string directoryName) { var skydriveListing = SkyDriveHelper.ListSkyDriveRootDirectories(); var skydriveId = (from e in skydriveListing where e.Name == directoryName select e.Id).FirstOrDefault(); return(skydriveId); }
/// <summary> /// Write blob /// </summary> /// <param name="container"></param> /// <param name="blobName"></param> /// <param name="blob"></param> /// <param name="parallelUploadFactor"></param> /// <param name="chunkSizeInMB"></param> public void WriteBlob(string containerName, string blobName, Blob blob, int parallelUploadFactor = 1, int chunkSizeInMB = 4) { var url = containerName + "/" + blob.Name; url = url.Replace(SkyDriveHelper.OneDrivePrefix, ""); if (destinationDirectory == null) { // check if target folder exists. // if not, create it. destinationDirectory = SkyDriveHelper.GetSkyDriveDirectory(containerName); if (destinationDirectory == null) { destinationDirectory = SkyDriveHelper.CreateFolder(url); } } var urlTemplate = @"https://apis.live.net/v5.0/{0}/files/{1}?access_token={2}"; var requestUrl = string.Format(urlTemplate, destinationDirectory.Id, blobName, accessToken); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl); request.Method = "PUT"; Stream dataStream = request.GetRequestStream(); Stream inputStream = null; // get stream to data. if (blob.BlobSavedToFile) { inputStream = new FileStream(blob.FilePath, FileMode.Open); } else { inputStream = new MemoryStream(blob.Data); } int bytesRead; int readSize = 64000; int totalSize = 0; byte[] arr = new byte[readSize]; do { bytesRead = inputStream.Read(arr, 0, readSize); if (bytesRead > 0) { totalSize += bytesRead; dataStream.Write(arr, 0, bytesRead); } }while (bytesRead > 0); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close(); }
/// <summary> /// Lists all blobs in a container. /// Can be supplied a blobPrefix which basically acts as virtual directory options. /// eg, if we have blobs called: "virt1/virt2/myblob" and /// "virt1/virt2/myblob2" /// Although the blob names are the complete strings mentioned above, we might like to think that the blobs /// are just called myblob and myblob2. We can supply a blobPrefix of "virt1/virt2/" which we can *think* of /// as a directory, but again, its just really a prefix behind the scenes. /// /// For other sytems (not Azure) the blobPrefix might be real directories.... will need to investigate /// </summary> /// <param name="containerName"></param> /// <param name="blobPrefix"></param> /// <returns></returns> public IEnumerable <BasicBlobContainer> ListBlobsInContainer(string containerName = null, string blobPrefix = null, bool debug = false) { var skydriveListing = SkyDriveHelper.ListSkyDriveDirectoryContent(containerName); foreach (var skyDriveEntry in skydriveListing) { var blob = new BasicBlobContainer(); blob.Name = skyDriveEntry.Name; var resolvedOneDriveEntry = SkyDriveHelper.ListSkyDriveFileWithUrl(skyDriveEntry.Id); // keep display name same as name until determine otherwise. blob.DisplayName = blob.Name; blob.Container = containerName; blob.Url = string.Format("{0}&blobName={1}", resolvedOneDriveEntry.Source, blob.Name); // modify link so we can determine blob name purely from link. yield return(blob); } }
/// <summary> /// Makes a usable URL for a blob. This will need to handle security on the source blob. /// Each cloud provider is different. /// Cloud providers developed: /// Azure /// S3xx /// /// Cloud providers soon: /// Dropbox /// Onedrive /// </summary> /// <param name="origBlob"></param> /// <returns></returns> private static string GeneratedAccessibleUrl(BasicBlobContainer origBlob) { var sourceUrl = origBlob.Url; // +"/" + origBlob.Name; string url = ""; // if S3, then generate signed url. if (S3Helper.MatchHandler(sourceUrl)) { var bucket = S3Helper.GetBucketFromUrl(sourceUrl); var key = origBlob.Name; url = S3Helper.GeneratePreSignedUrl(bucket, key); } else if (AzureHelper.MatchHandler(sourceUrl)) { // generate Azure signed url. var client = AzureHelper.GetSourceCloudBlobClient(sourceUrl); var policy = new SharedAccessBlobPolicy(); policy.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-1); policy.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(ConfigHelper.SharedAccessSignatureDurationInSeconds / 60); policy.Permissions = SharedAccessBlobPermissions.Read; var blob = client.GetBlobReferenceFromServer(new Uri(sourceUrl)); url = sourceUrl + blob.GetSharedAccessSignature(policy); } else if (DropboxHelper.MatchHandler(sourceUrl)) { // need shorter url. (no base url); var uri = new Uri(sourceUrl); var shortUrl = uri.PathAndQuery; var client = DropboxHelper.GetClient(); var media = client.GetMedia(shortUrl); return(media.Url); } else if (SkyDriveHelper.MatchHandler(sourceUrl)) { throw new NotImplementedException("Blobcopy against onedrive is not implemented yet"); } Console.WriteLine("shared url is {0}", url); return(url); }
private void SkyDriveLogout() { SkyDriveHelper.Logout(); _skyDriveSessionStatus = LiveConnectSessionStatus.NotConnected; SkyDriveStatusText = AppResources.AccountDisconnectedStatusText; }
public SkyDriveHandler(string url) { accessToken = SkyDriveHelper.GetAccessToken(); baseUrl = url; }