//public static void QueueDownloadFile(string[] filePartLinks, string file = null) public static void QueueDownloadFile(MongoCollectionManager<QueueDownloadFile> mongoQueueDownloadFileManager, string[] filePartLinks, string directory = null, string filename = null) { //ServerKey key = null; //key = new ServerKey { }; //Uri uri = new Uri(url); //string file = uri.Segments[uri.Segments.Length - 1]; //IRequestDownloadLinks downloadLinks = null; //DownloadItemLink[] downloadItemLinks = DownloadItemLink.CreateDownloadItemLinkArray(downloadLinks); //string[] downloadLinks2 = null; //PostDownloadLinks postDownloadLinks = PostDownloadLinks.Create(downloadLinks2); //DownloadItemLink[] downloadItemLinks2 = DownloadItemLink.CreateDownloadItemLinkArray(postDownloadLinks); DownloadItemLink itemLink = DownloadItemLink.CreateDownloadItemLink(filePartLinks); // Key (ServerKey) of QueueDownloadFile is null, no save to _mongoDownloadedFileManager in DownloadManager QueueDownloadFile downloadFile = new QueueDownloadFile { DownloadItemLinks = new DownloadItemLink[] { itemLink }, RequestTime = DateTime.Now, Directory = directory, Filename = filename //Directory = file != null ? zPath.GetDirectoryName(file) : null, //Filename = file != null ? zPath.GetFileName(file) : null }; downloadFile.Id = mongoQueueDownloadFileManager.GetNewId(); mongoQueueDownloadFileManager.Save(downloadFile.Id, downloadFile); }
//public static void QueueDownloadFile(string[] filePartLinks, string file = null) public static void QueueDownloadFile(MongoCollectionManager <QueueDownloadFile> mongoQueueDownloadFileManager, string[] filePartLinks, string directory = null, string filename = null) { //ServerKey key = null; //key = new ServerKey { }; //Uri uri = new Uri(url); //string file = uri.Segments[uri.Segments.Length - 1]; //IRequestDownloadLinks downloadLinks = null; //DownloadItemLink[] downloadItemLinks = DownloadItemLink.CreateDownloadItemLinkArray(downloadLinks); //string[] downloadLinks2 = null; //PostDownloadLinks postDownloadLinks = PostDownloadLinks.Create(downloadLinks2); //DownloadItemLink[] downloadItemLinks2 = DownloadItemLink.CreateDownloadItemLinkArray(postDownloadLinks); DownloadItemLink itemLink = DownloadItemLink.CreateDownloadItemLink(filePartLinks); // Key (ServerKey) of QueueDownloadFile is null, no save to _mongoDownloadedFileManager in DownloadManager QueueDownloadFile downloadFile = new QueueDownloadFile { DownloadItemLinks = new DownloadItemLink[] { itemLink }, RequestTime = DateTime.Now, Directory = directory, Filename = filename //Directory = file != null ? zPath.GetDirectoryName(file) : null, //Filename = file != null ? zPath.GetFileName(file) : null }; downloadFile.Id = mongoQueueDownloadFileManager.GetNewId(); mongoQueueDownloadFileManager.Save(downloadFile.Id, downloadFile); }
private void SaveQueueDownloadFile(QueueDownloadFile downloadFile) { if (downloadFile.Modified) { _mongoQueueDownloadFileManager.Save(downloadFile.Id, downloadFile); downloadFile.Modified = false; } }
private void ManageNewDownloadFiles() { if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 01 begin"); } int nb = _downloadClient.GetDownloadCount(); if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 02 : search new download link : current download count {0}", nb); } if (nb >= _maxSimultaneousDownload) { if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end"); } return; } foreach (QueueDownloadFile queueDownloadFile in _mongoQueueDownloadFileManager.Find("{}", sort: "{ _id: 1 }")) { QueueDownloadFile queueDownloadFile2 = queueDownloadFile; if (_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id)) { queueDownloadFile2 = GetQueueDownloadFile(queueDownloadFile2.Id); } while (true) { if (!_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id)) { if (!_queueDownloadFiles.TryAdd(queueDownloadFile2.Id, queueDownloadFile2)) { pb.Trace.WriteLine("error adding QueueDownloadFile id {0} to ConcurrentDictionary _queueDownloadFiles", queueDownloadFile2.Id); } if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05 : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.File \"{2}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05 : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.Directory \"{2}\" queueDownloadFile.Filename \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.Directory, queueDownloadFile2.Filename); } } DownloadLinkRef downloadLinkRef = GetNextDownloadLink(queueDownloadFile2); if (downloadLinkRef == null) { if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03 : no more link to download for \"{0}\"", queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03 : no more link to download for \"{0}\" \"{1}\"", queueDownloadFile2.Directory, queueDownloadFile2.Filename); //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename); } // sauver dans mongo Downloaded _mongoDownloadedFileManager if (queueDownloadFile2.DownloadNbInProgress == 0) { EndDownload(queueDownloadFile2); } else { SaveQueueDownloadFile(queueDownloadFile2); } break; } else { downloadLinkRef.DownloadId = _downloadClient.AddDownload(downloadLinkRef.DebridedDownloadLink, downloadLinkRef.File, startNow: true); queueDownloadFile2.Modified = true; if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 06 : start download DownloadId {0} file \"{1}\"", downloadLinkRef.DownloadId, downloadLinkRef.File); } DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef); if (filePartLink != null) { filePartLink.State = DownloadState.DownloadStarted; filePartLink.StartDownloadTime = DateTime.Now; } if (queueDownloadFile2.StartDownloadTime == null) { queueDownloadFile2.StartDownloadTime = DateTime.Now; } queueDownloadFile2.DownloadNbInProgress++; SaveQueueDownloadFile(queueDownloadFile2); if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 08 : _currentDownloadFiles.Add({0})", downloadLinkRef.DownloadId); } _currentDownloadFiles.Add(downloadLinkRef.DownloadId, downloadLinkRef); _mongoCurrentDownloadFileManager.Save(BsonValue.Create(downloadLinkRef.DownloadId), downloadLinkRef); if (++nb >= _maxSimultaneousDownload) { break; } } } if (nb >= _maxSimultaneousDownload) { break; } } if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end"); } }
private void EndDownload(QueueDownloadFile queueDownloadFile) { try { if (_trace) { pb.Trace.WriteLine("DownloadManager.EndDownload() 01 : directory \"{0}\" filename \"{1}\"", queueDownloadFile.Directory, queueDownloadFile.Filename); } DownloadedFile downloadedFile = new DownloadedFile(); downloadedFile.Key = queueDownloadFile.Key; downloadedFile.File = zPath.Combine(queueDownloadFile.Directory, queueDownloadFile.Filename); if (queueDownloadFile.UncompleteDownload) { downloadedFile.State = DownloadState.DownloadFailed; } else { downloadedFile.State = DownloadState.DownloadCompleted; } downloadedFile.DownloadItemLinks = queueDownloadFile.DownloadItemLinks; downloadedFile.DownloadedFiles = queueDownloadFile.DownloadItemLinks.GetDownloadFilePartLinks().Select(filePartLink => filePartLink.DownloadedFile).ToArray(); downloadedFile.RequestTime = queueDownloadFile.RequestTime; downloadedFile.StartDownloadTime = queueDownloadFile.StartDownloadTime; downloadedFile.EndDownloadTime = queueDownloadFile.EndDownloadTime; downloadedFile.DownloadDuration = queueDownloadFile.DownloadDuration; if (downloadedFile.Key != null) { _mongoDownloadedFileManager.RemoveFromKey(downloadedFile.Key); downloadedFile.Id = _mongoDownloadedFileManager.GetNewId(); _mongoDownloadedFileManager.Save(downloadedFile.Id, downloadedFile); } _mongoQueueDownloadFileManager.Remove(queueDownloadFile.Id); // _uncompressFile if (_uncompressManager != null && ContainsCompressFiles(queueDownloadFile)) { TaskManager.AddTask(new zTask { name = "Uncompress download file", task = () => { string[] uncompressFiles = UncompressFiles(queueDownloadFile.DownloadItemLinks.GetDownloadFilePartLinks().Select(filePartLink => filePartLink.DownloadedPath).ToArray()); if (queueDownloadFile.Directory != null) { uncompressFiles = SetDirectoryFiles(uncompressFiles, queueDownloadFile.Directory); } downloadedFile.UncompressFiles = uncompressFiles; if (downloadedFile.Id != null) { _mongoDownloadedFileManager.Save(downloadedFile.Id, downloadedFile); } } }); if (_onDownloaded != null) { TaskManager.AddTask(new zTask { name = "onDownloaded", task = () => _onDownloaded(downloadedFile) }); } } else if (_onDownloaded != null) { _onDownloaded(downloadedFile); } QueueDownloadFile downloadFile2; if (!_queueDownloadFiles.TryRemove(queueDownloadFile.Id, out downloadFile2)) { pb.Trace.WriteLine("error unable to remove downloadFile with id {0} from ConcurrentDictionary _downloadFiles (DownloadManager<TKey>.ManageEndDownloadFiles())", queueDownloadFile.Id); } _mongoQueueDownloadFileManager.Remove(queueDownloadFile.Id); } catch (Exception exception) { pb.Trace.WriteLine("error in DownloadManager.EndDownload() : {0}", exception.Message); pb.Trace.WriteLine(exception.StackTrace); } }