/// <summary>
        /// This function allows to download a specific file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetPath"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public void DownloadFile(String filePath, String targetPath, FileOperationProgressChanged delProgress)
        {
            // check parameter
            if (filePath == null || targetPath == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // get path and filename
            var ph   = new PathHelper(filePath);
            var dir  = ph.GetDirectoryName();
            var file = ph.GetFileName();

            // check if we are in root
            if (dir.Length == 0)
            {
                dir = "/";
            }

            // get parent container
            var container = GetFolder(dir);

            // download file
            DownloadFile(container, file, targetPath, delProgress);
        }
Exemplo n.º 2
0
 public void Transfer(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
 {
     // call the transfer interface in the service provider
     if (direction == nTransferDirection.nUpload)
     {
         if (_session.ServiceConfiguration.Limits.MaxUploadFileSize != -1 && targetDataStream.Length > _session.ServiceConfiguration.Limits.MaxUploadFileSize)
         {
             throw new Exceptions.SharpBoxException(Exceptions.SharpBoxErrorCodes.ErrorLimitExceeded);
         }
         else
         {
             _service.UploadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
         }
     }
     else
     {
         if (_session.ServiceConfiguration.Limits.MaxDownloadFileSize != -1 && _fsEntry.Length > _session.ServiceConfiguration.Limits.MaxDownloadFileSize)
         {
             throw new Exceptions.SharpBoxException(Exceptions.SharpBoxErrorCodes.ErrorLimitExceeded);
         }
         else
         {
             _service.DownloadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
         }
     }
 }
        public void TransferAsyncProgress(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
        {
            var ctx = new BaseFileEntryDataTransferAsyncContext
                          {
                              ProgressCallback = progressCallback,
                              ProgressContext = progressContext
                          };

            Transfer(targetDataStream, direction, FileOperationProgressChangedAsyncHandler, ctx);
        }
        public void Transfer(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
        {
            if (direction == nTransferDirection.nUpload)
            {
                if (!CanTransfer(direction, targetDataStream.Length))
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorLimitExceeded);

                _service.UploadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
            }
            else
            {
                if (!CanTransfer(direction, _fsEntry.Length))
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorLimitExceeded);
                
                _service.DownloadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
            }
        }
 public void Transfer(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
 {
     // call the transfer interface in the service provider
     if (direction == nTransferDirection.nUpload)
     {
         if (_session.ServiceConfiguration.Limits.MaxUploadFileSize != -1 && targetDataStream.Length > _session.ServiceConfiguration.Limits.MaxUploadFileSize)
             throw new Exceptions.SharpBoxException(Exceptions.SharpBoxErrorCodes.ErrorLimitExceeded);
         else
             _service.UploadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
     }
     else
     {
         if (_session.ServiceConfiguration.Limits.MaxDownloadFileSize != -1 && _fsEntry.Length > _session.ServiceConfiguration.Limits.MaxDownloadFileSize)
             throw new Exceptions.SharpBoxException(Exceptions.SharpBoxErrorCodes.ErrorLimitExceeded);
         else
             _service.DownloadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
     }
 }
        public void Transfer(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
        {
            if (direction == nTransferDirection.nUpload)
            {
                if (!CanTransfer(direction, targetDataStream.Length))
                {
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorLimitExceeded);
                }

                _service.UploadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
            }
            else
            {
                if (!CanTransfer(direction, _fsEntry.Length))
                {
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorLimitExceeded);
                }

                _service.DownloadResourceContent(_session, _fsEntry, targetDataStream, progressCallback, progressContext);
            }
        }
        /// <summary>
        /// This method allows to upload the data from a given filestream into a target file
        /// </summary>
        /// <param name="uploadDataStream"></param>
        /// <param name="targetFileName"></param>
        /// <param name="targetContainer"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public ICloudFileSystemEntry UploadFile(Stream uploadDataStream, String targetFileName, ICloudDirectoryEntry targetContainer, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (String.IsNullOrEmpty(targetFileName) || uploadDataStream == null || targetContainer == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // create the upload file
            var newFile = CreateFile(targetContainer, targetFileName);

            if (newFile == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
            }

            // upload data
            newFile.GetDataTransferAccessor().Transfer(uploadDataStream, nTransferDirection.nUpload, delProgress, null);

            // go ahead
            return(newFile);
        }
        /// <summary>
        /// This function allows to upload a local file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetDirectory"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public ICloudFileSystemEntry UploadFile(string filePath, string targetDirectory, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (String.IsNullOrEmpty(filePath))
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            return(UploadFile(filePath, targetDirectory, Path.GetFileName(filePath), delProgress));
        }
        /// <summary>
        /// This function allows to upload a local file. Remote file will be created with the name specifed by
        /// the targetFileName argument
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetDirectory"></param>
        /// <param name="targetFileName"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public ICloudFileSystemEntry UploadFile(string filePath, string targetDirectory, string targetFileName, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (String.IsNullOrEmpty(filePath) || String.IsNullOrEmpty(targetFileName) || targetDirectory == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // get target container
            var target = GetFolder(targetDirectory);

            if (target == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
            }

            // upload file
            using (Stream s = File.OpenRead(filePath))
            {
                return(UploadFile(s, targetFileName, target, delProgress));
            }
        }
        /// <summary>
        /// This function allows to download a specific file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetPath"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>        
        public void DownloadFile(String filePath, String targetPath, FileOperationProgressChanged delProgress)
        {
            // check parameter
            if (filePath == null || targetPath == null)
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);

            // get path and filename
            var ph = new PathHelper(filePath);
            String dir = ph.GetDirectoryName();
            String file = ph.GetFileName();

            // check if we are in root
            if (dir.Length == 0)
                dir = "/";

            // get parent container
            ICloudDirectoryEntry container = GetFolder(dir);

            // download file
            DownloadFile(container, file, targetPath, delProgress);
        }
        /// <summary>
        /// This function allowes to upload a local file. Remote file will be created with the name specifed by
        /// the targetFileName argument
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="targetContainer"></param>
        /// <param name="targetFileName"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public ICloudFileSystemEntry UploadFile(String filePath, ICloudDirectoryEntry targetContainer, string targetFileName, FileOperationProgressChanged delProgress)
        {
            // check parameter
            if (String.IsNullOrEmpty(filePath) || String.IsNullOrEmpty(targetFileName) || targetContainer == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // check if the target is a real file
            if (!File.Exists(filePath))
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
            }

            // build the source stream
            using (var srcStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // create the upload file
                var newFile = CreateFile(targetContainer, targetFileName);
                if (newFile == null)
                {
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);
                }

                // upload the data
                newFile.GetDataTransferAccessor().Transfer(srcStream, nTransferDirection.nUpload, delProgress, null);

                // go ahead
                return(newFile);
            }
        }
		/// <summary>
		/// This function allowes to upload a local file. Remote file will be created with the name specifed by
		/// the targetFileName argument
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="targetContainer"></param>
		/// <param name="targetFileName"></param>
        /// <param name="delProgress"></param>
		/// <returns></returns>
        public ICloudFileSystemEntry UploadFile(String filePath, ICloudDirectoryEntry targetContainer, string targetFileName, FileOperationProgressChanged delProgress)
		{
			// check parameter
			if (String.IsNullOrEmpty(filePath) || String.IsNullOrEmpty(targetFileName) || targetContainer == null)
				throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);

			// check if the target is a real file
			if (!File.Exists(filePath))
				throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);

			// build the source stream
			using (var srcStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				// create the upload file
				ICloudFileSystemEntry newFile = CreateFile(targetContainer, targetFileName);
				if (newFile == null)
					throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);

                // upload the data
                newFile.GetDataTransferAccessor().Transfer(srcStream, nTransferDirection.nUpload, delProgress, null);

				// go ahead
				return newFile;
			}
		}
        /// <summary>
        /// This functions allows to download a specific file
        ///
        /// Valid Exceptions are:
        ///  SharpBoxException(nSharpBoxErrorCodes.ErrorFileNotFound);
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="targetPath"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public void DownloadFile(ICloudDirectoryEntry parent, String name, String targetPath, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (parent == null || name == null || targetPath == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // expand environment in target path
            targetPath = Environment.ExpandEnvironmentVariables(targetPath);

            // get the file entry
            var file = parent.GetChild(name, true);

            using (var targetData = new FileStream(Path.Combine(targetPath, file.Name), FileMode.Create, FileAccess.Write, FileShare.None))
            {
                file.GetDataTransferAccessor().Transfer(targetData, nTransferDirection.nDownload, delProgress, null);
            }
        }
Exemplo n.º 14
0
        public void TransferAsyncProgress(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, object progressContext)
        {
            var ctx = new BaseFileEntryDataTransferAsyncContext
            {
                ProgressCallback = progressCallback,
                ProgressContext  = progressContext
            };

            Transfer(targetDataStream, direction, FileOperationProgressChangedAsyncHandler, ctx);
        }
		/// <summary>
		/// This function allows to upload a local file
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="targetDirectory"></param>
        /// <param name="delProgress"></param>
		/// <returns></returns>
        public ICloudFileSystemEntry UploadFile(string filePath, string targetDirectory, FileOperationProgressChanged delProgress)
		{
			// check parameters
			if (String.IsNullOrEmpty(filePath))
				throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);

            return UploadFile(filePath, targetDirectory, Path.GetFileName(filePath), delProgress);
		}
