develop #10
37
Serwer/Serwer.Api/Controllers/ImageController.cs
Normal file
37
Serwer/Serwer.Api/Controllers/ImageController.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serwer.Infrastructure.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Api.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
private readonly IImageService _imageService;
|
||||
public ImageController(IImageService imageService)
|
||||
{
|
||||
_imageService = imageService;
|
||||
}
|
||||
|
||||
[HttpPost("Process")]
|
||||
public async Task<IActionResult> Process(IFormFile image)
|
||||
{
|
||||
if(image == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await image.CopyToAsync(memoryStream);
|
||||
await _imageService.Process(image.Name, image.ContentType, memoryStream.ToArray());
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
@ -63,6 +63,7 @@ namespace Serwer.Api
|
||||
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
||||
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<IImageService, ImageService>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
}
|
||||
|
||||
@ -71,12 +72,17 @@ namespace Serwer.Api
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseCors(builder =>
|
||||
{
|
||||
builder.AllowAnyHeader();
|
||||
builder.AllowAnyMethod();
|
||||
builder.AllowAnyOrigin();
|
||||
});
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Serwer.Api v1"));
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
32
Serwer/Serwer.Core/Domain/File.cs
Normal file
32
Serwer/Serwer.Core/Domain/File.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Core.Domain
|
||||
{
|
||||
public class File
|
||||
{
|
||||
public string Name { get; protected set; }
|
||||
public string ContentType { get; protected set; }
|
||||
public byte[] Bytes { get; protected set; }
|
||||
public long SizeBytes => Bytes.Length;
|
||||
|
||||
protected File()
|
||||
{
|
||||
}
|
||||
|
||||
protected File(string name, string contentType, byte[] bytes)
|
||||
{
|
||||
Name = name;
|
||||
ContentType = contentType;
|
||||
Bytes = bytes;
|
||||
}
|
||||
|
||||
public static File Empty => new File();
|
||||
|
||||
public static File Create(string name, string contentType, byte[] bytes)
|
||||
=> new File(name, contentType, bytes);
|
||||
}
|
||||
}
|
13
Serwer/Serwer.Infrastructure/Services/IImageHandler.cs
Normal file
13
Serwer/Serwer.Infrastructure/Services/IImageHandler.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public interface IImageHandler
|
||||
{
|
||||
|
||||
}
|
||||
}
|
14
Serwer/Serwer.Infrastructure/Services/IImageService.cs
Normal file
14
Serwer/Serwer.Infrastructure/Services/IImageService.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Serwer.Core.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public interface IImageService
|
||||
{
|
||||
Task Process(string name, string contentType, byte[] bytes);
|
||||
}
|
||||
}
|
12
Serwer/Serwer.Infrastructure/Services/ImageHandler.cs
Normal file
12
Serwer/Serwer.Infrastructure/Services/ImageHandler.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
class ImageHandler
|
||||
{
|
||||
}
|
||||
}
|
19
Serwer/Serwer.Infrastructure/Services/ImageService.cs
Normal file
19
Serwer/Serwer.Infrastructure/Services/ImageService.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Serwer.Core.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public class ImageService : IImageService
|
||||
{
|
||||
private readonly ISet<File> _files = new HashSet<File>();
|
||||
public async Task Process(string name, string contentType, byte[] bytes)
|
||||
{
|
||||
var file = File.Create(name, contentType, bytes);
|
||||
await Task.FromResult(_files.Add(file));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user