Skip to content

Commit

Permalink
Add backend inProgress status to prevent multiple exports from a sing…
Browse files Browse the repository at this point in the history
…le user (in case of front-end interruption of the first).
  • Loading branch information
imnasnainaec committed Nov 23, 2020
1 parent bc84f77 commit 6f4db7f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
40 changes: 30 additions & 10 deletions Backend/Controllers/LiftController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,45 @@ internal async Task<IActionResult> ExportLiftFile(string projectId, string userI
return new UnsupportedMediaTypeResult();
}

// Ensure project exists and has words
// Ensure project exists
var proj = _projectService.GetProject(projectId);
if (proj is null)
{
return new NotFoundObjectResult(projectId);
}
var words = await _wordRepo.GetAllWords(projectId);
if (words.Count == 0)

// Check if another export started
if (_liftService.IsExportInProgress(userId))
{
return new BadRequestResult();
return new ConflictResult();
}

// Export the data to a zip, read into memory, and delete zip
var exportedFilepath = CreateLiftExport(projectId);
// Store in-progress status for the export
_liftService.SetExportInProgress(userId, true);

// Store the temporary path to the exported file for user to download later.
_liftService.StoreExport(userId, exportedFilepath);
await _notifyService.Clients.All.SendAsync("DownloadReady", userId);
return new OkObjectResult(projectId);
try
{
// Ensure project has words
var words = await _wordRepo.GetAllWords(projectId);
if (words.Count == 0)
{
_liftService.SetExportInProgress(userId, false);
return new BadRequestResult();
}

// Export the data to a zip, read into memory, and delete zip
var exportedFilepath = CreateLiftExport(projectId);

// Store the temporary path to the exported file for user to download later.
_liftService.StoreExport(userId, exportedFilepath);
await _notifyService.Clients.All.SendAsync("DownloadReady", userId);
return new OkObjectResult(projectId);
}
catch (Exception error)
{
_liftService.SetExportInProgress(userId, false);
throw error;
}
}

internal string CreateLiftExport(string projectId)
Expand Down
2 changes: 2 additions & 0 deletions Backend/Interfaces/ILiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ ILexiconMerger<LiftObject, LiftEntry, LiftSense, LiftExample> GetLiftImporterExp
void StoreExport(string key, string filePath);
string? RetrieveExport(string key);
bool DeleteExport(string key);
void SetExportInProgress(string key, bool isInProgress);
bool IsExportInProgress(string key);
}
}
23 changes: 22 additions & 1 deletion Backend/Services/LiftApiServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ public LiftService()
_liftExports = new Dictionary<string, string>();
}

private readonly string inProgress = "IN_PROGRESS";

/// <summary> Store status that a user's export is in-progress. </summary>
public void SetExportInProgress(string userId, bool isInProgress = true)
{
_liftExports.Remove(userId);
if (isInProgress)
{
_liftExports.Add(userId, inProgress);
}
}

/// <summary> Query whether user has an in-progress export. </summary>
public bool IsExportInProgress(string userId)
{
if (!_liftExports.ContainsKey(userId))
{
return false;
}
return _liftExports[userId] == inProgress;
}

/// <summary> Store filePath for a user's Lift export. </summary>
public void StoreExport(string userId, string filePath)
Expand All @@ -101,7 +122,7 @@ public void StoreExport(string userId, string filePath)
/// <returns> Path to the Lift file on disk. </returns>
public string? RetrieveExport(string userId)
{
if (!_liftExports.ContainsKey(userId))
if (!_liftExports.ContainsKey(userId) || _liftExports[userId] == inProgress)
{
return null;
}
Expand Down

0 comments on commit 6f4db7f

Please sign in to comment.