Пример #1
0
        //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);
        }
Пример #2
0
        //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);
        }
Пример #3
0
        private void ManageEndDownloadFiles()
        {
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 01 begin");
            }
            foreach (int downloadId in _currentDownloadFiles.Keys.ToArray())
            {
                DownloadLinkRef downloadLinkRef = _currentDownloadFiles[downloadId];
                DownloaderState state           = _downloadClient.GetDownloadStateById(downloadLinkRef.DownloadId);
                if (state == DownloaderState.Ended || state == DownloaderState.EndedWithError || state == DownloaderState.UnknowDownload)
                {
                    QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                    downloadFile.Modified = true;
                    DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef);
                    if (state == DownloaderState.Ended)
                    {
                        filePartLink.State = DownloadState.DownloadCompleted;
                    }
                    else
                    {
                        downloadFile.UncompleteDownload = true;
                        DownloadItemLink itemLink = GetDownloadItemLink(downloadLinkRef);
                        itemLink.UncompleteDownload = true;
                        filePartLink.State          = DownloadState.DownloadFailed;
                    }

                    EndDownloadFilePart(downloadId);
                }
            }
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 06 end");
            }
        }
Пример #4
0
        private void EndDownloadFilePart(int downloadId)
        {
            try
            {
                DownloadLinkRef      downloadLinkRef   = _currentDownloadFiles[downloadId];
                QueueDownloadFile    queueDownloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                DownloadFilePartLink filePartLink      = GetDownloadFilePartLink(downloadLinkRef);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 02                   : end download file : state {0} file \"{1}\"", filePartLink.State, filePartLink.File);
                }

                filePartLink.DownloadedPath = _downloadClient.GetDownloadLocalFileById(downloadLinkRef.DownloadId);
                string file = filePartLink.DownloadedPath;
                if (queueDownloadFile.Directory != null)
                {
                    file = zpath.PathSetDirectory(file, queueDownloadFile.Directory);
                }
                filePartLink.DownloadedFile        = file;
                filePartLink.EndDownloadTime       = DateTime.Now;
                filePartLink.DownloadDuration      = filePartLink.EndDownloadTime - filePartLink.StartDownloadTime;
                queueDownloadFile.EndDownloadTime  = filePartLink.EndDownloadTime;
                queueDownloadFile.DownloadDuration = queueDownloadFile.EndDownloadTime - queueDownloadFile.StartDownloadTime;

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 03                   : _currentDownloadFiles.Remove({0})", downloadId);
                }
                _currentDownloadFiles.Remove(downloadId);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 04                   : _mongoCurrentDownloadFileManager.Remove({0})", downloadLinkRef.DownloadId);
                }
                _mongoCurrentDownloadFileManager.Remove(downloadLinkRef.DownloadId);

                queueDownloadFile.DownloadNbInProgress--;

                _downloadClient.RemoveDownloadById(downloadLinkRef.DownloadId);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 05                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile.Id, queueDownloadFile.DownloadNbInProgress, queueDownloadFile.AllDownloadLinkTreated, queueDownloadFile.Directory, queueDownloadFile.Filename);
                }
                if (queueDownloadFile.DownloadNbInProgress == 0 && queueDownloadFile.AllDownloadLinkTreated)
                {
                    EndDownload(queueDownloadFile);
                }
                else
                {
                    SaveQueueDownloadFile(queueDownloadFile);
                }
            }
            catch (Exception exception)
            {
                pb.Trace.WriteLine("error in DownloadManager.EndDownloadFilePart() : {0}", exception.Message);
                pb.Trace.WriteLine(exception.StackTrace);
            }
        }
Пример #5
0
 private void SaveQueueDownloadFile(QueueDownloadFile downloadFile)
 {
     if (downloadFile.Modified)
     {
         _mongoQueueDownloadFileManager.Save(downloadFile.Id, downloadFile);
         downloadFile.Modified = false;
     }
 }
Пример #6
0
        private QueueDownloadFile GetQueueDownloadFile(BsonValue id)
        {
            QueueDownloadFile downloadFile = null;

            if (!_queueDownloadFiles.TryGetValue(id, out downloadFile))
            {
                pb.Trace.WriteLine("error get QueueDownloadFile id {0} from ConcurrentDictionary _queueDownloadFiles", id);
                return(null);
            }
            return(downloadFile);
        }
