예제 #1
0
        public static void Delete(string name)
        {
            BackupController bc = new BackupController(null, name, Guid.NewGuid());

            try
            {
                Directory.Delete(bc.BackupFolder, recursive: true);
            }
            catch (Exception)
            {
                ErrorCode ec = ErrorCode.COULD_NOT_DELETE_FOLDER;
                throw new BackupNotPossibleException(name, ec, "Folder".PairedWith(bc.BackupFolder));
            }
            StoreProgress.Done(name);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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));
        }