Beispiel #1
0
        internal override async Task <bool> ChangePermissionsync(SmartItem item, string permission)
        {
            SmartItem eitem;

            if (await _commandChmodAsync(item.FullName, permission))
            {
                eitem = GetServerItem(item.ItemName, item.ItemFolder);
                if (eitem != null)
                {
                    eitem.Status = ItemStatus.PermissionChanged;
                    TransferEvents.ItemStatusChanged(eitem);
                }

                AppHistory.Add(item.ItemName, item.ItemFolder, item.Permissions, permission, ItemStatus.PermissionChanged);
                item.Permissions = permission;
                return(true);
            }
            else if (await retryAsync())
            {
                return(await ChangePermissionsync(item, permission));
            }

            AppHistory.Add(item.ItemName, item.ItemFolder, item.Permissions, permission, ItemStatus.PermissionError);

            eitem = GetServerItem(item.ItemName, item.ItemFolder);
            if (eitem != null)
            {
                eitem.Status = ItemStatus.PermissionError;
                TransferEvents.ItemStatusChanged(eitem);
            }

            return(false);
        }
Beispiel #2
0
        internal override async Task <bool> CreateFolderAsync(string name, string path, bool replaceSpaces = true)
        {
            if (path.NullEmpty())
            {
                return(false);
            }

            SmartItem item = GetServerItem(name, path);

            if (item == null)
            {
                item = InsertItem(new SmartItem(name, path, DateTime.Now.Ticks));
            }

            if (item != null)
            {
                item.Status = ItemStatus.Creating;
                TransferEvents.ItemStatusChanged(item);
            }

            bool created = await _createServerFolderAsync(path + name);

            AppHistory.Add(name, path, " ", path + name, (created) ? ItemStatus.Created : ItemStatus.CreateError);
            item = GetServerItem(name, path);
            if (item != null)
            {
                item.HasError = !created;
                item.Status   = (created) ? ItemStatus.Created : ItemStatus.CreateError;
                TransferEvents.ItemStatusChanged(item);
            }

            return(created);
        }
Beispiel #3
0
        internal override async Task <bool> DeleteItemAsync(SmartItem item)
        {
            SmartItem eitem = GetServerItem(item.ItemName, item.ItemFolder);

            if (eitem != null)
            {
                eitem.Status = ItemStatus.Deleting;
                TransferEvents.ItemStatusChanged(eitem);
            }

            bool deleted = false;

            if (item.IsFile)
            {
                if (await _commandDeleAsync(item.FullName))
                {
                    deleted = true;
                }
            }
            else if (isRMDA)
            {
                if (await _commandRmdaAsync(item.FullName))
                {
                    deleted = true;
                }
            }
            else if (await _deleteFolderAsync(item.FullName))
            {
                deleted = true;
            }

            if (deleted)
            {
                AppHistory.Add(item.ItemName, item.ItemFolder, item.ItemFolder, " ", ItemStatus.Deleted);
                eitem = GetServerItem(item.ItemName, item.ItemFolder);
                if (eitem != null)
                {
                    Items.Remove(eitem);
                }

                return(true);
            }
            else if (await retryAsync())
            {
                return(await DeleteItemAsync(item));
            }

            AppHistory.Add(item.ItemName, item.ItemFolder, item.ItemFolder, " ", ItemStatus.DeleteError);

            eitem = GetServerItem(item.ItemName, item.ItemFolder);
            if (eitem != null)
            {
                eitem.Status = ItemStatus.DeleteError;
                TransferEvents.ItemStatusChanged(eitem);
            }

            return(false);
        }
