/// <summary> /// Installs a package. /// </summary> /// <param name="fileName"></param> public List<PackageImportEventArgs> InstallPackage(string fileName) { // Validate that the package is still valid, just in case somebody else deleted it string packagePath = Path.Combine(ProgramDataFolder.MapPath("Packages"), fileName); FileInfo packageDetails = new FileInfo(packagePath); List<PackageImportEventArgs> events = null; if (packageDetails.Exists) { // Load the package and install it events = new List<PackageImportEventArgs>(); using (Stream packageStream = File.OpenRead(packagePath)) { using (Package package = new Package(packageStream)) { package.Message += delegate(object source, PackageImportEventArgs args) { events.Add(args); }; package.Install(); // Update the list XmlDocument packageList = LoadPackageList(); package.Manifest.IsInstalled = true; UpdatePackagesList(package.Manifest, packageList, true); SavePackageList(packageList); } } } return events; }
/// <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> /// 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; }