Example #1
0
        /// <summary>
        /// 是否存在于下载列表中
        /// </summary>
        /// <param name="downloading"></param>
        /// <returns></returns>
        private async Task <bool> IsExist(DownloadingItem downloading)
        {
            bool isExist = downloadingList.Contains(downloading);

            if (isExist)
            {
                return(true);
            }
            else
            {
                // 先恢复为waiting状态,暂停状态下Remove会导致文件重新下载,原因暂不清楚
                await AriaClient.UnpauseAsync(downloading.Downloading.Gid);

                // 移除下载项
                var ariaRemove = await AriaClient.RemoveAsync(downloading.Downloading.Gid);

                if (ariaRemove == null || ariaRemove.Result == downloading.Downloading.Gid)
                {
                    // 从内存中删除下载项
                    await AriaClient.RemoveDownloadResultAsync(downloading.Downloading.Gid);
                }

                return(false);
            }
        }
Example #2
0
        public DownloadStatus GetDownloadStatus(string gid)
        {
            string filePath = "";

            while (true)
            {
                var status = AriaClient.TellStatus(gid);
                if (status == null || status.Result == null)
                {
                    continue;
                }

                if (status.Result.Result == null && status.Result.Error != null)
                {
                    if (status.Result.Error.Message.Contains("is not found"))
                    {
                        OnDownloadFinish(false, null, gid, status.Result.Error.Message);
                        return(DownloadStatus.ABORT);
                    }
                }

                if (status.Result.Result.Files != null && status.Result.Result.Files.Count >= 1)
                {
                    filePath = status.Result.Result.Files[0].Path;
                }

                long totalLength     = long.Parse(status.Result.Result.TotalLength);
                long completedLength = long.Parse(status.Result.Result.CompletedLength);
                long speed           = long.Parse(status.Result.Result.DownloadSpeed);
                // 回调
                OnTellStatus(totalLength, completedLength, speed, gid);

                if (status.Result.Result.Status == "complete")
                {
                    break;
                }
                if (status.Result.Result.ErrorCode != null && status.Result.Result.ErrorCode != "0")
                {
                    Console.WriteLine("ErrorMessage: " + status.Result.Result.ErrorMessage);

                    //// 如果返回状态码不是200,则继续
                    //if (status.Result.Result.ErrorMessage.Contains("The response status is not successful"))
                    //{
                    //    Thread.Sleep(1000);
                    //    continue;
                    //}

                    // aira中删除记录
                    var ariaRemove1 = AriaClient.RemoveDownloadResultAsync(gid);
                    Console.WriteLine(ariaRemove1);

                    // 返回回调信息,退出函数
                    OnDownloadFinish(false, null, gid, status.Result.Result.ErrorMessage);
                    return(DownloadStatus.FAILED);
                }

                // 降低CPU占用
                Thread.Sleep(500);
            }
            OnDownloadFinish(true, filePath, gid, null);
            return(DownloadStatus.SUCCESS);
        }
Example #3
0
        /// <summary>
        /// 获取gid下载项的状态
        ///
        /// TODO
        /// 对于下载的不同状态的返回值的测试
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public DownloadResult GetDownloadStatus(string gid, Action action = null)
        {
            string filePath = "";

            while (true)
            {
                Task <AriaTellStatus> status = AriaClient.TellStatus(gid);
                if (status == null || status.Result == null)
                {
                    continue;
                }

                if (status.Result.Result == null && status.Result.Error != null)
                {
                    if (status.Result.Error.Message.Contains("is not found"))
                    {
                        OnDownloadFinish(false, null, gid, status.Result.Error.Message);
                        return(DownloadResult.ABORT);
                    }
                }

                if (status.Result.Result.Files != null && status.Result.Result.Files.Count >= 1)
                {
                    filePath = status.Result.Result.Files[0].Path;
                }

                long totalLength     = long.Parse(status.Result.Result.TotalLength);
                long completedLength = long.Parse(status.Result.Result.CompletedLength);
                long speed           = long.Parse(status.Result.Result.DownloadSpeed);

                // 回调
                OnTellStatus(totalLength, completedLength, speed, gid);

                // 在外部执行
                if (action != null)
                {
                    action.Invoke();
                }

                if (status.Result.Result.Status == "complete")
                {
                    break;
                }
                if (status.Result.Result.ErrorCode != null && status.Result.Result.ErrorCode != "0")
                {
                    if (status.Result != null)
                    {
                        Utils.Debugging.Console.PrintLine("ErrorMessage: " + status.Result.Result.ErrorMessage);
                        LogManager.Error("AriaManager", status.Result.Result.ErrorMessage);
                    }

                    //// 如果返回状态码不是200,则继续
                    //if (status.Result.Result.ErrorMessage.Contains("The response status is not successful"))
                    //{
                    //    Thread.Sleep(1000);
                    //    continue;
                    //}

                    // aira中删除记录
                    Task <AriaRemove> ariaRemove1 = AriaClient.RemoveDownloadResultAsync(gid);
                    Utils.Debugging.Console.PrintLine(ariaRemove1);
                    if (ariaRemove1.Result != null)
                    {
                        LogManager.Debug("AriaManager", ariaRemove1.Result.Result);
                    }

                    // 返回回调信息,退出函数
                    OnDownloadFinish(false, null, gid, status.Result.Result.ErrorMessage);
                    return(DownloadResult.FAILED);
                }

                // 降低CPU占用
                Thread.Sleep(100);
            }
            OnDownloadFinish(true, filePath, gid, null);
            return(DownloadResult.SUCCESS);
        }