Beispiel #4
0
        internal override async Task <bool> RenameItemAsync(SmartItem item, string toName)
        {
            string newName = item.ItemFolder + toName;

            SmartItem eitem = GetServerItem(item.ItemName, item.ItemFolder);

            if (eitem != null)
            {
                eitem.Status = ItemStatus.Renaming;
                TransferEvents.ItemStatusChanged(eitem);
            }

            if (await commandRnfrAsync(item.FullName) && await commandRntoAsync(newName))
            {
                AppHistory.Add(toName, item.ItemFolder, item.ItemName, toName, ItemStatus.Renamed);
                eitem = GetServerItem(item.ItemName, item.ItemFolder, null, true);
                if (eitem != null)
                {
                    eitem.ItemName = toName;
                    eitem.FullName = newName;
                    eitem.Status   = ItemStatus.Renamed;
                    TransferEvents.ItemStatusChanged(eitem);
                }
                return(true);
            }
            else if (await retryAsync())
            {
                return(await RenameItemAsync(item, toName));
            }
            AppHistory.Add(item.ItemName, item.ItemFolder, item.ItemName, toName, ItemStatus.RenameError);

            eitem = GetServerItem(item.ItemName, item.ItemFolder);
            if (eitem != null)
            {
                eitem.Status = ItemStatus.RenameError;
                TransferEvents.ItemStatusChanged(eitem);
            }

            return(false);
        }
Beispiel #5
0
        internal override async Task <bool> MoveItemAsync(SmartItem item, string toPath)
        {
            SmartItem eitem = GetServerItem(item.ItemName, item.ItemFolder);

            if (eitem != null)
            {
                eitem.Status = ItemStatus.Moving;
                TransferEvents.ItemStatusChanged(eitem);
            }

            if (await commandRnfrAsync(item.FullName) && await commandRntoAsync(toPath + item.ItemName))
            {
                AppHistory.Add(item.ItemName, toPath, item.ItemFolder, toPath, ItemStatus.Moved);
                eitem = GetServerItem(item.ItemName, item.ItemFolder);
                if (eitem != null)
                {
                    Items.Remove(eitem);
                }
                return(true);
            }
            else if (await retryAsync())
            {
                return(await MoveItemAsync(item, toPath));
            }

            AppHistory.Add(item.ItemName, item.ItemFolder, item.ItemFolder, toPath, ItemStatus.MoveError);

            eitem = GetServerItem(item.ItemName, item.ItemFolder);
            if (eitem != null)
            {
                eitem.Status = ItemStatus.MoveError;
                TransferEvents.ItemStatusChanged(eitem);
            }

            return(false);
        }
Beispiel #6
0
        protected override async Task _sendFileAsync(string source)
        {
            FileStream fs = null;

            fs = FileHelper.OpenRead(source);
            if (fs == null)
            {
                TransferEvent.Item.HasError = true; return;
            }

            byte[] buffer;
            long   tbytes          = 0;
            int    bytes           = 0;
            bool   connectionOpend = false;

            if (TransferEvent.Item.Status == ItemStatus.Resuming)
            {
                tbytes = await _commandAppeAsync(TransferEvent.Item.FullName);

                connectionOpend = (tbytes > 0);

                if (connectionOpend && !FileHelper.Seek(fs, tbytes))
                {
                    TransferEvent.Item.HasError = true;
                    if (fs != null)
                    {
                        fs.Dispose();
                    }
                    return;
                }
            }
            else
            {
                connectionOpend = await _commandStorAsync(TransferEvent.Item.FullName);
            }

            if (connectionOpend)
            {
                await setupDataStreamAsync(false);

                try
                {
                    while (true)
                    {
                        if (Paused)
                        {
                            while (Paused && !IsCanceled)
                            {
                                await Task.Delay(200);
                            }
                        }
                        if (IsCanceled || FlagSkipIt)
                        {
                            break;
                        }

                        int _bufferSize = BufferSize;
                        buffer = new byte[_bufferSize];

                        bytes = 0;

                        bytes = await fs.ReadAsync(buffer, 0, buffer.Length);

                        if (bytes == 0)
                        {
                            break;
                        }
                        await DataStream.WriteAsync(buffer, 0, bytes);

                        tbytes += bytes;
                        TransferEvent.ItemSent         = tbytes;
                        TransferEvent.Item.Transferred = tbytes;

                        if (tbytes == fs.Length)
                        {
                            break;
                        }
                    }
                }
                catch { }
            }
            if (fs != null)
            {
                fs.Dispose();
            }

            if (FlagSkipIt)
            {
                TransferEvent.Item.Status = ItemStatus.Ignored;
                FlagSkipIt = false;
                await commandAbortAsync();

                return;
            }
            else
            {
                closeDataConnection();
            }

            TransferEvent.Item.HasError = (!connectionOpend || !(await getResponseAsync() == 226));
            if (TransferEvent.Item.HasError && await retryAsync())
            {
                if (tbytes > 0)
                {
                    TransferEvent.Item.Status = ItemStatus.Resuming;
                    if (TransferEvent.Item.Destination == BrowsedPath)
                    {
                        TransferEvents.ItemStatusChanged(TransferEvent.Item);
                    }
                }
                await _sendFileAsync(source);
            }
        }
