private async Task <InstalledFileInfo> InstallFileAsync(bool force, InstalledFileInfo file) { var directory = Path.GetDirectoryName(file.FullPath); if (!_fs.Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var fileResult = new InstalledFileInfo { FullPath = file.FullPath, RegistryFile = file.RegistryFile }; if (_fs.File.Exists(file.FullPath)) { if (force) { _fs.File.Delete(file.FullPath); } else if (string.IsNullOrEmpty(file.RegistryFile.Hash?.Value)) { file.Status = FileStatus.Installed; return(file); } else { return(ValidateHash(force, file, fileResult, _fs.File.ReadAllText(file.FullPath))); } } if (string.IsNullOrEmpty(file.RegistryFile.Url)) { file.Status = FileStatus.NotDownloadable; file.Reason = $"No URL provided."; return(file); } if (!Uri.IsWellFormedUriString(file.RegistryFile.Url, UriKind.Absolute)) { file.Status = FileStatus.NotDownloadable; file.Reason = $"Provided file URL '{file.RegistryFile.Url}' is not a valid url."; return(file); } var response = await _http.GetAsync(file.RegistryFile.Url).ConfigureAwait(false); try { response.EnsureSuccessStatusCode(); } catch (Exception exc) { fileResult.Status = FileStatus.NotDownloadable; fileResult.Reason = $"Failed to download file from {file.RegistryFile.Url}: Received status {response.StatusCode}.\n{exc.Message}"; return(fileResult); } var content = await response.Content.ReadAsStringAsync(); return(ValidateHash(force, file, fileResult, content)); }
private InstalledFileInfo ValidateHash(bool force, InstalledFileInfo file, InstalledFileInfo fileResult, string content) { var lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); var hash = Hashing.GetHash(lines); if (!force && hash != file.RegistryFile.Hash.Value) { fileResult.Status = FileStatus.HashMismatch; fileResult.Reason = $"Expected hash {file.RegistryFile.Hash.Value}, received {hash}. The file was either corrupted during download, or the wrong hash was pushed to the registry."; return(fileResult); } else { _fs.File.WriteAllText(file.FullPath, content); fileResult.Status = FileStatus.Installed; return(fileResult); } }
private async Task <InstalledFileInfo> GetPackageFileInfo(string packagePath, RegistryFile file) { if (!RegistryFile.ValidFilename.IsMatch(file.Filename)) { throw new UnauthorizedAccessException($"Only files relative to the package (file.cs) or to vam (/file.cs) are accepted. Value: '{file.Filename}'"); } var fullPath = file.Filename.StartsWith("/") ? Path.Combine(_folders.FromRelativeToVam(file.Filename.Substring(1))) : Path.Combine(packagePath, file.Filename); var info = new InstalledFileInfo { FullPath = fullPath, RegistryFile = file }; var(status, reason) = await GetFileStatusAsync(file, fullPath).ConfigureAwait(false); info.Status = status; info.Reason = reason; return(info); }