Skip to content

Commit

Permalink
update regex
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Oct 16, 2023
1 parent 3556fa8 commit 0e5def7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
53 changes: 32 additions & 21 deletions src/modules/mix.portal/Domain/Services/MixApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Mix.Portal.Domain.Services
{
public sealed class MixApplicationService : TenantServiceBase, IMixApplicationService
{
static string[] excludeFileNames = { "jquery" };
private readonly IQueueService<MessageQueueModel> _queueService;
private readonly IThemeService _themeService;
private readonly MixIdentityService _mixIdentityService;
Expand Down Expand Up @@ -66,9 +67,9 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
{
try
{
templateId = await ReplaceIndex(templateId, name, deployUrl, baseHref);
var files = MixFileHelper.GetTopFiles(deployUrl, true);
var folders = string.Join('|', MixFileHelper.GetTopDirectories(deployUrl));
templateId = await ReplaceIndex(templateId, name, deployUrl, baseHref);
foreach (var file in files)
{
if (file.Extension == MixFileExtensions.Js || file.Extension == MixFileExtensions.Css)
Expand Down Expand Up @@ -97,11 +98,11 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
throw new MixException(MixErrorStatus.Badrequest, "Invalid Application Package");
}

Regex regex = new("((?<=src=\")|(?<=href=\"))(?!(http[^\\s]+))(.+?)(\\.+?)");

Regex regex = new("((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
Regex baseHrefRegex = new("(base href=\"(.+?)\")");
indexFile.Content = indexFile.Content.Replace("[basePath]/", string.Empty);
indexFile.Content = regex.Replace(indexFile.Content, $"/{deployUrl}/$3$4");
indexFile.Content = regex.Replace(indexFile.Content, $"$2/{deployUrl}/$5.$7");
indexFile.Content = baseHrefRegex.Replace(indexFile.Content, $"base href=\"{baseHref}\"")
.Replace("[baseRoute]", deployUrl)

Expand Down Expand Up @@ -137,29 +138,39 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
}
}

private Task ReplaceContent(FileModel file, string folders, string appFolder)
private Task ReplaceContent(FileModel file, string folders, string deployUrl)
{
if (!string.IsNullOrEmpty(file.Content))
if (!string.IsNullOrEmpty(file.Content) && !excludeFileNames.Contains(file.Filename))
{
_ = AlertAsync(_hubContext.Clients.Group("Theme"), "Status", 200, $"Modifying {file.Filename}{file.Extension}");
Regex rg = new($"((\\\"|\\')(({folders})))");
Regex rgSplash = new($"((\\.\\/)({folders}))");
Regex rgSplash1 = new($"((\\(\\/|\\`\\/)(({folders})))");
if (rgSplash.IsMatch(file.Content))
try
{
file.Content = rgSplash.Replace(file.Content, $"/{appFolder}/$3");
}
if (rgSplash1.IsMatch(file.Content))
{
file.Content = rgSplash1.Replace(file.Content, $"$2{appFolder}/$3");
_ = AlertAsync(_hubContext.Clients.Group("Theme"), "Status", 200, $"Modifying {file.Filename}{file.Extension}");
Regex rg = null;
if (string.IsNullOrEmpty(folders))
{
rg = new($"((\\\"|\\'|\\(|\\`){1}(\\.)?(\\/)?(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
if (rg.IsMatch(file.Content))
{
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5.$7");
}
}
else
{
rg = new($"((\\\"|\\'|\\(|\\`)(\\.)?(\\/)?({folders})(([0-9a-zA-Z\\/\\._-])+).(js|css|svg|png|PNG|gif|GIF|jp[e]?g|JP[E]?G))");
if (rg.IsMatch(file.Content))
{
file.Content = rg.Replace(file.Content, $"$2/{deployUrl}/$5$6.$8");
}
}

file.Content = file.Content.Replace("[basePath]", $"/{deployUrl}");

MixFileHelper.SaveFile(file);
}
if (rg.IsMatch(file.Content))
catch (Exception ex)
{
file.Content = rg.Replace(file.Content, $"$2/{appFolder}/$3");
throw new MixException(MixErrorStatus.ServerError, ex);
}
file.Content = file.Content.Replace("[basePath]", $"/{appFolder}");

MixFileHelper.SaveFile(file);
}
return Task.CompletedTask;
}
Expand Down
23 changes: 11 additions & 12 deletions src/platform/mix.service/Services/MixPermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.EntityFrameworkCore;
using Mix.Constant.Constants;
using Mix.Database.Entities.Account;
using Mix.Database.Entities.Cms;
using Mix.Database.Entities.MixDb;
using Mix.Database.Services;
using Mix.Heart.Exceptions;
Expand Down Expand Up @@ -33,8 +32,11 @@ public async Task Reload()
{
if (!GlobalConfigService.Instance.IsInit)
{
UnitOfWorkInfo<MixCmsContext> cmsUow = new(new MixCmsContext(_databaseService));
UnitOfWorkInfo<MixDbDbContext> mixdbUow = new(new MixDbDbContext(_databaseService));
if (_databaseService.DatabaseProvider == Heart.Enums.MixDatabaseProvider.SQLITE)
{
return;
}
UnitOfWorkInfo<MixDbDbContext> uow = new(new MixDbDbContext(_databaseService));
try
{
RoleEndpoints = new Dictionary<string, string[]>();
Expand All @@ -44,19 +46,17 @@ public async Task Reload()

foreach (var role in roles)
{
var permissionIds = cmsUow.DbContext.MixDatabaseAssociation
var permissionIds = uow.DbContext.MixDatabaseAssociation
.Where(m => m.GuidParentId == role.Id)
.Select(m => m.ChildId)
.ToList();
var endpointIds = cmsUow.DbContext.MixDatabaseAssociation
.Select(m => m.ChildId);
var endpointIds = uow.DbContext.MixDatabaseAssociation
.Where(m => m.ParentDatabaseName == MixDatabaseNames.SYSTEM_PERMISSION
&& m.ChildDatabaseName == MixDatabaseNames.SYSTEM_PERMISSION_ENDPOINT
&& permissionIds.Contains(m.ParentId))
.Select(m => m.ChildId)
.ToList();
.Select(m => m.ChildId);

// TODO: PermissionEndpoint cannot initial at first time
var endpoints = await mixdbUow.DbContext.PermissionEndpoint.Where(
var endpoints = await uow.DbContext.PermissionEndpoint.Where(
m => endpointIds.Contains(m.Id)
&& !string.IsNullOrEmpty(m.Path)
)
Expand Down Expand Up @@ -87,8 +87,7 @@ public async Task Reload()
}
finally
{
cmsUow.Dispose();
mixdbUow.Dispose();
uow.Dispose();
accountDbContext.Dispose();
}
}
Expand Down

0 comments on commit 0e5def7

Please sign in to comment.