Пример #1
0
        static async Task DownloadNext()
        {
            if (isDownloading || _downloadStack.Count == 0)
            {
                return;
            }
            isDownloading = true;
            var url = _downloadStack[0];

            try
            {
                var files = SL.GetItems <LocalFile>(x => x.Url == url);
                foreach (var file in files)
                {
                    try
                    {
                        if (Exists(file.Path))
                        {
                            DeleteFile(file.Path);
                        }
                        SL.DeleteItem(file);
                    }
                    catch (Exception) { }
                }
                var name      = Guid.NewGuid().ToString();
                var extension = Path.GetExtension(url);
                var filename  = name + extension;
                var path      = CachePath(filename);
                var pp        = PublicPath(filename);
                await DownloadAsync(url, path);

                var localFile = new LocalFile {
                    ID = name, Url = url, Path = path
                };
                var result = SL.SaveItem(localFile, true, true) != null;
                _downloadStack.Remove(url);
                isDownloading = false;

                DownloadNext();

                if (result)
                {
                    SendChanged(new Object(), new FileChangedArgs(localFile));
                }

                return;
            }
            catch (Exception ex)
            {
                SL.Report("Error DownloadFile()", ex);
                _downloadStack.Remove(url);
                isDownloading = false;
                DownloadNext();
                return;
            }
        }