POS_Sprint_3 #5

Merged
s426229 merged 10 commits from POS_Sprint_3 into develop 2020-12-30 17:30:24 +01:00
11 changed files with 135 additions and 42 deletions
Showing only changes of commit 362cfdcee0 - Show all commits

View File

@ -13,8 +13,8 @@ namespace Serwer.Api.Controllers
[ApiController] [ApiController]
public class ImageController : ControllerBase public class ImageController : ControllerBase
{ {
private readonly IImageService _imageService; private readonly IImageHandler _imageService;
public ImageController(IImageService imageService) public ImageController(IImageHandler imageService)
{ {
_imageService = imageService; _imageService = imageService;
} }

View File

@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serwer.Infrastructure.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Serwer.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SearchController : ControllerBase
{
private readonly ISearchService _searchService;
public SearchController(ISearchService searchService)
{
_searchService = searchService;
}
[HttpGet("{query}")]
public async Task<IActionResult> Search(string query)
{
var results = await _searchService.Search(query);
return Ok(results);
}
}
}

View File

@ -89,8 +89,9 @@ namespace Serwer.Api
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize()); services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings)); services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
services.AddScoped<IUserRepository, UserRepository>(); services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IImageService, ImageService>(); services.AddScoped<IImageHandler, ImageHandler>();
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
services.AddScoped<ISearchService, SearchService>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serwer.Core.Domain
{
public class SearchResult
{
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serwer.Infrastructure.DTO
{
public class SearchResultDTO
{
public string Title { get; set; }
public string Link { get; set; }
public string Snippet { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System; using Serwer.Core.Domain;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -8,6 +9,6 @@ namespace Serwer.Infrastructure.Services
{ {
public interface IImageHandler public interface IImageHandler
{ {
Task<string> Process(string name, string contentType, byte[] bytes);
} }
} }

View File

@ -1,14 +1,13 @@
using Serwer.Core.Domain; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Serwer.Infrastructure.DTO;
namespace Serwer.Infrastructure.Services namespace Serwer.Infrastructure.Services
{ {
public interface IImageService public interface ISearchService
{ {
Task<string> Process(string name, string contentType, byte[] bytes); Task<IList<SearchResultDTO>> Search(string query);
} }
} }

View File

@ -1,12 +1,30 @@
using System; using Serwer.Core.Domain;
using Serwer.Infrastructure.Settings;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Tesseract;
namespace Serwer.Infrastructure.Services namespace Serwer.Infrastructure.Services
{ {
class ImageHandler public class ImageHandler : IImageHandler
{ {
private readonly ISet<File> _files = new HashSet<File>();
private readonly string _env;
public ImageHandler(IHostEnviroment hostEnviroment)
{
_env = hostEnviroment.RootPath;
}
public async Task<string> Process(string name, string contentType, byte[] bytes)
{
var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
var img = Pix.LoadFromMemory(bytes);
var res = engine.Process(img);
var txt = res.GetText();
return await Task.FromResult(txt);
}
} }
} }

View File

@ -1,30 +0,0 @@
using Serwer.Core.Domain;
using Serwer.Infrastructure.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;
namespace Serwer.Infrastructure.Services
{
public class ImageService : IImageService
{
private readonly ISet<File> _files = new HashSet<File>();
private readonly string _env;
public ImageService(IHostEnviroment hostEnviroment)
{
_env = hostEnviroment.RootPath;
}
public async Task<string> Process(string name, string contentType, byte[] bytes)
{
var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
var img = Pix.LoadFromMemory(bytes);
var res = engine.Process(img);
var txt = res.GetText();
return await Task.FromResult(txt);
}
}
}

View File

@ -0,0 +1,46 @@
using Google.Apis.Customsearch.v1;
using Google.Apis.Services;
using Newtonsoft.Json;
using Serwer.Infrastructure.DTO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Serwer.Infrastructure.Services
{
public class SearchService : ISearchService
{
private readonly IImageHandler _imageHandler;
private const string apiKey = "AIzaSyCagG6QyQyBuJNb1YDuK25qSzoC0Rrjo0c";
private const string searchEngineId = "17b946686537c46e3";
public SearchService(IImageHandler imageHandler)
{
_imageHandler = imageHandler;
}
public async Task<IList<SearchResultDTO>> Search(string query)
{
WebRequest webRequest = WebRequest.Create($"https://www.googleapis.com/customsearch/v1?key={apiKey}&cx={searchEngineId}&q={query}");
using (var stream = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
var response = await stream.ReadToEndAsync();
dynamic jsonData = JsonConvert.DeserializeObject(response);
var results = new List<SearchResultDTO>();
foreach(var item in jsonData.items)
{
results.Add(new SearchResultDTO
{
Title = item.title,
Link = item.link,
Snippet = item.snippet
});
}
return results;
}
}
}
}

View File

@ -6,6 +6,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" /> <PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Google.Apis.Customsearch.v1" Version="1.49.0.2084" />
<PackageReference Include="Google.Apis.CustomSearchAPI.v1" Version="1.49.0.2172" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" /> <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
<PackageReference Include="Tesseract" Version="4.1.1" /> <PackageReference Include="Tesseract" Version="4.1.1" />