Files
PowderCoatingLogix/src/PowderCoating.Web/Controllers/StorageMigrationController.cs
T
2026-04-23 21:38:24 -04:00

74 lines
2.8 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PowderCoating.Application.Interfaces;
using PowderCoating.Shared.Constants;
namespace PowderCoating.Web.Controllers;
/// <summary>
/// SuperAdmin-only tool for migrating locally stored media files from the on-disk
/// <c>media/</c> folder to Azure Blob Storage via
/// <see cref="IStorageMigrationService"/>. Intended to be run once (or a small number
/// of times) during a storage-backend transition — it is not a routine operation.
/// </summary>
[Authorize(Policy = AppConstants.Policies.SuperAdminOnly)]
public class StorageMigrationController : Controller
{
private readonly IStorageMigrationService _migrationService;
private readonly IWebHostEnvironment _environment;
public StorageMigrationController(
IStorageMigrationService migrationService,
IWebHostEnvironment environment)
{
_migrationService = migrationService;
_environment = environment;
}
/// <summary>
/// Renders the migration status page showing the local <c>media/</c> path, whether
/// the directory exists, and how many files it currently contains. This information
/// lets operators confirm there is work to migrate before triggering the potentially
/// long-running <see cref="Migrate"/> action.
/// </summary>
public IActionResult Index()
{
var mediaPath = Path.Combine(_environment.ContentRootPath, "media");
ViewBag.MediaPath = mediaPath;
ViewBag.MediaExists = Directory.Exists(mediaPath);
if (Directory.Exists(mediaPath))
{
var fileCount = Directory.EnumerateFiles(mediaPath, "*.*", SearchOption.AllDirectories).Count();
ViewBag.LocalFileCount = fileCount;
}
else
{
ViewBag.LocalFileCount = 0;
}
return View();
}
/// <summary>
/// Executes the migration of all local <c>media/</c> files to Azure Blob Storage
/// and renders a Results view with the outcome.
/// <para>
/// The <paramref name="deleteAfterMigration"/> flag controls whether source files
/// are removed from disk after successful upload. It defaults to <c>false</c> so
/// that operators can verify the migration result before committing to deletion.
/// The actual upload logic is encapsulated in <see cref="IStorageMigrationService"/>
/// so it can be tested independently of the HTTP layer.
/// </para>
/// </summary>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Migrate(bool deleteAfterMigration = false)
{
var mediaPath = Path.Combine(_environment.ContentRootPath, "media");
var result = await _migrationService.MigrateFilesystemToAzureAsync(mediaPath, deleteAfterMigration);
return View("Results", result);
}
}