Skip to content

Commit b2f4b19

Browse files
committed
Refactor endpoints to use API key from header
All UploadController endpoints now require the API key via the "API-Key" HTTP header instead of as a route parameter. Updated error messages to reference "API key" and removed the key from unauthorized responses. Added helper method for header extraction.
1 parent cefd011 commit b2f4b19

1 file changed

Lines changed: 41 additions & 25 deletions

File tree

UDToolAPI/Controllers/UploadController.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ public class UploadController : ControllerBase
99
{
1010
private static readonly string TempPath = Path.Combine(Path.GetTempPath(), "UDToolAPI");
1111
private static readonly string KeysPath = Path.Combine(TempPath, "keys.txt");
12+
private static readonly string ApiKeyHeaderName = "API-Key";
13+
14+
private string? GetApiKeyFromHeader()
15+
{
16+
return Request.Headers[ApiKeyHeaderName].ToString();
17+
}
1218

1319
private string GetKeyPath(string key)
1420
{
@@ -49,11 +55,13 @@ public IActionResult CreateKey()
4955
}
5056

5157
// Endpoint to upload a file with a specific name
52-
[HttpPost("{key}/{fileName}")]
53-
public async Task<IActionResult> Upload(IFormFile file, string key, string fileName)
58+
[HttpPost("{fileName}")]
59+
public async Task<IActionResult> Upload(IFormFile file, string fileName)
5460
{
61+
var key = GetApiKeyFromHeader();
62+
5563
if (string.IsNullOrEmpty(key))
56-
return BadRequest("Key is required.");
64+
return BadRequest("API key is required.");
5765

5866
if (System.IO.File.Exists(KeysPath))
5967
{
@@ -75,22 +83,24 @@ public async Task<IActionResult> Upload(IFormFile file, string key, string fileN
7583
}
7684
else
7785
{
78-
return Unauthorized(new { message = "Key is not valid.", key });
86+
return Unauthorized(new { message = "Key is not valid." });
7987
}
8088
}
8189
else
8290
{
8391
System.IO.File.Create(KeysPath).Dispose();
84-
return Unauthorized(new { message = "Key is not valid.", key });
92+
return Unauthorized(new { message = "Key is not valid." });
8593
}
8694
}
8795

8896
// Endpoint to list all files in the temp directory
89-
[HttpGet("{key}/list")]
90-
public IActionResult List(string key)
97+
[HttpGet("list")]
98+
public IActionResult List()
9199
{
100+
var key = GetApiKeyFromHeader();
101+
92102
if (string.IsNullOrEmpty(key))
93-
return BadRequest("Key is required.");
103+
return BadRequest("API key is required.");
94104

95105
if (System.IO.File.Exists(KeysPath))
96106
{
@@ -108,22 +118,24 @@ public IActionResult List(string key)
108118
}
109119
else
110120
{
111-
return Unauthorized(new { message = "Key is not valid.", key });
121+
return Unauthorized(new { message = "Key is not valid." });
112122
}
113123
}
114124
else
115125
{
116126
System.IO.File.Create(KeysPath).Dispose();
117-
return Unauthorized(new { message = "Key is not valid.", key });
127+
return Unauthorized(new { message = "Key is not valid." });
118128
}
119129
}
120130

121131
// Endpoint to download a file by name
122-
[HttpGet("{key}/{fileName}")]
123-
public IActionResult Download(string key, string fileName)
132+
[HttpGet("{fileName}")]
133+
public IActionResult Download(string fileName)
124134
{
135+
var key = GetApiKeyFromHeader();
136+
125137
if (string.IsNullOrEmpty(key))
126-
return BadRequest("Key is required.");
138+
return BadRequest("API key is required.");
127139

128140
if (string.IsNullOrEmpty(fileName))
129141
return BadRequest("File name is required.");
@@ -144,22 +156,24 @@ public IActionResult Download(string key, string fileName)
144156
}
145157
else
146158
{
147-
return Unauthorized(new { message = "Key is not valid.", key });
159+
return Unauthorized(new { message = "Key is not valid." });
148160
}
149161
}
150162
else
151163
{
152164
System.IO.File.Create(KeysPath).Dispose();
153-
return Unauthorized(new { message = "Key is not valid.", key });
165+
return Unauthorized(new { message = "Key is not valid." });
154166
}
155167
}
156168

157169
// Endpoint to find files containing the search term in their name
158-
[HttpGet("{key}/search/{searchTerm}")]
159-
public IActionResult Search(string key, string searchTerm)
170+
[HttpGet("search/{searchTerm}")]
171+
public IActionResult Search(string searchTerm)
160172
{
173+
var key = GetApiKeyFromHeader();
174+
161175
if (string.IsNullOrEmpty(key))
162-
return BadRequest("Key is required.");
176+
return BadRequest("API key is required.");
163177

164178
if (string.IsNullOrEmpty(searchTerm))
165179
return BadRequest("Search term is required.");
@@ -176,22 +190,24 @@ public IActionResult Search(string key, string searchTerm)
176190
}
177191
else
178192
{
179-
return Unauthorized(new { message = "Key is not valid.", key });
193+
return Unauthorized(new { message = "Key is not valid." });
180194
}
181195
}
182196
else
183197
{
184198
System.IO.File.Create(KeysPath).Dispose();
185-
return Unauthorized(new { message = "Key is not valid.", key });
199+
return Unauthorized(new { message = "Key is not valid." });
186200
}
187201
}
188202

189203
// Endpoint to delete a file by name
190-
[HttpDelete("{key}/{fileName}")]
191-
public IActionResult Delete(string key, string fileName)
204+
[HttpDelete("{fileName}")]
205+
public IActionResult Delete(string fileName)
192206
{
207+
var key = GetApiKeyFromHeader();
208+
193209
if (string.IsNullOrEmpty(key))
194-
return BadRequest("Key is required.");
210+
return BadRequest("API key is required.");
195211

196212
if (string.IsNullOrEmpty(fileName))
197213
return BadRequest("File name is required.");
@@ -213,13 +229,13 @@ public IActionResult Delete(string key, string fileName)
213229
}
214230
else
215231
{
216-
return Unauthorized(new { message = "Key is not valid.", key });
232+
return Unauthorized(new { message = "Key is not valid." });
217233
}
218234
}
219235
else
220236
{
221237
System.IO.File.Create(KeysPath).Dispose();
222-
return Unauthorized(new { message = "Key is not valid.", key });
238+
return Unauthorized(new { message = "Key is not valid." });
223239
}
224240
}
225241
}

0 commit comments

Comments
 (0)