Beispiel #7
0
        protected override async Task _getFileAsync()
        {
            FileStream fs = null;
            if (TransferEvent.Item.Status == ItemStatus.Resuming)
                fs = FileHelper.OpenWrite(TransferEvent.Item.Destination + TransferEvent.Item.ItemName);
            else fs = FileHelper.Create(TransferEvent.Item.Destination + TransferEvent.Item.ItemName);

            if (fs == null) { TransferEvent.Item.HasError = true; closeDataConnection(); return; }

            byte[] buffer;
            long tbytes = 0;
            int bytes = 0;

            if (TransferEvent.Item.Status == ItemStatus.Resuming)
            {
                if (FileHelper.Seek(fs, tbytes)) tbytes = fs.Length;
                else
                {
                    TransferEvent.Item.HasError = true;
                    if (fs != null) fs.Dispose();
                    closeDataConnection(); return;
                }
            }

            int code = await _commandRetrAsync(TransferEvent.Item.FullName, tbytes);
            if ((code == 0) && await retryAsync()) await _getFileAsync();
            if ((code != 150) && (code != 125) && (code != 226))
            {
                TransferEvent.Item.HasError = true;
                closeDataConnection();
                if (fs != null) fs.Dispose();
                if (tbytes == 0) FileHelper.Delete(TransferEvent.Item.Destination + TransferEvent.Item.ItemName);
                return;
            }

            await setupDataStreamAsync(true);

            try
            {
                while (true)
                {
                    if (Paused) while (Paused && !IsCanceled) { await Task.Delay(200); }
                    if (IsCanceled || FlagSkipIt) break;

                    int _bufferSize = BufferSize;
                    buffer = new byte[_bufferSize];

                    bytes = 0;

                    bytes = await DataStream.ReadAsync(buffer, 0, buffer.Length);
                    if (bytes == 0) break;
                    await fs.WriteAsync(buffer, 0, bytes);

                    tbytes += bytes;
                    TransferEvent.ItemSent = tbytes;
                    TransferEvent.Item.Transferred = TransferEvent.Item.Length - tbytes;
                }
            }
            catch { }

            if (fs != null) fs.Dispose();

            if (FlagSkipIt)
            {
                TransferEvent.Item.Status = ItemStatus.Ignored;
                FlagSkipIt = false;
                await commandAbortAsync();
                return;
            }
            else closeDataConnection();

            TransferEvent.Item.HasError = !((code == 226) || (await getResponseAsync() == 226));
            if (TransferEvent.Item.HasError && await retryAsync())
            {
                if (tbytes > 0)
                {
                    TransferEvent.Item.Status = ItemStatus.Resuming;
                    if (TransferEvent.Item.ItemFolder == BrowsedPath) TransferEvents.ItemStatusChanged(TransferEvent.Item);
                }
                await _getFileAsync();
            }
        }