Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        private void Downloaded(DownloadedFile downloadedFile)
        {
            string message = GetDownloadStateText2(downloadedFile.State);
            IPostToDownload post = null;

            if (downloadedFile.Key != null)
                post = LoadPost(downloadedFile.Key);
            //Trace.WriteLine("Downloaded : downloadedFile.Key {0}", downloadedFile.Key != null ? downloadedFile.Key.ToString() : "(null)");
            //Trace.WriteLine("Downloaded : post {0}", post != null ? post.ToString() : "(null)");

            SaveDownloadedFileInfo(post, downloadedFile);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(GetPostMessage(post, message));
            if (downloadedFile.DownloadedFiles != null)
            {
                foreach (string file in downloadedFile.DownloadedFiles)
                    sb.AppendLine(string.Format("  file : \"{0}\"", file));
            }
            if (downloadedFile.UncompressFiles != null)
            {
                foreach (string file in downloadedFile.UncompressFiles)
                    sb.AppendLine(string.Format("  uncompress file : \"{0}\"", file));
            }
            Trace.Write(sb.ToString());

            MailAddLine(GetPostMessage(post, message, formated: false));
            if (downloadedFile.DownloadedFiles != null)
            {
                foreach (string file in downloadedFile.DownloadedFiles)
                    MailAddLine(string.Format("  file : \"{0}\"", file));
            }
            if (downloadedFile.UncompressFiles != null)
            {
                foreach (string file in downloadedFile.UncompressFiles)
                    MailAddLine(string.Format("  uncompress file : \"{0}\"", file));
            }
        }
Exemplo n.º 4
0
        private void SaveDownloadedFileInfo(IPostToDownload post, DownloadedFile downloadedFile)
        {
            //if (_downloadDirectory == null || downloadedFile.File == null)
            //    return;
            //string file = zPath.Combine(_downloadDirectory, zPath.GetDirectoryName(downloadedFile.File), _infoDirectory, zPath.GetFileName(downloadedFile.File)) + _infoSuffixFile;

            if (_downloadDirectory == null || downloadedFile.DownloadedFiles.Length == 0)
                return;
            // save info file to sub-directory .i, file = .i\filename.i
            string file = downloadedFile.DownloadedFiles[0];
            //file = zPath.Combine(_downloadDirectory, zPath.GetDirectoryName(file), _infoDirectory, zPath.GetFileName(file)) + _infoSuffixFile;
            file = zPath.Combine(_downloadDirectory, InfoFile.GetInfoFile(file));

            zfile.CreateFileDirectory(file);
            BsonDocument postDocument;
            if (post != null)
                postDocument = post.ToBsonDocument();
            else
                postDocument = new BsonDocument();
            new BsonDocument { { "Post", postDocument }, { "DownloadedFile", downloadedFile.ToBsonDocument() } }.zSave(file);
        }
Exemplo n.º 5
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);
            }
        }