Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhiqin committed Jul 7, 2023
1 parent 2c8fafc commit aa798ca
Show file tree
Hide file tree
Showing 75 changed files with 688 additions and 583 deletions.
19 changes: 19 additions & 0 deletions OMDb.Core/Const/Const.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OMDb.Core.Const
{
public class Const
{
public const string BlankSpace = " ";

public const string LineBreak = "\n";

public const double Scale_Cover = 160.0 / 260.0;
}
}

21 changes: 21 additions & 0 deletions OMDb.Core/Const/MediaTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OMDb.Core.Const
{
public class MediaTypes
{
public static readonly ImmutableList<string> Image = ImmutableList.Create("BMP", "JPG", "PNG", "TIF", "GIF", "PCX", "TGA", "EXIF", "FPX", "SVG", "PSD", "CDR", "PCD", "DXF", "UFO", "EPS", "AI", "RAW", "WMF", "WEBP", "AVIF", "APNG");

public static readonly ImmutableList<string> Video = ImmutableList.Create("AVI","WMV","MPEG","MP4","M4V","MOV","ASF","FLV","F4V","RMVB","RM","3GP","VOB");

public static readonly ImmutableList<string> VideoSub = ImmutableList.Create("SRT","WEBVTT","STL","SBV","ASS","DFXP","TTML");

public static readonly ImmutableList<string> Audio = ImmutableList.Create("MP3","WAV","WMA","MP2","Flac","MIDI","RA","APE","AAC","CDA","MOV");

}
}
File renamed without changes.
103 changes: 91 additions & 12 deletions OMDb.Core/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using ImageMagick;
using OMDb.Core.Const;
using OMDb.Core.Models;
using OMDb.Core.Services;
using OMDb.Core.Utils.Extensions;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors;
using System.Net;

namespace OMDb.Core.Helpers
{
Expand Down Expand Up @@ -49,6 +53,9 @@ public static ImageInfo GetImageInfo(string path)
}





private static async Task<MemoryStream> ToMemoryStreamAsync(Image image)
{
MemoryStream stream = new MemoryStream();
Expand All @@ -57,7 +64,7 @@ private static async Task<MemoryStream> ToMemoryStreamAsync(Image image)
return stream;
}