Exemplo n.º 16
0
 public void UploadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, object progressContext)
 {
     _service.UploadResourceContent(session, fileSystemEntry, targetDataStream, progressCallback, progressContext);
 }
        /// <summary>
        /// This functions allows to download a specific file
        ///         
        /// Valid Exceptions are:        
        ///  SharpBoxException(nSharpBoxErrorCodes.ErrorFileNotFound);
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="targetPath"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public void DownloadFile(ICloudDirectoryEntry parent, String name, String targetPath, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (parent == null || name == null || targetPath == null)
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);


#if !WINDOWS_PHONE && !ANDROID
            // expand environment in target path
            targetPath = Environment.ExpandEnvironmentVariables(targetPath);
#endif
            // get the file entry
			ICloudFileSystemEntry file = parent.GetChild(name, true);
            using (var targetData = new FileStream(Path.Combine(targetPath, file.Name), FileMode.Create, FileAccess.Write, FileShare.None))
            {
                file.GetDataTransferAccessor().Transfer(targetData, nTransferDirection.nDownload, delProgress, null);	
            }
        }
 public void UploadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, object progressContext)
 {
     _service.UploadResourceContent(session, fileSystemEntry, targetDataStream, progressCallback, progressContext);
 }
		/// <summary>
		/// This function allows to upload a local file. Remote file will be created with the name specifed by
		/// the targetFileName argument
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="targetDirectory"></param>
		/// <param name="targetFileName"></param>
        /// <param name="delProgress"></param>
		/// <returns></returns>
        public ICloudFileSystemEntry UploadFile(string filePath, string targetDirectory, string targetFileName, FileOperationProgressChanged delProgress)
		{
			// check parameters
			if (String.IsNullOrEmpty(filePath) || String.IsNullOrEmpty(targetFileName) || targetDirectory == null)
				throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);

			// get target container
			ICloudDirectoryEntry target = GetFolder(targetDirectory);
			if (target == null)
				throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);

            // upload file
            using (Stream s = File.OpenRead(filePath))
            {
                return UploadFile(s, targetFileName, target, delProgress);
            }
		}
