Skip to content

Commit cefd011

Browse files
committed
Store files in per-key subdirectories under TempPath
Refactored file storage to use a separate subdirectory for each key, improving isolation and organization. All file operations now target the key-specific directory. Also moved keys.txt into the TempPath directory.
1 parent 0aefa81 commit cefd011

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

UDToolAPI/Controllers/UploadController.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ namespace UDToolAPI.Controllers
88
public class UploadController : ControllerBase
99
{
1010
private static readonly string TempPath = Path.Combine(Path.GetTempPath(), "UDToolAPI");
11-
private static readonly string KeysPath = "keys.txt";
11+
private static readonly string KeysPath = Path.Combine(TempPath, "keys.txt");
12+
13+
private string GetKeyPath(string key)
14+
{
15+
return Path.Combine(TempPath, key);
16+
}
1217

1318
[HttpGet("/key/check/{key}")]
1419
public IActionResult GetKey(string key)
@@ -58,9 +63,10 @@ public async Task<IActionResult> Upload(IFormFile file, string key, string fileN
5863
if (file == null || file.Length == 0)
5964
return BadRequest("No file uploaded.");
6065

61-
Directory.CreateDirectory(TempPath);
66+
var keyPath = GetKeyPath(key);
67+
Directory.CreateDirectory(keyPath);
6268

63-
var filePath = Path.Combine(TempPath, fileName);
69+
var filePath = Path.Combine(keyPath, fileName);
6470
using (var stream = new FileStream(filePath, FileMode.Create))
6571
{
6672
await file.CopyToAsync(stream);
@@ -91,10 +97,11 @@ public IActionResult List(string key)
9197
var keys = System.IO.File.ReadAllLines(KeysPath);
9298
if (keys.Contains(key))
9399
{
94-
if (!Directory.Exists(TempPath))
100+
var keyPath = GetKeyPath(key);
101+
if (!Directory.Exists(keyPath))
95102
return Ok(new List<string>());
96103

97-
var fileNames = Directory.GetFiles(TempPath)
104+
var fileNames = Directory.GetFiles(keyPath)
98105
.Select(Path.GetFileName)
99106
.ToList();
100107
return Ok(fileNames);
@@ -126,7 +133,8 @@ public IActionResult Download(string key, string fileName)
126133
var keys = System.IO.File.ReadAllLines(KeysPath);
127134
if (keys.Contains(key))
128135
{
129-
var filePath = Path.Combine(TempPath, fileName);
136+
var keyPath = GetKeyPath(key);
137+
var filePath = Path.Combine(keyPath, fileName);
130138

131139
if (!System.IO.File.Exists(filePath))
132140
return NotFound("File not found.");
@@ -161,7 +169,8 @@ public IActionResult Search(string key, string searchTerm)
161169
var keys = System.IO.File.ReadAllLines(KeysPath);
162170
if (keys.Contains(key))
163171
{
164-
var files = Directory.GetFiles(TempPath, $"*{searchTerm}*");
172+
var keyPath = GetKeyPath(key);
173+
var files = Directory.GetFiles(keyPath, $"*{searchTerm}*");
165174
var fileNames = files.Select(Path.GetFileName).ToArray();
166175
return Ok(fileNames);
167176
}
@@ -192,7 +201,8 @@ public IActionResult Delete(string key, string fileName)
192201
var keys = System.IO.File.ReadAllLines(KeysPath);
193202
if (keys.Contains(key))
194203
{
195-
var filePath = Path.Combine(TempPath, fileName);
204+
var keyPath = GetKeyPath(key);
205+
var filePath = Path.Combine(keyPath, fileName);
196206

197207
if (!System.IO.File.Exists(filePath))
198208
return NotFound("File not found.");

0 commit comments

Comments
 (0)