protected virtual Task GetDownloadFileTask(string sourceFile, string tmpFile, CancellationToken ct) { var downloadFileTask = new Task(() => { if (!File.Exists(tmpFile)) { var service = ServiceProviderProxy.GetInstallerWebService(); RaiseOnProgressChangedEvent(0); RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage); Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, tmpFile)); long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { // Throw OperationCancelledException if there is an incoming cancel request ct.ThrowIfCancellationRequested(); content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(tmpFile, content); downloaded += content.Length; // Update download progress RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, string.Format(DownloadProgressMessage, downloaded / 1024, fileSize / 1024)); RaiseOnProgressChangedEvent(Convert.ToInt32((downloaded * 100) / fileSize)); if (content.Length < ChunkSize) { break; } } RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, "100%"); Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } }, ct); return(downloadFileTask); }
private void DownloadFile(string sourceFile, string destinationFile) { try { var service = ServiceProviderProxy.GetInstallerWebService(); // Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, destinationFile)); long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, string.Format(DownloadProgressMessage, downloaded / 1024, fileSize / 1024)); // RaiseOnProgressChangedEvent(Convert.ToInt32((downloaded * 100) / fileSize)); if (content.Length < ChunkSize) { break; } } // RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, "100%"); // Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }
/// <summary> /// Displays process progress. /// </summary> private void LoadAppDistributiveInternal() { // try { var service = ServiceProviderProxy.GetInstallerWebService(); // string dataFolder = FileUtils.GetDataDirectory(); string tmpFolder = FileUtils.GetTempDirectory(); if (!Directory.Exists(dataFolder)) { Directory.CreateDirectory(dataFolder); Log.WriteInfo("Data directory created"); } if (Directory.Exists(tmpFolder)) { FileUtils.DeleteTempDirectory(); } if (!Directory.Exists(tmpFolder)) { Directory.CreateDirectory(tmpFolder); Log.WriteInfo("Tmp directory created"); } string fileToDownload = null; if (!string.IsNullOrEmpty(localFile)) { fileToDownload = localFile; } else { fileToDownload = Path.GetFileName(remoteFile); } string destinationFile = Path.Combine(dataFolder, fileToDownload); string tmpFile = Path.Combine(tmpFolder, fileToDownload); //check whether file already downloaded if (!File.Exists(destinationFile)) { if (string.IsNullOrEmpty(remoteFile)) { //need to get remote file name RaiseOnStatusChangedEvent(ConnectingRemotServiceMessage); // RaiseOnProgressChangedEvent(0); // DataSet ds = service.GetReleaseFileInfo(componentCode, version); // RaiseOnProgressChangedEvent(100); // if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataRow row = ds.Tables[0].Rows[0]; remoteFile = row["FullFilePath"].ToString(); fileToDownload = Path.GetFileName(remoteFile); destinationFile = Path.Combine(dataFolder, fileToDownload); tmpFile = Path.Combine(tmpFolder, fileToDownload); } else { throw new Exception("Installer not found"); } } // download file to tmp folder RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage); // RaiseOnProgressChangedEvent(0); // DownloadFile(remoteFile, tmpFile); // RaiseOnProgressChangedEvent(100); // copy downloaded file to data folder RaiseOnStatusChangedEvent(CopyingSetupFilesMessage); // RaiseOnProgressChangedEvent(0); // Ensure that the target does not exist. if (File.Exists(destinationFile)) { FileUtils.DeleteFile(destinationFile); } File.Move(tmpFile, destinationFile); // RaiseOnProgressChangedEvent(100); } // unzip file RaiseOnStatusChangedEvent(PreparingSetupFilesMessage); // RaiseOnProgressChangedEvent(0); // UnzipFile(destinationFile, tmpFolder); // RaiseOnProgressChangedEvent(100); // RaiseOnOperationCompletedEvent(); } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } Log.WriteError("Loader module error", ex); // RaiseOnOperationFailedEvent(ex); } }