예제 #1
0
        void ICloudFileDataTransfer.Serialize(System.Runtime.Serialization.IFormatter dataFormatter, object objectGraph)
        {
            using (var cache = new MemoryStream())
            {
                // serialize into the cache
                dataFormatter.Serialize(cache, objectGraph);

                // go to start
                cache.Position = 0;

                // transfer the cache
                _fsEntry.GetDataTransferAccessor().Transfer(cache, nTransferDirection.nUpload);
            }
        }
        public Core.File SaveFile(Core.File file, Stream fileStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException("fileStream");
            }
            ICloudFileSystemEntry entry = null;

            if (file.ID != null)
            {
                entry = SharpBoxProviderInfo.Storage.GetFile(MakePath(file.ID), null);
            }
            else if (file.FolderID != null)
            {
                var folder = GetFolderById(file.FolderID);

                file.Title = GetAvailableTitle(file.Title, folder, IsExist);

                entry = SharpBoxProviderInfo.Storage.CreateFile(folder, file.Title);
            }
            if (entry != null)
            {
                entry.GetDataTransferAccessor().Transfer(fileStream, nTransferDirection.nUpload);
                return(ToFile(entry));
            }
            return(null);
        }
        /// <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);
            }
        }
예제 #4
0
        public File <string> SaveFile(File <string> file, Stream fileStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException("fileStream");
            }
            ICloudFileSystemEntry entry = null;

            if (file.ID != null)
            {
                entry = ProviderInfo.Storage.GetFile(MakePath(file.ID), null);
            }
            else if (file.FolderID != null)
            {
                var folder = GetFolderById(file.FolderID);
                file.Title = GetAvailableTitle(file.Title, folder, IsExist);
                entry      = ProviderInfo.Storage.CreateFile(folder, file.Title);
            }

            if (entry == null)
            {
                return(null);
            }

            try
            {
                entry.GetDataTransferAccessor().Transfer(fileStream.GetBuffered(), nTransferDirection.nUpload);
            }
            catch (SharpBoxException e)
            {
                var webException = (WebException)e.InnerException;
                if (webException != null)
                {
                    var response = ((HttpWebResponse)webException.Response);
                    if (response != null)
                    {
                        if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
                        {
                            throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_Create);
                        }
                    }
                    throw;
                }
            }

            if (file.ID != null && !entry.Name.Equals(file.Title))
            {
                file.Title = GetAvailableTitle(file.Title, entry.Parent, IsExist);
                ProviderInfo.Storage.RenameFileSystemEntry(entry, file.Title);
            }

            return(ToFile(entry));
        }
        /// <summary>
        /// This method returns a data stream which allows to downloads the
        /// file data
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parent"></param>
        /// <param name="targetStream"></param>
        /// <returns></returns>
        public void DownloadFile(String name, ICloudDirectoryEntry parent, Stream targetStream)
        {
            // check parameters
            if (parent == null || name == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidParameters);
            }

            // get the file entry
            ICloudFileSystemEntry file = parent.GetChild(name);

            // download the data
            file.GetDataTransferAccessor().Transfer(targetStream, nTransferDirection.nDownload);
        }
        /// <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));
        }
예제 #7
0
        /// <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);
            }
        }
예제 #8
0
        public File SaveFile(File file, Stream fileStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException("fileStream");
            }
            ICloudFileSystemEntry entry = null;

            if (file.ID != null)
            {
                entry = SharpBoxProviderInfo.Storage.GetFile(MakePath(file.ID), null);
            }
            else if (file.FolderID != null)
            {
                var folder = GetFolderById(file.FolderID);
                try
                {
                    //Check existense
                    if (SharpBoxProviderInfo.Storage.GetFileSystemObject(file.Title, folder) != null)
                    {
                        throw new ArgumentException(string.Format(Web.Files.Resources.FilesCommonResource.Error_FileAlreadyExists, file.Title));
                    }
                }
                catch (ArgumentException)
                {
                    throw;
                }
                catch (Exception)
                {
                }

                entry = SharpBoxProviderInfo.Storage.CreateFile(folder, file.Title);
            }
            if (entry != null)
            {
                entry.GetDataTransferAccessor().Transfer(fileStream, nTransferDirection.nUpload);
                return(ToFile(entry));
            }
            return(null);
        }
        /// <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);
        }
예제 #10
0
        internal static String GetFileFromDropbox(ICloudDirectoryEntry directory, String fileName, out bool fileExists)
        {
            fileExists = false;
            if (!ExistsChildOnDropbox(directory, fileName))
            {
                return(String.Empty);
            }

            ICloudFileSystemEntry file = directory.GetChild(fileName);

            String content;

            using (Stream streamFile = file.GetDataTransferAccessor().GetDownloadStream())
            {
                using (StreamReader reader = new StreamReader(streamFile, Encoding.UTF8))
                {
                    content = reader.ReadToEnd();
                }
            }

            fileExists = true;
            return(content);
        }