Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added import functionality for redirects module #284

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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