Skip to content

Commit

Permalink
Fix for file creation with bad filename
Browse files Browse the repository at this point in the history
  • Loading branch information
d2dyno1 committed Dec 22, 2021
1 parent b1f5855 commit dc44115
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ClipboardCanvas/Helpers/Filesystem/FilesystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static async Task<SafeWrapper<StorageFile>> CreateFile(string path, Creat
return new SafeWrapper<StorageFile>(null, parentFolder.Details);
}

string fileName = Path.GetFileName(path);
string fileName = RemoveIllegalFileNameCharacters(Path.GetFileName(path));

return await CreateFile(parentFolder, fileName, collision);
}
Expand All @@ -138,7 +138,7 @@ public static async Task<SafeWrapper<StorageFile>> CreateFile(StorageFolder pare
return new SafeWrapper<StorageFile>(null, OperationErrorCode.InvalidArgument, new ArgumentNullException(), "The provided folder is null.");
}

return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFileAsync(fileName, collision).AsTask());
return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFileAsync(RemoveIllegalFileNameCharacters(fileName), collision).AsTask());
}

public static async Task<SafeWrapper<StorageFolder>> CreateFolder(string path, CreationCollisionOption collision = CreationCollisionOption.GenerateUniqueName)
Expand All @@ -165,5 +165,10 @@ public static async Task<SafeWrapper<StorageFolder>> CreateFolder(StorageFolder

return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFolderAsync(name, collision).AsTask());
}

private static string RemoveIllegalFileNameCharacters(string dangerousFileName)
{
return string.Join("_", dangerousFileName.Split(Path.GetInvalidFileNameChars()));
}
}
}

0 comments on commit dc44115

Please sign in to comment.