This repository was archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileService.cs
More file actions
66 lines (58 loc) · 2.16 KB
/
FileService.cs
File metadata and controls
66 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
namespace SvgPathExtractor
{
public class FileService
{
public string FolderPath { get; set; }
public string OutputPath { get; set; } = string.Empty;
private string[]? Files { get; set; }
private List<FileOutput> RegexResults { get; set; }
public FileService()
{
FolderPath = Directory.GetCurrentDirectory();
RegexResults = new List<FileOutput>();
}
public void CreateNewDirectory()
{
string localPath = Directory.GetCurrentDirectory();
OutputPath = Directory.CreateDirectory($"{localPath}\\output").FullName;
}
public void ReadAllFiles()
{
Files = Directory.GetFiles(FolderPath, "*.svg");
RegexService regex = new RegexService();
for (int id = 0; id < Files.Length; id++ )
{
if (File.Exists(Files[id]))
{
string name = Path.GetFileNameWithoutExtension(Files[id]);
Console.WriteLine($"File {name}.svg exists. reading...");
string fileContent = File.ReadAllText(Files[id]);
string path = regex.GetSvgPath(fileContent);
string fullPath = regex.GetPathElementAsString(fileContent);
RegexResults.Add(new FileOutput { ID = id + 1, Path = path, FullPath = fullPath, Name = name });
}
}
}
public async Task WriteOutputToJson()
{
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};
foreach (var item in RegexResults)
{
await using FileStream createStream = File.Create($"{OutputPath}\\{item.Name}.json");
await JsonSerializer.SerializeAsync(createStream, item, options);
}
}
}
}