Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.0.2 #3

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

## General

- [x] Optimize code and fragment info methods
- [x] Fix workflows (publishing)
- [x] Move settings to embedded resource
- [x] Optimize web requests
- [x] Create console logging utility
- [x] Remove unused libraries

## CLI Args

- [x] Clear web download cache
8 changes: 1 addition & 7 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

## General

- [x] Optimize code and fragment info methods
- [ ] Localization
- [ ] ~~Add web link caching with hash checking~~

## CLI Args

- [ ] Open web cache directory in explorer
- [ ] Add comments
39 changes: 17 additions & 22 deletions src/App.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CommandLine;
using Newtonsoft.Json;
using SixLabors.ImageSharp.Processing.Processors.Transforms;

using System.Diagnostics;
using System.Net.Http.Headers;
using System.Reflection;

Expand Down Expand Up @@ -62,41 +61,46 @@

[Option('c', "clear", Required = false, Default = false, HelpText = "Clear downloaded cache folder.")]
public bool ClearCache { get; set; }

}

public static class Logger
{
public enum LogLevel
{
Verbose,
Info,
Warn,
Warning,
Error
}

private static readonly Dictionary<LogLevel, int> LevelColors = new()
{
{ LogLevel.Info, 4 }, // Blue
{ LogLevel.Warn, 3 }, // Yellow
{ LogLevel.Error, 1 } // Red
{ LogLevel.Verbose, 7 }, // White
{ LogLevel.Info, 4 }, // Blue
{ LogLevel.Warning, 3 }, // Yellow
{ LogLevel.Error, 1 } // Red
};

private static readonly Dictionary<LogLevel, string> LevelPrefix= new()
{
{ LogLevel.Verbose, Messages.LogCategory_Verbose },
{ LogLevel.Info, Messages.LogCategory_Info },
{ LogLevel.Warn, Messages.LogCategory_Warning },
{ LogLevel.Warning, Messages.LogCategory_Warning },
{ LogLevel.Error, Messages.LogCategory_Error }
};

private static readonly Dictionary<LogLevel, int> LevelExitCodes = new()
{
{ LogLevel.Verbose, 0 },
{ LogLevel.Info, 0 },
{ LogLevel.Warn, 0 },
{ LogLevel.Warning, 0 },
{ LogLevel.Error, 1 }
};

public static void LogMsg(LogLevel Level, string Message)
{
Console.WriteLine($"\u001b[3{LevelColors[Level]}m{LevelPrefix[Level]}:\u001b[37m {Message}\u001b[39m");
Console.WriteLine($"\u001b[3{LevelColors[Level]}m{LevelPrefix[Level]}{((LevelPrefix[Level].Length > 0) ? ": " : "")}\u001b[37m{Message}\u001b[39m");
}

public static void LogExit(LogLevel Level, string Message)
Expand Down Expand Up @@ -129,23 +133,14 @@

private void Run(Options CommandLineOptions)
{
Assembly ExecutingAssembly = Assembly.GetExecutingAssembly();
Stream SettingsStream = ExecutingAssembly.GetManifestResourceStream("TYM.settings.json");
string SettingsContent = new StreamReader(SettingsStream).ReadToEnd();

dynamic Settings = JsonConvert.DeserializeObject<dynamic>(SettingsContent);

if (CommandLineOptions.ListResamplers)
{
Console.WriteLine("\u001b[32mAvailable resampling algorithms:");
Console.Write("\x1b[33m");
typeof(KnownResamplers).GetProperties().ToList().ForEach(x => Console.WriteLine(x.Name));
Console.WriteLine("\x1b[39m");

LogMsg(LogLevel.Info, Messages.Message_AvailableResamplers);
typeof(KnownResamplers).GetProperties().ToList().ForEach(x => LogMsg(LogLevel.Verbose, x.Name));
Environment.Exit(0);
}

string DownloadDirectory = Path.Combine(Path.GetTempPath(), (string)Settings["tempDirectoryName"]);
string DownloadDirectory = Path.Combine(Path.GetTempPath(), Settings.tempDirectoryName);
if (CommandLineOptions.ClearCache)
{
Directory.Delete(DownloadDirectory, true);
Expand All @@ -164,7 +159,7 @@
{
HttpClient Client = new();

List<string> SupportedMimeTypes = ((string[])Settings["supportedImageFormats"]).ToList();
List<string> SupportedMimeTypes = Settings.supportedImageFormats.Split(",").Select(x => x = $"image/{x}").ToList();
SupportedMimeTypes.ForEach(x => Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(x)));

Task<HttpResponseMessage> GetTask = Client.GetAsync(WebURI);
Expand All @@ -173,7 +168,7 @@

if (Response.IsSuccessStatusCode)
{
string ResponseMimeType = Response.Content.Headers.ContentType.MediaType;

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Converting null literal or possible null value to non-nullable type.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Converting null literal or possible null value to non-nullable type.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Converting null literal or possible null value to non-nullable type.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Converting null literal or possible null value to non-nullable type.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Converting null literal or possible null value to non-nullable type.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Dereference of a possibly null reference.

Check warning on line 171 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Converting null literal or possible null value to non-nullable type.

if (SupportedMimeTypes.Any(x => x == ResponseMimeType))
{
Expand All @@ -190,7 +185,7 @@
}
else
{
LogExit(LogLevel.Error, Messages.Error_InvalidMimeType, ResponseMimeType);

Check warning on line 188 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Possible null reference argument for parameter 'Argument' in 'void Logger.LogExit(LogLevel Level, string Message, string Argument)'.

Check warning on line 188 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Possible null reference argument for parameter 'Argument' in 'void Logger.LogExit(LogLevel Level, string Message, string Argument)'.

Check warning on line 188 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Possible null reference argument for parameter 'Argument' in 'void Logger.LogExit(LogLevel Level, string Message, string Argument)'.
}
}
else
Expand All @@ -216,12 +211,12 @@

private static void ProcessImageFile(Options CommandLineOptions, string ImagePath)
{
PropertyInfo? ResamplerProperty = typeof(KnownResamplers).GetProperty(CommandLineOptions.ResamplerName);

Check warning on line 214 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Possible null reference argument for parameter 'name' in 'PropertyInfo? Type.GetProperty(string name)'.

Check warning on line 214 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Possible null reference argument for parameter 'name' in 'PropertyInfo? Type.GetProperty(string name)'.

Check warning on line 214 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Possible null reference argument for parameter 'name' in 'PropertyInfo? Type.GetProperty(string name)'.
if (ResamplerProperty == null)
{
LogExit(LogLevel.Error, Messages.Error_InvalidResampler);
}
IResampler? Resampler = (IResampler)ResamplerProperty.GetValue(typeof(KnownResamplers));

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Dereference of a possibly null reference.

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Converting null literal or possible null value to non-nullable type.

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Dereference of a possibly null reference.

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Converting null literal or possible null value to non-nullable type.

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Dereference of a possibly null reference.

Check warning on line 219 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Converting null literal or possible null value to non-nullable type.
if (Resampler == null)
{
LogExit(LogLevel.Error, Messages.Error_ResamplerError);
Expand All @@ -234,7 +229,7 @@
{"Stretch", ResizeMode.Stretch},
{"Center", ResizeMode.Pad}
};
if (!AvailableResizeModes.ContainsKey(CommandLineOptions.ResizeMethod))

Check warning on line 232 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, ResizeMode>.ContainsKey(string key)'.

Check warning on line 232 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, ResizeMode>.ContainsKey(string key)'.

Check warning on line 232 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, ResizeMode>.ContainsKey(string key)'.
{
LogExit(LogLevel.Error, Messages.Error_InvalidResizeMode);
}
Expand All @@ -256,7 +251,7 @@
{
Size = TargetSize,
Mode = SelectedResizeMode,
Sampler = Resampler

Check warning on line 254 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (windows-latest, win)

Possible null reference assignment.

Check warning on line 254 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (ubuntu-latest, linux)

Possible null reference assignment.

Check warning on line 254 in src/App.cs

View workflow job for this annotation

GitHub Actions / Debug build (macos-latest, osx)

Possible null reference assignment.
}));

string Buffer = "";
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<data name="LogCategory.Info" xml:space="preserve">
<value>INFO</value>
</data>
<data name="LogCategory.Verbose" xml:space="preserve">
<value />
</data>
<data name="LogCategory.Warning" xml:space="preserve">
<value>WARNING</value>
</data>
Expand Down
6 changes: 3 additions & 3 deletions src/Properties/Messages.tr.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Properties/Messages.tr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<data name="LogCategory.Info" xml:space="preserve">
<value>BILGI</value>
</data>
<data name="LogCategory.Verbose" xml:space="preserve">
<value />
</data>
<data name="LogCategory.Warning" xml:space="preserve">
<value>UYARI</value>
</data>
Expand Down
81 changes: 81 additions & 0 deletions src/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading