public static ErrorCode Restore(UnitOfWork unit, string name, out Guid ssid) { ErrorCode result = ErrorCode.SUCCESS; lock (PADLOCK_ACTION) { try { BackupController bc = new BackupController(unit, name, Guid.NewGuid()); bc.VerifyRestore(); try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(bc.BackupDataFolder("Context") + @"appdata.xml"); string xmlString = xmlDocument.OuterXml; using (StringReader read = new StringReader(xmlString)) { Type outType = typeof(Data); XmlSerializer serializer = new XmlSerializer(outType); using (XmlReader reader = new XmlTextReader(read)) { bc.Data = (Data)serializer.Deserialize(reader); reader.Close(); } read.Close(); } } catch (Exception e) { result = ErrorCode.COULD_NOT_LOAD_XML; throw new BackupNotPossibleException(e, name, result); } bc.Data.SiteSetting.Name = name; bc.Data.SiteSetting.IsDefault = false; try { bc.CopyDirectoryContentWithIncludedFiles(bc.BackupDataFolder("Content"), bc.ContentFolder); } catch (Exception e) { result = ErrorCode.COULD_NOT_COPY_FOLDER; throw new BackupNotPossibleException(e, name, result, "From".PairedWith(bc.BackupDataFolder("Content")), "To".PairedWith(bc.ContentFolder)); } try { bc.CopyDirectoryContentWithIncludedFiles(bc.BackupDataFolder("Views"), bc.ViewsFolder); } catch (Exception e) { result = ErrorCode.COULD_NOT_COPY_FOLDER; throw new BackupNotPossibleException(e, name, result, "From".PairedWith(bc.BackupDataFolder("Views")), "To".PairedWith(bc.ViewsFolder)); } ssid = bc.SiteSettingId; bc.SaveDataToUnit(); } catch (Exception e) { if (result == ErrorCode.SUCCESS) { result = ErrorCode.UNKNOWN; } ssid = Guid.Empty; throw new BackupNotPossibleException(e, name, result); } } return(result); }
public static ErrorCode Store(UnitOfWork unit, string name, Guid siteSettingId) { unit = _Globals.Instance.ChangeSiteSettingId(siteSettingId, unit); if (StoreProgress.Get(name) != ErrorCode.NULL) { return(ErrorCode.NAME_ALREADY_EXISTS); } StoreProgress.Set(name, ErrorCode.RUNNING); Task.Run(() => { try { lock (PADLOCK_ACTION) { if (string.IsNullOrWhiteSpace(name) || name.Where(c => !char.IsLetterOrDigit(c)).Count() > 0) { StoreProgress.Set(name, ErrorCode.NAME_INVALID); throw new BackupNotPossibleException(name, "Name".PairedWith(name)); } BackupController bc = new BackupController(unit, name, siteSettingId); bc.SetDatas(); bc.VerifyStore(); Directory.CreateDirectory(bc.BackupDataFolder("Context")); try { XmlDocument xmlDocument = new XmlDocument(); XmlSerializer serializer = new XmlSerializer(bc.Data.GetType()); using (MemoryStream stream = new MemoryStream()) { serializer.Serialize(stream, bc.Data); stream.Position = 0; xmlDocument.Load(stream); xmlDocument.Save(bc.BackupDataFolder("Context") + @"appdata.xml"); stream.Close(); } } catch (Exception e) { StoreProgress.Set(name, ErrorCode.COULD_NOT_SAVE_TO_XML); throw new BackupNotPossibleException(e, name); } try { bc.CopyDirectoryContentWithIncludedFiles(bc.ContentFolder, bc.BackupDataFolder("Content")); } catch (Exception e) { StoreProgress.Set(name, ErrorCode.COULD_NOT_COPY_FOLDER); throw new BackupNotPossibleException(e, name, "From".PairedWith(bc.ContentFolder), "To".PairedWith(bc.BackupDataFolder("Content"))); } try { bc.CopyDirectoryContentWithIncludedFiles(bc.ViewsFolder, bc.BackupDataFolder("Views")); } catch (Exception e) { StoreProgress.Set(name, ErrorCode.COULD_NOT_COPY_FOLDER); throw new BackupNotPossibleException(e, name, "From".PairedWith(bc.ViewsFolder), "To".PairedWith(bc.BackupDataFolder("Views"))); } ZipFile.CreateFromDirectory(bc.BackupDataFolder(), bc.ZipFolder(), CompressionLevel.Fastest, true); StoreProgress.Done(name); } } catch (Exception e) { StoreProgress.SetFailureIfRunning(name); throw new BackupNotPossibleException(e, name); } }); return(StoreProgress.Get(name)); }