Esempio n. 1
0
        /// <inheritdoc/>
        public void DeleteBackups(FileInfo file)
        {
            var soft = file.GetSoftDeleteFileFor();

            if (soft != null)
            {
                soft.Delete();
            }
            var allBackups = BackupFile.GetAllBackupsFor(file, Setting);

            foreach (var backup in allBackups)
            {
                if (backup.File != null)
                {
                    backup.File.Delete();
                }
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public virtual void AfterSuccessfulSave(FileInfo file)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.ExtensionIsNotAnyOf(file, BackupExtensions, "file");
            file.DeleteSoftDeleteFileFor();
            var allBackups = BackupFile.GetAllBackupsFor(file, Setting);

            if (allBackups.Count == 0)
            {
                return;
            }

            foreach (var backup in allBackups)
            {
                backup.File.DeleteSoftDeleteFileFor();
            }

            if (Setting.NumberOfBackups > 0)
            {
                while (allBackups.Count > Setting.NumberOfBackups) // this is not efficient but the number of backups should be low
                {
                    var backupFile = allBackups.MinBy(x => x.TimeStamp);
                    backupFile.File.HardDelete();
                    allBackups.Remove(backupFile);
                }
            }

            if (Setting.MaxAgeInDays > 0 && Setting.MaxAgeInDays < Int32.MaxValue)
            {
                while (true) // this is not efficient but the number of backups should be low
                {
                    var backupFile = allBackups.MinBy(x => x.TimeStamp);
                    var days       = (DateTime.Now - backupFile.TimeStamp).Days;
                    if (days < Setting.MaxAgeInDays)
                    {
                        break;
                    }
                    backupFile.File.HardDelete();
                    allBackups.Remove(backupFile);
                }
            }
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public virtual bool BeforeSave(FileInfo file)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.ExtensionIsNotAnyOf(file, BackupExtensions, "file");
            file.Refresh();
            if (!file.Exists)
            {
                return(false);
            }

            if (!Setting.IsCreatingBackups)
            {
                var softDelete = file.SoftDelete();
                return(softDelete != null);
            }
            var backupFile = BackupFile.CreateFor(file, Setting);

            Backup(file, backupFile);
            return(true);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        internal virtual void Restore(FileInfo file)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.ExtensionIsNotAnyOf(file, BackupExtensions, "file");

            var softDelete = file.WithAppendedExtension(FileHelper.SoftDeleteExtension);

            if (softDelete.Exists)
            {
                Restore(file, softDelete);
                return;
            }

            var backup = BackupFile.GetRestoreFileFor(file, Setting);

            if (backup != null)
            {
                Restore(file, backup);
            }
        }