private ActionResult CreatePackageInternal(string apiKey)
{
Guid parsedApiKey;
if (!Guid.TryParse(apiKey, out parsedApiKey)) return new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, string.Format(CultureInfo.CurrentCulture, Strings.InvalidApiKey, apiKey));
var user = userSvc.FindByApiKey(parsedApiKey);
if (user == null) return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, String.Format(CultureInfo.CurrentCulture, Strings.ApiKeyNotAuthorized, "push"));
if (Request.ContentLength > MAX_ALLOWED_CONTENT_LENGTH)
{
return new HttpStatusCodeWithBodyResult(HttpStatusCode.RequestEntityTooLarge,String.Format(CultureInfo.CurrentCulture, Strings.PackageTooLarge, MAX_ALLOWED_CONTENT_LENGTH / ONE_MB));
}
var packageToPush = ReadPackageFromRequest();
// Ensure that the user can push packages for this partialId.
var packageRegistration = packageSvc.FindPackageRegistrationById(packageToPush.Id, useCache: false);
if (packageRegistration != null)
{
if (!packageRegistration.IsOwner(user)) return new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, String.Format(CultureInfo.CurrentCulture, Strings.ApiKeyNotAuthorized, "push"));
var existingPackage = packageRegistration.Packages.FirstOrDefault(p => p.Version.Equals(packageToPush.Version.ToString(), StringComparison.OrdinalIgnoreCase));
if (existingPackage != null)
{
switch (existingPackage.Status)
{
case PackageStatusType.Rejected:
var testReporterUser = userSvc.FindByUserId(settings.PackageOperationsUserKey);
if (existingPackage.PackageCleanupResultDate.HasValue &&
testReporterUser != null &&
existingPackage.ReviewedById == testReporterUser.Key
)
{
//allow rejected by cleanup to return a value
break;
}
return new HttpStatusCodeWithBodyResult(
HttpStatusCode.Conflict, string.Format("This package has been {0} and can no longer be submitted.", existingPackage.Status.GetDescriptionOrValue().ToLower()));
case PackageStatusType.Submitted:
//continue on
break;
default:
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict, String.Format(CultureInfo.CurrentCulture, Strings.PackageExistsAndCannotBeModified, packageToPush.Id, packageToPush.Version));
}
}
}
try
{
packageSvc.CreatePackage(packageToPush, user);
}
catch (Exception ex)
{
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict, string.Format("This package had an issue pushing: {0}", ex.Message));
}
return new HttpStatusCodeWithBodyResult(HttpStatusCode.Created, "Package has been pushed and will show up once moderated and approved.");
}