public static async void DrawBannerCoverAsync(List<ImageInfo> covers, ImageInfo bg,string savedPath)
public static async void DrawBannerCoverAsync(List<ImageInfo> covers, ImageInfo bg, string savedPath)
{
using (Image image = Image.Load(bg.FullPath))
{
Expand All @@ -67,9 +74,9 @@ public static async void DrawBannerCoverAsync(List<ImageInfo> covers, ImageInfo
images.Add(Image.Load(cover.FullPath));
}
int width = (int)(bg.Width * 0.16);
foreach(Image coverImage in images)
foreach (Image coverImage in images)
{
coverImage.Mutate(x => x.Resize(width, coverImage.Height * (width/ coverImage.Width)));
coverImage.Mutate(x => x.Resize(width, coverImage.Height * (width / coverImage.Width)));
}
double span = bg.Width * 0.025;
Point[] points = new Point[images.Count];
Expand Down Expand Up @@ -157,7 +164,7 @@ public static async Task<MemoryStream> DrawWaterfallAsync(List<string> covers, s
}
}
}
foreach(var temp in images)
foreach (var temp in images)
{
temp.Dispose();
}
Expand All @@ -182,7 +189,7 @@ public static async Task<MemoryStream> DrawWaterfallAsync(List<string> covers, M
foreach (Image coverImage in images)
{
var cutRect = CalPerfectResizeRectangle(coverImage.Width, coverImage.Height, width, height);
coverImage.Mutate(x => x.Resize(width, height, KnownResamplers.Bicubic, cutRect,new Rectangle(0,0,width,height),true));
coverImage.Mutate(x => x.Resize(width, height, KnownResamplers.Bicubic, cutRect, new Rectangle(0, 0, width, height), true));
}
int columCount = (int)Math.Ceiling(image.Width / (float)width);
int rowCount = (int)Math.Ceiling(image.Height / (float)height) + 1;
Expand Down Expand Up @@ -219,19 +226,19 @@ public static async Task<MemoryStream> DrawWaterfallAsync(List<string> covers, M
/// <param name="savedPath"></param>
/// <param name="width">如果为0则依据height等比例缩放</param>
/// <param name="height">如果为0则依据width等比例缩放</param>
public static async void ResetSizeAsync(string path,string savedPath, int width, int height)
public static async void ResetSizeAsync(string path, string savedPath, int width, int height)
{
using (Image image = Image.Load(path))
{
if(width != 0 && height != 0)
if (width != 0 && height != 0)
{
image.Mutate(x => x.Resize(width, height));
}
else if(width != 0)
else if (width != 0)
{
image.Mutate(x => x.Resize(width, image.Height * (width / image.Width)));
}
else if(height != 0)
else if (height != 0)
{
image.Mutate(x => x.Resize(image.Width * (height / image.Height), height));
}
Expand Down Expand Up @@ -333,7 +340,7 @@ public static async Task<MemoryStream> ResetSizeAsync(MemoryStream inputStream,
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static async Task<MemoryStream> BlurAsync(string path,float sigma = 10)
public static async Task<MemoryStream> BlurAsync(string path, float sigma = 10)
{
using (Image image = Image.Load(path))
{
Expand All @@ -350,7 +357,7 @@ public static async Task<MemoryStream> BlurAsync(string path,float sigma = 10)
/// <param name="targetWidth"></param>
/// <param name="targetHeight"></param>
/// <returns></returns>
private static Rectangle CalPerfectResizeRectangle(int srcWidth,int srcHeight,int targetWidth,int targetHeight)
private static Rectangle CalPerfectResizeRectangle(int srcWidth, int srcHeight, int targetWidth, int targetHeight)
{
Rectangle resultRect;
//宽高比越大,图片越矮胖
Expand All @@ -373,9 +380,81 @@ private static Rectangle CalPerfectResizeRectangle(int srcWidth,int srcHeight,in
//原图宽高比小于目标宽高比,说明目标图片要变矮胖
//保留X,Y要裁剪
double srcCutHeight = targetHeight * srcWidth / targetWidth;
resultRect = new Rectangle(0,(int)(srcHeight / 2 - srcCutHeight / 2),srcWidth, (int)srcCutHeight);
resultRect = new Rectangle(0, (int)(srcHeight / 2 - srcCutHeight / 2), srcWidth, (int)srcCutHeight);
}
return resultRect;
}


/// <summary>
/// 获取图片最佳排序
/// </summary>
/// <param name="paths">图片路径</param>
/// <param name="scale">期望长宽比</param>
/// <returns></returns>
public static List<string> GetBestImg(IEnumerable<string> paths, double scale)
{
List<ImageInfo> infos = new List<Core.Models.ImageInfo>();
var path_imgs = paths.Where(a => MediaTypes.Image.Contains(Path.GetExtension(a).Remove(".").ToUpper())).Where(a => File.Exists(a));
if (!path_imgs.IsNullOrEmptyOrWhiteSpazeOrCountZero())
{
foreach (var path in Core.Helpers.RandomHelper.RandomList(path_imgs, 100))//仅对100张照片计算
infos.Add(Core.Helpers.ImageHelper.GetImageInfo(path));

//优先长宽比更适配的图片
List<ImageInfo> sortedInfos;
sortedInfos = infos.OrderBy(p => Math.Abs(p.Scale - scale)).OrderByDescending(p => p.Length).ToList();

int[] weights = new int[sortedInfos.Count];
for (int i = 0; i < sortedInfos.Count; i++)
{
weights[i] = i + 1;//权重从1开始递增
}
var coverItems = Core.Helpers.RandomHelper.RandomList(sortedInfos, weights, 1);//获取最优的
return coverItems?.Select(p => p.FullPath).ToList();
}
else
return null;
}


/// <summary>
/// 用远程地址获取文件字节流
/// </summary>
/// <param name="path">URL</param>
/// <returns></returns>
public static byte[] GetUrlMemoryStream(string path)
{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

Check warning on line 429 in OMDb.Core/Helpers/ImageHelper.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.'
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

List<byte> btlst = new List<byte>();
int b = responseStream.ReadByte();
while (b > -1)
{
btlst.Add((byte)b);
b = responseStream.ReadByte();
}
byte[] bts = btlst.ToArray();
return bts;
}

public static bool IsSupportImg(string file)
{
if (!string.IsNullOrEmpty(file))
{
var ext = System.IO.Path.GetExtension(file).Replace(".", "");
return MediaTypes.Image.Contains(ext.ToLower());
}
else
{
return false;
}
}



}
}
30 changes: 0 additions & 30 deletions OMDb.Core/Helpers/LogHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion OMDb.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion OMDb.Core/Models/EntryCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using OMDb.Core.DbModels;
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion OMDb.Core/Models/EntryCollectionItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using OMDb.Core.DbModels;
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions OMDb.Core/Models/EntryMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public bool Save(string file)
{
try
{
JsonSerializerSettings settings = new JsonSerializerSettings { Formatting = Formatting.Indented };
var json = JsonConvert.SerializeObject(this);
System.IO.File.WriteAllText(file, json);
return true;
Expand Down
2 changes: 1 addition & 1 deletion OMDb.Core/Models/EntryName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion OMDb.Core/Models/ExtractsLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion OMDb.Core/Models/WatchHistory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OMDb.Core.Extensions;
using OMDb.Core.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions OMDb.Core/OMDb.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DotNetCore.NPOI" Version="1.2.3" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="12.2.1" />
<PackageReference Include="Magick.NET.Core" Version="12.2.1" />
Expand Down
3 changes: 1 addition & 2 deletions OMDb.Core/Services/CommonService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OMDb.Core.Extensions;
using OMDb.Core.Models;
using OMDb.Core.Models;
using OMDb.Core.Utils.StringUtil;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OMDb.Core.Services
{
public static class EntryLabelService
public static class EntryLabelClassService
{
/// <summary>
/// 查詢所有Entry&Label對應關係
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OMDb.Core.Services
{
public static class LabelService
public static class LabelClassService
{
private static bool IsLocalDbValid()
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public static async Task<List<LabelClassDb>> GetAllLabelAsync(string currentDb)
public static List<LabelClassDb> GetAllLabel(string currentDb)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"select * from Label where DbCenterId='{0}'", currentDb);
sb.AppendFormat(@"select * from LabelClass where DbCenterId='{0}'", currentDb);
return DbService.DCDb.Ado.SqlQuery<LabelClassDb>(sb.ToString());
}

Expand Down
6 changes: 3 additions & 3 deletions OMDb.Core/Services/DbService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OMDb.Core.DbModels;
using OMDb.Core.DbModels.ManagerCenterDb;
using OMDb.Core.Helpers;
using OMDb.Core.Models;
using OMDb.Core.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -167,7 +167,7 @@ internal static bool SetMCDb(string connet)
}
catch (Exception ex)
{
LogHelper.Instance._logger.Error(ex);
Logger.Error(ex);
return false;
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ internal static bool SetDCDb(string connet)
}
catch (Exception ex)
{
LogHelper.Instance._logger.Error(ex);
Logger.Error(ex);
return false;
}
}
Expand Down
Loading

0 comments on commit aa798ca

Please sign in to comment.