Re-fabrication

This commit is contained in:
2018-03-26 19:27:29 +02:00
parent e00bcbe275
commit 8fbe56c67e
499 changed files with 7510 additions and 12466 deletions

View File

@ -1,30 +0,0 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Yavsc.Billing;
using Yavsc.Models.Billing;
namespace Yavsc.Helpers
{
public static class BillingHelpers
{
public static decimal Addition(this List<IBillItem> items) => items.Aggregate<IBillItem, decimal>(0m, (t, l) => t + l.Count * l.UnitaryCost);
public static decimal Addition(this List<CommandLine> items) => items.Select(i=>((IBillItem)i)).ToList().Addition();
public static string GetBillText(this IBillable query) {
string total = query.GetBillItems().Addition().ToString("C", CultureInfo.CurrentUICulture);
string bill = string.Join("\n", query.GetBillItems().Select(l=> $"{l.Name} {l.Description} : {l.UnitaryCost} € " + ((l.Count != 1) ? "*"+l.Count.ToString() : ""))) +
$"\n\nTotal: {total}";
return bill;
}
public static FileInfo GetBillInfo(this IBillable bill)
{
var suffix = bill.GetIsAcquitted() ? "-ack":null;
var filename = bill.GetFileBaseName()+".pdf";
return new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
}
}
}

View File

@ -1,21 +0,0 @@
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Yavsc.Helpers
{
using Models.societe.com;
public static class ComapnyInfoHelpers { 
public static async Task<CompanyInfoMessage> CheckSiren(this HttpClient web,
string siren, CompanyInfoSettings api)
{
using (var request = new HttpRequestMessage(HttpMethod.Get,
string.Format(Constants.CompanyInfoUrl,siren,api.ApiKey))) {
using (var response = await web.SendAsync(request)) {
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
return payload.ToObject<CompanyInfoMessage>();
}
}
}
}
}

View File

