示例#1
0
    private void FinishDownLoadTask(DownloadTask task)
    {
        List <DownloadTask> downLoadList = null;

        if (newDownloadTasks.TryGetValue(task.priority, out downLoadList))
        {
            if (downLoadList.Contains(task))
            {
                downLoadList.Remove(task);
            }
            if (downLoadList.Count <= 0)
            {
                if (priorityList.Contains(task.priority))
                {
                    priorityList.Remove(task.priority);
                }
                if (newDownloadTasks.ContainsKey(task.priority))
                {
                    newDownloadTasks.Remove(task.priority);
                }
            }
        }
        if (task != null)
        {
            task.Dispose();
        }
    }
示例#2
0
 public void BreakDownload(DownloadTask task)
 {
     if (task != null)
     {
         List <DownloadTask> list = null;
         if (newDownloadTasks.TryGetValue((int)task.priority, out list))
         {
             if (list.Contains(task))
             {
                 list.Remove(task);
             }
             if (list.Count <= 0 && priorityList.Contains((int)task.priority))
             {
                 priorityList.Remove((int)task.priority);
             }
         }
         task.Dispose();
     }
 }
        public async Task StartDownload()
        {
            try
            {
                ProgressBar.Tag       = this;
                ProgressLabel.Text    = "";
                ProgressLabel.Visible = true;
                Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
                FileInfo     file          = new FileInfo(SavePath);
                DialogResult overwriteFile = DialogResult.Yes;
                if (file.Exists)
                {
                    switch (ParentDownload.OverwriteMode)
                    {
                    case 0:
                        overwriteFile = DialogResult.No;
                        break;

                    case 1:
                        overwriteFile = DialogResult.Yes;
                        break;

                    case 2:
                        if (FileInfo.Modified > file.CreationTime)
                        {
                            overwriteFile = DialogResult.Yes;
                        }
                        else
                        {
                            overwriteFile = DialogResult.No;
                        }
                        break;

                    case 3:
                        overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo);
                        break;
                    }
                    if (overwriteFile == DialogResult.Yes)
                    {
                        file.Delete();
                    }
                }

                if (overwriteFile == DialogResult.Yes)
                {
                    CustomHeaders.Add("X-Requested-With", "XMLHttpRequest");
                    string downloadPath = EncodeWebUrl(FileInfo.PublicUrl.OriginalString);
                    if (ParentDownload.CloudService == CloudServiceType.Allsync)
                    {
                        var encodedUrl = new Uri(downloadPath);
                        var host       = encodedUrl.Host;
                        downloadPath = $"https://{host}/public.php/webdav{EncodeAllsyncUrl(FileInfo.Path)}";
                    }

                    //var logFileName = $"download-log-{DateTime.Now.ToString("MM-dd-yyyy")}.txt";
                    //string log = $"{DateTime.Now}\ndownloadPath: {downloadPath}\nSavePath: {SavePath}\n";
                    //File.AppendAllText(logFileName, log);

                    DownloadTask = DownloadFileAsync(downloadPath, SavePath, Progress, ParentDownload.cancellationTokenSource.Token, _networkCredential);
                    await DownloadTask;
                }
                ParentDownload.UpdateQueue(this);
            }
            catch (Exception ex)
            {
                if (DownloadTask.IsCanceled)
                {
                    DownloadTask.Dispose();
                }
                //MessageBox.Show(ex.Message);
            }
        }
        public async void StartDownload()
        {
            try
            {
                ProgressBar.Tag       = this;
                ProgressLabel.Text    = "";
                ProgressLabel.Visible = true;
                Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
                FileInfo     file          = new FileInfo(SavePath);
                DialogResult overwriteFile = DialogResult.Yes;
                if (file.Exists)
                {
                    switch (MegaDownload.OverwriteMode)
                    {
                    case 0:
                        overwriteFile = DialogResult.No;
                        break;

                    case 1:
                        overwriteFile = DialogResult.Yes;
                        break;

                    case 2:
                        if (Node.ModificationDate > file.CreationTime)
                        {
                            overwriteFile = DialogResult.Yes;
                        }
                        else
                        {
                            overwriteFile = DialogResult.No;
                        }
                        break;

                    case 3:
                        overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo);
                        break;
                    }
                    if (overwriteFile == DialogResult.Yes)
                    {
                        file.Delete();
                    }
                }
                if (overwriteFile == DialogResult.Yes)
                {
                    DownloadTask = MegaClient.DownloadFileAsync(Node, SavePath, Progress, MegaDownload.cancellationTokenSource.Token);
                    await DownloadTask;
                }
                MegaDownload.UpdateQueue(this);
            }
            catch (Exception ex)
            {
                if (DownloadTask.IsCanceled)
                {
                    DownloadTask.Dispose();
                }
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                //Finished = true;
                //ProgressBar.Tag = null;
                //ProgressBar.Value = 0;
                //ProgressLabel.Visible = false;
            }
        }
示例#5
0
        public async void StartDownload()
        {
            try
            {
                ProgressBar.Tag       = this;
                ProgressLabel.Text    = "";
                ProgressLabel.Visible = true;
                Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
                FileInfo     file          = new FileInfo(SavePath);
                DialogResult overwriteFile = DialogResult.Yes;
                if (file.Exists)
                {
                    switch (MegaDownload.OverwriteMode)
                    {
                    case 0:
                        overwriteFile = DialogResult.No;
                        break;

                    case 1:
                        overwriteFile = DialogResult.Yes;
                        break;

                    case 2:
                        if (Node.ModificationDate > file.CreationTime)
                        {
                            overwriteFile = DialogResult.Yes;
                        }
                        else
                        {
                            overwriteFile = DialogResult.No;
                        }
                        break;

                    case 3:
                        overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo);
                        break;
                    }
                    if (overwriteFile == DialogResult.Yes)
                    {
                        file.Delete();
                    }
                }
                if (overwriteFile == DialogResult.Yes)
                {
                    DownloadTask = MegaClient.DownloadFileAsync(Node, SavePath, Progress, MegaDownload.cancellationTokenSource.Token);
                    await DownloadTask;
                }
                MegaDownload.UpdateQueue(this);
            }
            catch (Exception ex)
            {
                if (DownloadTask.IsCanceled)
                {
                    DownloadTask.Dispose();
                    if (File.Exists(SavePath))
                    {
                        File.Delete(SavePath);
                    }
                    ProgressBar.Value  = 0;
                    ProgressLabel.Text = "";
                }
                else
                {
                    var    logFileName = $"00-DOWNLOAD-LOG-{DateTime.Now.ToString("MM-dd-yyyy")}.txt";
                    string log         = $"\n{DateTime.Now}\nNode id: {Node.Id}\nSavePath:{SavePath}\nexception: {ex.Message}\n";
                    File.AppendAllText(logFileName, log);
                }
            }
        }