public async Task ProcessFile(BackgroundDownloadFile file)
        {
            if (!(await NativeAudioPlayer.VerifyMp3(file.Destination, true)))
            {
                LogManager.Shared.Log("Failed Download", file);
                Download(file);
                return;
            }
            Files.Remove(file.TrackId);
            if (TasksDictionary.ContainsKey(file.Id))
            {
                TasksDictionary.Remove(file.Id);
            }
            var destination = Path.Combine(Locations.MusicDir, Path.GetFileName(file.Destination));

            if (File.Exists(destination))
            {
                File.Delete(destination);
            }
            File.Move(file.Destination, destination);

            LogManager.Shared.Log("File Proccessed", file);
            await OfflineManager.Shared.TrackDownloaded(file.TrackId);

            NotificationManager.Shared.ProcDownloaderStarted();
        }
        async void Download(BackgroundDownloadFile file, int tryCount = 0)
        {
            try{
                if (Files.ContainsKey(file.TrackId))
                {
                    return;
                }
                if (session == null || session.Configuration == null)
                {
                    LogManager.Shared.Log("Null Session!!!!");
                }
                if (tryCount > 5)
                {
                    LogManager.Shared.Report(new Exception("Cannot download track. DownloadTask is null"));
                    return;
                }
                file.SessionId = session.Configuration.Identifier;
                using (var request = new NSMutableUrlRequest(new NSUrl(file.Url)))
                {
                    //request.CachePolicy = NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData;
                    if (file.Headers != null)
                    {
                        var headers = file.Headers.Where(x => !string.IsNullOrWhiteSpace(x.Key) && !string.IsNullOrWhiteSpace(x.Value)).ToList();
                        request.Headers = NSDictionary.FromObjectsAndKeys(headers.Select(x => (NSString)x.Value).ToArray(),
                                                                          headers.Select(x => (NSString)x.Key).ToArray());
                    }
                    else
                    {
                        LogManager.Shared.Log("Null Headers");
                    }
                    var downloadTask = session.CreateDownloadTask(request);
                    if (downloadTask == null)
                    {
                        session.InvalidateAndCancel();
                        session = InitBackgroundSession(SharedContainerIdentifier);
                        await Task.Delay(5000);

                        Download(file, tryCount++);
                        return;
                    }
                    downloadTask.TaskDescription = Newtonsoft.Json.JsonConvert.SerializeObject(file);
                    file.TaskId = (int)downloadTask.TaskIdentifier;
                    TasksDictionary[file.Id] = downloadTask;
                    downloadTask.Resume();
                    Files[file.TrackId] = file;
                }
                NotificationManager.Shared.ProcDownloaderStarted();
            }
            catch (Exception ex) {
                ex.Data["Try Count"] = tryCount;
                ex.Data["BackgroundDownloadFile"] = Newtonsoft.Json.JsonConvert.SerializeObject(file);
                LogManager.Shared.Report(ex);
            }
        }
예제 #3
0
        public async Task Download(Track track)
        {
            try
            {
                if (track == null)
                {
                    return;
                }
                if (Files.ContainsKey(track.Id))
                {
                    return;
                }

                LogManager.Shared.Log("Download Track", track);
                var url = await MusicManager.Shared.GetDownloadUrl(track);

                if (url == null)
                {
                    return;
                }
                if (File.Exists(Path.Combine(Locations.MusicDir, track.FileName)))
                {
                    await OfflineManager.Shared.TrackDownloaded(track.Id);

                    return;
                }
                var filePath = Path.Combine(Locations.TempRelative, track.FileName);
                var file     = new BackgroundDownloadFile
                {
                    TrackId     = track.Id,
                    Url         = url.Url,
                    Headers     = url.Headers,
                    Destination = filePath,
                };
                if (File.Exists(filePath))
                {
                    await ProcessFile(file);
                }
                else
                {
                    Download(file);
                }
            }
            catch (Exception ex)
            {
                LogManager.Shared.Report(ex);
            }
        }
        internal static void AddController(string url, BackgroundDownload controller)
        {
            lock (locker) {
                var list = GetControllers(url) ?? new List <BackgroundDownload>();
                list.Add(controller);
            }
            BackgroundDownloadFile file;

            if (!Files.TryGetValue(url, out file))
            {
                Files [url] = file = new BackgroundDownloadFile {
                    Destination = controller.Destination,
                    Url         = url,
                };
                saveState();
            }
        }
        async Task repairDownload(BackgroundDownloadFile file)
        {
            try
            {
                LogManager.Shared.Log("Repairing Download", file);
                var url = await MusicManager.Shared.GetDownloadUrl(file.Track);

                file.Url         = url.Url;
                file.Headers     = url.Headers;
                file.RetryCount += 1;
                Files.Remove(file.TrackId);
                Download(file);
            }
            catch (Exception ex)
            {
                ex.Data["MediaItem"] = file.TrackId;
                LogManager.Shared.Report(ex);
            }
        }
예제 #6
0
 public void Log(string message, BackgroundDownloadFile file,
                 [CallerMemberName] string memberName    = "",
                 [CallerFilePath] string sourceFilePath  = "",
                 [CallerLineNumber] int sourceLineNumber = 0)
 {
     try
     {
         var dictionary = new Dictionary <string, string>()
         {
             { "Error", file?.Error ?? "NULL" },
             { "Track Id", file?.TrackId ?? "NULL" },
             { "Song ID", file?.Track?.SongId ?? "NULL" },
             { "Method", memberName },
             { "File", CleansFilePath(sourceFilePath) },
             { "Line number", sourceLineNumber.ToString() },
         };
         TrackEvent(message, dictionary);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
 }
 public void Resume(BackgroundDownloadFile file)
 {
     App.ShowNotImplmented();
 }
 public void Cancel(BackgroundDownloadFile file)
 {
     App.ShowNotImplmented();
 }
 internal static void AddController(string url, BackgroundDownload controller)
 {
     lock (locker) {
         var list = GetControllers (url) ?? new List<BackgroundDownload>();
         list.Add (controller);
     }
     BackgroundDownloadFile file;
     if (!Files.TryGetValue (url, out file)) {
         Files [url] = file = new BackgroundDownloadFile {
             Destination = controller.Destination,
             Url = url,
         };
         saveState ();
     }
 }
 internal static void AddController(string url, BackgroundDownload controller)
 {
     lock (locker) {
         var list = GetControllers (url) ?? new List<BackgroundDownload>();
         list.Add (controller);
     }
     BackgroundDownloadFile file;
     if (!Files.TryGetValue (url, out file)) {
         Console.WriteLine ("Adding URL {0}", url);
         Files [url] = file = new BackgroundDownloadFile {
             Destination = MakeRelativePath(BaseDir, controller.Destination),
             Url = url,
             SessionId = controller.SessionId
         };
     }
     saveState ();
 }