refactor: make exporting consistent

This commit is contained in:
Gary Sharp
2025-02-06 19:14:36 +11:00
parent f946f3250c
commit 67f1c2a5d1
69 changed files with 908 additions and 921 deletions
@@ -409,7 +409,6 @@ namespace Disco.Web.Areas.API.Controllers
#endregion
#region Exporting
internal const string ExportSessionCacheKey = "UserFlagExportContext_{0}";
[DiscoAuthorize(Claims.Config.UserFlag.Export)]
public virtual ActionResult Export(ExportModel Model)
@@ -418,39 +417,26 @@ namespace Disco.Web.Areas.API.Controllers
throw new ArgumentNullException(nameof(Model));
// Start Export
var exportContext = UserFlagExportTask.ScheduleNow(Model.Options);
// Store Export Context in Web Cache
string key = string.Format(ExportSessionCacheKey, exportContext.TaskStatus.SessionId);
HttpRuntime.Cache.Insert(key, exportContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
// Set Task Finished Url
var finishedActionResult = MVC.Config.UserFlag.Export(exportContext.TaskStatus.SessionId, null, null);
exportContext.TaskStatus.SetFinishedUrl(Url.Action(finishedActionResult));
var exportContext = new UserFlagExportContext(Model.Options);
var taskContext = ExportTask.ScheduleNowCacheResult(exportContext, id => Url.Action(MVC.Config.UserFlag.Export(id, null, null)));
// Try waiting for completion
if (exportContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(2)))
return RedirectToAction(finishedActionResult);
if (taskContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(1)))
return RedirectToAction(MVC.Config.UserFlag.Export(taskContext.Id, null, null));
else
return RedirectToAction(MVC.Config.Logging.TaskStatus(exportContext.TaskStatus.SessionId));
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskContext.TaskStatus.SessionId));
}
[DiscoAuthorize(Claims.Config.UserFlag.Export)]
public virtual ActionResult ExportRetrieve(string Id)
public virtual ActionResult ExportRetrieve(Guid id)
{
if (string.IsNullOrWhiteSpace(Id))
throw new ArgumentNullException("Id");
string key = string.Format(ExportSessionCacheKey, Id);
var context = HttpRuntime.Cache.Get(key) as ExportTaskContext<UserFlagExportOptions>;
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", nameof(Id));
if (!ExportTask.TryFromCache(id, out var context))
throw new ArgumentException("The export id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(Id));
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(id));
if (context.Result.RecordCount == 0)
throw new ArgumentException("No records were found to export", nameof(Id));
throw new ArgumentException("No records were found to export", nameof(id));
var fileStream = context.Result.Result;