POS_Sprint_3 #5
@ -13,8 +13,8 @@ namespace Serwer.Api.Controllers
|
||||
[ApiController]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
private readonly IImageService _imageService;
|
||||
public ImageController(IImageService imageService)
|
||||
private readonly IImageHandler _imageService;
|
||||
public ImageController(IImageHandler imageService)
|
||||
{
|
||||
_imageService = imageService;
|
||||
}
|
||||
|
28
Serwer/Serwer.Api/Controllers/SearchController.cs
Normal file
28
Serwer/Serwer.Api/Controllers/SearchController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -89,8 +89,9 @@ 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<IImageHandler, ImageHandler>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddScoped<ISearchService, SearchService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
13
Serwer/Serwer.Core/Domain/SearchResult.cs
Normal file
13
Serwer/Serwer.Core/Domain/SearchResult.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.Core.Domain
|
||||
{
|
||||
public class SearchResult
|
||||
{
|
||||
|
||||
}
|
||||
}
|
15
Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs
Normal file
15
Serwer/Serwer.Infrastructure/DTO/SearchResultDto.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Serwer.Core.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -8,6 +9,6 @@ namespace Serwer.Infrastructure.Services
|
||||
{
|
||||
public interface IImageHandler
|
||||
{
|
||||
|
||||
Task<string> Process(string name, string contentType, byte[] bytes);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
using Serwer.Core.Domain;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Serwer.Infrastructure.DTO;
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,12 +1,30 @@
|
||||
using System;
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
46
Serwer/Serwer.Infrastructure/Services/SearchService.cs
Normal file
46
Serwer/Serwer.Infrastructure/Services/SearchService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
|
||||
<PackageReference Include="Tesseract" Version="4.1.1" />
|
||||
|
Loading…
Reference in New Issue
Block a user