1

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.

air
  • 11
  • 3
  • Try ensuring that the file in filePath has an extension with a conditional (if) statement. This should make sure the folder isn't deleted. – JosephDoggie Aug 10 '23 at 13:19
  • https://stackoverflow.com/questions/1395205/better-way-to-check-if-a-path-is-a-file-or-a-directory – Max Naumov Aug 10 '23 at 13:27
  • The folder exists and it contains files. I watched it while debugging the code, and even with the if(uploadedFiles.Length > 0) check Folder is deleted. If you mean it – air Aug 10 '23 at 13:31
  • There is nothing wrong in the code. Are you deleting files present in cloud storage. Like Azure blob storage ? – Abhishek Vyas Aug 11 '23 at 11:09

1 Answers1

1

What is the result, is there any error? As I know, File.Delete does not throw any error if file is not exists.

  • If you are certain about the path, please check folder's permission to delete operations.
  • Please ensure that the file extensions are correct which you put.
  • Last option is that the file is still in use by some background process. Then it does not fail but it does not delete the file.
Mert Akkanat
  • 111
  • 7