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*/);
        }
Esempio n. 3
0
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="itemPath"></param>
        /// <param name="CacheBypass"></param>
        /// <returns></returns>
        public async Task <FSItem> FetchNode(string itemPath, bool CacheBypass = false)
        {
            FSItem item = null;

            if (!CacheBypass)
            {
                item = CacheStorage.GetItemByPath(itemPath);
            }

            if (item != null)
            {
                return(item);
            }

            if (itemPath == string.Empty)
            {
                itemPath = "\\";
            }

            AmazonNode node;

            // try as a directory
            node = await amazon.Nodes.GetNodeByPath(itemPath, true);

            if (node == null)
            {
                // try as a file
                node = await amazon.Nodes.GetNodeByPath(itemPath, false);

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

            item = FSItem.FromNode(itemPath, node);
            CacheStorage.AddItem(item);

            return(item);
        }
Esempio n. 4
0
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="parent"></param>
        /// <param name="allowExisting"></param>
        /// <returns></returns>
        public async Task <FSItem> CreateDirectory(string filePath, FSItem parent = null, bool allowExisting = true)
        {
            if (filePath == "\\" || filePath == ".." || filePath == ".")
            {
                return(null);
            }
            var dir = Path.GetDirectoryName(filePath);

            if (dir == ".." || dir == ".")
            {
                return(null);
            }

            if (parent == null)
            {
                parent = await FetchNode(dir);

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

            var        name = Path.GetFileName(filePath);
            AmazonNode node = null;

            try
            {
                node = await amazon.Nodes.CreateFolder(parent.Id, name);
            }
            catch (Azi.Tools.HttpWebException x)
            {
                if (x.StatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    if (!allowExisting)
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }

            if (node == null) // in case of duplicate; other cases are re-thrown
            {
                node = await amazon.Nodes.GetNodeByPath(filePath, true);
            }

            if (node == null)
            {
                throw new InvalidOperationException("Could not retrieve node information " + filePath);
            }

            var item = FSItem.FromNode(filePath, node);

            CacheStorage.AddItem(item);

            return(item);
        }