/// <summary> /// Adds template into the system /// </summary> /// <param name="package">template zip package</param> /// <returns></returns> public static IGalleryTemplate AddTemplate(ZipArchivePackage package) { CuratedGalleryTemplate output = null; DirectoryInfo extractdir = null; try { extractdir = ExtractPackage(package, Path.Combine(TemporaryDirectory, Guid.NewGuid().ToString())); #region Get package descriptor and validate package if (extractdir.GetFiles("*.html.xslt").Count() != 1) throw new Exception("Package does not have entry point file (*.html.xslt)"); var xfiles = extractdir.GetFiles("*.xml"); if (xfiles.Length == 0) throw new Exception("Template descriptor was not found"); if (xfiles.Length != 1) throw new Exception("Template package has several xml files. Template descriptor resolving problem"); GalleryTemplateDescriptor descriptor = LoadTemplateDescriptor(xfiles[0]); #endregion Get package descriptor and validate package #region Add template into application storage and make some actions with file system //add the template into the app storage var template = ObjectMapper.DoMapping<GalleryTemplateInfo>(descriptor); template.Package = package; OperationResult<OperationResults, GalleryTemplate> rslt = null; try { rslt = GalleryRuntime.GalleryRepository.AddTemplate(template); } catch (Exception ex) { Logger.WriteError(ex); throw; } extractdir.CopyTo(GetTemplatePath(rslt.Output.ID)); output = new CuratedGalleryTemplate(); ObjectMapper.DoMapping(descriptor, output); ObjectMapper.DoMapping(rslt.Output, output); #endregion Add template into application storage } finally { if (extractdir != null && extractdir.Exists) { try { extractdir.Remove(); } catch (Exception ex) { Logger.WriteInfo(ex.ToString()); } } } //add template into registered template list lock (m_TemplatesSyncRoot) { if (m_Templates == null) m_Templates = LoadTemplates(); m_Templates.Add(output); } return output; }
protected static DirectoryInfo ExtractPackage(ZipArchivePackage package, string dirpath) { var extractdir = new DirectoryInfo(dirpath); using (var stream = new MemoryStream(package.FileContent)) { using (var zip = ZipFile.Read(stream)) { zip.ExtractAll(extractdir.FullName); } } return extractdir; }