Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

Retrieve Nuget Package Metadata from IFormFile or MemoryStream

$
0
0

I have an API I am creating that retrieves an IFormFile that is a Nuget package. I need to retrieve the 'BuildVersion-X.X.X.X' value from the metadata. All the research I have found so far is just retrieving it from a local file path or from a repository. I have found a way around it to get what I want as shown in the code below, but it is not the most elegant solution as there is a lot of string and manipulation involved. Is there a better way to retrieve the information I need from the metadata in the Nuspec file?

`// If build version is not included, retrieve it from file                if (string.IsNullOrEmpty(request.BuildVersion))                {                    try                    {                        IFormFile file = request.File;                        file.Headers.ContentType = "application/zip";                        Stream unzippedStream = file.OpenReadStream();                        MemoryStream data = new();                        string[] packageNameArray = file.FileName.Split('_');                        string packageName = packageNameArray.First();                        using (ZipFile zip = ZipFile.Read(unzippedStream))                        {                            zip[$"{packageName}.nuspec"].Extract(data);                        }                        data.Seek(0, SeekOrigin.Begin);                        byte[] dataArray = data.ToArray();                        string text = Encoding.UTF8.GetString(dataArray);                        string[] textArray = text.Split("<tags>");                        string[] otherTextArray = textArray[1].Split("</tags>");                        string buildVersion = otherTextArray[0];                        string[] buildVersionArray = buildVersion.Split("-");                        string currentBuildVersion = buildVersionArray[1];                        request.BuildVersion = currentBuildVersion;                    }                    catch                    {                        request.BuildVersion = null;                    }`

Nuspec Metadata

I have tried the code above as this has been the only thing that seems to work. I cannot retrieve the Nuget package from a file path as it is being sent in the API request. Any suggestions or libraries to do so more elegantly would be appreciated.


Viewing all articles
Browse latest Browse all 3067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>