Skip to content

Commit

Permalink
Fix #825 properly
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed Mar 7, 2021
1 parent 9f42632 commit a912614
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void Dispose()

private static AnimationProvider InitAnimationProvider(Uri path, MetaProvider meta)
{
var ext = Path.GetExtension(path.OriginalString).ToLower();
var ext = Path.GetExtension(path.LocalPath).ToLower();
var type = Providers.First(p => p.Key.Contains(ext) || p.Key.Contains("*")).Value;

var provider = type.CreateInstance<AnimationProvider>(path.OriginalString, meta);
var provider = type.CreateInstance<AnimationProvider>(path, meta);

return provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{
internal abstract class AnimationProvider : IDisposable
{
protected AnimationProvider(string path, MetaProvider meta)
protected AnimationProvider(Uri path, MetaProvider meta)
{
Path = path;
Meta = meta;
}

public string Path { get; }
public Uri Path { get; }

public MetaProvider Meta { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ internal class APngProvider : AnimationProvider
private int _lastEffectivePreviousPreviousFrameIndex;
private NativeProvider _nativeImageProvider;

public APngProvider(string path, MetaProvider meta) : base(path, meta)
public APngProvider(Uri path, MetaProvider meta) : base(path, meta)
{
if (!IsAnimatedPng(path))
if (!IsAnimatedPng(path.LocalPath))
{
_nativeImageProvider = new NativeProvider(path, meta);
Animator = _nativeImageProvider.Animator;
return;
}

var decoder = new APNGBitmap(path);
var decoder = new APNGBitmap(path.LocalPath);

_baseFrame = decoder.DefaultImage;
_frames = new List<FrameInfo>(decoder.Frames.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
{
internal class DcrawProvider : NativeProvider
{
public DcrawProvider(string path, MetaProvider meta) : base(path, meta)
public DcrawProvider(Uri path, MetaProvider meta) : base(path, meta)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ internal class GifProvider : AnimationProvider
private bool _isPlaying;
private NativeProvider _nativeProvider;

public GifProvider(string path, MetaProvider meta) : base(path, meta)
public GifProvider(Uri path, MetaProvider meta) : base(path, meta)
{
if (!ImageAnimator.CanAnimate(Image.FromFile(path)))
if (!ImageAnimator.CanAnimate(Image.FromFile(path.LocalPath)))
{
_nativeProvider = new NativeProvider(path, meta);
return;
}

_fileHandle = (Bitmap) Image.FromFile(path);
_fileHandle = (Bitmap) Image.FromFile(path.LocalPath);

_fileHandle.SetResolution(DpiHelper.DefaultDpi * DpiHelper.GetCurrentScaleFactor().Horizontal,
DpiHelper.DefaultDpi * DpiHelper.GetCurrentScaleFactor().Vertical);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
{
internal class ImageMagickProvider : AnimationProvider
{
public ImageMagickProvider(string path, MetaProvider meta) : base(path, meta)
public ImageMagickProvider(Uri path, MetaProvider meta) : base(path, meta)
{
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
Expand Down Expand Up @@ -90,7 +90,7 @@ public override Task<BitmapSource> GetRenderedFrame(int index)
try
{
using (var mi = new MagickImage(Path, settings))
using (var mi = new MagickImage(Path.LocalPath, settings))
{
var profile = mi.GetColorProfile();
if (mi.ColorSpace == ColorSpace.RGB || mi.ColorSpace == ColorSpace.sRGB ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
{
internal class NativeProvider : AnimationProvider
{
public NativeProvider(string path, MetaProvider meta) : base(path, meta)
public NativeProvider(Uri path, MetaProvider meta) : base(path, meta)
{
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
Expand Down Expand Up @@ -55,7 +55,7 @@ public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(Path);
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
// specific renderSize to avoid .net's double to int conversion
img.DecodePixelWidth = rotate ? decodeHeight : decodeWidth;
Expand Down Expand Up @@ -94,7 +94,7 @@ public override Task<BitmapSource> GetRenderedFrame(int index)
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(Path);
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
img.DecodePixelWidth = (int) (rotate ? fullSize.Height : fullSize.Width);
img.DecodePixelHeight = (int) (rotate ? fullSize.Width : fullSize.Height);
Expand Down
30 changes: 30 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Media.Imaging;
using QuickLook.Common.Helpers;

Expand All @@ -37,5 +40,32 @@ public static void DpiHack(BitmapSource img)
dpiX?.SetValue(img, newDpiX);
dpiY?.SetValue(img, newDpiY);
}

public static Uri FilePathToFileUrl(string filePath)
{
var uri = new StringBuilder();
foreach (var v in filePath)
if (v >= 'a' && v <= 'z' || v >= 'A' && v <= 'Z' || v >= '0' && v <= '9' ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\x80')
uri.Append(v);
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
uri.Append('/');
else
uri.Append($"%{(int) v:X2}");
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
else
uri.Insert(0, "file:///");

try
{
return new Uri(uri.ToString());
}
catch
{
return null;
}
}
}
}
2 changes: 1 addition & 1 deletion QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void View(string path, ContextObject context)
? $"{Path.GetFileName(path)}"
: $"{size.Width}×{size.Height}: {Path.GetFileName(path)}";

_ip.ImageUriSource = new Uri(path);
_ip.ImageUriSource = Helper.FilePathToFileUrl(path);
}

public void Cleanup()
Expand Down

0 comments on commit a912614

Please sign in to comment.