Ejemplo n.º 1
0
        protected async Task <SmartItem[]> _prepareForUpload(string[] items, string destination)
        {
            List <SmartItem> uploadList = new List <SmartItem>();
            IList <string>   folders    = new List <string>();

            await Task.Run(async delegate
            {
                for (int i = 0; i < items.Length; i++)
                {
                    if (Paused)
                    {
                        while (Paused && !IsCanceled)
                        {
                            await Task.Delay(200);
                        }
                    }
                    if (IsCanceled)
                    {
                        break;
                    }
                    if (FileHelper.Exists(items[i]))
                    {
                        FileInfo file = null;
                        try
                        {
                            file = new FileInfo(items[i]);
                            uploadList.Add(new SmartItem(file, destination));
                            TransferEvent.TotalSize += file.Length;
                            TransferEvent.TotalFiles++;
                        }
                        catch (Exception exp) { ExceptionHelper.Log(exp); }
                        file = null;
                    }
                    else if (DirectoryHelper.Exists(items[i]))
                    {
                        folders.Add(items[i]);
                    }
                }
                items = null;

                for (int i = 0; i < folders.Count; i++)
                {
                    if (Paused)
                    {
                        while (Paused && !IsCanceled)
                        {
                            await Task.Delay(200);
                        }
                    }
                    if (IsCanceled)
                    {
                        break;
                    }
                    uploadList.AddRange(await _getLocalItems(new DirectoryInfo(folders[i]), destination, -1, uploadList.Count));
                }
                folders = null;
            });

            if (IsCanceled)
            {
                return new SmartItem[] { }
            }
            ;
            return(uploadList.ToArray());
        }
Ejemplo n.º 2
0
        private async Task setTransferAction(bool IsUpload, SmartItem[] IIDP, int j)
        {
            if (TransferEvent.Item.Exist)
            {
                if (TransferEvents.TillCloseAction != TransferAction.Unknown)
                {
                    TransferEvent.Action = TransferEvents.TillCloseAction;
                }
                else if (TransferEvent.SessionAction != TransferAction.Unknown)
                {
                    TransferEvent.Action = TransferEvent.SessionAction;
                }
                else
                {
                    TransferEvent.RequestingAction(IIDP[j], ((TransferEvent.Item.Length > 1048576) &&
                                                             (TransferEvent.Item.Length > IIDP[j].Length)));
                }
            }

            switch (TransferEvent.Action)
            {
            case TransferAction.Unknown:
                TransferEvent.Item.Status = IsUpload ? ItemStatus.Uploading : ItemStatus.Downloading;
                break;

            case TransferAction.Ignore:
                TransferEvent.Item.Status = ItemStatus.Ignored;
                break;

            case TransferAction.Rename:
                if (TransferEvent.Item.Exist)
                {
                    string newName = await IIDP.GetNewName(IIDP[j]);

                    if (IsUpload)
                    {
                        if (await RenameItemAsync(IIDP[j], newName))
                        {
                            IIDP[j].ItemName          = newName;
                            IIDP[j].FullName          = IIDP[j].ItemFolder + newName;
                            TransferEvent.Item.Exist  = false;
                            TransferEvent.Item.Status = ItemStatus.Uploading;
                        }
                        else
                        {
                            TransferEvent.Item.Status = ItemStatus.UploadError; TransferEvent.Item.HasError = true;
                        }
                    }
                    else
                    {
                        bool renamed;

                        if (IIDP[j].IsFile)
                        {
                            renamed = FileHelper.Rename(IIDP[j].FullName, IIDP[j].ItemFolder + @"\" + newName);
                        }
                        else
                        {
                            renamed = DirectoryHelper.Rename(IIDP[j].FullName, IIDP[j].ItemFolder + @"\" + newName);
                        }

                        if (renamed)
                        {
                            IIDP[j].ItemName          = IIDP[j].ItemName = newName;
                            IIDP[j].FullName          = IIDP[j].ItemFolder + @"\" + newName;
                            TransferEvent.Item.Exist  = false;
                            TransferEvent.Item.Status = ItemStatus.Downloading;
                        }
                        else
                        {
                            TransferEvent.Item.Status = ItemStatus.DownloadError; TransferEvent.Item.HasError = true;
                        }
                    }
                }
                else
                {
                    TransferEvent.Item.Status = IsUpload ? ItemStatus.Uploading : ItemStatus.Downloading;
                }
                break;

            case TransferAction.Replace:
                TransferEvent.Item.Status = (IsUpload) ? ItemStatus.Replacing : ItemStatus.Downloading;
                break;

            case TransferAction.Resume:
                TransferEvent.Item.Status = ItemStatus.Resuming;
                break;
            }
        }