Skip to content

Commit

Permalink
Merge pull request #20 from NerosoftDev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Codespilot authored Jan 27, 2024
2 parents d37930a + 4bd9ac9 commit 28ac261
Show file tree
Hide file tree
Showing 24 changed files with 326 additions and 151 deletions.
6 changes: 5 additions & 1 deletion Source/Starfish.Client/Starfish.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="True" PackagePath="" />
<None Include="README.md" Pack="True" PackagePath="" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Starfish.Common\Starfish.Common.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 2 additions & 26 deletions Source/Starfish.Client/StarfishConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO.Compression;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Nerosoft.Starfish.Common;

namespace Nerosoft.Starfish.Client;

Expand Down Expand Up @@ -66,7 +67,7 @@ private async void OnHostChanged(object sender, HostChangedEventArgs args)
{
await client.GetConfigurationAsync((data, length) =>
{
var json = Decompress(data, length);
var json = GzipHelper.Decompress(data, length);
File.WriteAllText(_cacheFile, json, Encoding.UTF8);
if (_waitHandle.IsSet)
{
Expand All @@ -85,31 +86,6 @@ await client.GetConfigurationAsync((data, length) =>
}
}

private static string Decompress(byte[] data, int count)
{
var stream = new MemoryStream(data, 0, count);
var zip = new GZipStream(stream, CompressionMode.Decompress, true);
var destStream = new MemoryStream();
var buffer = new byte[0x1000];
while (true)
{
var reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}

destStream.Write(buffer, 0, reader);
}

zip.Close();
stream.Close();
destStream.Position = 0;
buffer = destStream.ToArray();
destStream.Close();
return Encoding.UTF8.GetString(buffer);
}

public void Dispose()
{
_waitHandle.Dispose();
Expand Down
64 changes: 64 additions & 0 deletions Source/Starfish.Common/GzipHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.IO.Compression;

namespace Nerosoft.Starfish.Common;
public static class GzipHelper
{
public static string CompressToBase64(string source)
{
var buffer = Compress(source);

return Convert.ToBase64String(buffer);
}

public static byte[] Compress(string source)
{
var data = Encoding.UTF8.GetBytes(source);

var stream = new MemoryStream();
var zip = new GZipStream(stream, CompressionMode.Compress, true);
zip.Write(data, 0, data.Length);
zip.Close();
var buffer = new byte[stream.Length];
stream.Position = 0;
_ = stream.Read(buffer, 0, buffer.Length);
stream.Close();

return buffer;
}

public static string DecompressFromBase64(string base64Data)
{
var data = Convert.FromBase64String(base64Data);
return Decompress(data);
}

public static string Decompress(byte[] data)
{
return Decompress(data, data.Length);
}

public static string Decompress(byte[] data, int count)
{
var stream = new MemoryStream(data, 0, count);
var zip = new GZipStream(stream, CompressionMode.Decompress, true);
var destStream = new MemoryStream();
var buffer = new byte[0x1000];
while (true)
{
var reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}

destStream.Write(buffer, 0, reader);
}

zip.Close();
stream.Close();
destStream.Position = 0;
buffer = destStream.ToArray();
destStream.Close();
return Encoding.UTF8.GetString(buffer);
}
}
3 changes: 3 additions & 0 deletions Source/Starfish.Common/Starfish.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.Text.Json" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ public interface ISettingApplicationService : IApplicationService
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<string> GetItemsInTextAsync(long appId, string environment, string format, CancellationToken cancellationToken = default);

Task PushRedisAsync(long appId, string environment, PushRedisRequestDto data, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Task PublishAsync(long appId, string environment, SettingPublishDto data,
public Task<string> GetSettingRawAsync(long appId, string environment, CancellationToken cancellationToken = default)
{
var useCase = LazyServiceProvider.GetRequiredService<IGetSettingRawUseCase>();
var input = new GetSettingRawUseCaseInput(appId, environment);
var input = new GetSettingRawInput(appId, environment);
return useCase.ExecuteAsync(input, cancellationToken)
.ContinueWith(t => t.Result.Result, cancellationToken);
}
Expand All @@ -101,4 +101,11 @@ public Task<string> GetItemsInTextAsync(long appId, string environment, string f
return Cryptography.Base64.Encrypt(text);
}, cancellationToken);
}

public Task PushRedisAsync(long appId, string environment, PushRedisRequestDto data, CancellationToken cancellationToken = default)
{
var useCase = LazyServiceProvider.GetRequiredService<IPushRedisUseCase>();
var input = new PushRedisInput(appId, environment, data);
return useCase.ExecuteAsync(input, cancellationToken);
}
}
4 changes: 0 additions & 4 deletions Source/Starfish.Service/Domain/Aggregates/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,4 @@ internal void CreateRevision(string version, string comment, string @operator)
Version = version;
PublishTime = DateTime.Now;
}

