public ActionResult AddLocalPackage(HttpPostedFileBase file)
{
if(file == null)
{
ModelState.AddModelError("PackageFileValidation", "No file selected. Please select a package file to upload.");
return LocalRepository();
}
if (!Path.GetExtension(file.FileName).EndsWith("nupkg"))
{
ModelState.AddModelError("PackageFileValidation", "The file uploaded is not a valid package file, only Nuget packages are supported");
return LocalRepository();
}
IPackage package;
try
{
package = new ZipPackage(file.InputStream);
}
catch (Exception ex)
{
LogHelper.Error<PackagingEditorController>("Package could not be unziped.", ex);
ModelState.AddModelError("PackageFileValidation", "The Nuget package file uploaded could not be read");
return LocalRepository();
}
try
{
var fileName = Path.Combine(BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.Source, file.FileName);
file.SaveAs(fileName);
}
catch (Exception ex)
{
ModelState.AddModelError("PackageFileValidation", "The package file could not be saved. " + ex.Message);
return LocalRepository();
}
if(!string.IsNullOrWhiteSpace(Request.Form["autoinstall"]))
{
BackOfficeRequestContext.PackageContext.LocalPackageManager.InstallPackage(package, false);
var logger = new PackageLogger(BackOfficeRequestContext, HttpContext, package);
var installation = new PackageInstallation(BackOfficeRequestContext, HttpContext, package);
//Copy files from package folder to destination and log results
var fileResults = installation.CopyPackageFiles();
foreach (var info in fileResults)
{
logger.Log(info.IsCopiable, info.Message());
}
//Import data and log results
var dataResults = installation.ImportData();
foreach (var attributeType in dataResults.AttributeTypes)
{
logger.Log(attributeType.IsImportable, string.Format("AttributeType {0}", attributeType.ObjectId.Value.ToString()));
}
foreach (var schema in dataResults.Schemas)
{
logger.Log(schema.IsImportable, string.Format("Schema {0}", schema.ObjectId.Value.ToString()));
}
foreach (var schemaRelation in dataResults.SchemaRelations)
{
logger.Log(schemaRelation.IsImportable, string.Format("Schema Relation {0}", schemaRelation.ObjectId.Value.ToString()));
}
foreach (var entity in dataResults.Entities)
{
logger.Log(entity.IsImportable, string.Format("Entity {0}", entity.ObjectId.Value.ToString()));
}
foreach (var entityRelation in dataResults.EntityRelations)
{
logger.Log(entityRelation.IsImportable, string.Format("Entity Relation {0}", entityRelation.ObjectId.Value.ToString()));
}
foreach (var language in dataResults.Languages)
{
logger.Log(language.IsImportable, string.Format("Language {0}", language.ObjectId.Value.ToString()));
}
//Notifications.Add(new NotificationMessage(package.Title + " has been installed", "Package installed", NotificationType.Success));
//SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);
return RedirectToAction("RecycleApplication", new { id = package.Id, state = PackageInstallationState.Installing });
}
Notifications.Add(new NotificationMessage(package.Title + " added to local repository", "Package added", NotificationType.Success));
SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);
return RedirectToAction("LocalRepository");
}