-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathApiResource.cs
More file actions
86 lines (73 loc) · 2.84 KB
/
ApiResource.cs
File metadata and controls
86 lines (73 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Text;
using System.Threading.Tasks;
using Waher.Content.Text;
using Waher.Networking.HTTP;
namespace Waher.WebService.Tesseract
{
/// <summary>
/// Tesseract API Resource.
/// </summary>
public class ApiResource : HttpResource, IHttpPostMethod
{
private readonly TesseractApi api;
private readonly HttpAuthenticationScheme[] authenticationSchemes;
/// <summary>
/// Tesseract API Resource.
/// </summary>
/// <param name="Api">API Class.</param>
/// <param name="AuthenticationSchemes">Authentication schemes.</param>
public ApiResource(TesseractApi Api, params HttpAuthenticationScheme[] AuthenticationSchemes)
: base("/Tesseract/Api")
{
this.api = Api;
this.authenticationSchemes = AuthenticationSchemes;
}
/// <summary>
/// If the resource is synchronous (i.e. returns a response in the method handler), or if it is asynchronous
/// (i.e. sends the response from another thread).
/// </summary>
public override bool Synchronous => true;
/// <summary>
/// If the resource handles sub-paths.
/// </summary>
public override bool HandlesSubPaths => false;
/// <summary>
/// If the resource uses user sessions.
/// </summary>
public override bool UserSessions => false;
/// <summary>
/// Any authentication schemes used to authenticate users before access is granted to the corresponding resource.
/// </summary>
/// <param name="Request">Current request</param>
public override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
{
return this.authenticationSchemes;
}
/// <summary>
/// If the POST method is allowed.
/// </summary>
public bool AllowsPOST => true;
/// <summary>
/// Executes the POST method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public async Task POST(HttpRequest Request, HttpResponse Response)
{
if (!this.api.ExeFound)
throw new ServiceUnavailableException("Tesseract executable not found on system.");
if (!Request.HasData)
throw new BadRequestException("No content included.");
string ContentType = Request.Header.ContentType?.Type;
if (string.IsNullOrEmpty(ContentType) || !ContentType.StartsWith("image/", StringComparison.InvariantCultureIgnoreCase))
throw new UnsupportedMediaTypeException("Expected image content.");
string Language = Request.Header["X-LANGUAGE"];
string PageSegmentationMode = Request.Header["X-PSM"];
byte[] ImageBin = await Request.ReadDataAsync();
string Text = await this.api.PerformOcr(ImageBin, ContentType, PageSegmentationMode, Language);
await Response.Return(PlainTextCodec.DefaultContentType + "; charset=utf-8", true, Encoding.UTF8.GetBytes(Text));
}
}
}