improve module export so that content can be saved to a file

This commit is contained in:
sbwalker
2025-05-15 08:56:21 -04:00
parent f3fcef52dd
commit a49b8728fd
6 changed files with 164 additions and 14 deletions

View File

@ -9,6 +9,10 @@ using Oqtane.Infrastructure;
using Oqtane.Repository;
using Oqtane.Security;
using System.Net;
using System.IO;
using System;
using static System.Net.WebRequestMethods;
using System.Net.Http;
namespace Oqtane.Controllers
{
@ -20,18 +24,22 @@ namespace Oqtane.Controllers
private readonly IPageRepository _pages;
private readonly IModuleDefinitionRepository _moduleDefinitions;
private readonly ISettingRepository _settings;
private readonly IFolderRepository _folders;
private readonly IFileRepository _files;
private readonly IUserPermissions _userPermissions;
private readonly ISyncManager _syncManager;
private readonly ILogManager _logger;
private readonly Alias _alias;
public ModuleController(IModuleRepository modules, IPageModuleRepository pageModules, IPageRepository pages, IModuleDefinitionRepository moduleDefinitions, ISettingRepository settings, IUserPermissions userPermissions, ITenantManager tenantManager, ISyncManager syncManager, ILogManager logger)
public ModuleController(IModuleRepository modules, IPageModuleRepository pageModules, IPageRepository pages, IModuleDefinitionRepository moduleDefinitions, ISettingRepository settings, IFolderRepository folders, IFileRepository files, IUserPermissions userPermissions, ITenantManager tenantManager, ISyncManager syncManager, ILogManager logger)
{
_modules = modules;
_pageModules = pageModules;
_pages = pages;
_moduleDefinitions = moduleDefinitions;
_settings = settings;
_folders = folders;
_files = files;
_userPermissions = userPermissions;
_syncManager = syncManager;
_logger = logger;
@ -248,6 +256,62 @@ namespace Oqtane.Controllers
return content;
}
// POST api/<controller>/export?moduleid=x&pageid=y&folderid=z
[HttpPost("export")]
[Authorize(Roles = RoleNames.Registered)]
public Result Export(int moduleid, int pageid, int folderid)
{
var result = new Result(false);
var module = _modules.GetModule(moduleid);
if (module != null && module.SiteId == _alias.SiteId && _userPermissions.IsAuthorized(User, module.SiteId, EntityNames.Page, pageid, PermissionNames.Edit) &&
_userPermissions.IsAuthorized(User, module.SiteId, EntityNames.Folder, folderid, PermissionNames.Edit))
{
// get content
var content = _modules.ExportModule(moduleid);
// get folder
var folder = _folders.GetFolder(folderid, false);
string folderPath = _folders.GetFolderPath(folder);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// create text file
var filename = Utilities.GetTypeNameLastSegment(module.ModuleDefinitionName, 0) + moduleid.ToString() + ".json";
string filepath = Path.Combine(folderPath, filename);
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
}
System.IO.File.WriteAllText(filepath, content);
// register file
var file = _files.GetFile(folderid, filename);
if (file == null)
{
file = new Models.File { FolderId = folderid, Name = filename, Extension = "txt", Size = (int)new FileInfo(filepath).Length, ImageWidth = 0, ImageHeight = 0 };
_files.AddFile(file);
}
else
{
file.Size = (int)new FileInfo(filepath).Length;
_files.UpdateFile(file);
}
result.Success = true;
result.Message = filename;
_logger.Log(LogLevel.Information, this, LogFunction.Read, "Content Exported For Module {ModuleId} To Folder {FolderId}", moduleid, folderid);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Export Attempt For Module {Module} To Folder {FolderId}", moduleid, folderid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
return result;
}
// POST api/<controller>/import?moduleid=x&pageid=y
[HttpPost("import")]
[Authorize(Roles = RoleNames.Registered)]