Exemplo n.º 20
0
        /// <summary>
        /// This methid download the content of a file resource into a target download stream
        /// </summary>
        /// <param name="session"></param>
        /// <param name="fileSystemEntry"></param>
        /// <param name="targetDataStream"></param>
        /// <param name="progressCallback"></param>
        /// <param name="progressContext"></param>
        public virtual void DownloadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, object progressContext)
        {
            // build the download stream
            using (var data = CreateDownloadStream(session, fileSystemEntry))
            {
                // copy the data
                var res = StreamHelper.CopyStreamData(this, data, targetDataStream, CloudStorage.FileStreamCopyCallback, progressCallback, fileSystemEntry, progressContext);
                if (res.ResultCode == StreamHelperResultCodes.Aborted)
                {
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorTransferAbortedManually);
                }

                // commit everything
                CommitStreamOperation(session, fileSystemEntry, nTransferDirection.nDownload, data);
            }
        }
        /// <summary>
        /// This method uploads data into a file resource 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="fileSystemEntry"></param>
        /// <param name="targetDataStream"></param>
        /// <param name="progressCallback"></param>
        /// <param name="progressContext"></param>
        public virtual void UploadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, object progressContext)
        {
            // build the stream stream
            using (Stream data = CreateUploadStream(session, fileSystemEntry, targetDataStream.Length))
            {
                // copy the data                
                StreamHelperResult res = StreamHelper.CopyStreamData(this, targetDataStream, data, CloudStorage.FileStreamCopyCallback, progressCallback, fileSystemEntry, progressContext);
                if (res.ResultCode == StreamHelperResultCodes.Aborted)
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorTransferAbortedManually);

                // flush the upload stream to clean the caches
                data.Flush();

                // commit everything
                CommitStreamOperation(session, fileSystemEntry, nTransferDirection.nUpload, data);
            }
        }
        /// <summary>
        /// This method allows to upload the data from a given filestream into a target file
        /// </summary>
        /// <param name="uploadDataStream"></param>
        /// <param name="targetFileName"></param>
        /// <param name="targetContainer"></param>
        /// <param name="delProgress"></param>
        /// <returns></returns>
        public ICloudFileSystemEntry UploadFile(Stream uploadDataStream, String targetFileName, ICloudDirectoryEntry targetContainer, FileOperationProgressChanged delProgress)
        {
            // check parameters
            if (String.IsNullOrEmpty(targetFileName) || uploadDataStream == null || targetContainer == null)
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);

            // create the upload file
            ICloudFileSystemEntry newFile = CreateFile(targetContainer, targetFileName);
            if (newFile == null)
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorFileNotFound);

            // upload data
            newFile.GetDataTransferAccessor().Transfer(uploadDataStream, nTransferDirection.nUpload, delProgress, null);
            
            // go ahead
            return newFile;            
        }