@ -19,22 +19,60 @@ namespace Yavsc.Helpers
{
public static class FileSystemHelpers
{
public static Func<string,string,long,string>
SignFileNameFormat = new Func<string,string,long,string> ((signType,billingCode,estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
public static UserDirectoryInfo GetUserFiles(this ClaimsPrincipal user, string subdir)
public static FileRecievedInfo ReceiveProSignature(this ClaimsPrincipal user, string billingCode, long estimateId, IFormFile formFile, string signtype)
{
var item = new FileRecievedInfo();
item.FileName = SignFileNameFormat("pro",billingCode,estimateId);
item.MimeType = formFile.ContentDisposition;
var destFileName = Path.Combine(Startup.SiteSetup.UserFiles.Bills, item.FileName);
UserDirectoryInfo di = new UserDirectoryInfo(Startup.UserFilesDirName, user.Identity.Name, subdir);
var fi = new FileInfo(destFileName);
if (fi.Exists) item.Overriden = true;
return di;
using (var org = formFile.OpenReadStream())
{
Image i = Image.FromStream(org);
using (Bitmap source = new Bitmap(i))
{
source.Save(destFileName, ImageFormat.Png);
}
}
return item;
}
private static void CreateAvatars(this ApplicationUser user, Bitmap source)
{
var dir = Startup.SiteSetup.UserFiles.Avatars;
var name = user.UserName + ".png";
var smallname = user.UserName + ".s.png";
var xsmallname = user.UserName + ".xs.png";
using (Bitmap newBMP = new Bitmap(source, 128, 128))
{
newBMP.Save(Path.Combine(
dir, name), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 64, 64))
{
newBMP.Save(Path.Combine(
dir, smallname), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 32, 32))
{
newBMP.Save(Path.Combine(
dir, xsmallname), ImageFormat.Png);
}
}
public static string InitPostToFileSystem(
this ClaimsPrincipal user,
string subpath)
{
var root = Path.Combine(Startup.UserFilesDirName, user.Identity.Name);
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.Identity.Name);
var diRoot = new DirectoryInfo(root);
if (!diRoot.Exists) diRoot.Create();
if (!string.IsNullOrWhiteSpace(subpath)) {
@ -64,7 +102,7 @@ namespace Yavsc.Helpers
var item = new FileRecievedInfo();
// form-data; name="file"; filename="capt0008.jpg"
ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition);
item.FileName = Yavsc.Abstract.FileSystem.FileSystemHelpers.FilterFileName (destFileName ?? contentDisposition.FileName);
item.FileName = Yavsc.Abstract.FileSystem.AbstractFileSystemHelpers.FilterFileName (destFileName ?? contentDisposition.FileName);
item.MimeType = contentDisposition.DispositionType;
item.DestDir = root;
var fi = new FileInfo(Path.Combine(root, item.FileName));
@ -150,51 +188,6 @@ namespace Yavsc.Helpers
return item;
}
private static void CreateAvatars(this ApplicationUser user, Bitmap source)
{
var dir = Startup.SiteSetup.UserFiles.Avatars;
var name = user.UserName + ".png";
var smallname = user.UserName + ".s.png";
var xsmallname = user.UserName + ".xs.png";
using (Bitmap newBMP = new Bitmap(source, 128, 128))
{
newBMP.Save(Path.Combine(
dir, name), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 64, 64))
{
newBMP.Save(Path.Combine(
dir, smallname), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 32, 32))
{
newBMP.Save(Path.Combine(
dir, xsmallname), ImageFormat.Png);
}
}
public static Func<string,string,long,string>
SignFileNameFormat = new Func<string,string,long,string> ((signType,billingCode,estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
public static FileRecievedInfo ReceiveProSignature(this ClaimsPrincipal user, string billingCode, long estimateId, IFormFile formFile, string signtype)
{
var item = new FileRecievedInfo();
item.FileName = SignFileNameFormat("pro",billingCode,estimateId);
item.MimeType = formFile.ContentDisposition;
var destFileName = Path.Combine(Startup.SiteSetup.UserFiles.Bills, item.FileName);
var fi = new FileInfo(destFileName);
if (fi.Exists) item.Overriden = true;
using (var org = formFile.OpenReadStream())
{
Image i = Image.FromStream(org);
using (Bitmap source = new Bitmap(i))
{
source.Save(destFileName, ImageFormat.Png);
}
}
return item;
}
}
}

View File

@ -1,136 +0,0 @@
//
// GoogleHelpers.cs
//
// Author:
// Paul Schneider <paul@pschneider.fr>
//
// Copyright (c) 2015 GNU GPL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Compute.v1;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Util.Store;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Util;
namespace Yavsc.Helpers
{
using Models;
using Models.Calendar;
using Services;
using Server.Helpers;
/// <summary>
/// Google helpers.
/// </summary>
public static class GoogleHelpers
{
public static ServiceAccountCredential OupsGetCredentialForApi(IEnumerable<string> scopes)
{
var initializer = new ServiceAccountCredential.Initializer(Startup.GoogleSettings.Account.client_email);
initializer = initializer.FromPrivateKey(Startup.GoogleSettings.Account.private_key);
initializer.Scopes = scopes;
var credential = new ServiceAccountCredential(initializer);
return credential;
}
public static async Task<GoogleCredential> GetCredentialForApi(IEnumerable<string> scopes)
{
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
var baseClientService = new BaseClientService.Initializer()
{
HttpClientInitializer = credential
};
var compute = new ComputeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(scopes);
}
return credential;
}
public static async Task<IdentityUserLogin<string>> GetGoogleUserLoginAsync(
this ApplicationDbContext context,
string yavscUserId)
{
var user = context.Users.FirstOrDefaultAsync(u=>u.Id==yavscUserId);
if (user==null) return null;
var googleLogin = await context.UserLogins.FirstOrDefaultAsync(
x => x.UserId == yavscUserId && x.LoginProvider == "Google"
);
return googleLogin;
}
public static async Task<UserCredential> GetGoogleCredential(IDataStore store, string googleUserLoginKey)
{
if (string.IsNullOrEmpty(googleUserLoginKey))
throw new InvalidOperationException("No Google login");
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer());
var token = await store.GetAsync<TokenResponse>(googleUserLoginKey);
// token != null
var c = SystemClock.Default;
if (token.IsExpired(c)) {
token = await RefreshToken(token);
}
return new UserCredential(flow, googleUserLoginKey, token);
}
public static async Task<Period[]> GetFreeTime (this ICalendarManager manager, string calId, DateTime startDate, DateTime endDate)
{
var evlist = await manager.GetCalendarAsync(calId, startDate, endDate, null) ;
var result = evlist.Items
.Where(
ev => ev.Transparency == "transparent"
)
.Select(
ev => new Period {
Start = ev.Start.DateTime.Value,
End = ev.End.DateTime.Value
}
);
return result.ToArray();
}
public static async Task<TokenResponse> RefreshToken(TokenResponse oldResponse)
{
string ep = " https://www.googleapis.com/oauth2/v4/token";
// refresh_token client_id client_secret grant_type=refresh_token
try {
using (var m = new SimpleJsonPostMethod(ep)) {
return await m.Invoke<TokenResponse>(
new { refresh_token= oldResponse.RefreshToken, client_id=Startup.GoogleSettings.ClientId,
client_secret=Startup.GoogleSettings.ClientSecret,
grant_type="refresh_token" }
);
}
}
catch (Exception ex) {
throw new Exception ("Quelque chose s'est mal passé à l'envoi",ex);
}
}
}
}