From 189a2469562731e56113672c9cababe2d76f1673 Mon Sep 17 00:00:00 2001
From: sergeydryomin <33347470+sergeydryomin@users.noreply.github.com>
Date: Fri, 16 Aug 2019 17:05:35 +0300
Subject: [PATCH] deleting_themes_from_Extensions_doesnt_remove_it_from_themes
(#2950)
---
.../Common/Utilities/FileSystemUtils.cs | 39 ++++++++++------
.../FileSystemUtilsTests.cs | 44 +++++++++++++++++++
2 files changed, 69 insertions(+), 14 deletions(-)
diff --git a/DNN Platform/Library/Common/Utilities/FileSystemUtils.cs b/DNN Platform/Library/Common/Utilities/FileSystemUtils.cs
index fc4551e17f6..fd0cd92dd7e 100644
--- a/DNN Platform/Library/Common/Utilities/FileSystemUtils.cs
+++ b/DNN Platform/Library/Common/Utilities/FileSystemUtils.cs
@@ -200,7 +200,7 @@ public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string
try
{
//Open File Stream
- fs = File.OpenRead(filePath.Replace("/", "\\"));
+ fs = File.OpenRead(FixPath(filePath));
//Read file into byte array buffer
var buffer = new byte[fs.Length];
@@ -234,7 +234,7 @@ public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string
/// -----------------------------------------------------------------------------
///
- /// Trys to copy a file in the file system
+ /// Tries to copy a file in the file system
///
/// The name of the source file
/// The name of the destination file
@@ -253,16 +253,17 @@ public static void CopyFile(string sourceFileName, string destFileName)
/// Deletes file in areas with a high degree of concurrent file access (i.e. caching, logging)
/// This solves file concurrency issues under heavy load.
///
- /// String
+ /// String
/// Int16
/// Int16
/// Boolean
///
///
/// -----------------------------------------------------------------------------
- public static bool DeleteFileWithWait(string filename, Int16 waitInMilliseconds, Int16 maxAttempts)
+ public static bool DeleteFileWithWait(string fileName, Int16 waitInMilliseconds, Int16 maxAttempts)
{
- if (!File.Exists(filename))
+ fileName = FixPath(fileName);
+ if (!File.Exists(fileName))
{
return true;
}
@@ -277,9 +278,9 @@ public static bool DeleteFileWithWait(string filename, Int16 waitInMilliseconds,
i = i + 1;
try
{
- if (File.Exists(filename))
+ if (File.Exists(fileName))
{
- File.Delete(filename);
+ File.Delete(fileName);
}
fileDeleted = true; //we don't care if it didn't exist...the operation didn't fail, that's what we care about
}
@@ -298,15 +299,15 @@ public static bool DeleteFileWithWait(string filename, Int16 waitInMilliseconds,
/// -----------------------------------------------------------------------------
///
- /// Trys to delete a file from the file system
+ /// Tries to delete a file from the file system
///
/// The name of the file
/// -----------------------------------------------------------------------------
public static void DeleteFile(string fileName)
{
+ fileName = FixPath(fileName);
if (File.Exists(fileName))
{
- fileName = fileName.Replace('/', '\\');
File.SetAttributes(fileName, FileAttributes.Normal);
File.Delete(fileName);
}
@@ -348,7 +349,7 @@ public static void UnzipResources(ZipInputStream zipStream, string destPath)
}
if (!zipEntry.IsDirectory && (!string.IsNullOrEmpty(localFileName)))
{
- var fileNamePath = Path.Combine(destPath, localFileName).Replace("/", "\\");
+ var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
try
{
if (File.Exists(fileNamePath))
@@ -410,7 +411,7 @@ public static string DeleteFiles(Array arrPaths)
strPath = strPath.Substring(0, pos);
}
- strPath = strPath.Trim().Replace("/", "\\").TrimStart('\\');
+ strPath = FixPath(strPath).TrimStart('\\');
if (!string.IsNullOrEmpty(strPath))
{
strPath = Path.Combine(Globals.ApplicationMapPath, strPath);
@@ -457,6 +458,7 @@ public static void DeleteFilesRecursive(string strRoot, string filter)
{
if (!String.IsNullOrEmpty(strRoot))
{
+ strRoot = FixPath(strRoot);
if (Directory.Exists(strRoot))
{
foreach (string strFolder in Directory.EnumerateDirectoryPaths(strRoot))
@@ -467,7 +469,7 @@ public static void DeleteFilesRecursive(string strRoot, string filter)
DeleteFilesRecursive(strFolder, filter);
}
}
- foreach (string strFile in Directory.EnumerateFilePaths(strRoot).Where(f => f.Contains(filter)))
+ foreach (string strFile in Directory.EnumerateFilePaths(new DirectoryInfo(strRoot)).Where(f => f.Contains(filter)))
{
try
{
@@ -484,8 +486,8 @@ public static void DeleteFilesRecursive(string strRoot, string filter)
public static void DeleteFolderRecursive(string strRoot)
{
- strRoot = strRoot.Replace("/", "\\");
- if (string.IsNullOrEmpty(strRoot) || !Directory.Exists(strRoot.Trim()))
+ strRoot = FixPath(strRoot);
+ if (string.IsNullOrEmpty(strRoot) || !Directory.Exists(strRoot))
{ Logger.Info(strRoot + " does not exist. ");
return;
}
@@ -520,5 +522,14 @@ public static void DeleteFolderRecursive(string strRoot)
}
}
+ public static string FixPath(string input)
+ {
+ if (string.IsNullOrEmpty(input))
+ {
+ return input;
+ }
+
+ return input.Trim().Replace("/", "\\");
+ }
}
}
diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/FileSystemUtilsTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Core/FileSystemUtilsTests.cs
index cb06bd902ea..3aa1ffed394 100644
--- a/DNN Platform/Tests/DotNetNuke.Tests.Core/FileSystemUtilsTests.cs
+++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/FileSystemUtilsTests.cs
@@ -119,6 +119,50 @@ public void AddToZip_Should_Able_To_Add_Multiple_Files()
}
}
+ [Test]
+ public void DeleteFile_Should_Delete_File()
+ {
+ //Action
+ var testPath = Globals.ApplicationMapPath + $"/Test{Guid.NewGuid().ToString().Substring(0, 8)}.txt";
+ using (StreamWriter sw = File.CreateText(testPath))
+ {
+ sw.WriteLine("48");
+ }
+
+ FileSystemUtils.DeleteFile(testPath);
+
+ //Assert
+ bool res = File.Exists(testPath.Replace("/", "\\"));
+ Assert.IsFalse(res);
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase(" ")]
+ [TestCase("/")]
+ [TestCase("Test/Test ")]
+ public void FixPath_Should_Change_Slashes_And_Trim(string input)
+ {
+ //Action
+ var result = FileSystemUtils.FixPath(input);
+
+ //Assert
+ if (string.IsNullOrEmpty(input))
+ {
+ Assert.IsTrue(input == result);
+ }
+ else if(string.IsNullOrWhiteSpace(input))
+ {
+ Assert.IsTrue(result == string.Empty);
+ }
+ else
+ {
+ Assert.IsFalse(result.Contains(" "));
+ Assert.IsFalse(result.Contains("/"));
+ }
+
+ }
+
private void PrepareRootPath(string rootPath)
{
if (!Directory.Exists(rootPath))