mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
[Peek] Open file in Read only (#25945)
* Only open FilStream in read-only mode; Release propertyStore handle after getting the file properties
(cherry picked from commit 3b1481da2c)
* Update calls to PropertyStoreHelper
* Add disposable property store
* Make GetPropertyStoreFromPath return Disposable property store
* correct typo
* correct typo
* Remove nullable in DisposablePropertyStore
* Add property getters
* Remove usued method
* Correct typo
* Correct typo again...
* Update description
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using Peek.Common.Models;
|
||||
|
||||
namespace Peek.Common.Extensions
|
||||
{
|
||||
public sealed class DisposablePropertyStore : IDisposable
|
||||
{
|
||||
private readonly IPropertyStore _propertyStore;
|
||||
|
||||
public DisposablePropertyStore(IPropertyStore propertyStore)
|
||||
{
|
||||
_propertyStore = propertyStore;
|
||||
}
|
||||
|
||||
public void GetValue(ref PropertyKey key, out PropVariant pv)
|
||||
{
|
||||
_propertyStore!.GetValue(ref key, out pv);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.ReleaseComObject(_propertyStore);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,8 @@ namespace Peek.Common.Extensions
|
||||
{
|
||||
Size? size = null;
|
||||
|
||||
var propertyStore = item.PropertyStore;
|
||||
var width = propertyStore.TryGetUInt(PropertyKey.ImageHorizontalSize);
|
||||
var height = propertyStore.TryGetUInt(PropertyKey.ImageVerticalSize);
|
||||
var width = item.Width;
|
||||
var height = item.Height;
|
||||
|
||||
if (width != null && height != null)
|
||||
{
|
||||
@@ -36,8 +35,7 @@ namespace Peek.Common.Extensions
|
||||
public static Size? GetSvgSize(this IFileSystemItem item)
|
||||
{
|
||||
Size? size = null;
|
||||
|
||||
using (FileStream stream = System.IO.File.OpenRead(item.Path))
|
||||
using (FileStream stream = new FileStream(item.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
|
||||
{
|
||||
XmlReaderSettings settings = new XmlReaderSettings();
|
||||
settings.Async = true;
|
||||
@@ -95,8 +93,7 @@ namespace Peek.Common.Extensions
|
||||
sizeInBytes = (ulong)folder.Size;
|
||||
break;
|
||||
case FileItem _:
|
||||
var propertyStore = item.PropertyStore;
|
||||
sizeInBytes = propertyStore.TryGetULong(PropertyKey.FileSizeBytes) ?? 0;
|
||||
sizeInBytes = item.FileSizeBytes;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -117,8 +114,7 @@ namespace Peek.Common.Extensions
|
||||
contentType = storageFolder.DisplayType;
|
||||
break;
|
||||
default:
|
||||
var propertyStore = item.PropertyStore;
|
||||
contentType = propertyStore.TryGetString(PropertyKey.FileType) ?? string.Empty;
|
||||
contentType = item.FileType;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Peek.Common.Extensions
|
||||
/// <param name="propertyStore">The property store</param>
|
||||
/// <param name="key">The pkey</param>
|
||||
/// <returns>The uint value</returns>
|
||||
public static uint? TryGetUInt(this IPropertyStore propertyStore, PropertyKey key)
|
||||
public static uint? TryGetUInt(this DisposablePropertyStore propertyStore, PropertyKey key)
|
||||
{
|
||||
if (propertyStore == null)
|
||||
{
|
||||
@@ -54,7 +54,7 @@ namespace Peek.Common.Extensions
|
||||
/// <param name="propertyStore">The property store</param>
|
||||
/// <param name="key">the pkey</param>
|
||||
/// <returns>the ulong value</returns>
|
||||
public static ulong? TryGetULong(this IPropertyStore propertyStore, PropertyKey key)
|
||||
public static ulong? TryGetULong(this DisposablePropertyStore propertyStore, PropertyKey key)
|
||||
{
|
||||
if (propertyStore == null)
|
||||
{
|
||||
@@ -89,7 +89,7 @@ namespace Peek.Common.Extensions
|
||||
/// <param name="propertyStore">The property store</param>
|
||||
/// <param name="key">The pkey</param>
|
||||
/// <returns>The string value</returns>
|
||||
public static string? TryGetString(this IPropertyStore propertyStore, PropertyKey key)
|
||||
public static string? TryGetString(this DisposablePropertyStore propertyStore, PropertyKey key)
|
||||
{
|
||||
if (propertyStore == null)
|
||||
{
|
||||
@@ -116,46 +116,5 @@ namespace Peek.Common.Extensions
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper method that retrieves an array of string values from the given property store.
|
||||
/// </summary>
|
||||
/// <param name="propertyStore">The property store</param>
|
||||
/// <param name="key">The pkey</param>
|
||||
/// <returns>The array of string values</returns>
|
||||
public static string[]? TryGetStringArray(this IPropertyStore propertyStore, PropertyKey key)
|
||||
{
|
||||
if (propertyStore == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
PropVariant propVar;
|
||||
propertyStore.GetValue(ref key, out propVar);
|
||||
|
||||
List<string>? values = null;
|
||||
|
||||
if ((VarEnum)propVar.Vt == (VarEnum.VT_LPWSTR | VarEnum.VT_VECTOR))
|
||||
{
|
||||
values = new List<string>();
|
||||
for (int elementIndex = 0; elementIndex < propVar.Calpwstr.CElems; elementIndex++)
|
||||
{
|
||||
var stringVal = Marshal.PtrToStringUni(Marshal.ReadIntPtr(propVar.Calpwstr.PElems, elementIndex));
|
||||
if (stringVal != null)
|
||||
{
|
||||
values.Add(stringVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return values?.ToArray();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user