using Nop.Core; using Nop.Core.Infrastructure; namespace Nop.Services.Common; /// /// Maintenance service /// public partial class MaintenanceService : IMaintenanceService { #region Fields protected readonly INopFileProvider _fileProvider; #endregion #region Ctor public MaintenanceService(INopFileProvider fileProvider) { _fileProvider = fileProvider; } #endregion #region Utilities /// /// Get directory path for backs /// /// A value indicating whether a directory should be created if it doesn't exist /// protected virtual string GetBackupDirectoryPath(bool ensureFolderCreated = true) { var path = _fileProvider.GetAbsolutePath(NopCommonDefaults.DbBackupsPath); if (ensureFolderCreated) _fileProvider.CreateDirectory(path); return path; } #endregion #region Methods /// /// Gets all backup files /// /// Backup file collection public virtual IList GetAllBackupFiles() { var path = GetBackupDirectoryPath(); if (!_fileProvider.DirectoryExists(path)) throw new NopException("Backup directory not exists"); return _fileProvider.GetFiles(path, $"*.{NopCommonDefaults.DbBackupFileExtension}") .OrderByDescending(p => _fileProvider.GetLastWriteTime(p)).ToList(); } /// /// Returns the path to the backup file /// /// The name of the backup file /// The path to the backup file public virtual string GetBackupPath(string backupFileName) { return _fileProvider.Combine(GetBackupDirectoryPath(), backupFileName); } /// /// Creates a path to a new database backup file /// /// Path to a new database backup file public virtual string CreateNewBackupFilePath() { return _fileProvider.Combine(GetBackupDirectoryPath(), $"database_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}_{CommonHelper.GenerateRandomDigitCode(10)}.{NopCommonDefaults.DbBackupFileExtension}"); } #endregion }