/// <summary> /// Updates a package in the list of packages. /// </summary> /// <param name="manifest">The manifest of the package.</param> /// <param name="packageList">The XML document containing the list.</param> /// <param name="addPackage">Whether to add or remove the package</param> private void UpdatePackagesList(PackageManifest manifest, XmlDocument packageList, bool addPackage) { XmlElement packageElement = packageList.SelectSingleNode( string.Format(System.Globalization.CultureInfo.CurrentCulture, "/packages/package[@name='{0}']", manifest.Name)) as XmlElement; if (packageElement == null) { if (addPackage) { // Generate a new package element packageElement = packageList.CreateElement("package"); packageElement.SetAttribute("name", manifest.Name); packageElement.SetAttribute("description", manifest.Description); packageElement.SetAttribute("type", manifest.Type.ToString()); packageElement.SetAttribute("group", manifest.Group); packageElement.SetAttribute("file", manifest.FileName); packageList.DocumentElement.AppendChild(packageElement); } } else { if (!addPackage) { packageElement.ParentNode.RemoveChild(packageElement); } } if (addPackage) { // Update the installed status - this is the only thing that should change packageElement.SetAttribute("installed", manifest.IsInstalled ? "yes" : "no"); } }
/// <summary> /// Imports a package. /// </summary> /// <param name="context"></param> /// <param name="velocityContext"></param> private void ImportPackage(HttpContext context, Hashtable velocityContext) { HttpPostedFile file = context.Request.Files["package"]; if (file.ContentLength == 0) { velocityContext["Error"] = this.translations.Translate("No file selected to import!"); } else { PackageManifest manifest = manager.StorePackage(file.FileName, file.InputStream); if (manifest == null) { // If there is no manifest, then the package is invalid velocityContext["Error"] = this.translations.Translate("Invalid package - manifest file is missing"); } else { // Otherwise pass on the details to the view generator velocityContext["Result"] = this.translations.Translate("Package '{0}' has been loaded", manifest.Name); velocityContext["InstallPackage"] = manifest.FileName; } } }
/// <summary> /// Stores a package locally on the server /// </summary> /// <param name="fileName">The file name of the package.</param> /// <param name="stream">The stream containing the package.</param> public PackageManifest StorePackage(string fileName, Stream stream) { // Initialise the path and make sure the folder is there FileInfo packageDetails = new FileInfo(fileName); string packagePath = Path.Combine(ProgramDataFolder.MapPath("Packages"), packageDetails.Name); packageDetails = new FileInfo(packagePath); if (!packageDetails.Directory.Exists) { packageDetails.Directory.Create(); } // We will always overwrite an existing packages, maybe we should do a back-up first? if (packageDetails.Exists) { packageDetails.Delete(); } SaveToFile(stream, packageDetails.FullName); bool delete = false; // Load the package and extract the manifest details PackageManifest manifest = null; using (Stream inputStream = File.OpenRead(packagePath)) { using (Package newPackage = new Package(inputStream)) { if (!newPackage.IsValid) { delete = true; } else { manifest = newPackage.Manifest; manifest.FileName = packageDetails.Name; } } } if (manifest != null) { // Update the package list XmlDocument packageList = LoadPackageList(); UpdatePackagesList(manifest, packageList, true); SavePackageList(packageList); } // Don't forget to clean-up, otherwise the packages directory will have invalid packages if (delete && packageDetails.Exists) { packageDetails.Delete(); } return(manifest); }
/// <summary> /// Initialises a new package from a stream. /// </summary> /// <param name="packageStream">The stream that contains the package.</param> public Package(Stream packageStream) { // Extract all the files into a temporary location ExtractAllFiles(packageStream); // Attempt to load the manifest string manifestFile = Path.Combine(tempFolder, "Manifest.xml"); if (File.Exists(manifestFile)) { XmlSerializer serialiser = new XmlSerializer(typeof(PackageManifest)); using (Stream inputStream = File.OpenRead(manifestFile)) { manifest = serialiser.Deserialize(inputStream) as PackageManifest; inputStream.Close(); } } }
/// <summary> /// Removes a package stored locally on the server /// </summary> /// <param name="fileName">The file name of the package.</param> /// <returns>The name of the package.</returns> public string RemovePackage(string fileName) { // Initialise the path and make sure the folder is there FileInfo packageDetails = new FileInfo(fileName); string packagePath = Path.Combine(ProgramDataFolder.MapPath("Packages"), packageDetails.Name); packageDetails = new FileInfo(packagePath); string packageName = null; if (packageDetails.Directory.Exists) { // Load the package and extract the manifest details PackageManifest manifest = null; using (Stream inputStream = File.OpenRead(packagePath)) { using (Package newPackage = new Package(inputStream)) { if (newPackage.IsValid) { manifest = newPackage.Manifest; manifest.FileName = packageDetails.Name; } } } if (manifest != null) { // Update the package list XmlDocument packageList = LoadPackageList(); UpdatePackagesList(manifest, packageList, false); SavePackageList(packageList); packageName = manifest.Name; } // Finally, remove the package packageDetails.Delete(); } return(packageName); }
/// <summary> /// Lists all the available packages. /// </summary> /// <returns></returns> public virtual List <PackageManifest> ListPackages() { List <PackageManifest> packages = new List <PackageManifest>(); // Load the document XmlDocument document = LoadPackageList(); // Parse each item and add it in foreach (XmlElement packageElement in document.SelectNodes("/packages/package")) { PackageManifest manifest = new PackageManifest(); manifest.Name = packageElement.GetAttribute("name"); manifest.Description = packageElement.GetAttribute("description"); manifest.Type = (PackageType)Enum.Parse(typeof(PackageType), packageElement.GetAttribute("type")); manifest.FileName = packageElement.GetAttribute("file"); manifest.IsInstalled = (packageElement.GetAttribute("installed") == "yes"); packages.Add(manifest); } return(packages); }
/// <summary> /// Lists all the available packages. /// </summary> /// <returns></returns> public virtual List<PackageManifest> ListPackages() { List<PackageManifest> packages = new List<PackageManifest>(); // Load the document XmlDocument document = LoadPackageList(); // Parse each item and add it in foreach (XmlElement packageElement in document.SelectNodes("/packages/package")) { PackageManifest manifest = new PackageManifest(); manifest.Name = packageElement.GetAttribute("name"); manifest.Description = packageElement.GetAttribute("description"); manifest.Type = (PackageType)Enum.Parse(typeof(PackageType), packageElement.GetAttribute("type")); manifest.FileName = packageElement.GetAttribute("file"); manifest.IsInstalled = (packageElement.GetAttribute("installed") == "yes"); packages.Add(manifest); } return packages; }
/// <summary> /// Updates a package in the list of packages. /// </summary> /// <param name="manifest">The manifest of the package.</param> /// <param name="packageList">The XML document containing the list.</param> /// <param name="addPackage">Whether to add or remove the package</param> private void UpdatePackagesList(PackageManifest manifest, XmlDocument packageList, bool addPackage) { XmlElement packageElement = packageList.SelectSingleNode( string.Format(System.Globalization.CultureInfo.CurrentCulture,"/packages/package[@name='{0}']", manifest.Name)) as XmlElement; if (packageElement == null) { if (addPackage) { // Generate a new package element packageElement = packageList.CreateElement("package"); packageElement.SetAttribute("name", manifest.Name); packageElement.SetAttribute("description", manifest.Description); packageElement.SetAttribute("type", manifest.Type.ToString()); packageElement.SetAttribute("file", manifest.FileName); packageList.DocumentElement.AppendChild(packageElement); } } else { if (!addPackage) { packageElement.ParentNode.RemoveChild(packageElement); } } if (addPackage) { // Update the installed status - this is the only thing that should change packageElement.SetAttribute("installed", manifest.IsInstalled ? "yes" : "no"); } }