internal void Archive(string @operator)
{
}
}
22 changes: 3 additions & 19 deletions Source/Starfish.Service/Domain/Business/SettingArchiveBusiness.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO.Compression;
using Nerosoft.Euonia.Business;
using Nerosoft.Euonia.Business;
using Nerosoft.Euonia.Domain;
using Nerosoft.Starfish.Common;
using Newtonsoft.Json;

namespace Nerosoft.Starfish.Domain;
Expand Down Expand Up @@ -30,7 +30,7 @@ protected async Task ExecuteAsync(long appId, string environment, string userNam

archive ??= SettingArchive.Create(appId, environment);

archive.Update(Compress(json), userName);
archive.Update(GzipHelper.CompressToBase64(json), userName);

if (archive.Id > 0)
{
Expand All @@ -41,20 +41,4 @@ protected async Task ExecuteAsync(long appId, string environment, string userNam
await ArchiveRepository.InsertAsync(archive, true, cancellationToken);
}
}

private static string Compress(string source)
{
var data = Encoding.UTF8.GetBytes(source);

var stream = new MemoryStream();
var zip = new GZipStream(stream, CompressionMode.Compress, true);
zip.Write(data, 0, data.Length);
zip.Close();
var buffer = new byte[stream.Length];
stream.Position = 0;
_ = stream.Read(buffer, 0, buffer.Length);
stream.Close();

return Convert.ToBase64String(buffer);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nerosoft.Euonia.Business;
using Google.Protobuf.WellKnownTypes;
using Nerosoft.Euonia.Business;
using Nerosoft.Starfish.Service;

// ReSharper disable MemberCanBePrivate.Global
Expand Down Expand Up @@ -87,6 +88,19 @@ protected async Task FetchAsync(long appId, string environment, CancellationToke
[FactoryInsert]
protected override async Task InsertAsync(CancellationToken cancellationToken = default)
{
var permission = await AppInfoRepository.CheckPermissionAsync(AppId, Identity.GetUserIdOfInt64(), cancellationToken);

switch (permission)
{
case 0:
throw new UnauthorizedAccessException();
case 1:
case 2:
break;
default:
throw new ArgumentOutOfRangeException();
}

var appInfo = await AppInfoRepository.GetAsync(AppId, cancellationToken);

if (appInfo == null)
Expand Down
31 changes: 24 additions & 7 deletions Source/Starfish.Service/Domain/Business/SettingPublishBusiness.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nerosoft.Euonia.Business;
using Nerosoft.Euonia.Claims;
using Nerosoft.Euonia.Domain;

// ReSharper disable UnusedMember.Global
Expand All @@ -10,17 +11,33 @@ namespace Nerosoft.Starfish.Domain;
/// </summary>
public class SettingPublishBusiness : CommandObject<SettingPublishBusiness>, IDomainService
{
private readonly ISettingRepository _repository;
[Inject]
public IAppInfoRepository AppInfoRepository { get; set; }

public SettingPublishBusiness(ISettingRepository repository)
{
_repository = repository;
}
[Inject]
public ISettingRepository SettingRepository { get; set; }

[Inject]
public UserPrincipal Identity { get; set; }

[FactoryExecute]
protected async Task ExecuteAsync(long appId, string environment, CancellationToken cancellationToken = default)
{
var aggregate = await _repository.GetAsync(appId, environment, true, [], cancellationToken);
var permission = await AppInfoRepository.CheckPermissionAsync(appId, Identity.GetUserIdOfInt64(), cancellationToken);

switch(permission)
{
case 0:
throw new UnauthorizedAccessException();
case 1:
break;
case 2:
throw new UnauthorizedAccessException();
default:
throw new ArgumentOutOfRangeException();
}

var aggregate = await SettingRepository.GetAsync(appId, environment, true, [], cancellationToken);

if (aggregate == null)
{
Expand All @@ -34,6 +51,6 @@ protected async Task ExecuteAsync(long appId, string environment, CancellationTo

aggregate.SetStatus(SettingStatus.Published);

await _repository.UpdateAsync(aggregate, true, cancellationToken);
await SettingRepository.UpdateAsync(aggregate, true, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface IAppInfoRepository : IBaseRepository<DataContext, AppInfo, long
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<AppInfo> GetByCodeAsync(string code, CancellationToken cancellationToken = default);

Task<int> CheckPermissionAsync(long appId, long userId, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nerosoft.Euonia.Repository;
using Microsoft.EntityFrameworkCore;
using Nerosoft.Euonia.Repository;
using Nerosoft.Starfish.Domain;
using Nerosoft.Starfish.Service;

Expand Down Expand Up @@ -30,4 +31,27 @@ public Task<AppInfo> GetByCodeAsync(string code, CancellationToken cancellationT
var predicate = AppInfoSpecification.CodeEquals(code).Satisfy();
return GetAsync(predicate, null, cancellationToken);
}

public async Task<int> CheckPermissionAsync(long appId, long userId, CancellationToken cancellationToken = default)
{
var query = from app in Context.Set<AppInfo>()
join team in Context.Set<Team>() on app.TeamId equals team.Id
join member in Context.Set<TeamMember>() on team.Id equals member.TeamId
where app.Id == appId
select new
{
member.UserId,
IsOwner = member.UserId == team.OwnerId
};

var result = await query.ToListAsync(cancellationToken);

var user = result.FirstOrDefault(x => x.UserId == userId);
if (user == null)
{
return 0;
}

return user.IsOwner ? 1 : 2;
}
}
18 changes: 4 additions & 14 deletions Source/Starfish.Service/UseCases/Setting/GetSettingRawUseCase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using Nerosoft.Euonia.Application;
using Nerosoft.Euonia.Linq;
using Nerosoft.Starfish.Domain;
using Nerosoft.Starfish.Repository;

namespace Nerosoft.Starfish.UseCases;

public interface IGetSettingRawUseCase : IUseCase<GetSettingRawUseCaseInput, GetSettingRawUseCaseOutput>;
public interface IGetSettingRawUseCase : IUseCase<GetSettingRawInput, GetSettingRawUseCaseOutput>;

public record GetSettingRawUseCaseOutput(string Result) : IUseCaseOutput;

public record GetSettingRawUseCaseInput(long AppId, string Environment) : IUseCaseInput;
public record GetSettingRawInput(long AppId, string Environment) : IUseCaseInput;

public class GetSettingRawUseCase : IGetSettingRawUseCase
{
Expand All @@ -20,17 +18,9 @@ public GetSettingRawUseCase(ISettingArchiveRepository repository)
_repository = repository;
}

public Task<GetSettingRawUseCaseOutput> ExecuteAsync(GetSettingRawUseCaseInput input, CancellationToken cancellationToken = default)
public Task<GetSettingRawUseCaseOutput> ExecuteAsync(GetSettingRawInput input, CancellationToken cancellationToken = default)
{
ISpecification<SettingArchive>[] specifications =
{
SettingArchiveSpecification.AppIdEquals(input.AppId),
SettingArchiveSpecification.EnvironmentEquals(input.Environment)
};

var predicate = new CompositeSpecification<SettingArchive>(PredicateOperator.AndAlso, specifications).Satisfy();

return _repository.GetAsync(predicate, cancellationToken)
return _repository.GetAsync(input.AppId, input.Environment, cancellationToken)
.ContinueWith(t => new GetSettingRawUseCaseOutput(t.Result.Data), cancellationToken);
}
}
Loading

0 comments on commit 28ac261

Please sign in to comment.