Пример #7
0
        private DownloadFilePartLink GetDownloadFilePartLink(DownloadLinkRef downloadLinkRef)
        {
            QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);

            if (downloadFile != null)
            {
                return(downloadFile.DownloadItemLinks[downloadLinkRef.ItemIndex].ServerLinks[downloadLinkRef.ServerIndex].FilePartLinks[downloadLinkRef.FilePartIndex]);
            }
            else
            {
                return(null);
            }
        }
Пример #8
0
        private bool DebridLink(QueueDownloadFile queueDownloadFile, DownloadItemLink itemLink, DownloadServerLink serverLink, DownloadFilePartLink filePartLink, out string debridedLink, out string file)
        {
            if (!filePartLink.Debrided)
            {
                // http://s19.alldebrid.com/dl/f3nmdg2f05/Herc-FULLBluRay.part01.rar
                debridedLink               = _debrider.DebridLink(filePartLink.DownloadLink);
                filePartLink.Debrided      = true;
                queueDownloadFile.Modified = true;
                if (debridedLink != null)
                {
                    filePartLink.DebridedDownloadLink = debridedLink;

                    //file = queueDownloadFile.File;
                    file = queueDownloadFile.Filename;
                    string urlFileName = zPath.GetFileName(zurl.GetAbsolutePath(debridedLink));
                    if (file == null)
                    {
                        file = zPath.GetFileNameWithoutExtension(urlFileName);
                    }

                    if (queueDownloadFile.DownloadItemLinks.Length > 1)
                    {
                        file += "_" + itemLink.Name;
                    }
                    if (serverLink.FilePartLinks.Length > 1)
                    {
                        //file += ZipManager.GetZipFilePartName(zurl.GetFileName(debridedLink));
                        //file += ZipManager.GetZipFilePartName(urlFileName);
                        file += ZipArchive.GetZipFilePartName(urlFileName);
                    }
                    //file += zurl.GetExtension(debridedLink);
                    file += zPath.GetExtension(urlFileName);
                    if (queueDownloadFile.Directory != null)
                    {
                        file = zPath.Combine(queueDownloadFile.Directory, file);
                    }
                    filePartLink.File = file;

                    if (_trace)
                    {
                        pb.Trace.WriteLine("DownloadManager.DebridLink() 01                               : _debrider.DebridLink(\"{0}\") : \"{1}\"", filePartLink.DownloadLink, debridedLink);
                    }

                    return(true);
                }
            }
            debridedLink = null;
            file         = null;
            return(false);
        }
Пример #9
0
        public DownloadState GetDownloadFileState(ServerKey key)
        {
            DownloadedFile downloadedFile = GetDownloadedFile(key);

            if (downloadedFile != null)
            {
                return(downloadedFile.State);
            }
            QueueDownloadFile queueDownloadFile = GetQueueDownloadFile(key);

            if (queueDownloadFile != null)
            {
                return(DownloadState.WaitToDownload);
            }
            return(DownloadState.NotDownloaded);
        }
Пример #10
0
        public void AddFileToDownload(ServerKey key, IRequestDownloadLinks downloadLinks, string file = null)
        {
            DownloadItemLink[] downloadItemLinks = DownloadItemLink.CreateDownloadItemLinkArray(downloadLinks);

            QueueDownloadFile downloadFile = new QueueDownloadFile
            {
                Key = key,
                DownloadItemLinks                       = downloadItemLinks,
                RequestTime                             = DateTime.Now,
                Directory                               = file != null?zPath.GetDirectoryName(file) : null,
                                               Filename = file != null?zPath.GetFileName(file) : null
            };

            downloadFile.Id       = _mongoQueueDownloadFileManager.GetNewId();
            downloadFile.Modified = true;
            SaveQueueDownloadFile(downloadFile);
        }
