/// <summary> /// Imports a ZIP file (from stream) /// </summary> /// <param name="zipStream"></param> /// <param name="server"></param> /// <param name="portalSettings"></param> /// <param name="messages"></param> /// <returns></returns> public bool ImportZip(Stream zipStream, HttpServerUtility server, PortalSettings portalSettings, List <ExportImportMessage> messages, bool isAppImport) { if (!isAppImport && !_appId.HasValue) { throw new Exception("Could not import zip: No valid app id"); } if (messages == null) { messages = new List <ExportImportMessage>(); } var temporaryDirectory = server.MapPath(Path.Combine(SexyContent.TemporaryDirectory, System.Guid.NewGuid().ToString())); var success = true; try { if (!Directory.Exists(temporaryDirectory)) { Directory.CreateDirectory(temporaryDirectory); } // Extract ZIP archive to the temporary folder ExtractZipFile(zipStream, temporaryDirectory); string currentWorkingDir = temporaryDirectory; string[] baseDirectories = Directory.GetDirectories(currentWorkingDir); // Loop through each root-folder. For now only contains the "Apps" folder. foreach (var directoryPath in baseDirectories) { switch (Path.GetFileName(directoryPath)) { // Handle the App folder case "Apps": currentWorkingDir = Path.Combine(currentWorkingDir, "Apps"); // Loop through each app directory foreach (var appDirectory in Directory.GetDirectories(currentWorkingDir)) { var appId = new int?(); var xmlSearchPattern = isAppImport ? "App.xml" : "*.xml"; // Stores the number of the current xml file to process var xmlIndex = 0; // Import XML file(s) foreach (var xmlFileName in Directory.GetFiles(appDirectory, "*.xml")) { var fileContents = File.ReadAllText(Path.Combine(appDirectory, xmlFileName)); var import = new XmlImport(); if (isAppImport) { if (!import.IsCompatible(_zoneId, fileContents)) { throw new Exception("The " + (isAppImport ? "app" : "package") + " is not compatible with this version of 2sxc."); } var folder = XDocument.Parse(fileContents).Element("SexyContent") .Element("Entities").Elements("Entity").Single(e => e.Attribute("AttributeSetStaticName").Value == "2SexyContent-App") .Elements("Value").First(v => v.Attribute("Key").Value == "Folder").Attribute("Value").Value; var appPath = System.IO.Path.Combine(SexyContent.AppBasePath(PortalSettings.Current), folder); // Do not import (throw error) if the app directory already exists if (Directory.Exists(HttpContext.Current.Server.MapPath(appPath))) { throw new Exception("The app could not be installed because the app-folder '" + appPath + "' already exists. Please remove or rename the folder and install the app again."); } if (xmlIndex == 0) { // Handle PortalFiles folder string portalTempRoot = Path.Combine(appDirectory, "PortalFiles"); if (Directory.Exists(portalTempRoot)) { CopyAllFilesDnnPortal(portalTempRoot, "", false, messages); } } import.ImportApp(_zoneId, fileContents, out appId); } else { appId = _appId.Value; if (xmlIndex == 0 && import.IsCompatible(_zoneId, fileContents)) { // Handle PortalFiles folder string portalTempRoot = Path.Combine(appDirectory, "PortalFiles"); if (Directory.Exists(portalTempRoot)) { CopyAllFilesDnnPortal(portalTempRoot, "", false, messages); } } import.ImportXml(_zoneId, appId.Value, fileContents); } messages.AddRange(import.ImportLog); xmlIndex++; } var sexy = new SexyContent(_zoneId, appId.Value); // Copy all files in 2sexy folder to (portal file system) 2sexy folder string templateRoot = server.MapPath(SexyContent.GetTemplatePathRoot(SexyContent.TemplateLocations.PortalFileSystem, sexy.App)); string appTemplateRoot = Path.Combine(appDirectory, "2sexy"); if (Directory.Exists(appTemplateRoot)) { ImportExportHelpers.CopyAllFiles(appTemplateRoot, templateRoot, false, messages); } } // Reset CurrentWorkingDir currentWorkingDir = temporaryDirectory; break; } } } catch (Exception e) { // Add error message and return false messages.Add(new ExportImportMessage("Could not import the " + (isAppImport ? "app" : "package") + ": " + e.Message, ExportImportMessage.MessageTypes.Error)); Exceptions.LogException(e); success = false; } finally { try { // Finally delete the temporary directory Directory.Delete(temporaryDirectory, true); } catch (IOException e) { // The folder itself or files inside may be used by other processes. // Deleting the folder recursively will fail in such cases // If deleting is not possible, just leave the temporary folder as it is } } return(success); }
public MemoryStream ExportApp() { // Get Export XML var attributeSets = _sexy.GetAvailableAttributeSets(SexyContent.AttributeSetScope).ToList(); attributeSets.AddRange(_sexy.GetAvailableAttributeSets(SexyContent.AttributeSetScopeApps)); attributeSets = attributeSets.Where(a => !a.UsesConfigurationOfAttributeSet.HasValue).ToList(); // Special case: Entities of the template attributesets and field properties should not be exported here //var templateAttributeSets = _sexy.GetAvailableAttributeSets().Where(a => a.StaticName == SexyContent.AttributeSetStaticNameTemplateContentTypes // || a.StaticName == SexyContent.AttributeSetStaticNameTemplateMetaData // || a.StaticName.StartsWith("@")); var attributeSetIds = attributeSets.Select(p => p.AttributeSetId.ToString()).ToArray(); var entities = SexyContent.GetInitialDataSource(_zoneId, _appId).Out["Default"].List; var entityIds = entities.Where(e => e.Value.AssignmentObjectTypeId != SexyContent.AssignmentObjectTypeIDSexyContentTemplate && e.Value.AssignmentObjectTypeId != DataSource.AssignmentObjectTypeIdFieldProperties) .Select(e => e.Value.EntityId.ToString()).ToArray(); var templateIds = _sexy.GetTemplates(PortalSettings.Current.PortalId).Select(p => p.TemplateID.ToString()).ToArray(); var messages = new List <ExportImportMessage>(); var xmlExport = new XmlExport(_zoneId, _appId, true); var xml = xmlExport.ExportXml(attributeSetIds, entityIds, templateIds, out messages); #region Copy needed files to temporary directory var temporaryDirectoryPath = HttpContext.Current.Server.MapPath(Path.Combine(SexyContent.TemporaryDirectory, System.Guid.NewGuid().ToString())); if (!Directory.Exists(temporaryDirectoryPath)) { Directory.CreateDirectory(temporaryDirectoryPath); } var tempDirectory = new DirectoryInfo(temporaryDirectoryPath); var appDirectory = tempDirectory.CreateSubdirectory("Apps/" + _sexy.App.Folder + "/"); var sexyDirectory = appDirectory.CreateSubdirectory("2sexy"); var portalFilesDirectory = appDirectory.CreateSubdirectory("PortalFiles"); // Copy app folder if (Directory.Exists(_sexy.App.PhysicalPath)) { ImportExportHelpers.CopyAllFiles(_sexy.App.PhysicalPath, sexyDirectory.FullName, false, messages); } // Copy PortalFiles foreach (var file in xmlExport.ReferencedFiles) { var portalFilePath = Path.Combine(portalFilesDirectory.FullName, Path.GetDirectoryName(file.RelativePath.Replace('/', '\\'))); if (!Directory.Exists(portalFilePath)) { Directory.CreateDirectory(portalFilePath); } File.Copy(file.PhysicalPath, Path.Combine(portalFilesDirectory.FullName, file.RelativePath.Replace('/', '\\'))); } // Save export xml File.AppendAllText(System.IO.Path.Combine(appDirectory.FullName, "App.xml"), xml); #endregion // Zip directory and return as stream var stream = new MemoryStream(); var zipStream = new ZipOutputStream(stream); zipStream.SetLevel(6); ZipFolder(tempDirectory.FullName + "\\", tempDirectory.FullName + "\\", zipStream); zipStream.Finish(); tempDirectory.Delete(true); return(stream); }