예제 #1
0
        public async void Upload(UploadMethod uploadMethod, int megabytes, bool useAsync, bool useNonAsciiFilename)
        {
            var shareFileClient = GetShareFileClient();
            var rootFolder = shareFileClient.Items.Get().Execute();
            var testFolder = new Folder { Name = RandomString(30) + ".txt" };

            testFolder = shareFileClient.Items.CreateFolder(rootFolder.url, testFolder).Execute();
            var file = GetFileToUpload(1024 * 1024 * megabytes, useNonAsciiFilename);
            var uploadSpec = new UploadSpecificationRequest(file.Name, file.Length, testFolder.url, uploadMethod);

            UploaderBase uploader;

            if (useAsync)
            {
                uploader = shareFileClient.GetAsyncFileUploader(uploadSpec, file);
            }
            else
            {
                uploader = shareFileClient.GetFileUploader(uploadSpec, file);
            }

            var progressInvocations = 0;
            var bytesTransferred = 0L;
            uploader.OnTransferProgress += (sender, args) =>
            {
                bytesTransferred = args.Progress.BytesTransferred;
                progressInvocations++;
            };

            UploadResponse uploadResponse;

            if (useAsync)
            {
                uploadResponse = await ((AsyncUploaderBase)uploader).UploadAsync();
            }
            else
            {
                uploadResponse = ((SyncUploaderBase)uploader).Upload();
            }

            shareFileClient.Items.Delete(testFolder.url);

            uploadResponse.FirstOrDefault().Should().NotBeNull();
            var expectedInvocations = Math.Ceiling((double)file.Length / UploaderBase.DefaultBufferLength) + 1;

            bytesTransferred.Should().Be(1024 * 1024 * megabytes);

            if (uploadMethod == UploadMethod.Standard)
            {
                progressInvocations.Should().Be((int)expectedInvocations, "Standard should be predictable for number of progress callbacks");
            }
            else if (uploadMethod == UploadMethod.Threaded)
            {
                progressInvocations.Should()
                    .BeLessOrEqualTo(
                        (int)expectedInvocations,
                        "Threaded scales, therefore byte ranges vary and are less predictable.  We should see no more expectedInvoations");
            }
        }
예제 #2
0
        private Task <models.Folder> CreateFolder(RemoteFolder folder, Uri parent)
        {
            var sfFolder = new models.Folder
            {
                Name = folder.Name,
            };

            // set permissions here or after all child uploads are done?
            // probably here so platform doesn't have to propagate down?
            return(RetryAsync(() => api.Items.CreateFolder(parent, sfFolder).ExecuteAsync(), FOLDER_RETRY_COUNT));
        }
        public void GetObjectUri_WithNullStream()
        {
            // Arrange
            var folderId = GetId();
            var folder = new Folder
            {
                Id = folderId,
                url = new Uri(BaseUriString + "Items(" + folderId + ")")
            };

            // Act
            var streamObjectUri = folder.GetObjectUri(true);

            // Assert
            streamObjectUri.ToString().Should().Be(BaseUriString + "Items(" + folderId + ")");
        }
예제 #4
0
        /// <summary>
        /// Upload contents recursively
        /// </summary>
        private void UploadRecursive(ShareFileClient client, int uploadId, FileSystemInfo source, Models.Item target, ActionType actionType)
        {
            if (source is DirectoryInfo)
            {
                var newFolder = new Models.Folder() { Name = source.Name };
                bool isExist = false;

                if (Synchronize)
                {
                    try
                    {
                        string path = String.Format("/{0}", source.Name);
                        Item item = null;
                        try
                        {
                            item = client.Items.ByPath(target.url, path).Execute();
                        }
                        catch (ODataException e)
                        {
                            if (e.Code != System.Net.HttpStatusCode.NotFound)
                            {
                                throw e;
                            }
                        }

                        if (item != null && item is Folder)
                        {
                            isExist = true;
                            newFolder = (Folder)item;
                        }
                    }
                    catch { }
                }

                if (!isExist)
                {
                    newFolder = client.Items.CreateFolder(target.url, newFolder, OverWrite, false).Execute();
                }

                ActionManager actionManager = new ActionManager(this, source.Name);

                foreach (var fsInfo in ((DirectoryInfo)source).EnumerateFileSystemInfos())
                {
                    if (fsInfo is DirectoryInfo && Recursive)
                    {
                        UploadRecursive(client, uploadId, fsInfo, newFolder, actionType);
                    }
                    else if (fsInfo is FileInfo)
                    {
                        IAction uploadAction = new UploadAction(FileSupport, client, fsInfo, newFolder, Details, actionType);
                        actionManager.AddAction(uploadAction);
                    }
                }

                actionManager.Execute();
            }
        }
