Skip to content

Commit

Permalink
Added import functionality for redirects module (#284)
Browse files Browse the repository at this point in the history
* Draft

* Added most logic

* Cleanup / some improvemend in statusses / statii?

* Minor changes based on pr feedback

* Added check if url doesnt appear double in importfile

* Added check for exact match

---------

Co-authored-by: Ambert van Unen <[email protected]>
Co-authored-by: Ambert <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent cc1ba69 commit 97d4600
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using Umbraco.Cms.Web.Common.ApplicationBuilder;
using Umbraco.Extensions;
using SeoToolkit.Umbraco.Common.Core.Constants;
using SeoToolkit.Umbraco.Common.Core.Extensions;
using SeoToolkit.Umbraco.Common.Core.Services.SettingsService;
using SeoToolkit.Umbraco.Redirects.Core.Components;
using SeoToolkit.Umbraco.Redirects.Core.Config;
using SeoToolkit.Umbraco.Redirects.Core.Config.Models;
using SeoToolkit.Umbraco.Redirects.Core.Controllers;
using SeoToolkit.Umbraco.Redirects.Core.Helpers;
using SeoToolkit.Umbraco.Redirects.Core.Interfaces;
using SeoToolkit.Umbraco.Redirects.Core.Middleware;
using SeoToolkit.Umbraco.Redirects.Core.Repositories;
Expand Down Expand Up @@ -42,11 +42,12 @@ public void Compose(IUmbracoBuilder builder)
{
builder.Trees().RemoveTreeController<RedirectsTreeController>();
}

builder.Components().Append<EnableModuleComponent>();

builder.Services.AddUnique<IRedirectsRepository, RedirectsRepository>();
builder.Services.AddUnique<IRedirectsService, RedirectsService>();
builder.Services.AddTransient<RedirectsImportHelper>();

if (!disabledModules.Contains(DisabledModuleConstant.Middleware))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SeoToolkit.Umbraco.Redirects.Core.Constants;

public class ImportConstants
{
public const string SessionAlias = "uploadedImportRedirectsFile";
public const string SessionFileTypeAlias = "uploadedFileType";
public const string SessionDomainId = "selectedDomain";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Linq;
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SeoToolkit.Umbraco.Redirects.Core.Constants;
using SeoToolkit.Umbraco.Redirects.Core.Enumerators;
using SeoToolkit.Umbraco.Redirects.Core.Helpers;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
Expand All @@ -22,16 +27,19 @@ public class RedirectsController : UmbracoAuthorizedApiController
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly ILocalizationService _localizationService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly RedirectsImportHelper _redirectsImportHelper;


public RedirectsController(IRedirectsService redirectsService,
IUmbracoContextFactory umbracoContextFactory,
ILocalizationService localizationService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
IBackOfficeSecurityAccessor backOfficeSecurityAccessor, RedirectsImportHelper redirectsImportHelper)
{
_redirectsService = redirectsService;
_umbracoContextFactory = umbracoContextFactory;
_localizationService = localizationService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_redirectsImportHelper = redirectsImportHelper;
}

[HttpPost]
Expand Down Expand Up @@ -134,5 +142,63 @@ public IActionResult Delete(DeleteRedirectsPostModel postModel)
_redirectsService.Delete(postModel.Ids);
return GetAll(1, 20);
}

[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
public IActionResult Validate(ImportRedirectsFileExtension fileExtension, string domain, IFormFile file)
{
if (file.Length == 0)
{
return BadRequest("Please select a file");
}

using var memoryStream = new MemoryStream();
file.CopyTo(memoryStream);

var result = _redirectsImportHelper.Validate(fileExtension, memoryStream, domain);
if (result.Success)
{

// Storing the file contents in session for later import
HttpContext.Session.Set(ImportConstants.SessionAlias, memoryStream.ToArray());
HttpContext.Session.SetString(ImportConstants.SessionFileTypeAlias, fileExtension.ToString());
HttpContext.Session.SetString(ImportConstants.SessionDomainId, domain);

return Ok();
}

return UnprocessableEntity(!string.IsNullOrWhiteSpace(result.Status) ? result.Status : "Something went wrong during the validation");
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
public IActionResult Import()
{
var fileContent = HttpContext.Session.Get(ImportConstants.SessionAlias);
var fileExtensionString = HttpContext.Session.GetString(ImportConstants.SessionFileTypeAlias);
var domain= HttpContext.Session.GetString(ImportConstants.SessionDomainId);

if (fileContent == null || fileExtensionString == null || domain == null)
{
return BadRequest("Something went wrong during import, please try again");
}

if (!Enum.TryParse(fileExtensionString, out ImportRedirectsFileExtension fileExtension))
{
return UnprocessableEntity("Invalid file extension.");
}

using var memoryStream = new MemoryStream(fileContent);
var result = _redirectsImportHelper.Import(fileExtension, memoryStream, domain);
if (result.Success)
{
return Ok();
}

return UnprocessableEntity(!string.IsNullOrWhiteSpace(result.Status) ? result.Status : "Something went wrong during the import");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeoToolkit.Umbraco.Redirects.Core.Enumerators;

public enum ImportRedirectsFileExtension
{
Csv,
Excel
}
Loading

0 comments on commit 97d4600

Please sign in to comment.