mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
rework messages
+ addition LocalStorageService + tests
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using Govor.API.Services.Authentication.Interfaces;
|
||||
using Govor.Core;
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Core.Services;
|
||||
using Govor.Data.Repositories;
|
||||
using Govor.API.Services;
|
||||
|
||||
|
||||
namespace Govor.API.Services.Authentication;
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Govor.Core.Models;
|
||||
|
||||
namespace Govor.API.Services.Authentication.Interfaces;
|
||||
|
||||
public interface IAccountService
|
||||
{
|
||||
public Task<string> RegistrationAsync(string name, string password);
|
||||
public Task<string> LoginAsync(string name, string password);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Govor.Core.Models;
|
||||
|
||||
namespace Govor.API.Services.Authentication.Interfaces;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
string GenerateJwtToken(User user);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Govor.API.Services.Authentication.Interfaces;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using Govor.Core.Models;
|
||||
|
||||
namespace Govor.API.Services;
|
||||
|
||||
public interface IGroupService
|
||||
{
|
||||
ChatGroup GetGroupByInvite(string code);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Microsoft.AspNetCore.Http; // для IFormFile
|
||||
|
||||
namespace Govor.API.Services;
|
||||
|
||||
public interface IStorageService
|
||||
{
|
||||
Task<string> SaveAsync(byte[] data, string fileName);
|
||||
Task<Stream> LoadAsync(string url);
|
||||
Task RemoveAsync(string url);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
namespace Govor.API.Services;
|
||||
|
||||
public class LocalStorageService : IStorageService
|
||||
{
|
||||
private readonly string _storagePath;
|
||||
|
||||
public LocalStorageService(IWebHostEnvironment hostingEnvironment)
|
||||
{
|
||||
_storagePath = Path.Combine(hostingEnvironment.ContentRootPath, "uploads");
|
||||
|
||||
if (!Directory.Exists(_storagePath))
|
||||
{
|
||||
Directory.CreateDirectory(_storagePath);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> SaveAsync(byte[] data, string fileName)
|
||||
{
|
||||
if(data is null || data.Length == 0)
|
||||
throw new ArgumentException("Invalid file", nameof(data));
|
||||
|
||||
if(fileName is null || fileName.Length == 0)
|
||||
throw new ArgumentException("Invalid file name", nameof(fileName));
|
||||
|
||||
var date = DateTime.UtcNow.ToString("yyyy/MM");
|
||||
|
||||
var folder = Path.Combine(_storagePath, date);
|
||||
Directory.CreateDirectory(folder);
|
||||
|
||||
var uniqueFileName = $"{Guid.NewGuid()}_{Path.GetFileName(fileName)}";
|
||||
var fullPath = Path.Combine(folder, uniqueFileName);
|
||||
|
||||
await using var stream = new FileStream(fullPath, FileMode.Create);
|
||||
stream.WriteAsync(data, 0, data.Length);
|
||||
|
||||
return Path.Combine(date, uniqueFileName);
|
||||
}
|
||||
|
||||
public async Task<Stream> LoadAsync(string url)
|
||||
{
|
||||
var filePath = Path.Combine(_storagePath, Path.GetFileName(url));
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
throw new FileNotFoundException("File not found.", filePath);
|
||||
|
||||
await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
return file;
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(string url)
|
||||
{
|
||||
var path = Path.Combine(_storagePath, Path.GetFileName(url));
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user