예제 #5
0
        /// <summary>
        /// Start Upload to Sharefile location
        /// </summary>
        private void StartUpload(ShareFileClient client, int uploadId, Models.Item target, ICollection<string> resolvedPaths, ActionType actionType)
        {
            int transactionId = new Random((int)DateTime.Now.Ticks).Next();

            ActionManager actionManager = new ActionManager(this, string.Empty);
            bool firstIteration = true;

            foreach (string path in resolvedPaths)
            {
                FileAttributes attr = System.IO.File.GetAttributes(path);
                FileSystemInfo source = ((attr & FileAttributes.Directory) == FileAttributes.Directory) ? new DirectoryInfo(path) : source = new FileInfo(path);

                // create an extra parent folder if CreateRoot flag is specified on target location
                if (firstIteration && CreateRoot)
                {
                    DirectoryInfo parentFolder = Directory.GetParent(path);
                    var newFolder = new Models.Folder() { Name = parentFolder.Name };
                    target = client.Items.CreateFolder(target.url, newFolder, OverWrite, false).Execute();
                    firstIteration = false;
                }

                if (source is DirectoryInfo)
                {
                    UploadRecursive(client, uploadId, source, target, actionType);
                }
                else
                {
                    IAction uploadAction = new UploadAction(FileSupport, client, source, target, Details, actionType);
                    actionManager.AddAction(uploadAction);
                }
            }

            actionManager.Execute();

            if (Strict)
            {
                foreach (string path in resolvedPaths)
                {
                    FileAttributes attr = System.IO.File.GetAttributes(path);
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        DirectoryInfo source = new DirectoryInfo(path);

                        var children = client.Items.GetChildren(target.url).Execute();
                        
                        foreach (var child in children.Feed)
                        {
                            if (child is Models.Folder && child.Name.Equals(source.Name))
                            {
                                DeleteSharefileStrictRecursive(client, source as DirectoryInfo, child);
                                break;
                            }
                        }
                    }
                }
            }

            if (Move)
            {
                foreach (string path in resolvedPaths)
                {
                    FileAttributes attr = System.IO.File.GetAttributes(path);
                    FileSystemInfo source = ((attr & FileAttributes.Directory) == FileAttributes.Directory) ? new DirectoryInfo(path) : source = new FileInfo(path);

                    DeleteLocalItemRecursive(source, Recursive);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Create local folder
        /// </summary>
        private DirectoryInfo CreateLocalFolder(DirectoryInfo target, Folder source)
        {
            string sourceFolderName = string.Empty;

            // if source is user's Home/Root folder then specify default name
            if (source.Info.IsAHomeFolder == true && source.Info.IsAStartFolder == true)
            {
                sourceFolderName = Utility.DefaultSharefileFolder;
            }
            else
            {
                sourceFolderName = source.FileName;
            }
            var subdirCheck = new DirectoryInfo(System.IO.Path.Combine(target.FullName, sourceFolderName));

            if (subdirCheck.Exists)
            {
                if (Synchronize)
                    return subdirCheck;

                if (!OverWrite)
                    throw new IOException("Path " + subdirCheck.FullName + " already exists. Use -Overwrite to ignore");
            }

            return target.CreateSubdirectory(sourceFolderName);
        }
예제 #7
0
        public static async Task<string> Upload(ShareFileClient sfClient, Folder destinationFolder)
        {
            var file = System.IO.File.Open("SampleFileUpload.txt", FileMode.OpenOrCreate);
            var uploadRequest = new UploadSpecificationRequest
            {
                FileName = "SampleFileUpload.txt",
                FileSize = file.Length,
                Details = "Sample details",
                Parent = destinationFolder.url
            };

            var uploader = sfClient.GetAsyncFileUploader(uploadRequest,
                new PlatformFileStream(file, file.Length, "SampleFileUpload.txt"));

            var uploadResponse = await uploader.UploadAsync();

            return uploadResponse.First().Id;
        }
예제 #8
0
        public static async Task<Folder> CreateFolder(ShareFileClient sfClient, Folder parentFolder)
        {
            // Create instance of the new folder we want to create.  Only a few properties 
            // on folder can be defined, others will be ignored.
            var newFolder = new Folder
            {
                Name = "Sample Folder",
                Description = "Created by SF Client SDK"
            };

            return await sfClient.Items.CreateFolder(parentFolder.url, newFolder, overwrite: true).ExecuteAsync();
        }
예제 #9
0
        public Type VerifyUploader(int megabytes, bool useAsync, CapabilityName[] capabilityNames)
        {
            var shareFileClient = GetShareFileClient();
            var testFolder = new Folder { Name = RandomString(30) + ".txt" };
            var file = GetFileToUpload(1024 * 1024 * megabytes, false);
            var uploadSpec = new UploadSpecificationRequest(file.Name, file.Length, testFolder.url);
            if (capabilityNames != null)
            {
                uploadSpec.ProviderCapabilities =
                    new List<Capability>(capabilityNames.Select(x => new Capability { Name = x }));
            }

            UploaderBase uploader;

            if (useAsync)
            {
                uploader = shareFileClient.GetAsyncFileUploader(uploadSpec, file);
            }
            else
            {
                uploader = shareFileClient.GetFileUploader(uploadSpec, file);
            }

            return uploader.GetType();
        }
예제 #10
0
        protected Folder GetFolder()
        {
            var folder = new Folder
            {
                Children = new List<Item>()
                {
                    new File
                    {
                        FileName = "File 1.txt",
                    }
                },
                FileName = "Folder 1"
            };

            return folder;
        }
예제 #11
0
        private void RecursiveUpload(ShareFileClient client, int uploadId, FileSystemInfo source, Models.Item target)
        {
            if (source is DirectoryInfo)
            {
                var newFolder = new Models.Folder() { Name = source.Name };
                newFolder = client.Items.CreateFolder(target.url, newFolder, Force || ResumeSupport.IsPending, false).Execute();

                ActionManager actionManager = new ActionManager(this, source.Name);
                ActionType actionType = Force ? ActionType.Force : ActionType.None;

                foreach (var fsInfo in ((DirectoryInfo)source).EnumerateFileSystemInfos())
                {
                    if (fsInfo is DirectoryInfo)
                    {
                        RecursiveUpload(client, uploadId, fsInfo, newFolder);
                    }
                    else if (fsInfo is FileInfo)
                    {
                        if (!ResumeSupport.IsPending || !ResumeSupport.CheckFileStatus(fsInfo.Name))
                        {
                            IAction uploadAction = new UploadAction(FileSupport, client, fsInfo, newFolder, Details, actionType);
                            actionManager.AddAction(uploadAction);
                        }
                    }
                }

                actionManager.Execute();
            }
            else if (source is FileInfo)
            {
                ActionManager actionManager = new ActionManager(this, source.Name);
                if (!ResumeSupport.IsPending || !ResumeSupport.CheckFileStatus(source.Name))
                {
                    ActionType actionType = Force || ResumeSupport.IsPending ? ActionType.Force : ActionType.None;
                    IAction uploadAction = new UploadAction(FileSupport, client, source, target, Details, actionType);
                    actionManager.AddAction(uploadAction);
                }
                actionManager.Execute();
            }
        }
예제 #12
0
        public override void Copy(ODataObject source, JsonSerializer serializer)
        {
            if(source == null || serializer == null) return;
            base.Copy(source, serializer);

            var typedSource = source as User;
            if(typedSource != null)
            {
                Account = typedSource.Account;
                Company = typedSource.Company;
                TotalSharedFiles = typedSource.TotalSharedFiles;
                Contacted = typedSource.Contacted;
                FullName = typedSource.FullName;
                ReferredBy = typedSource.ReferredBy;
                Notifications = typedSource.Notifications;
                DefaultZone = typedSource.DefaultZone;
                FirstName = typedSource.FirstName;
                LastName = typedSource.LastName;
                DateCreated = typedSource.DateCreated;
                FullNameShort = typedSource.FullNameShort;
                Emails = typedSource.Emails;
                IsConfirmed = typedSource.IsConfirmed;
                Password = typedSource.Password;
                Preferences = typedSource.Preferences;
                Security = typedSource.Security;
                FavoriteFolders = typedSource.FavoriteFolders;
                HomeFolder = typedSource.HomeFolder;
                Devices = typedSource.Devices;
                Integrations = typedSource.Integrations;
                VirtualRoot = typedSource.VirtualRoot;
                Roles = typedSource.Roles;
                Info = typedSource.Info;
            }
            else
            {
                JToken token;
                if(source.TryGetProperty("Account", out token) && token.Type != JTokenType.Null)
                {
                    Account = (Account)serializer.Deserialize(token.CreateReader(), typeof(Account));
                }
                if(source.TryGetProperty("Company", out token) && token.Type != JTokenType.Null)
                {
                    Company = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("TotalSharedFiles", out token) && token.Type != JTokenType.Null)
                {
                    TotalSharedFiles = (int?)serializer.Deserialize(token.CreateReader(), typeof(int?));
                }
                if(source.TryGetProperty("Contacted", out token) && token.Type != JTokenType.Null)
                {
                    Contacted = (int?)serializer.Deserialize(token.CreateReader(), typeof(int?));
                }
                if(source.TryGetProperty("FullName", out token) && token.Type != JTokenType.Null)
                {
                    FullName = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("ReferredBy", out token) && token.Type != JTokenType.Null)
                {
                    ReferredBy = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("Notifications", out token) && token.Type != JTokenType.Null)
                {
                    Notifications = (IEnumerable<Notification>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<Notification>));
                }
                if(source.TryGetProperty("DefaultZone", out token) && token.Type != JTokenType.Null)
                {
                    DefaultZone = (Zone)serializer.Deserialize(token.CreateReader(), typeof(Zone));
                }
                if(source.TryGetProperty("FirstName", out token) && token.Type != JTokenType.Null)
                {
                    FirstName = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("LastName", out token) && token.Type != JTokenType.Null)
                {
                    LastName = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("DateCreated", out token) && token.Type != JTokenType.Null)
                {
                    DateCreated = (DateTime?)serializer.Deserialize(token.CreateReader(), typeof(DateTime?));
                }
                if(source.TryGetProperty("FullNameShort", out token) && token.Type != JTokenType.Null)
                {
                    FullNameShort = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("Emails", out token) && token.Type != JTokenType.Null)
                {
                    Emails = (IEnumerable<string>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<string>));
                }
                if(source.TryGetProperty("IsConfirmed", out token) && token.Type != JTokenType.Null)
                {
                    IsConfirmed = (bool?)serializer.Deserialize(token.CreateReader(), typeof(bool?));
                }
                if(source.TryGetProperty("Password", out token) && token.Type != JTokenType.Null)
                {
                    Password = (string)serializer.Deserialize(token.CreateReader(), typeof(string));
                }
                if(source.TryGetProperty("Preferences", out token) && token.Type != JTokenType.Null)
                {
                    Preferences = (UserPreferences)serializer.Deserialize(token.CreateReader(), typeof(UserPreferences));
                }
                if(source.TryGetProperty("Security", out token) && token.Type != JTokenType.Null)
                {
                    Security = (UserSecurity)serializer.Deserialize(token.CreateReader(), typeof(UserSecurity));
                }
                if(source.TryGetProperty("FavoriteFolders", out token) && token.Type != JTokenType.Null)
                {
                    FavoriteFolders = (IEnumerable<FavoriteFolder>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<FavoriteFolder>));
                }
                if(source.TryGetProperty("HomeFolder", out token) && token.Type != JTokenType.Null)
                {
                    HomeFolder = (Folder)serializer.Deserialize(token.CreateReader(), typeof(Folder));
                }
                if(source.TryGetProperty("Devices", out token) && token.Type != JTokenType.Null)
                {
                    Devices = (IEnumerable<DeviceUser>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<DeviceUser>));
                }
                if(source.TryGetProperty("Integrations", out token) && token.Type != JTokenType.Null)
                {
                    Integrations = (IEnumerable<SafeEnum<IntegrationProvider>>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<SafeEnum<IntegrationProvider>>));
                }
                if(source.TryGetProperty("VirtualRoot", out token) && token.Type != JTokenType.Null)
                {
                    VirtualRoot = (Folder)serializer.Deserialize(token.CreateReader(), typeof(Folder));
                }
                if(source.TryGetProperty("Roles", out token) && token.Type != JTokenType.Null)
                {
                    Roles = (IEnumerable<SafeEnum<UserRole>>)serializer.Deserialize(token.CreateReader(), typeof(IEnumerable<SafeEnum<UserRole>>));
                }
                if(source.TryGetProperty("Info", out token) && token.Type != JTokenType.Null)
                {
                    Info = (UserInfo)serializer.Deserialize(token.CreateReader(), typeof(UserInfo));
                }
            }
        }
