Skip to content

Commit

Permalink
Import Recipe from Json (#6255)
Browse files Browse the repository at this point in the history
  • Loading branch information
agriffard authored Jul 12, 2020
1 parent 1783ab6 commit 725fa64
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Deployment/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public Task BuildNavigationAsync(string name, NavigationBuilder builder)
.Permission(Permissions.Import)
.LocalNav()
)
.Add(S["JSON Import"], S["JSON Import"].PrefixPosition(), deployment => deployment
.Action("Json", "Import", new { area = "OrchardCore.Deployment" })
.Permission(Permissions.Import)
.LocalNav()
)
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Localization;
using OrchardCore.Admin;
using OrchardCore.Deployment.Services;
using OrchardCore.Deployment.ViewModels;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.Deployment.Controllers
{
Expand All @@ -19,19 +22,22 @@ public class ImportController : Controller
private readonly IAuthorizationService _authorizationService;
private readonly INotifier _notifier;
private readonly IHtmlLocalizer H;
private readonly IStringLocalizer S;

public ImportController(
IDeploymentManager deploymentManager,
IAuthorizationService authorizationService,
INotifier notifier,
IHtmlLocalizer<ImportController> localizer
IHtmlLocalizer<ImportController> htmlLocalizer,
IStringLocalizer<ImportController> stringLocalizer
)
{
_deploymentManager = deploymentManager;
_authorizationService = authorizationService;
_notifier = notifier;

H = localizer;
H = htmlLocalizer;
S = stringLocalizer;
}

public async Task<IActionResult> Index()
Expand Down Expand Up @@ -82,7 +88,7 @@ public async Task<IActionResult> Import(IFormFile importedPackage)

await _deploymentManager.ImportDeploymentPackageAsync(new PhysicalFileProvider(tempArchiveFolder));

_notifier.Success(H["Deployment package imported"]);
_notifier.Success(H["Deployment package imported."]);
}
finally
{
Expand All @@ -102,7 +108,55 @@ public async Task<IActionResult> Import(IFormFile importedPackage)
_notifier.Error(H["Please add a file to import."]);
}

return RedirectToAction("Index");
return RedirectToAction(nameof(Index));
}

public async Task<IActionResult> Json()
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.Import))
{
return Forbid();
}

return View();
}

[HttpPost]
public async Task<IActionResult> Json(ImportJsonViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.Import))
{
return Forbid();
}

if (!model.Json.IsJson())
{
ModelState.AddModelError(nameof(model.Json), S["The recipe is written in an incorrect json format."]);
}

if (ModelState.IsValid)
{
var tempArchiveFolder = PathExtensions.Combine(Path.GetTempPath(), Path.GetRandomFileName());

try
{
Directory.CreateDirectory(tempArchiveFolder);
System.IO.File.WriteAllText(Path.Combine(tempArchiveFolder, "Recipe.json"), model.Json);

await _deploymentManager.ImportDeploymentPackageAsync(new PhysicalFileProvider(tempArchiveFolder));

_notifier.Success(H["Recipe imported."]);
}
finally
{
if (Directory.Exists(tempArchiveFolder))
{
Directory.Delete(tempArchiveFolder, true);
}
}
}

return View(model);
}
}
}
7 changes: 7 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Deployment/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro
defaults: new { controller = typeof(ImportController).ControllerName(), action = nameof(ImportController.Index) }
);

routes.MapAreaControllerRoute(
name: "DeploymentPlanImportJson",
areaName: "OrchardCore.Deployment",
pattern: _adminOptions.AdminUrlPrefix + "/DeploymentPlan/Import/Json",
defaults: new { controller = typeof(ImportController).ControllerName(), action = nameof(ImportController.Json) }
);

routes.MapAreaControllerRoute(
name: "DeploymentPlanExportFileExecute",
areaName: "OrchardCore.Deployment",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OrchardCore.Deployment.ViewModels
{
public class ImportJsonViewModel
{
public string Json { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@model OrchardCore.Deployment.ViewModels.ImportJsonViewModel
<h1>@RenderTitleSegments(T["JSON Import"])</h1>

<style asp-name="codemirror"></style>
<script asp-name="codemirror" depends-on="admin" at="Foot"></script>
<script asp-name="codemirror-addon-display-autorefresh" at="Foot"></script>
<script asp-name="codemirror-mode-javascript" at="Foot"></script>

@Html.ValidationSummary()

<form asp-action="Json">
<div class="form-group">
<textarea asp-for="Json" class="form-control mb-2" rows="20"></textarea>
</div>
<button class="btn btn-primary" type="submit">@T["Import"]</button>
</form>

<script at="Foot">
$(function () {
var textArea = document.getElementById('@Html.IdFor(x => x.Json)');
var editor = CodeMirror.fromTextArea(textArea, {
autoRefresh: true,
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: { name: "javascript" },
});
});
</script>

0 comments on commit 725fa64

Please sign in to comment.