public static ErrorCode Download(string name, out FileStream file, out string filename) { ErrorCode ec = ErrorCode.SUCCESS; try { BackupController bc = new BackupController(null, name, Guid.NewGuid()); string path = bc.ZipFolder(); file = new FileStream(path, FileMode.Open, FileAccess.Read); filename = Path.GetFileName(path); } catch (Exception) { ec = ErrorCode.COULD_NOT_DOWNLOAD_ZIP; throw new BackupNotPossibleException(name, ec); } return(ec); }
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)); }