public void DownloadPackage(HttpContextBase context)
        {
            RouteData routeData = GetRouteData(context);
            // Get the package file name from the route
            string packageId = routeData.GetRequiredString("packageId");
            var    version   = new SemanticVersion(routeData.GetRequiredString("version"));

            string filename = packageId + "." + version.ToString() + ".nupkg";

            IPackage requestedPackage = _azureRepository.FindPackage(packageId, version);

            if (requestedPackage != null)
            {
                CloudBlockBlob blob = _azureRepository.GetBlob(requestedPackage);

                MemoryStream ms = new MemoryStream();
                blob.DownloadToStream(ms);

                context.Response.Clear();
                context.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", filename));
                context.Response.ContentType = "application/octet-stream";
                context.Response.BinaryWrite(ms.ToArray());
                context.Response.End();
            }
            else
            {
                // Package not found
                WritePackageNotFound(context, packageId, version);
            }
        }
Exemple #2
0
        public void DownloadPackage(HttpContextBase context)
        {
            RouteData routeData = GetRouteData(context);
            // Get the package file name from the route
            string packageId = routeData.GetRequiredString("packageId");
            var    version   = new SemanticVersion(routeData.GetRequiredString("version"));

            string filename = packageId + "." + version.ToString() + ".nupkg";

            IPackage requestedPackage = _azureRepository.FindPackage(packageId, version);

            if (requestedPackage != null)
            {
                try
                {
                    var blob = _azureRepository.GetBlob(requestedPackage);

                    if (!blob.Exists())
                    {
                        WritePackageNotFound(context, packageId, version);
                        return;
                    }

                    var ms = new MemoryStream();
                    blob.DownloadToStream(ms);

                    context.Response.Clear();
                    context.Response.AddHeader("content-disposition", $"attachment; filename={filename}");
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.BinaryWrite(ms.ToArray());
                    context.Response.Flush();
                    context.Response.SuppressContent = true;
                    context.Response.End();
                }
                catch (Exception exception)
                {
                    WritePackageNotFound(context, packageId, version);
                }
            }
            else
            {
                // Package not found
                WritePackageNotFound(context, packageId, version);
            }
        }