/// <summary> /// Comments reagarding this method is specified in interface /// </summary> /// <param name="destinationPath"></param> /// <returns></returns> public async Task <string> PullGeoliteDataBaseAsync(string destinationPath) { if (string.IsNullOrEmpty(destinationPath)) { NullArgument.Throw(nameof(destinationPath)); } if (!Directory.Exists(destinationPath)) { throw new DirectoryNotFoundException($"Following directory was not found {destinationPath}"); } var httpResponse = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, geoliteUrl.Uri)); httpResponse.EnsureSuccessStatusCode(); string geoliteDbPath = Path.Combine(destinationPath, geoliteDbFileName); using (Stream stream = await httpResponse.Content.ReadAsStreamAsync()) using (var fileStream = File.Create(geoliteDbPath)) { if (stream.CanRead) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); fileStream.Write(buffer, 0, buffer.Length); } else { throw new HttpRequestException("HttpClient returned invalid content. Gzip expected"); } } return(geoliteDbPath); }
public string DecompressGzip(string gzipPath, bool deleteGzipFile = true) { if (string.IsNullOrEmpty(gzipPath)) { NullArgument.Throw(nameof(gzipPath)); } if (!File.Exists(gzipPath)) { throw new FileNotFoundException($"File was not found {gzipPath}"); } var gzipFileInfo = new FileInfo(gzipPath); // Just crop filename. Example, from "geolite.tar.gz" to "geolite.tar" string decompressedFileName = gzipFileInfo.FullName.Remove (gzipFileInfo.FullName.Length - gzipFileInfo.Extension.Length); using (var compressedFileStream = new FileStream(path: gzipPath, FileMode.Open)) using (var decompressedFileStream = new FileStream(path: decompressedFileName, FileMode.Create)) using (var gzipStream = new GZipStream(compressedFileStream, CompressionMode.Decompress)) { gzipStream.CopyTo(decompressedFileStream); } if (deleteGzipFile) { File.Delete(gzipPath); } return(decompressedFileName); }
public string ExtractDbFileFromArchive(string archivePath, bool deleteArchive = true) { if (string.IsNullOrEmpty(archivePath)) { NullArgument.Throw(nameof(archivePath)); } if (!File.Exists(archivePath)) { throw new FileNotFoundException($"Following file was not found {archivePath}"); } var archiveInfo = new FileInfo(archivePath); // Dir where we would like to extract our archive to string directoryToExtractArhive = Path.Combine(archiveInfo.DirectoryName, "GeoliteDb_Extract_Folder"); string directoryToPutDbFile = archiveInfo.DirectoryName; using (var decompressedFileStream = new FileStream(path: archivePath, FileMode.Open)) using (var tarStream = TarArchive.CreateInputTarArchive(decompressedFileStream)) tarStream.ExtractContents(directoryToExtractArhive); if (deleteArchive) { File.Delete(archivePath); } var extractedArchive = new DirectoryInfo(directoryToExtractArhive); FileInfo[] dbFiles = extractedArchive.GetFiles("*.mmdb", SearchOption.AllDirectories); if (dbFiles.Length > 1) { throw new FileLoadException($"More that one .mmdb exist exist in {extractedArchive}"); } FileInfo geoliteDatabase = dbFiles.FirstOrDefault(); if (geoliteDatabase is null) { throw new FileLoadException($"No file of type .mmdb has been found {extractedArchive}"); } string geoliteDatabaseNewPath = Path.Combine(directoryToPutDbFile, geoliteDatabase.Name); geoliteDatabase.MoveTo(geoliteDatabaseNewPath, overwrite: true); // delete extracted folder Directory.Delete(directoryToExtractArhive, true); return(geoliteDatabaseNewPath); }
public GeoliteHttpClient(HttpClient httpClient, IOptionsSnapshot <GeoliteUrlOptions> optionsSnapshot) { if (optionsSnapshot.Value is null) { NullArgument.Throw(nameof(GeoliteUrlOptions)); } this.httpClient = httpClient; geoliteUrl = optionsSnapshot.Value; }
public string Build(AppDbOptions ops, string dbName) { if (ops == null || string.IsNullOrEmpty(dbName)) { NullArgument.Throw(argument: nameof(AppDbOptions)); } return($"Username={ops.UserName};" + $"Password={ops.Password};" + $"Host={ops.Host};" + $"Port={ops.Port};" + $"Database={dbName}"); }