Skip to content

Commit 1db2821

Browse files
Squashed commit of the following:
commit 0977b2b Author: kevinvenclovas <venclovas.kevin@googlemail.com> Date: Sun May 4 09:16:24 2025 +0200 added VATInvoicing Report commit c0d905a Author: kevinvenclovas <venclovas.kevin@googlemail.com> Date: Sun Mar 9 13:22:05 2025 +0100 remove Ude.NetStandard commit e19f0f1 Author: kevinvenclovas <venclovas.kevin@googlemail.com> Date: Thu Feb 20 19:16:09 2025 +0100 optimize code commit df935e6 Author: kevinvenclovas <venclovas.kevin@googlemail.com> Date: Thu Feb 20 18:52:39 2025 +0100 update readme
1 parent 8e0d338 commit 1db2821

File tree

7 files changed

+152
-35
lines changed

7 files changed

+152
-35
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ Please see [here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Sou
138138
>>
139139
>> ***This is not required and will operate normally without the ProxyAddress being set.***
140140
141+
142+
### Configuration using Docker - Linux
143+
You need to add windows-1252 encoding
144+
```csharp
145+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
146+
```
147+
148+
141149
### Order List, For more orders sample please check [Here](https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/main/Source/FikaAmazonAPI.SampleCode/ReportsSample.cs).
142150
```CSharp
143151
ParameterOrderList serachOrderList = new ParameterOrderList();

Source/FikaAmazonAPI/FikaAmazonAPI.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<PackageReference Include="System.Collections" Version="4.3.0" />
4444
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
4545
<PackageReference Include="System.Reflection" Version="4.3.0" />
46-
<PackageReference Include="Ude.NetStandard" Version="1.2.0" />
4746
</ItemGroup>
4847
<ItemGroup>
4948
<None Include="..\..\Others\icon\icon.jpg">

Source/FikaAmazonAPI/ReportGeneration/ReportDataTable/Table.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Text.RegularExpressions;
8+
using System.Threading;
89

910
namespace FikaAmazonAPI.ReportGeneration.ReportDataTable
1011
{
@@ -51,7 +52,7 @@ public Table(params string[] header)
5152

5253
public static Table ConvertFromCSV(string path, char separator = '\t')
5354
{
54-
var lines = File.ReadAllLines(path, Encoding.UTF8);
55+
var lines = File.ReadAllLines(path, EncodingHelper.GetEncodingFromCulture(Thread.CurrentThread.CurrentCulture));
5556

5657
var table = new Table(lines.First().Split(separator));
5758

Source/FikaAmazonAPI/Services/ReportService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private async Task<string> GetFileAsync(ReportDocument reportDocument, Cancellat
247247

248248
cancellationToken.ThrowIfCancellationRequested();
249249

250-
FileTransform.ConvertFileToUtf8(tempFilePath);
250+
FileTransform.SetFileEncoding(tempFilePath);
251251

252252
return tempFilePath;
253253
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace FikaAmazonAPI.Utils
7+
{
8+
public class EncodingDetector
9+
{
10+
private static readonly Encoding[] CommonEncodings = {
11+
Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode,
12+
Encoding.UTF32, Encoding.ASCII, Encoding.GetEncoding("ISO-8859-1")
13+
};
14+
15+
public static Encoding DetectEncoding(string filePath, int sampleSize = 4096)
16+
{
17+
byte[] buffer = File.ReadAllBytes(filePath);
18+
return DetectEncoding(buffer, sampleSize);
19+
}
20+
21+
public static Encoding DetectEncoding(byte[] buffer, int sampleSize = 4096)
22+
{
23+
if (buffer == null || buffer.Length == 0)
24+
throw new ArgumentException("Buffer is empty");
25+
26+
// Schritt 1: Prüfe auf BOM
27+
Encoding bomEncoding = CheckBOM(buffer);
28+
if (bomEncoding != null)
29+
return bomEncoding;
30+
31+
// Schritt 2: Prüfe auf UTF-8
32+
if (IsUTF8(buffer))
33+
return Encoding.UTF8;
34+
35+
// Schritt 3: Prüfe auf bekannte Encodings durch Heuristik
36+
return HeuristicDetection(buffer);
37+
}
38+
39+
private static Encoding CheckBOM(byte[] buffer)
40+
{
41+
if (buffer.Length >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF)
42+
return Encoding.UTF8; // UTF-8 mit BOM
43+
44+
if (buffer.Length >= 2 && buffer[0] == 0xFF && buffer[1] == 0xFE)
45+
return Encoding.Unicode; // UTF-16 LE
46+
47+
if (buffer.Length >= 2 && buffer[0] == 0xFE && buffer[1] == 0xFF)
48+
return Encoding.BigEndianUnicode; // UTF-16 BE
49+
50+
if (buffer.Length >= 4 && buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF)
51+
return Encoding.UTF32; // UTF-32 BE
52+
53+
if (buffer.Length >= 4 && buffer[0] == 0xFF && buffer[1] == 0xFE && buffer[2] == 0x00 && buffer[3] == 0x00)
54+
return Encoding.UTF32; // UTF-32 LE
55+
56+
return null;
57+
}
58+
59+
private static bool IsUTF8(byte[] buffer)
60+
{
61+
int i = 0;
62+
while (i < buffer.Length)
63+
{
64+
if (buffer[i] <= 0x7F)
65+
{
66+
i++;
67+
continue;
68+
}
69+
70+
if (buffer[i] >= 0xC2 && buffer[i] <= 0xDF)
71+
{
72+
if (i + 1 < buffer.Length && (buffer[i + 1] & 0xC0) == 0x80)
73+
{
74+
i += 2;
75+
continue;
76+
}
77+
}
78+
else if (buffer[i] >= 0xE0 && buffer[i] <= 0xEF)
79+
{
80+
if (i + 2 < buffer.Length && (buffer[i + 1] & 0xC0) == 0x80 && (buffer[i + 2] & 0xC0) == 0x80)
81+
{
82+
i += 3;
83+
continue;
84+
}
85+
}
86+
else if (buffer[i] >= 0xF0 && buffer[i] <= 0xF4)
87+
{
88+
if (i + 3 < buffer.Length && (buffer[i + 1] & 0xC0) == 0x80 && (buffer[i + 2] & 0xC0) == 0x80 && (buffer[i + 3] & 0xC0) == 0x80)
89+
{
90+
i += 4;
91+
continue;
92+
}
93+
}
94+
95+
return false;
96+
}
97+
return true;
98+
}
99+
100+
private static Encoding HeuristicDetection(byte[] buffer)
101+
{
102+
int asciiCount = buffer.Count(b => b <= 127);
103+
int extendedCount = buffer.Length - asciiCount;
104+
105+
if (extendedCount == 0)
106+
return Encoding.ASCII; // ASCII-Dateien enthalten keine Sonderzeichen
107+
108+
return Encoding.GetEncoding("ISO-8859-1"); // Latin-1 als Fallback für ANSI
109+
}
110+
}
111+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Globalization;
2+
using System.Text;
3+
4+
namespace FikaAmazonAPI.Utils
5+
{
6+
public static class EncodingHelper
7+
{
8+
public static Encoding GetEncodingFromCulture(CultureInfo culture)
9+
{
10+
string cultureName = culture.Name.ToLower();
11+
12+
return cultureName switch
13+
{
14+
"en-us" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
15+
"de-de" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
16+
"fr-fr" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
17+
"ja-jp" => Encoding.GetEncoding("shift_jis"), // Japanisch
18+
"zh-cn" => Encoding.GetEncoding("gb2312"), // Vereinfachtes Chinesisch
19+
"ru-ru" => Encoding.GetEncoding("Windows-1251"), // Kyrillisch
20+
"ko-kr" => Encoding.GetEncoding("ks_c_5601-1987"), // Koreanisch
21+
"ar-sa" => Encoding.GetEncoding("Windows-1256"), // Arabisch
22+
_ => Encoding.UTF8 // Standard als Fallback
23+
};
24+
}
25+
}
26+
}

Source/FikaAmazonAPI/Utils/FileTransform.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Security.Cryptography;
66
using System.Text;
77
using System.Threading;
8-
using Ude;
98

109
namespace FikaAmazonAPI.Utils
1110
{
@@ -56,39 +55,12 @@ public static string Decompress(string fileName)
5655
}
5756
}
5857

59-
public static void ConvertFileToUtf8(string filePath)
58+
public static void SetFileEncoding(string filePath)
6059
{
61-
string content = File.ReadAllText(filePath, DetectFileEncoding(filePath));
62-
File.WriteAllText(filePath, content, GetEncodingFromCulture(Thread.CurrentThread.CurrentCulture));
63-
}
64-
static Encoding DetectFileEncoding(string filePath)
65-
{
66-
using (FileStream fs = File.OpenRead(filePath))
67-
{
68-
CharsetDetector detector = new CharsetDetector();
69-
detector.Feed(fs);
70-
detector.DataEnd();
71-
72-
return detector.Charset != null ? Encoding.GetEncoding(detector.Charset) : Encoding.UTF8;
73-
}
60+
var detectedEncoding = EncodingDetector.DetectEncoding(filePath);
61+
string content = File.ReadAllText(filePath, detectedEncoding);
62+
File.WriteAllText(filePath, content, EncodingHelper.GetEncodingFromCulture(Thread.CurrentThread.CurrentCulture));
7463
}
7564

76-
static Encoding GetEncodingFromCulture(CultureInfo culture)
77-
{
78-
string cultureName = culture.Name.ToLower();
79-
80-
return cultureName switch
81-
{
82-
"en-us" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
83-
"de-de" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
84-
"fr-fr" => Encoding.GetEncoding("Windows-1252"), // Westeuropa (ANSI)
85-
"ja-jp" => Encoding.GetEncoding("shift_jis"), // Japanisch
86-
"zh-cn" => Encoding.GetEncoding("gb2312"), // Vereinfachtes Chinesisch
87-
"ru-ru" => Encoding.GetEncoding("Windows-1251"), // Kyrillisch
88-
"ko-kr" => Encoding.GetEncoding("ks_c_5601-1987"), // Koreanisch
89-
"ar-sa" => Encoding.GetEncoding("Windows-1256"), // Arabisch
90-
_ => Encoding.UTF8 // Standard als Fallback
91-
};
92-
}
9365
}
9466
}

0 commit comments

Comments
 (0)