예제 #13
0
 /// <summary>
 /// Create Folder
 /// </summary>
 /// <example>
 /// {
 /// "Name":"Folder Name",
 /// "Description":"Description",
 /// "Zone":{ "Id":"z014766e-8e96-4615-86aa-57132a69843c" }
 /// }
 /// </example>
 /// <remarks>
 /// Creates a new Folder.
 /// The POST body must contain the serialized object.
 /// For top-level folders, use Items/Folder.
 /// The Zone object may only be provided for top-level folders. The Zone object must
 /// contain a zone ID.
 /// </remarks>
 /// <param name="parentUrl"></param>
 /// <param name="folder"></param>
 /// <param name="overwrite"></param>
 /// <param name="passthrough"></param>
 /// <returns>
 /// the new Folder
 /// </returns>
 public IQuery<Folder> CreateFolder(Uri parentUrl, Folder folder, bool overwrite = false, bool passthrough = false)
 {
     var sfApiQuery = new ShareFile.Api.Client.Requests.Query<Folder>(Client);
     sfApiQuery.Action("Folder");
     sfApiQuery.Uri(parentUrl);
     sfApiQuery.QueryString("overwrite", overwrite);
     sfApiQuery.QueryString("passthrough", passthrough);
     sfApiQuery.Body = folder;
     sfApiQuery.HttpMethod = "POST";
     return sfApiQuery;
 }