Пример #11
0
 private bool ContainsCompressFiles(QueueDownloadFile downloadFile)
 {
     foreach (DownloadItemLink itemLink in downloadFile.DownloadItemLinks)
     {
         if (itemLink.SelectedServerIndex != -1)
         {
             foreach (DownloadFilePartLink filePartLink in itemLink.ServerLinks[itemLink.SelectedServerIndex].FilePartLinks)
             {
                 //if (CompressManager.IsCompressFile(filePartLink.DownloadedPath))
                 if (_uncompressManager.CompressManager.IsCompressFile(filePartLink.DownloadedPath))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Пример #12
0
        private bool UnprotectLink(QueueDownloadFile downloadFile, DownloadServerLink serverLink, DownloadFilePartLink filePartLink, int filePartLinkIndex)
        {
            if (_protectLink != null && _protectLink.IsLinkProtected(filePartLink.DownloadLink))
            {
                string[] links = _protectLink.UnprotectLink(filePartLink.DownloadLink);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.UnprotectLink() 01                            : _protectLink.UnprotectLink(\"{0}\") : {1}", filePartLink.DownloadLink, links.zToStringValues());
                }

                if (links == null || links.Length == 0)
                {
                    pb.Trace.WriteLine("error unable to unprotect link \"{0}\"", filePartLink.DownloadLink);
                    return(false);
                }
                downloadFile.Modified = true;
                filePartLink.ProtectedDownloadLink = filePartLink.DownloadLink;
                filePartLink.DownloadLink          = links[0];
                if (links.Length > 1)
                {
                    List <DownloadFilePartLink> filePartLinks = new List <DownloadFilePartLink>();
                    for (int i = 0; i <= filePartLinkIndex; i++)
                    {
                        filePartLinks.Add(serverLink.FilePartLinks[i]);
                    }
                    for (int i = 1; i < links.Length; i++)
                    {
                        DownloadFilePartLink filePartLink2 = new DownloadFilePartLink();
                        filePartLink2.Downloaded   = false;
                        filePartLink2.DownloadLink = links[i];
                        filePartLink2.State        = DownloadState.NotDownloaded;
                        filePartLinks.Add(filePartLink2);
                    }
                    for (int i = filePartLinkIndex + 1; i < serverLink.FilePartLinks.Length; i++)
                    {
                        filePartLinks.Add(serverLink.FilePartLinks[i]);
                    }
                    serverLink.FilePartLinks = filePartLinks.ToArray();
                }
            }
            return(true);
        }
Пример #13
0
        private void ControlDownloadFiles()
        {
            foreach (int downloadId in _currentDownloadFiles.Keys.ToArray())
            {
                DownloadLinkRef downloadLinkRef = _currentDownloadFiles[downloadId];
                DownloaderState state           = _downloadClient.GetDownloadStateById(downloadLinkRef.DownloadId);
                if (state == DownloaderState.NeedToPrepare)
                {
                    if (_downloadClient.GetDownloadRetryCountById(downloadLinkRef.DownloadId) == _downloadClient.GetMaxRetryCount())
                    {
                        QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                        downloadFile.Modified = true;
                        DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef);

                        downloadFile.UncompleteDownload = true;
                        DownloadItemLink itemLink = GetDownloadItemLink(downloadLinkRef);
                        itemLink.UncompleteDownload = true;
                        filePartLink.State          = DownloadState.DownloadFailed;

                        EndDownloadFilePart(downloadId);
                    }
                }
            }
        }
Пример #14
0
        private DownloadLinkRef GetNextDownloadLink(QueueDownloadFile queueDownloadFile)
        {
            if (queueDownloadFile.AllDownloadLinkTreated)
            {
                return(null);
            }
            int itemIndex = 0;

            foreach (DownloadItemLink itemLink in queueDownloadFile.DownloadItemLinks)
            {
                if (!itemLink.Downloaded)
                {
                    if (!itemLink.NoDownloadLinkFound)
                    {
                        if (itemLink.SelectedServerIndex == -1)
                        {
                            var q           = (from server in itemLink.ServerLinks select new { server = server, rate = DownloadFileServerInfo.GetLinkRate(server.Name) }).OrderBy(server => server.rate).Select(server => server.server);
                            int serverIndex = 0;
                            foreach (DownloadServerLink serverLink in q)
                            {
                                DownloadFilePartLink filePartLink = serverLink.FilePartLinks[0];
                                if (!UnprotectLink(queueDownloadFile, serverLink, filePartLink, 0))
                                {
                                    continue;
                                }
                                string debridedLink = null;
                                string file         = null;
                                if (DebridLink(queueDownloadFile, itemLink, serverLink, filePartLink, out debridedLink, out file))
                                {
                                    itemLink.SelectedServerIndex = serverIndex;
                                    queueDownloadFile.Modified   = true;
                                    return(new DownloadLinkRef
                                    {
                                        QueueDownloadFileId = queueDownloadFile.Id, ItemIndex = itemIndex, ServerIndex = serverIndex, FilePartIndex = 0, DebridedDownloadLink = debridedLink, File = file
                                    });
                                }
                                serverIndex++;
                            }
                            itemLink.NoDownloadLinkFound         = true;
                            queueDownloadFile.UncompleteDownload = true;
                            queueDownloadFile.Modified           = true;
                            string message = string.Format("can't find download link for item no {0}", itemIndex + 1);
                            //pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - file \"{3}\" key {4}", DateTime.Now, message, null, queueDownloadFile.File, queueDownloadFile.Key);
                            pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - directory \"{3}\" filename \"{4}\" key {5}", DateTime.Now, message, null, queueDownloadFile.Directory, queueDownloadFile.Filename, queueDownloadFile.Key);
                        }
                        else
                        {
                            DownloadServerLink serverLink = itemLink.ServerLinks[itemLink.SelectedServerIndex];
                            // dont use foreach because UnprotectLink() may change serverLink.FilePartLinks
                            for (int filePartIndex = 0; filePartIndex < serverLink.FilePartLinks.Length; filePartIndex++)
                            {
                                DownloadFilePartLink filePartLink = serverLink.FilePartLinks[filePartIndex];
                                if (filePartLink.State != DownloadState.NotDownloaded)
                                {
                                    continue;
                                }
                                string errorMessage = null;
                                if (UnprotectLink(queueDownloadFile, serverLink, filePartLink, filePartIndex))
                                {
                                    string debridedLink = null;
                                    string file         = null;
                                    if (DebridLink(queueDownloadFile, itemLink, serverLink, filePartLink, out debridedLink, out file))
                                    {
                                        return(new DownloadLinkRef {
                                            QueueDownloadFileId = queueDownloadFile.Id, ItemIndex = itemIndex, ServerIndex = itemLink.SelectedServerIndex, FilePartIndex = filePartIndex, DebridedDownloadLink = debridedLink, File = file
                                        });
                                    }
                                    else
                                    {
                                        errorMessage = "can't find download link";
                                    }
                                }
                                else
                                {
                                    errorMessage = "error unable to unprotect link";
                                }
                                if (errorMessage != null)
                                {
                                    queueDownloadFile.UncompleteDownload = true;
                                    queueDownloadFile.Modified           = true;
                                    errorMessage = errorMessage + string.Format(" for item no {0} server no {1} part no {2}", itemIndex + 1, itemLink.SelectedServerIndex + 1, filePartIndex + 1);
                                    //pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - link \"{3}\" file \"{4}\" key {5}", DateTime.Now, errorMessage, null, filePartLink.DownloadLink, queueDownloadFile.File, queueDownloadFile.Key);
                                    pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - link \"{3}\" directory \"{4}\" filename \"{5}\" key {6}", DateTime.Now, errorMessage, null, filePartLink.DownloadLink, queueDownloadFile.Directory, queueDownloadFile.Filename, queueDownloadFile.Key);
                                }
                            }
                        }
                    }
                }
                itemIndex++;
            }
            queueDownloadFile.AllDownloadLinkTreated = true;
            queueDownloadFile.Modified = true;
            return(null);
        }
