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");
        }
Exemple #3
0
        private void Save(string filename, MemoryStream memoryStream)
        {
            var storage = new CloudStorage();
            storage.Open(_cloudStorageConfiguration, _cloudeStorageCredentials);

            var backupFolder = storage.GetFolder(ArchiveFolderPath);
            if (backupFolder == null) { throw new Exception("Cloud folder not found: " + ArchiveFolderPath); }

            var cloudFile = storage.CreateFile(backupFolder, filename);
            using (var cloudStream = cloudFile.GetContentStream(FileAccess.Write))
            {
                cloudStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Position);
            }

            if (storage.IsOpened) { storage.Close(); }
        }
        /// <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);
        }