refact
This commit is contained in:
parent
5de53a3cba
commit
476d35ae8a
270 changed files with 477 additions and 401 deletions
68
src/isnd/Services/EmailSender.cs
Normal file
68
src/isnd/Services/EmailSender.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MailKit.Net.Smtp;
|
||||
using MimeKit;
|
||||
|
||||
using System;
|
||||
using isn.Interfaces;
|
||||
using isn.Entities;
|
||||
|
||||
namespace isn.Services
|
||||
{
|
||||
public class EmailSender : IEmailSender, IMailer
|
||||
{
|
||||
public EmailSender(IOptions<SmtpSettings> smtpSettings,
|
||||
Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
|
||||
{
|
||||
Options = smtpSettings.Value;
|
||||
Env = env;
|
||||
}
|
||||
public SmtpSettings Options { get; } //set only via Secret Manager
|
||||
public Microsoft.AspNetCore.Hosting.IHostingEnvironment Env { get; }
|
||||
public Task SendEmailAsync(string email, string subject, string message)
|
||||
{
|
||||
return Execute(Options.SenderName, subject, message, email);
|
||||
}
|
||||
|
||||
public async Task Execute(string name, string subject, string message, string email)
|
||||
{
|
||||
await SendMailAsync(name, email, subject, message);
|
||||
}
|
||||
|
||||
public async Task SendMailAsync(string name, string email, string subjet, string body)
|
||||
{
|
||||
try {
|
||||
var message = new MimeMessage();
|
||||
message.From.Add(new MailboxAddress(Options.SenderName, Options.SenderEMail));
|
||||
message.To.Add(new MailboxAddress(name??email, email));
|
||||
message.Body = new TextPart("html") { Text = body };
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
client.ServerCertificateValidationCallback = (s,c,h,e)=>true;
|
||||
if (Env.IsDevelopment() || Options.UserName == null)
|
||||
{
|
||||
await client.ConnectAsync(Options.Server, Options.Port, MailKit.Security.SecureSocketOptions.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
await client.ConnectAsync(Options.Server);
|
||||
}
|
||||
if (Options.UserName != null)
|
||||
{
|
||||
await client.AuthenticateAsync(Options.UserName, Options.Password);
|
||||
}
|
||||
await client.SendAsync(message);
|
||||
await client.DisconnectAsync(true);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvalidOperationException(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
140
src/isnd/Services/PackageManager.cs
Normal file
140
src/isnd/Services/PackageManager.cs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using isn.Controllers;
|
||||
using isn.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NuGet.Versioning;
|
||||
using Unleash;
|
||||
|
||||
namespace isnd.Services
|
||||
{
|
||||
public class PackageManager
|
||||
{
|
||||
ApplicationDbContext dbContext;
|
||||
public PackageManager(ApplicationDbContext dbContext)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
public IndexResult SearchByName(string query,
|
||||
int skip, int take,bool prerelease = false,
|
||||
string packageType = null)
|
||||
{
|
||||
var scope = dbContext.Packages
|
||||
.Include(p => p.Versions)
|
||||
.Where(
|
||||
p => (CamelCaseMatch(p.Id, query) || SeparatedByMinusMatch(p.Id, query))
|
||||
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
|
||||
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
|
||||
);
|
||||
return new IndexResult
|
||||
{
|
||||
totalHits = scope.Count(),
|
||||
data = scope.OrderBy(p => p.Id)
|
||||
.Skip(skip).Take(take).ToArray()
|
||||
};
|
||||
}
|
||||
public AutoCompleteResult AutoComplete (string id,
|
||||
int skip, int take, bool prerelease = false,
|
||||
string packageType = null)
|
||||
{
|
||||
var scope = dbContext.PackageVersions.Where(
|
||||
v => v.PackageId == id
|
||||
&& (prerelease || !v.IsPrerelease)
|
||||
&& (packageType == null || v.Type == packageType)
|
||||
)
|
||||
.OrderBy(v => v.FullString);
|
||||
return new AutoCompleteResult
|
||||
{
|
||||
totalHits = scope.Count(),
|
||||
data = scope.Select(v => v.FullString)
|
||||
.Skip(skip).Take(take).ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
// TODO stocker MetaData plutôt que FullString en base,
|
||||
// et en profiter pour corriger ce listing
|
||||
public string[] GetVersions(
|
||||
string id,
|
||||
NuGetVersion parsedVersion,
|
||||
bool prerelease = false,
|
||||
string packageType = null,
|
||||
int skip = 0,
|
||||
int take = 25)
|
||||
{
|
||||
return dbContext.PackageVersions.Where(
|
||||
v => v.PackageId == id
|
||||
&& (prerelease || !v.IsPrerelease)
|
||||
&& (packageType == null || v.Type == packageType)
|
||||
&& (parsedVersion.CompareTo(new SemanticVersion(v.Major, v.Minor, v.Patch)) < 0)
|
||||
)
|
||||
.OrderBy(v => v.FullString)
|
||||
.Select(v => v.FullString)
|
||||
.Skip(skip).Take(take).ToArray();
|
||||
}
|
||||
|
||||
protected static bool CamelCaseMatch(string id, string q)
|
||||
{
|
||||
// Assert.False (q==null);
|
||||
string query = q;
|
||||
if (query.Length == 0) return false;
|
||||
|
||||
while (id.Length > 0)
|
||||
{
|
||||
int i = 0;
|
||||
while (id.Length > i && char.IsLower(id[i])) i++;
|
||||
if (i == 0) break;
|
||||
id = id.Substring(i);
|
||||
if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected static bool SeparatedByMinusMatch(string id, string q)
|
||||
{
|
||||
foreach (var part in id.Split('-'))
|
||||
{
|
||||
if (part.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
internal List<Resource> GetResources(IUnleash unleashClient)
|
||||
{
|
||||
var res = new List<Resource>();
|
||||
if (unleashClient.IsEnabled("pkg-push"))
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package",
|
||||
type = "PackagePublish/2.0.0",
|
||||
comment = "Package Publish service"
|
||||
});
|
||||
if (unleashClient.IsEnabled("pkg-get"))
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package",
|
||||
type = "PackageBaseAddress/3.0.0",
|
||||
comment = "Package Base Address service"
|
||||
});
|
||||
if (unleashClient.IsEnabled("pkg-autocomplete"))
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package/index.json",
|
||||
type = "SearchAutocompleteService/3.5.0",
|
||||
comment = "Auto complete service"
|
||||
});
|
||||
if (unleashClient.IsEnabled("pkg-search"))
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package/index.json",
|
||||
type = "SearchQueryService/3.5.0",
|
||||
comment = "Search Query service"
|
||||
});
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
src/isnd/Services/SearchResult.cs
Normal file
16
src/isnd/Services/SearchResult.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using isn.Data;
|
||||
|
||||
namespace isnd.Services
|
||||
{
|
||||
public class IndexResult
|
||||
{
|
||||
public int totalHits { get; set; }
|
||||
public Package[] data { get; set; }
|
||||
}
|
||||
|
||||
public class AutoCompleteResult
|
||||
{
|
||||
public int totalHits { get; set; }
|
||||
public string[] data { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue