Exemplo n.º 1
0
        void IAction.CopyFileItem(ProgressInfo progressInfo)
        {
            var fileInfo = (FileInfo)child;

            Models.Item fileItem = null;
            fileName = child.Name;
            try
            {
                fileItem = client.Items.ByPath(uploadTarget.url, "/" + child.Name).Execute();
            }
            catch (ODataException e) {
                if (e.Code != System.Net.HttpStatusCode.NotFound)
                {
                    throw e;
                }
            }

            bool duplicate       = fileItem != null && fileItem is Models.File;
            bool hashcodeMatches = duplicate ? (fileItem as Models.File).Hash.Equals(Utility.GetMD5HashFromFile(child.FullName)) : false;

            if (duplicate && actionType == ActionType.None)
            {
                throw new IOException("File already exist");
            }
            else if (!duplicate || actionType == ActionType.Force || (actionType == ActionType.Sync && !hashcodeMatches))
            {
                var uploadSpec = new UploadSpecificationRequest
                {
                    CanResume   = false,
                    Details     = details,
                    FileName    = fileInfo.Name,
                    FileSize    = fileInfo.Length,
                    Method      = UploadMethod.Threaded,
                    Parent      = uploadTarget.url,
                    ThreadCount = 4,
                    Raw         = true
                };

                using (var platformFileInfo = new PlatformFileInfo(fileInfo))
                {
                    var uploader = client.GetAsyncFileUploader(uploadSpec, platformFileInfo);

                    progressInfo.ProgressTotal(progressInfo.FileIndex, fileInfo.Length);

                    uploader.OnTransferProgress =
                        (sender, args) =>
                    {
                        if (args.Progress.TotalBytes > 0)
                        {
                            progressInfo.ProgressTransferred(progressInfo.FileIndex, args.Progress.BytesTransferred);
                        }
                    };

                    Task.Run(() => uploader.UploadAsync()).Wait();
                    fileSupportDelegate(fileInfo.Name);
                }
            }
        }
        void IAction.CopyFileItem()
        {
            var fileInfo = (FileInfo)child;

            Models.Item fileItem = null;
            try
            {
                fileItem = client.Items.ByPath(uploadTarget.url, "/" + child.Name).Execute();
            }
            catch (ODataException e) {
                if (e.Code != System.Net.HttpStatusCode.NotFound)
                {
                    throw e;
                }
            }

            bool duplicate       = fileItem != null && fileItem is Models.File;
            bool hashcodeMatches = duplicate ? (fileItem as Models.File).Hash.Equals(Utility.GetMD5HashFromFile(child.FullName)) : false;

            if (duplicate && actionType == ActionType.None)
            {
                throw new IOException("File already exist");
            }
            else if (!duplicate || actionType == ActionType.Force || (actionType == ActionType.Sync && !hashcodeMatches))
            {
                var uploadSpec = new UploadSpecificationRequest
                {
                    CanResume   = false,
                    Details     = details,
                    FileName    = fileInfo.Name,
                    FileSize    = fileInfo.Length,
                    Method      = UploadMethod.Threaded,
                    Parent      = uploadTarget.url,
                    ThreadCount = 4,
                    Raw         = true
                };

                var uploader = client.GetAsyncFileUploader(uploadSpec, new PlatformFileInfo(fileInfo));

                var progressBar = new ProgressBar(copySfItem);
                progressBar.SetProgress(fileInfo.Name, 0);
                uploader.OnTransferProgress =
                    (sender, args) =>
                {
                    int pct = 100;
                    if (args.Progress.TotalBytes > 0)
                    {
                        pct = (int)(((float)args.Progress.BytesTransferred / args.Progress.TotalBytes) * 100);
                    }
                    progressBar.SetProgress(fileInfo.Name, pct);
                };
                Task task = Task.Run(() => uploader.UploadAsync());
                task.ContinueWith(t => progressBar.Finish());
                task.Wait();
            }
        }