Пример #1
0
        private void ManageNewDownloadFiles()
        {
            int nb = _downloadClient.GetDownloadCount();

            if (nb >= _maxSimultaneousDownload)
            {
                return;
            }
            foreach (DownloadFile_v1 <TKey> downloadFile in _mongoQueueDownloadFileManager.Find("{ 'queueDownloadFile.state': 'WaitToDownload' }", sort: "{ _id: 1 }"))
            {
                if (downloadFile.state == DownloadState.WaitToDownload)
                {
                    if (!GetDownloadLink(downloadFile))
                    {
                        // no link found remove post from mongo queue
                        _mongoQueueDownloadFileManager.Remove(downloadFile.id);
                        pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - {1,-50} - {2,-25} - \"{3}\" - key {4}", DateTime.Now, "can't find download link", null, downloadFile.file, downloadFile.key);
                    }
                    else
                    {
                        // add download to download manager and save post to mongo queue
                        downloadFile.downloadId        = _downloadClient.AddDownload(downloadFile.downloadLink, downloadFile.file, startNow: true);
                        downloadFile.state             = DownloadState.DownloadStarted;
                        downloadFile.startDownloadTime = DateTime.Now;
                        _mongoQueueDownloadFileManager.Save(downloadFile.id, downloadFile);

                        if (!_downloadFiles.TryAdd(downloadFile.id, downloadFile))
                        {
                            pb.Trace.WriteLine("error adding downloadFile with id {0} to ConcurrentDictionary _downloadFiles (DownloadManager<TKey>.DownloadFile())", downloadFile.id);
                        }

                        if (++nb >= _maxSimultaneousDownload)
                        {
                            break;
                        }
                    }
                }
            }
        }
Пример #2
0
        public static bool Save(MongoCollectionManager_v1 <DownloadPostKey_v1, DownloadedFile_v2 <DownloadPostKey_v1> > mongoDownloadedFileManager, DownloadedFile_v2 <DownloadPostKey_v1> downloadedFile)
        {
            bool saved = false;

            if (mongoDownloadedFileManager.Load(downloadedFile.Key) == null)
            {
                downloadedFile.Id = mongoDownloadedFileManager.GetNewId();
                mongoDownloadedFileManager.Save(downloadedFile.Id, downloadedFile);
                saved = true;
            }
            //Trace.WriteLine("{0} : key {1}, file \"{2}\"", saved ? "saved    " : "not saved", downloadedFile.Key, downloadedFile.UncompressFiles != null ? downloadedFile.UncompressFiles.FirstOrDefault() : "null");
            Trace.WriteLine("{0} : key {1}, file \"{2}\"", saved ? "saved    " : "not saved", downloadedFile.Key,
                            downloadedFile.DownloadItemLinks != null ? downloadedFile.DownloadItemLinks.FirstOrDefault().ServerLinks.FirstOrDefault().FilePartLinks.FirstOrDefault().DownloadedFile : "null");
            return(true);
        }
Пример #3
0
        private void EndDownloadFile(DownloadFile_v1 <TKey> downloadFile)
        {
            try
            {
                DownloadedFile_v1 <TKey> downloadedFile = new DownloadedFile_v1 <TKey>
                {
                    key               = downloadFile.key,
                    downloadLinks     = downloadFile.downloadLinks,
                    downloadLink      = downloadFile.downloadLink,
                    file              = downloadFile.file,
                    state             = downloadFile.state,
                    requestTime       = downloadFile.requestTime,
                    startDownloadTime = downloadFile.startDownloadTime,
                    endDownloadTime   = DateTime.Now,
                    downloadDuration  = DateTime.Now - downloadFile.startDownloadTime
                };
                string downloadedPath    = _downloadClient.GetDownloadLocalFileById(downloadFile.downloadId);
                string downloadDirectory = zPath.GetDirectoryName(downloadFile.file);
                //if (_trace)
                //{
                //    pb.Trace.WriteLine("ManageEndDownloadFiles()                      : downloadFile.file : \"{0}\"", downloadFile.file);
                //    pb.Trace.WriteLine("ManageEndDownloadFiles()                      : _downloadClient.GetDownloadLocalFileById() : \"{0}\"", downloadedPath);
                //}
                downloadedFile.downloadedFile = zpath.PathSetDirectory(downloadedPath, downloadDirectory);
                downloadedFile.id             = _mongoDownloadedFileManager.GetNewId();

                _mongoDownloadedFileManager.Save(downloadedFile.id, downloadedFile);
                _mongoQueueDownloadFileManager.Remove(downloadFile.id);
                _downloadClient.RemoveDownloadById(downloadFile.downloadId);

                // _uncompressFile
                if (downloadFile.state == DownloadState.DownloadCompleted && _uncompressManager != null && CompressManager.IsCompressFile(downloadedPath))
                {
                    TaskManager.AddTask(new Task
                    {
                        name = "Uncompress download file",
                        task = () =>
                        {
                            UncompressFile(downloadedPath, downloadedFile, downloadDirectory);
                            _mongoDownloadedFileManager.Save(downloadedFile.id, downloadedFile);
                        }
                    });
                    if (_onDownloaded != null)
                    {
                        TaskManager.AddTask(new Task {
                            name = "onDownloaded", task = () => _onDownloaded(downloadedFile)
                        });
                    }
                }
                else if (_onDownloaded != null)
                {
                    _onDownloaded(downloadedFile);
                }

                DownloadFile_v1 <TKey> downloadFile2;
                if (!_downloadFiles.TryRemove(downloadFile.id, out downloadFile2))
                {
                    pb.Trace.WriteLine("error unable to remove downloadFile with id {0} from ConcurrentDictionary _downloadFiles (DownloadManager<TKey>.ManageEndDownloadFiles())", downloadFile.id);
                }
            }
            catch (Exception exception)
            {
                pb.Trace.WriteLine("error in DownloadManager_v1.EndDownloadFile() : {0}", exception.Message);
                pb.Trace.WriteLine(exception.StackTrace);
            }
        }