-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelStorageService.cs
More file actions
148 lines (112 loc) · 4.98 KB
/
ModelStorageService.cs
File metadata and controls
148 lines (112 loc) · 4.98 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using ModelManager.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace ModelManager
{
using System.Text.RegularExpressions;
public class ModelStorageService
{
private readonly string _basePath;
public ModelStorageService(IWebHostEnvironment env)
{
_basePath = Path.Combine(env.WebRootPath, "models");
if (!Directory.Exists(_basePath)) Directory.CreateDirectory(_basePath);
}
public List<ImageResourceInfo> GetModelRequiredImages(string modelName)
{
var folderPath = Path.Combine(_basePath, modelName);
var quizPath = Path.Combine(folderPath, $"{modelName}.quiz");
var resources = new List<ImageResourceInfo>();
if (!File.Exists(quizPath)) return resources;
var content = File.ReadAllText(quizPath);
var matches = Regex.Matches(content, @"\""([^\""]+\.(png|jpg|jpeg|gif))\""");
var uniqueNames = matches.Cast<Match>()
.Select(m => m.Groups[1].Value)
.Distinct();
foreach (var imgName in uniqueNames)
{
resources.Add(new ImageResourceInfo
{
Name = imgName,
// مسیر برای فرانت: /models/ModelName/image.png
Path = $"/models/{modelName}/{imgName}",
Exists = File.Exists(Path.Combine(folderPath, imgName))
});
}
return resources;
}
public async Task SaveImageAsync(string modelName, string fileName, IFormFile file)
{
var folderPath = Path.Combine(_basePath, modelName);
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
var safeFileName = Path.GetFileName(fileName);
var fullPath = Path.Combine(folderPath, safeFileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
public List<QuizModelInfo> GetAllModels()
{
var models = new List<QuizModelInfo>();
if (!Directory.Exists(_basePath)) return models;
foreach (var dir in Directory.GetDirectories(_basePath))
{
var folderName = Path.GetFileName(dir);
var jsonPath = Path.Combine(dir, "model.json");
if (File.Exists(jsonPath))
{
var jsonContent = File.ReadAllText(jsonPath);
var info = JsonSerializer.Deserialize<QuizModelInfo>(jsonContent) ?? new QuizModelInfo();
var mainImgPath = Path.Combine(dir, $"{folderName}.jpg");
info.ImagePath = File.Exists(mainImgPath) ? $"/models/{folderName}/{folderName}.jpg" : null;
models.Add(info);
}
}
return models;
}
public async Task SaveModelAsync(QuizModelInfo info, string quizContent)
{
var folderPath = Path.Combine(_basePath, info.Name);
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
var jsonPath = Path.Combine(folderPath, "model.json");
var jsonString = JsonSerializer.Serialize(info, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(jsonPath, jsonString);
var quizPath = Path.Combine(folderPath, $"{info.Name}.quiz");
await File.WriteAllTextAsync(quizPath, quizContent);
}
public void DeleteModel(string modelName)
{
var folderPath = Path.Combine(_basePath, modelName);
if (Directory.Exists(folderPath))
Directory.Delete(folderPath, true);
}
public QuizModelInfo GetModel(string name)
{
var folderPath = Path.Combine(_basePath, name);
var jsonPath = Path.Combine(folderPath, "model.json");
var quizPath = Path.Combine(folderPath, $"{name}.quiz");
if (!File.Exists(jsonPath)) return null;
var jsonContent = File.ReadAllText(jsonPath);
var info = JsonSerializer.Deserialize<QuizModelInfo>(jsonContent);
if (File.Exists(quizPath))
info.QuizContent = File.ReadAllText(quizPath);
return info;
}
public async Task SaveModelJsonOnlyAsync(QuizModelInfo info)
{
var folderPath = Path.Combine(_basePath, info.Name);
var jsonPath = Path.Combine(folderPath, "model.json");
var jsonString = JsonSerializer.Serialize(info, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(jsonPath, jsonString);
}
}
public class ImageResourceInfo
{
public string Name { get; set; }
public string Path { get; set; }
public bool Exists { get; set; }
}
}