public override IResult Execute(IResult previousResults) { try { if (string.IsNullOrWhiteSpace(Target)) throw new Exception("Target path is null or empty"); if (!File.Exists(Target)) return new FileNotFound {Target = Target}; if (!string.IsNullOrWhiteSpace(OutputFolder)) { if (!Directory.Exists(OutputFolder)) return new FileNotFound {Target = OutputFolder}; } bool vital; if (!bool.TryParse(Vital, out vital)) Log.Warn("Cannot convert vital value to valid boolean. Assuming vital = false."); var version = GetVersionForFile(Target, Version); var info = new FileInfo(Target); var task = Task<string>.Factory.StartNew(() => PathUtilities.HashFile(Target)); task.WaitUntilExited(); if (task.IsFaulted) Log.Warn("An issue occurred while attempting to generate the file hash: {0}", task.Exception); var wrapper = new FileInfoWrapper { FileName = info.Name, FileUrl = info.Name, Version = version.ToString(), Vital = vital, FileSize = info.Length, FileSha1 = task.Result, MsiProductCode = GetMsiPropertyFromFile(Target, "ProductCode"), MsiUpgradeCode = GetMsiPropertyFromFile(Target, "UpgradeCode"), }; var serializedWrapper = wrapper.ToXElement<FileInfoWrapper>(); // remove attributes from root serializedWrapper.Attributes().Remove(); var result = new XElement("InCommonProvision", new XAttribute("version", "1.0")); result.Add(serializedWrapper); if (!string.IsNullOrWhiteSpace(OutputKey)) { var settingsWrapper = new StringSettingWrapper(OutputKey, result.ToString(), null); SettingsManager.BindingProxy.SettingProperty = settingsWrapper; } if (!string.IsNullOrWhiteSpace(OutputFolder)) { var outputFileName = OutputFileName; if (string.IsNullOrWhiteSpace(OutputFileName)) outputFileName = GetInfoFilename(Target); File.WriteAllText(Path.Combine(OutputFolder, outputFileName), result.ToString(), new ASCIIEncoding()); } return new NextResult(); } catch (Exception e) { return new ExceptionOccurred(e); } }
protected virtual Uri GetDownloadUri(FileInfoWrapper info) { if (UriUtilities.IsValidAbsoluteUri(info.FileUrl)) return UriUtilities.ResolveAbsoluteUri(info.FileUrl); return UriUtilities.ResolveUri(info.FileUrl, string.IsNullOrWhiteSpace(info.BaseUrl) ? EndpointManager.GetEndpointForFunction(EndPointFunctions.GetFileInfo) : info.BaseUrl); }
static int Main(string[] args) { try { var source = GetArgument("-source=", args); if (string.IsNullOrWhiteSpace(source)) throw new Exception("No source specified"); var regularExpression = GetArgument("-exp=", args); if (!IsValidUri(source)) throw new Exception("Source is not a valid uri"); var target = GetArgument("-target=", args); if (string.IsNullOrWhiteSpace(target)) target = GetPathForUri(source); var temporaryPath = DownloadSource(source, target, regularExpression); if (string.IsNullOrWhiteSpace(temporaryPath)) throw new Exception("Could not transfer source to temporary path"); if (!File.Exists(temporaryPath)) throw new Exception("Temporary file does not exist"); var temporaryFileName = Path.GetFileName(temporaryPath); if (string.IsNullOrWhiteSpace(temporaryFileName)) throw new Exception("Could not determine temporary file name"); var temporaryFolder = Path.GetDirectoryName(temporaryPath); if (string.IsNullOrWhiteSpace(temporaryFolder)) throw new Exception("Could not determine temporary folder name"); bool verifySignature; if (!bool.TryParse(GetArgument("-requireSigned=", args), out verifySignature)) verifySignature = true; var info = new FileInfo(temporaryPath); if (verifySignature && !info.IsSigned()) { Console.WriteLine("File is not signed!"); return -1; } var hash = HashFile(temporaryPath); bool vital; bool.TryParse(GetArgument("-vital=", args), out vital); var wrapper = new FileInfoWrapper { FileName = info.Name, FileUrl = info.Name, Version = GetVersionForFile(temporaryPath, GetArgument("-version=", args)).ToString(), Vital = vital, FileSize = info.Length, FileSha1 = hash, MsiProductCode = GetMsiPropertyFromFile(temporaryPath, "ProductCode"), MsiUpgradeCode = GetMsiPropertyFromFile(temporaryPath, "UpgradeCode"), }; var serializedWrapper = wrapper.ToXElement<FileInfoWrapper>(); // remove attributes from root serializedWrapper.Attributes().Remove(); var result = new XElement("InCommonProvision", new XAttribute("version", "1.0")); result.Add(serializedWrapper); var infoName = GetArgument("-info=", args); if (string.IsNullOrWhiteSpace(infoName)) infoName = GetInfoFilename(temporaryPath); var infoPath = Path.Combine(temporaryFolder, infoName); File.WriteAllText( infoPath, result.ToString() , new ASCIIEncoding()); //copy outputfiles to target dir var outputFolder = GetOutputFolderPath(target); if (string.IsNullOrWhiteSpace(outputFolder)) throw new Exception("Could not determine output folder"); File.Copy(temporaryPath, Path.Combine(outputFolder, temporaryFileName), true); File.Copy(infoPath, Path.Combine(outputFolder, infoName), true); RemoveTemporaryFolder(temporaryFolder); return 0; } catch (Exception e) { Console.WriteLine("An issue occurred while processing the file: {0}", e.Message); return -1; } }
protected virtual IResult DownloadFileAsync(FileInfoWrapper info) { try { using (var client = new ConfigurableWebClient { KeepAlive = false }) { var targetPath = PathUtilities.DownloadFolder; if (!Directory.Exists(targetPath)) return new DirectoryNotFound { Target = targetPath }; var uri = GetDownloadUri(info); if (uri == null) return new CouldNotDownloadFile { Issue = "Could not determine host target", Target = info.FileName }; client.Credentials = GetDownloadCredentials(uri); client.DownloadFileCompleted += DownloadCompletedHandler; client.DownloadProgressChanged += DownloadProgressHandler; _completed = false; if (string.IsNullOrWhiteSpace(info.FileName)) return new CouldNotDownloadFile { Issue = "Could not determine local target", Target = info.FileName }; if (string.IsNullOrWhiteSpace(DisplayName)) DisplayName = info.FileName; targetPath = Path.Combine(targetPath, info.FileName); var resultWrapper = new DownloadInfoWrapper { DisplayName = DisplayName, Result = new NextResult(), StartTime = DateTime.UtcNow }; client.Headers.Add(HttpRequestHeader.UserAgent, string.Format("{0} {1}", Application.Current.GetProductName(), Application.Current.GetVersion())); InterveneIfConnectionLost(); client.DownloadFileAsync(uri, targetPath, resultWrapper); do { Thread.Sleep(5); Application.Current.DoEvents(); // check for cancel _cancelRequested = CancelRequested(); if (!_cancelRequested) continue; // handle cancel stuff client.CancelAsync(); _cancelRequested = true; SetCancelMessages(); } while (_completed == false); UserInterfaceUtilities.WaitForMilliseconds(DateTime.UtcNow, 250); UpdateProgressPercent(100); UpdateTimeEstimate(""); UpdateProgressText(resultWrapper.Result.IsOk() ? "Download complete!" : "Download failed!"); return resultWrapper.Result; } } catch (Exception e) { return new ExceptionOccurred(e); } }