Skip to content

Commit

Permalink
[API] Simplify FileUtils.IsFileUriOrPath (#285)
Browse files Browse the repository at this point in the history
Check for the presence of "file:" rather than the absence of each known non-file URI scheme

+semver:major
because this now recognizes third-party URI schemes as non-file
  • Loading branch information
papeh authored Jul 31, 2023
1 parent afbfc17 commit 7a34a25
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- [SIL.LCModel] Data migration now serializes dates using the culture-neutral `ToLCMTimeFormatWithMillisString` (LT-20698)
- [SIL.LCModel] `ReadWriteServices.LoadDateTime` now parses milliseconds correctly (LT-18205)

### Fixed

- [SIL.LCModel.Core] Copy `SIL.LCModel.Core.dll.config` to output directory
- [SIL.LCModel] Use `CaseFunctions` (to use the `WritingSystemDefinition.CaseAlias`, if any)
- [SIL.LCModel.Core] Use the `WritingSystemDefinition.CaseAlias`, if any, in `CaseFunctions`

### Changed

- [SIL.LCModel] `FileUtils.IsFileUriOrPath` checks for the presence of "file:" rather than the absence of known non-file URI schemes

### Deprecated

- [SIL.LCModel.Core] `new CaseFunctions(string)` in favor of the new `new CaseFunctions(CoreWritingSystemDefinition)`
Expand Down
3 changes: 3 additions & 0 deletions LCM.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=DLL_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Duolos/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=flid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=fwdata/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=icuuc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=LGPL/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ownerless/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Palaso/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Palaso_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sena/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=serv/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=silfw/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subdir/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subentry/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subrecord/@EntryIndexedValue">True</s:Boolean>
Expand Down
11 changes: 5 additions & 6 deletions src/SIL.LCModel.Utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,11 @@ public static bool IsFilePathValid(string filename)
/// ------------------------------------------------------------------------------------
public static bool IsFileUriOrPath(string uri)
{
string[] nonFileUris = { Uri.UriSchemeFtp, Uri.UriSchemeGopher, Uri.UriSchemeHttp,
Uri.UriSchemeHttps, Uri.UriSchemeMailto, Uri.UriSchemeNetPipe,
Uri.UriSchemeNetTcp, Uri.UriSchemeNews, Uri.UriSchemeNntp,
"silfw" };

return (nonFileUris.Where(x => uri.Trim().StartsWith(x + ":")).Count() == 0);
uri = uri.Trim();
// ":" instead of Uri.SchemeDelimiter in case someone enters http:\example.com
var limScheme = uri.IndexOf(":", StringComparison.Ordinal);
// Unix paths do not contain ":"; Windows paths begin "C:\" or similar
return limScheme == -1 || limScheme == 1 || uri.Substring(0, limScheme).Equals(Uri.UriSchemeFile);
}

/// ------------------------------------------------------------------------------------
Expand Down
32 changes: 12 additions & 20 deletions tests/SIL.LCModel.Utils.Tests/FileUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2009-2020 SIL International
// Copyright (c) 2009-2023 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)

Expand Down Expand Up @@ -42,25 +42,17 @@ public void TearDown()
#endregion

#region IsFileUriOrPath test
///--------------------------------------------------------------------------------------
/// <summary>
/// Tests the method IsFileUriOrPath
/// </summary>
///--------------------------------------------------------------------------------------
[Test]
public void IsFileUriOrPath()
{
Assert.IsTrue(FileUtils.IsFileUriOrPath("somePath/path"));
Assert.IsTrue(FileUtils.IsFileUriOrPath("/somePath/path"));
Assert.IsTrue(FileUtils.IsFileUriOrPath(@"somePath\path"));
Assert.IsTrue(FileUtils.IsFileUriOrPath(@"C:\somePath\path"));
Assert.IsTrue(FileUtils.IsFileUriOrPath("file://somePath/path"));

Assert.IsFalse(FileUtils.IsFileUriOrPath("http://www.google.com"));
Assert.IsFalse(FileUtils.IsFileUriOrPath("https://www.google.com"));
Assert.IsFalse(FileUtils.IsFileUriOrPath("ftp://www.google.com"));
Assert.IsFalse(FileUtils.IsFileUriOrPath(" http://www.google.com"));
}
[TestCase("somePath/path", ExpectedResult = true)]
[TestCase("/somePath/path", ExpectedResult = true)]
[TestCase(@"somePath\path", ExpectedResult = true)]
[TestCase(@"C:\somePath\path", ExpectedResult = true)]
[TestCase("file://somePath/path", ExpectedResult = true)]
[TestCase("http://www.google.com", ExpectedResult = false)]
[TestCase("https://www.google.com", ExpectedResult = false)]
[TestCase("ftp://www.google.com", ExpectedResult = false)]
[TestCase(" http://www.google.com", ExpectedResult = false)]
[TestCase("silfw:\\localhost\\link?app%3dFlex%26database%3dc%3a%5cFwProjects%5cSena+3%5cSena+3.fwdata", ExpectedResult = false)]
public bool IsFileUriOrPath(string path) => FileUtils.IsFileUriOrPath(path);
#endregion

#region IsFilePathValid test
Expand Down

0 comments on commit 7a34a25

Please sign in to comment.