There is a problem with working with files and directories inside the repository folder. The problem is that when you try to delete only files inside a certain folder, the "UploadedFilesForEncrypting" folder itself is deleted.
Here is the field with shared folder for users
public string FolderForEnc
{
get
{
return Path.Join(_webHostEnvironment.WebRootPath, "UploadedFilesForEncrypting");
}
}
Here is the controller itself in which there is a small logic for deleting files in the user folder
[HttpDelete("DeleteEncryptedFiles")]
public IActionResult DeleteFiles([FromHeader(Name = "ApiKey")] string apiKey)
{
try
{
// ЗДЕСЬ ПРОВЕРКА ПОДДЛИННОСТИ API КЛЮЧА И АВТОРИЗАЦИЯ ПОЛЬЗОВАТЕЛЯ
var userid = "air"; //_readUser.ReadUserIdByApiKey(apiKey)
string uploadsFolder = Path.Join(FolderForEnc, $"User({userid})");
if (Directory.Exists(uploadsFolder))
{
string[] uploadedFiles = Directory.GetFiles(uploadsFolder);
if(uploadedFiles.Length > 0)
{
foreach (string filePath in uploadedFiles)
{
System.IO.File.Delete(filePath);
}
return Ok(new { message = "Files deleted successfully" });
}
return NotFound("There are no files in your folder");
}
else
{
return NotFound("User folder not found");
}
}
catch (InvalidOperationException)
{
return BadRequest("User with this API key not found");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Tried changing Path.Join to Path.Combine, tried changing Directory.GetFiles(uploadsFolder) to Directory.GetFiles(uploadsFolder, "*", SearchOption.AllDirectories). I also tried to remove parameters and pass abstract data as in this example. None of this helped.