Пример #15
0
 private void SaveQueueDownloadFile(QueueDownloadFile downloadFile)
 {
     if (downloadFile.Modified)
     {
         _mongoQueueDownloadFileManager.Save(downloadFile.Id, downloadFile);
         downloadFile.Modified = false;
     }
 }
Пример #16
0
        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");
            }
        }
Пример #17
0
        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);
            }
        }
Пример #18
0
        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 Task
                    {
                        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 Task { 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);
            }
        }
Пример #19
0
        private DownloadLinkRef GetNextDownloadLink(QueueDownloadFile queueDownloadFile)
        {
            if (queueDownloadFile.AllDownloadLinkTreated)
                return null;
            int itemIndex = 0;
            foreach (DownloadItemLink itemLink in queueDownloadFile.DownloadItemLinks)
            {
                if (!itemLink.Downloaded)
                {
                    if (!itemLink.NoDownloadLinkFound)
                    {
                        if (itemLink.SelectedServerIndex == -1)
                        {
                            var q = (from server in itemLink.ServerLinks select new { server = server, rate = DownloadFileServerInfo.GetLinkRate(server.Name) }).OrderBy(server => server.rate).Select(server => server.server);
                            int serverIndex = 0;
                            foreach (DownloadServerLink serverLink in q)
                            {
                                DownloadFilePartLink filePartLink = serverLink.FilePartLinks[0];
                                if (!UnprotectLink(queueDownloadFile, serverLink, filePartLink, 0))
                                    continue;
                                string debridedLink = null;
                                string file = null;
                                if (DebridLink(queueDownloadFile, itemLink, serverLink, filePartLink, out debridedLink, out file))
                                {
                                    itemLink.SelectedServerIndex = serverIndex;
                                    queueDownloadFile.Modified = true;
                                    return new DownloadLinkRef
                                        { QueueDownloadFileId = queueDownloadFile.Id, ItemIndex = itemIndex, ServerIndex = serverIndex, FilePartIndex = 0, DebridedDownloadLink = debridedLink, File = file };
                                }
                                serverIndex++;
                            }
                            itemLink.NoDownloadLinkFound = true;
                            queueDownloadFile.UncompleteDownload = true;
                            queueDownloadFile.Modified = true;
                            string message = string.Format("can't find download link for item no {0}", itemIndex + 1);
                            //pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - file \"{3}\" key {4}", DateTime.Now, message, null, queueDownloadFile.File, queueDownloadFile.Key);
                            pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - directory \"{3}\" filename \"{4}\" key {5}", DateTime.Now, message, null, queueDownloadFile.Directory, queueDownloadFile.Filename, queueDownloadFile.Key);
                        }
                        else
                        {
                            DownloadServerLink serverLink = itemLink.ServerLinks[itemLink.SelectedServerIndex];
                            // dont use foreach because UnprotectLink() may change serverLink.FilePartLinks
                            for (int filePartIndex = 0; filePartIndex < serverLink.FilePartLinks.Length; filePartIndex++)
                            {
                                DownloadFilePartLink filePartLink = serverLink.FilePartLinks[filePartIndex];
                                if (filePartLink.State != DownloadState.NotDownloaded)
                                    continue;
                                string errorMessage = null;
                                if (UnprotectLink(queueDownloadFile, serverLink, filePartLink, filePartIndex))
                                {

                                    string debridedLink = null;
                                    string file = null;
                                    if (DebridLink(queueDownloadFile, itemLink, serverLink, filePartLink, out debridedLink, out file))
                                    {
                                        return new DownloadLinkRef { QueueDownloadFileId = queueDownloadFile.Id, ItemIndex = itemIndex, ServerIndex = itemLink.SelectedServerIndex, FilePartIndex = filePartIndex, DebridedDownloadLink = debridedLink, File = file };
                                    }
                                    else
                                    {
                                        errorMessage = "can't find download link";
                                    }
                                }
                                else
                                {
                                    errorMessage = "error unable to unprotect link";
                                }
                                if (errorMessage != null)
                                {
                                    queueDownloadFile.UncompleteDownload = true;
                                    queueDownloadFile.Modified = true;
                                    errorMessage = errorMessage + string.Format(" for item no {0} server no {1} part no {2}", itemIndex + 1, itemLink.SelectedServerIndex + 1, filePartIndex + 1);
                                    //pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - link \"{3}\" file \"{4}\" key {5}", DateTime.Now, errorMessage, null, filePartLink.DownloadLink, queueDownloadFile.File, queueDownloadFile.Key);
                                    pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - link \"{3}\" directory \"{4}\" filename \"{5}\" key {6}", DateTime.Now, errorMessage, null, filePartLink.DownloadLink, queueDownloadFile.Directory, queueDownloadFile.Filename, queueDownloadFile.Key);
                                }
                            }
                        }
                    }
                }
                itemIndex++;
            }
            queueDownloadFile.AllDownloadLinkTreated = true;
            queueDownloadFile.Modified = true;
            return null;
        }