Exemplo n.º 23
0
        /// <summary>
        /// This method uploads data into a file resource
        /// </summary>
        /// <param name="session"></param>
        /// <param name="fileSystemEntry"></param>
        /// <param name="targetDataStream"></param>
        /// <param name="progressCallback"></param>
        /// <param name="progressContext"></param>
        public virtual void UploadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, object progressContext)
        {
            // build the stream stream
            using (Stream data = CreateUploadStream(session, fileSystemEntry, targetDataStream.Length))
            {
                // copy the data
                StreamHelperResult res = StreamHelper.CopyStreamData(this, targetDataStream, data, CloudStorage.FileStreamCopyCallback, progressCallback, fileSystemEntry, progressContext);
                if (res.ResultCode == StreamHelperResultCodes.Aborted)
                {
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorTransferAbortedManually);
                }

                // flush the upload stream to clean the caches
                data.Flush();

                // commit everything
                CommitStreamOperation(session, fileSystemEntry, nTransferDirection.nUpload, data);
            }
        }
        /// <summary>
        /// Just a helper for the stream copy process
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pe"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        internal static StreamHelperResultCodes FileStreamCopyCallback(object sender, StreamHelperProgressEvent pe, params Object[] data)
        {
            // check array
            if (data.Length != 3)
            {
                return(StreamHelperResultCodes.OK);
            }

            // get the progess delegate
            FileOperationProgressChanged pc = data[0] as FileOperationProgressChanged;

            if (pc == null)
            {
                return(StreamHelperResultCodes.OK);
            }

            // get the file
            ICloudFileSystemEntry e = data[1] as ICloudFileSystemEntry;

            if (e == null)
            {
                return(StreamHelperResultCodes.OK);
            }

            // get the progress context
            Object progressContext = data[2];

            // create the eventargs element
            FileDataTransferEventArgs arg = new FileDataTransferEventArgs();

            arg.FileSystemEntry = e;
            arg.CurrentBytes    = pe.ReadBytesTotal;
            arg.CustomnContext  = progressContext;

            if (pe.TotalLength == -1)
            {
                arg.TotalBytes = e.Length;
            }
            else
            {
                arg.TotalBytes = pe.TotalLength;
            }

            arg.PercentageProgress  = pe.PercentageProgress;
            arg.TransferRateTotal   = pe.TransferRateTotal;
            arg.TransferRateCurrent = pe.TransferRateCurrent;

            // calc transfertime
            if (pe.TransferRateTotal != -1 && pe.TransferRateTotal > 0)
            {
                long bytesPerSecond = (arg.TransferRateTotal / 8) * 1000;

                if (bytesPerSecond > 0)
                {
                    long neededSeconds = (arg.TotalBytes - arg.CurrentBytes) / bytesPerSecond;
                    arg.OpenTransferTime = new TimeSpan(neededSeconds * TimeSpan.TicksPerSecond);
                }
                else
                {
                    arg.OpenTransferTime = new TimeSpan(long.MaxValue);
                }
            }
            else
            {
                arg.OpenTransferTime = new TimeSpan(long.MaxValue);
            }

            // call it
            pc(sender, arg);

            // create the ret value
            if (arg.Cancel)
            {
                return(StreamHelperResultCodes.Aborted);
            }
            else
            {
                return(StreamHelperResultCodes.OK);
            }
        }