Esempio n. 1
0
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="FileData"></param>
        /// <returns></returns>
        public async Task <long> ReplaceFile(UploadFileData FileData)
        {
            var itemLength = new FileInfo(FileData.File.Name).Length;
            var filename   = Path.GetFileName(FileData.File.Name);
            var fileUpload = new FileUpload();
            var cs         = new CancellationTokenSource();
            var token      = cs.Token;

            fileUpload.CancellationToken = token;
            fileUpload.AllowDuplicate    = false;
            fileUpload.ParentId          = FileData.ParentItem.Id;

            var ACDFilePath = FileData.RemoteFileName;

            // for upload we need a node id to replace
            if (string.IsNullOrEmpty(ACDFilePath))
            {
                ACDFilePath = Path.Combine(FileData.ParentItem.Path, filename);
            }
            var node = await FetchNode(ACDFilePath);

            if (node == null)
            {
                throw new FileNotFoundException("Remote file " + ACDFilePath + " not found");
            }
            fileUpload.FileName = node.Name;

            fileUpload.StreamOpener = () => new FileStream(FileData.File.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true);
            fileUpload.Progress     = (long position) =>
            {
                FileData.PauseEvent.WaitOne();
                FileData.Form.Activity = Progress.GetActivityProgress(
                    FileData.File.Name,
                    FileData.TotalProgress,
                    itemLength,
                    FileData.TotalProgress + position,
                    FileData.TotalSize,
                    FileData.TimestampStartOne,
                    FileData.TimestampStartTotal
                    );
                FileData.Form.SetProgressValue(FileData.TotalProgress + position, FileData.TotalSize);

                return(position);
            };
            FileData.Form.Canceled += (object sender, EventArgs e) =>
            {
                cs.Cancel(true);
            };

            var result = await amazon.Files.Overwrite(fileUpload);

            foreach (var newNode in result.metadata)
            {
                CacheStorage.AddItem(FSItem.FromNode(Path.Combine(FileData.ParentItem.Path, filename), newNode));
            }

            return(FileData.TotalProgress /* + resultNode.Length */);
        }
Esempio n. 2
0
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="FileData"></param>
        /// <returns></returns>
        public async Task <long> UploadNewFile(UploadFileData FileData)
        {
            var itemLength = new FileInfo(FileData.File.Name).Length;
            var totalBytes = Utility.BytesToString(itemLength);
            var filename   = Path.GetFileName(FileData.File.Name);
            var fileUpload = new FileUpload();

            fileUpload.AllowDuplicate = false;
            fileUpload.ParentId       = FileData.ParentItem.Id;
            fileUpload.FileName       = filename;
            fileUpload.StreamOpener   = () => new FileStream(FileData.File.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true);
            fileUpload.Progress       = (long position) =>
            {
                FileData.PauseEvent.WaitOne();
                FileData.Form.Activity = Progress.GetActivityProgress(
                    FileData.File.Name,
                    position,
                    itemLength,
                    FileData.TotalProgress + position,
                    FileData.TotalSize,
                    FileData.TimestampStartOne,
                    FileData.TimestampStartTotal
                    );
                FileData.Form.SetProgressValue(FileData.TotalProgress + position, FileData.TotalSize);

                return(position);
            };
            var cs    = new CancellationTokenSource();
            var token = cs.Token;

            fileUpload.CancellationToken = token;
            FileData.Form.Canceled      += (object sender, EventArgs e) =>
            {
                cs.Cancel(true);
            };

            var result = await amazon.Files.UploadNew(fileUpload);

            foreach (var node in result.metadata)
            {
                CacheStorage.AddItem(FSItem.FromNode(Path.Combine(FileData.ParentItem.Path, filename), node));
                if (node.size != null)
                {
                    FileData.TotalProgress += node.size.Value;
                }
            }

            return(FileData.TotalProgress /*+ node.Length*/);
        }