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); } }
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); } }