Skip to content

Commit

Permalink
image compression
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Nov 1, 2023
1 parent 30ff593 commit b5914ae
Show file tree
Hide file tree
Showing 23 changed files with 1,139 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/.angular
**/config
LICENSE
README.md
2 changes: 1 addition & 1 deletion OpenAlprWebhookProcessor/Alerts/AlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ await _alertClient.SendAlertAsync(new Alert()
Id = result.Id,
PlateNumber = result.BestNumber,
},
result.PlatePreviewJpeg,
result.PlateJpeg,
_cancellationTokenSource.Token);
}
catch (Exception ex)
Expand Down
5 changes: 3 additions & 2 deletions OpenAlprWebhookProcessor/Alerts/IAlertClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using System.Threading;
using System.Threading.Tasks;

namespace OpenAlprWebhookProcessor.Alerts
Expand All @@ -7,7 +8,7 @@ public interface IAlertClient
{
Task SendAlertAsync(
Alert alert,
string base64PreviewJpeg,
byte[] plateJpeg,
CancellationToken cancellationToken);

Task VerifyCredentialsAsync(
Expand Down
26 changes: 15 additions & 11 deletions OpenAlprWebhookProcessor/Alerts/Pushover/PushoverClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PushoverClient(IServiceProvider serviceProvider)

public async Task SendAlertAsync(
Alert alert,
string base64PreviewJpeg,
byte[] plateJpeg,
CancellationToken cancellationToken)
{
using (var scope = _serviceProvider.CreateScope())
Expand All @@ -39,7 +39,9 @@ public async Task SendAlertAsync(

var processorContext = scope.ServiceProvider.GetRequiredService<ProcessorContext>();

var clientSettings = await processorContext.PushoverAlertClients.FirstOrDefaultAsync(cancellationToken);
var clientSettings = await processorContext.PushoverAlertClients
.AsNoTracking()
.FirstOrDefaultAsync(cancellationToken);

var boundary = Guid.NewGuid().ToString();
using (var content = new MultipartFormDataContent(boundary))
Expand All @@ -51,9 +53,9 @@ public async Task SendAlertAsync(
content.Add(new StringContent(alert.PlateNumber + " " + alert.Description), "message");
content.Add(new StringContent("openalpr alert"), "title");

if (clientSettings.SendPlatePreview)
if (clientSettings.SendPlatePreview && plateJpeg != null)
{
content.Add(new ByteArrayContent(Convert.FromBase64String(base64PreviewJpeg)), "attachment", "attachment.jpg");
content.Add(new ByteArrayContent(plateJpeg), "attachment", "attachment.jpg");
}

try
Expand All @@ -67,15 +69,15 @@ public async Task SendAlertAsync(
{
var result = await response.Content.ReadAsStringAsync(cancellationToken);

logger.LogError("Failed to send alert via Pushover: " + result);
logger.LogError("Failed to send alert via Pushover: {result}", result);
throw new InvalidOperationException("failed");
}

logger.LogInformation("Alert sent via Pushover.");
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to send alert via Pushover: " + ex.Message);
logger.LogError(ex, "Failed to send alert via Pushover: {exception}", ex.Message);
throw new InvalidOperationException("failed");
}
}
Expand All @@ -92,27 +94,29 @@ public async Task VerifyCredentialsAsync(CancellationToken cancellationToken)

var processorContext = scope.ServiceProvider.GetRequiredService<ProcessorContext>();

var clientSettings = await processorContext.PushoverAlertClients.FirstOrDefaultAsync(cancellationToken);
var clientSettings = await processorContext.PushoverAlertClients
.AsNoTracking()
.FirstOrDefaultAsync(cancellationToken);

try
{
var result = await _httpClient.PostAsync(VerifyCredentialsUrl
.Replace("{0}", clientSettings.ApiToken)
.Replace("{1}", clientSettings.UserKey),
.Replace("{0}", clientSettings.ApiToken)
.Replace("{1}", clientSettings.UserKey),
null,
cancellationToken);

if (!result.IsSuccessStatusCode)
{
var message = await result.Content.ReadAsStringAsync(cancellationToken);
logger.LogError("Pushover credential check failed: " + message);
logger.LogError("Pushover credential check failed: {message}", message);
}

logger.LogInformation("Pushover credentials are valid.");
}
catch (Exception ex)
{
logger.LogError(ex, "Pushover credential check failed: " + ex.Message);
logger.LogError(ex, "Pushover credential check failed: {exception}", ex.Message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TestPushoverClientRequestHandler(ProcessorContext processorContext,
public async Task HandleAsync(CancellationToken cancellationToken)
{
var testPlateGroup = await _processorContext.PlateGroups
.Where(x => x.PlatePreviewJpeg != null)
.Where(x => x.PlateJpeg != null)
.FirstOrDefaultAsync(cancellationToken);

await _alertClient.VerifyCredentialsAsync(cancellationToken);
Expand All @@ -35,7 +35,7 @@ await _alertClient.SendAlertAsync(new Alert()
PlateNumber = testPlateGroup.BestNumber,
StrictMatch = false,
},
testPlateGroup.PlatePreviewJpeg,
testPlateGroup.PlateJpeg,
cancellationToken);
}
}
Expand Down
2 changes: 2 additions & 0 deletions OpenAlprWebhookProcessor/Data/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ public class Agent
public long LastSuccessfulScrapeEpoch { get; set; }

public bool IsDebugEnabled { get; set; }

public bool IsImageCompressionEnabled { get; set; }
}
}
Loading

0 comments on commit b5914ae

Please sign in to comment.