Пример #20
0
        private bool UnprotectLink(QueueDownloadFile downloadFile, DownloadServerLink serverLink, DownloadFilePartLink filePartLink, int filePartLinkIndex)
        {
            if (_protectLink != null && _protectLink.IsLinkProtected(filePartLink.DownloadLink))
            {
                string[] links = _protectLink.UnprotectLink(filePartLink.DownloadLink);

                if (_trace)
                    pb.Trace.WriteLine("DownloadManager.UnprotectLink() 01                            : _protectLink.UnprotectLink(\"{0}\") : {1}", filePartLink.DownloadLink, links.zToStringValues());

                if (links == null || links.Length == 0)
                {
                    pb.Trace.WriteLine("error unable to unprotect link \"{0}\"", filePartLink.DownloadLink);
                    return false;
                }
                downloadFile.Modified = true;
                filePartLink.ProtectedDownloadLink = filePartLink.DownloadLink;
                filePartLink.DownloadLink = links[0];
                if (links.Length > 1)
                {
                    List<DownloadFilePartLink> filePartLinks = new List<DownloadFilePartLink>();
                    for (int i = 0; i <= filePartLinkIndex; i++)
                        filePartLinks.Add(serverLink.FilePartLinks[i]);
                    for (int i = 1; i < links.Length; i++)
                    {
                        DownloadFilePartLink filePartLink2 = new DownloadFilePartLink();
                        filePartLink2.Downloaded = false;
                        filePartLink2.DownloadLink = links[i];
                        filePartLink2.State = DownloadState.NotDownloaded;
                        filePartLinks.Add(filePartLink2);
                    }
                    for (int i = filePartLinkIndex + 1; i < serverLink.FilePartLinks.Length; i++)
                        filePartLinks.Add(serverLink.FilePartLinks[i]);
                    serverLink.FilePartLinks = filePartLinks.ToArray();
                }
            }
            return true;
        }
