protected void ExecuteUpload(IExecuteBoxNetUploaderWorkflowMessage message, GenericNetworkCredentials authenticationToken, FileInfo inputFilePath) { var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; var task = Task.Factory.StartNew(() => { }); task.ContinueWith((t) => { BoxNetUploaderService.Uploaders.Add(message, new CancellableTask { Task = task, CancellationTokenSource = cancellationTokenSource }); var configuration = GetBoxNetConfiguration(); var storage = new CloudStorage(); var accessToken = storage.Open(configuration, authenticationToken); var folder = string.IsNullOrEmpty(message.Settings.Folder) ? storage.GetRoot() : storage.GetFolder(message.Settings.Folder); if (folder == null) { throw new Exception(string.Format("Folder does not exist - {0}", message.Settings.Folder)); } else { var file = storage.CreateFile(folder, inputFilePath.Name); var uploader = file.GetDataTransferAccessor(); using (var inputFileStream = inputFilePath.OpenRead()) { uploader.Transfer(inputFileStream, nTransferDirection.nUpload, FileOperationProgressChanged, task); } } if (storage.IsOpened) { storage.Close(); } } , cancellationToken) .ContinueWith((t) => { var executedBoxNetUploaderWorkflowMessage = new ExecutedBoxNetUploaderWorkflowMessage() { CorrelationId = message.CorrelationId, Cancelled = t.IsCanceled, Error = t.Exception }; var bus = BusDriver.Instance.GetBus(BoxNetUploaderService.BusName); bus.Publish(executedBoxNetUploaderWorkflowMessage); BoxNetUploaderService.Uploaders.Remove(message); }); }
public static void UploadFile(string filename, string path) { if (_credentials == null) return; else if (string.IsNullOrEmpty(_remoteDirectory)) return; Console.WriteLine("Uploading file to dropbox"); // instanciate a cloud storage configuration, e.g. for dropbox DropBoxConfiguration configuration = DropBoxConfiguration.GetStandardConfiguration(); // instanciate the cloudstorage manager CloudStorage storage = new CloudStorage(); // open the connection to the storage if (!storage.Open(configuration, _credentials)) { Console.WriteLine("Connection failed"); return; } ICloudDirectoryEntry folder = _remoteDirectory.Equals("root", StringComparison.CurrentCultureIgnoreCase) ? storage.GetRoot() : GetDirectoryPath(storage); // create the file ICloudFileSystemEntry file = storage.CreateFile(folder, filename); // upload the data Stream data = file.GetContentStream(FileAccess.Write); // build a stream read var writer = new BinaryWriter(data); var filedata = File.ReadAllBytes(path); writer.Write(filedata); // close the streamreader writer.Close(); // close the stream data.Close(); // close the cloud storage connection if (storage.IsOpened) { storage.Close(); } Console.WriteLine("Upload Complete"); }
protected void Page_Load(object sender, EventArgs e) { if (Session.IsReadOnly) { SubmitError("No session is availible.", Source); return; } var config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as DropBoxConfiguration; var callbackUri = new UriBuilder(Request.GetUrlRewriter()); if (!string.IsNullOrEmpty(Request.QueryString[AuthorizationUrlKey]) && Session[RequestTokenSessionKey] != null) { //Authorization callback try { var accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(config, ImportConfiguration.DropboxAppKey, ImportConfiguration.DropboxAppSecret, Session[RequestTokenSessionKey] as DropBoxRequestToken); Session[RequestTokenSessionKey] = null; //Exchanged var storage = new CloudStorage(); var base64token = storage.SerializeSecurityTokenToBase64Ex(accessToken, config.GetType(), new Dictionary<string, string>()); storage.Open(config, accessToken); //Try open storage! var root = storage.GetRoot(); if (root == null) throw new Exception(); SubmitToken(base64token, Source); } catch { SubmitError("Failed to open storage with token", Source); } } else { callbackUri.Query += string.Format("&{0}=1", AuthorizationUrlKey); config.AuthorizationCallBack = callbackUri.Uri; // create a request token var requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, ImportConfiguration.DropboxAppKey, ImportConfiguration.DropboxAppSecret); var authorizationUrl = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(config, requestToken); Session[RequestTokenSessionKey] = requestToken; //Store token into session!!! Response.Redirect(authorizationUrl); } }
protected void Page_Load(object sender, EventArgs e) { if (Session.IsReadOnly) { SubmitError("No session is availible.", Source); return; } var redirectUri = Request.GetUrlRewriter().GetLeftPart(UriPartial.Path); if (!string.IsNullOrEmpty(Request[AuthorizationCodeUrlKey])) { //we ready to obtain and store token var authCode = Request[AuthorizationCodeUrlKey]; var accessToken = SkyDriveAuthorizationHelper.GetAccessToken(ImportConfiguration.SkyDriveAppKey, ImportConfiguration.SkyDriveAppSecret, redirectUri, authCode); //serialize token var config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.SkyDrive); var storage = new CloudStorage(); var base64AccessToken = storage.SerializeSecurityTokenToBase64Ex(accessToken, config.GetType(), new Dictionary<string, string>()); //check and submit storage.Open(config, accessToken); var root = storage.GetRoot(); if (root != null) { SubmitToken(base64AccessToken, Source); } else { SubmitError("Failed to open storage with token", Source); } } else { var authCodeUri = SkyDriveAuthorizationHelper.BuildAuthCodeUrl(ImportConfiguration.SkyDriveAppKey, null, redirectUri); Response.Redirect(authCodeUri); } }
private void AuthenticateBoxNetButton_Click(object sender, RoutedEventArgs e) { authenticateBoxNetButton.IsEnabled = false; authenticateBoxNetLabel.Content = ""; try { var config = GetBoxNetConfiguration(); var credentials = CheckAuthenticationToken(DataModel.Element.BoxNetUsername, DataModel.Element.BoxNetPassword); var storage = new CloudStorage(); var accessToken = storage.Open(config, credentials); var rootFolder = storage.GetRoot(); authenticateBoxNetLabel.Content = "Authentication Successful"; } catch (Exception exception) { authenticateBoxNetLabel.Content = "Authentication Failure"; } finally { authenticateBoxNetButton.IsEnabled = true; } }
public void Test() { Uri u = new Uri("https://webdav.yandex.ru"); ICloudStorageConfiguration config = new WebDavConfiguration(u); GenericNetworkCredentials cred = new GenericNetworkCredentials(); cred.UserName = "******"; cred.Password = "******"; CloudStorage storage = new CloudStorage(); ICloudStorageAccessToken storageToken = storage.Open(config, cred); // storage.GetCloudConfiguration(nSupportedCloudConfigurations.WebDav); // After successful login you may do the necessary Directory/File manipulations by the SharpBox API // Here is the most often and simplest one ICloudDirectoryEntry root = storage.GetRoot(); var f =storage.GetFolder("/"); //f.First(). //var c =storage.GetCloudConfiguration(nSupportedCloudConfigurations.WebDav); //storage. storage.Close(); }
public static void DeleteDropboxImage(DropboxInfo dropBoxInfo) { // Make sure we remove it from the history, if no error occured config.runtimeDropboxHistory.Remove(dropBoxInfo.ID); config.DropboxUploadHistory.Remove(dropBoxInfo.ID); // get the config of dropbox Dropbox.DropBoxConfiguration dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as Dropbox.DropBoxConfiguration; // instanciate the cloudstorage manager CloudStorage storage = new CloudStorage(); // open the connection to the storage storage.Open(dropBoxConfig, config.DropboxAccessToken); // get the root entry of the cloud storage ICloudDirectoryEntry root = storage.GetRoot(); // delete a file ICloudFileSystemEntry fileSystemEntry = storage.GetFileSystemObject(dropBoxInfo.ID, root); if (fileSystemEntry != null) { storage.DeleteFileSystemEntry(fileSystemEntry); } // close the cloud storage connection if (storage.IsOpened) { storage.Close(); } dropBoxInfo.Image = null; }
/// <summary> /// Do the actual upload to Dropbox /// For more details on the available parameters, see: http://sharpbox.codeplex.com/ /// </summary> /// <param name="imageData">byte[] with image data</param> /// <returns>DropboxResponse</returns> public static DropboxInfo UploadToDropbox(byte[] imageData, string title, string filename) { // get the config of dropbox Dropbox.DropBoxConfiguration dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as Dropbox.DropBoxConfiguration; // instanciate the cloudstorage manager CloudStorage storage = new CloudStorage(); // open the connection to the storage storage.Open(dropBoxConfig, config.DropboxAccessToken); // get the root entry of the cloud storage ICloudDirectoryEntry root = storage.GetRoot(); if (root == null) { Console.WriteLine("No root object found"); } else { // create the file ICloudFileSystemEntry file = storage.CreateFile(root, filename); // build the data stream Stream data = new MemoryStream(imageData); // reset stream data.Position = 0; // upload data file.GetDataTransferAccessor().Transfer(data, nTransferDirection.nUpload, null, null); } // close the cloud storage connection if (storage.IsOpened) { storage.Close(); } return RetrieveDropboxInfo(filename); }
public static DropboxInfo RetrieveDropboxInfo(string filename) { LOG.InfoFormat("Retrieving Dropbox info for {0}", filename); DropboxInfo dropBoxInfo = new DropboxInfo(); dropBoxInfo.ID = filename; dropBoxInfo.Title = filename; dropBoxInfo.Timestamp = DateTime.Now; dropBoxInfo.WebUrl = string.Empty; // get the config of dropbox Dropbox.DropBoxConfiguration dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as Dropbox.DropBoxConfiguration; // instanciate the cloudstorage manager CloudStorage storage = new CloudStorage(); // get the root entry of the cloud storage ICloudDirectoryEntry root = storage.GetRoot(); // open the connection to the storage storage.Open(dropBoxConfig, config.DropboxAccessToken); dropBoxInfo.WebUrl = storage.GetFileSystemObjectUrl(dropBoxInfo.ID, root).ToString(); ICloudFileSystemEntry fileSystemEntry = storage.GetFileSystemObject(dropBoxInfo.ID, root); if (fileSystemEntry != null) { dropBoxInfo.Title = fileSystemEntry.Name; dropBoxInfo.Timestamp = fileSystemEntry.Modified; } // close the cloud storage connection if (storage.IsOpened) { storage.Close(); } return dropBoxInfo; }
private static ICloudDirectoryEntry GetDirectoryPath(CloudStorage storage) { return storage.GetFolder(_remoteDirectory, storage.GetRoot()) ?? storage.CreateFolder(_remoteDirectory, storage.GetRoot()); }
public byte[] GetFile(IStorageFile file) { byte[] fileData = null; CloudStorage cloudStorage = null; try { cloudStorage = new CloudStorage(); cloudStorage.Open(config, credentials); ICloudDirectoryEntry cloudDirectoryEntry; if (file.ParentFolder == null) cloudDirectoryEntry = cloudStorage.GetRoot(); else cloudDirectoryEntry = cloudStorage.GetFolder(file.ParentFolder.Path); if (cloudDirectoryEntry != null && cloudDirectoryEntry.Count != 0) { var cloudFile = cloudDirectoryEntry.FirstOrDefault(f => f.Name == file.Name); if (cloudFile != null) { using (var ms = new MemoryStream()) { cloudStorage.DownloadFile(cloudFile.Name, cloudDirectoryEntry, ms); fileData = ms.GetBuffer(); } } } } finally { if (cloudStorage != null && cloudStorage.IsOpened) cloudStorage.Close(); } return fileData; }