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

Juliata/filetypes #22538

Merged
merged 3 commits into from
Dec 7, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/modules/peek/Peek.Common/Models/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public File(string path)

public string Path { get; init; }

public string Extension => System.IO.Path.GetExtension(Path);
public string Extension => System.IO.Path.GetExtension(Path).ToLower();

public async Task<StorageFile> GetStorageFileAsync()
{
Expand Down
3 changes: 3 additions & 0 deletions src/modules/peek/Peek.FilePreviewer/Previewers/IPreviewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Peek.FilePreviewer.Previewers
{
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.Foundation;
Expand All @@ -12,6 +13,8 @@ public interface IPreviewer : INotifyPropertyChanged
{
bool IsPreviewLoaded { get; }

public static bool IsFileTypeSupported(string fileExt) => throw new NotImplementedException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be made abstract to not require an implementation.

Copy link
Contributor Author

@jth-ms jth-ms Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I had tried that but got this error: The interface 'IPreviewer' cannot be used as type argument. Static member 'IPreviewer.IsFileTypeSupported(string)' does not have a most specific implementation in the interface.

I don't think we can have static abstract members in a C# interface? My C# is a bit rusty, but I think it might relate to this open issue:
dotnet/csharplang#4436

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


public Task<Size> GetPreviewSizeAsync();

Task LoadPreviewAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Peek.FilePreviewer.Previewers
{
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
Expand Down Expand Up @@ -168,5 +169,75 @@ private static async Task<BitmapSource> GetBitmapFromHBitmapAsync(IntPtr hbitmap
NativeMethods.DeleteObject(hbitmap);
}
}

public static bool IsFileTypeSupported(string fileExt)
{
return _supportedFileTypes.Contains(fileExt);
}

private static readonly HashSet<string> _supportedFileTypes = new HashSet<string>
{
// Image types
".bmp",
".gif",
".jpg",
".jfif",
".jfi",
".jif",
".jpeg",
".jpe",
".png",
".tif",
".tiff",
".dib",

// ".heic", // Error in System.Drawing.Image.FromHbitmap(hbitmap);
".heif",
".hif",
".avif",
".jxr",
".wdp",
".ico",
".thumb",

// Raw types
".arw",
".cr2",

// ".crw", // Error in WICImageFactory.CreateDecoderFromFilename
// ".erf", // Error in WICImageFactory.CreateDecoderFromFilename
".kdc",
".mrw",
".nef",
".nrw",
".orf",
".pef",
".raf",
".raw",
".rw2",
".rwl",
".sr2",
".srw",
".srf",
".dcs",
".dcr",
".drf",
".k25",
".3fr",
".ari",
".bay",
".cap",
".iiq",
".eip",
".fff",
".mef",
".mdc",
".mos",
".R3D",
".rwz",
".x3f",
".ori",
".cr3",
};
}
}
21 changes: 5 additions & 16 deletions src/modules/peek/Peek.FilePreviewer/Previewers/PreviewerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,13 @@ public class PreviewerFactory
{
public IPreviewer? Create(File file)
{
// TODO: investigate performance of reflection to resolve previewer type
switch (file.Extension)
if (ImagePreviewer.IsFileTypeSupported(file.Extension))
{
case ".bmp":
case ".gif":
case ".jpg":
case ".jfif":
case ".jfi":
case ".jif":
case ".jpeg":
case ".jpe":
case ".png":
case ".tif":
case ".tiff":
return new ImagePreviewer(file);
default:
return null;
return new ImagePreviewer(file);
}

// Other previewer types check their supported file types here
return null;
}
}
}