Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleting_themes_from_Extensions_doesnt_remove_it_from_themes #2945

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions DNN Platform/Library/Common/Utilities/FileSystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -234,7 +234,7 @@ public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string

/// -----------------------------------------------------------------------------
/// <summary>
/// Trys to copy a file in the file system
/// Tries to copy a file in the file system
/// </summary>
/// <param name="sourceFileName">The name of the source file</param>
/// <param name="destFileName">The name of the destination file</param>
Expand All @@ -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.
/// </summary>
/// <param name="filename">String</param>
/// <param name="fileName">String</param>
/// <param name="waitInMilliseconds">Int16</param>
/// <param name="maxAttempts">Int16</param>
/// <returns>Boolean</returns>
/// <remarks>
/// </remarks>
/// -----------------------------------------------------------------------------
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;
}
Expand All @@ -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
}
Expand All @@ -298,15 +299,15 @@ public static bool DeleteFileWithWait(string filename, Int16 waitInMilliseconds,

/// -----------------------------------------------------------------------------
/// <summary>
/// Trys to delete a file from the file system
/// Tries to delete a file from the file system
/// </summary>
/// <param name="fileName">The name of the file</param>
/// -----------------------------------------------------------------------------
public static void DeleteFile(string fileName)
{
fileName = FixPath(fileName);
if (File.Exists(fileName))
{
fileName = fileName.Replace('/', '\\');
File.SetAttributes(fileName, FileAttributes.Normal);
File.Delete(fileName);
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -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
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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("/", "\\");
}
}
}
44 changes: 44 additions & 0 deletions DNN Platform/Tests/DotNetNuke.Tests.Core/FileSystemUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down