Exemplo n.º 1
0
        /// <inheritdoc/>
        public bool CanRename(FileInfo file, string newName)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.NotNullOrEmpty(newName, nameof(newName));
            var soft = file.GetSoftDeleteFileFor();

            if (soft.Exists)
            {
                if (!soft.CanRename(newName, Setting))
                {
                    return(false);
                }
            }

            var allBackups = BackupFile.GetAllBackupsFor(file, Setting);

            foreach (var backup in allBackups)
            {
                if (!backup.File.CanRename(newName, Setting))
                {
                    return(false);
                }
                soft = backup.File.GetSoftDeleteFileFor();
                if (soft.Exists)
                {
                    if (!soft.CanRename(newName, Setting))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public void Rename(FileInfo file, string newName, bool owerWrite)
        {
            Ensure.NotNull(file, nameof(file));
            Ensure.NotNullOrEmpty(newName, nameof(newName));
            var soft = file.GetSoftDeleteFileFor();

            if (soft.Exists)
            {
                var withNewName = soft.WithNewName(newName, Setting);
                soft.Rename(withNewName, true);
            }

            var allBackups = BackupFile.GetAllBackupsFor(file, Setting);

            foreach (var backup in allBackups)
            {
                var withNewName = backup.File.WithNewName(newName, Setting);
                backup.File.Rename(withNewName, owerWrite);
                soft = backup.File.GetSoftDeleteFileFor();
                if (soft.Exists)
                {
                    withNewName = soft.WithNewName(newName, Setting);
                    soft.Rename(withNewName, owerWrite);
                }
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public bool CanRestore(FileInfo file)
        {
            var softDelete = file.GetSoftDeleteFileFor();

            if (softDelete.Exists)
            {
                return(true);
            }
            var backups = BackupFile.GetAllBackupsFor(file, Setting);

            return(backups.Any());
        }
Exemplo n.º 4
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();
                }
            }
        }
Exemplo n.º 5
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);
                }
            }
        }