Beispiel #1
0
 public static LocalFile GetFile(string url)
 {
     if (string.IsNullOrWhiteSpace(url))
     {
         return(null);
     }
     try
     {
         var file = SL.GetItems <LocalFile>(x => x.Url == url).FirstOrDefault();
         if (string.IsNullOrWhiteSpace(file?.Path) || !Exists(file.Path))
         {
             Task.Run(async() =>
             {
                 await DownloadFile(url);
             });
             return(null);
         }
         else
         {
             return(file);
         }
     }
     catch (Exception ex)
     {
         SL.Report("Error GetFile()", ex);
         return(null);
     }
 }
Beispiel #2
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;
            }
        }