Пример #21
0
        private bool DebridLink(QueueDownloadFile queueDownloadFile, DownloadItemLink itemLink, DownloadServerLink serverLink, DownloadFilePartLink filePartLink, out string debridedLink, out string file)
        {
            if (!filePartLink.Debrided)
            {
                // http://s19.alldebrid.com/dl/f3nmdg2f05/Herc-FULLBluRay.part01.rar
                debridedLink = _debrider.DebridLink(filePartLink.DownloadLink);
                filePartLink.Debrided = true;
                queueDownloadFile.Modified = true;
                if (debridedLink != null)
                {
                    filePartLink.DebridedDownloadLink = debridedLink;

                    //file = queueDownloadFile.File;
                    file = queueDownloadFile.Filename;
                    string urlFileName = zPath.GetFileName(zurl.GetAbsolutePath(debridedLink));
                    if (file == null)
                        file = zPath.GetFileNameWithoutExtension(urlFileName);

                    if (queueDownloadFile.DownloadItemLinks.Length > 1)
                        file += "_" + itemLink.Name;
                    if (serverLink.FilePartLinks.Length > 1)
                        //file += ZipManager.GetZipFilePartName(zurl.GetFileName(debridedLink));
                        //file += ZipManager.GetZipFilePartName(urlFileName);
                        file += ZipArchive.GetZipFilePartName(urlFileName);
                    //file += zurl.GetExtension(debridedLink);
                    file += zPath.GetExtension(urlFileName);
                    if (queueDownloadFile.Directory != null)
                        file = zPath.Combine(queueDownloadFile.Directory, file);
                    filePartLink.File = file;

                    if (_trace)
                        pb.Trace.WriteLine("DownloadManager.DebridLink() 01                               : _debrider.DebridLink(\"{0}\") : \"{1}\"", filePartLink.DownloadLink, debridedLink);

                    return true;
                }
            }
            debridedLink = null;
            file = null;
            return false;
        }
Пример #22
0
        public void AddFileToDownload(ServerKey key, IRequestDownloadLinks downloadLinks, string file = null)
        {
            DownloadItemLink[] downloadItemLinks = DownloadItemLink.CreateDownloadItemLinkArray(downloadLinks);

            QueueDownloadFile downloadFile = new QueueDownloadFile
            {
                Key = key,
                DownloadItemLinks = downloadItemLinks,
                RequestTime = DateTime.Now,
                Directory = file != null ? zPath.GetDirectoryName(file) : null,
                Filename = file != null ? zPath.GetFileName(file) : null
            };
            downloadFile.Id = _mongoQueueDownloadFileManager.GetNewId();
            downloadFile.Modified = true;
            SaveQueueDownloadFile(downloadFile);
        }
Пример #23
0
 private bool ContainsCompressFiles(QueueDownloadFile downloadFile)
 {
     foreach (DownloadItemLink itemLink in downloadFile.DownloadItemLinks)
     {
         if (itemLink.SelectedServerIndex != -1)
         {
             foreach (DownloadFilePartLink filePartLink in itemLink.ServerLinks[itemLink.SelectedServerIndex].FilePartLinks)
             {
                 //if (CompressManager.IsCompressFile(filePartLink.DownloadedPath))
                 if (_uncompressManager.CompressManager.IsCompressFile(filePartLink.DownloadedPath))
                     return true;
             }
         }
     }
     return false;
 }