mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-24 04:12:32 +08:00
Only open FilStream in read-only mode; Release propertyStore handle after getting the file properties
This commit is contained in:
parent
c6182a43bb
commit
3b1481da2c
@ -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;
|
||||
var propertyStore = 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;
|
||||
var propertyStore = item.FileType;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,9 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Windows.Storage;
|
||||
|
||||
@ -15,17 +17,28 @@ namespace Peek.Common.Models
|
||||
{
|
||||
private StorageFile? storageFile;
|
||||
|
||||
private Lazy<IPropertyStore> _propertyStore;
|
||||
|
||||
public FileItem(string path)
|
||||
{
|
||||
Path = path;
|
||||
_propertyStore = new(() => PropertyStoreHelper.GetPropertyStoreFromPath(Path));
|
||||
var propertyStore = PropertyStoreHelper.GetPropertyStoreFromPath(Path);
|
||||
Width = propertyStore.TryGetUInt(PropertyKey.ImageHorizontalSize) ?? 0;
|
||||
Height = propertyStore.TryGetUInt(PropertyKey.ImageVerticalSize) ?? 0;
|
||||
FileSizeBytes = propertyStore.TryGetULong(PropertyKey.FileSizeBytes) ?? 0;
|
||||
FileType = propertyStore.TryGetString(PropertyKey.FileType) ?? string.Empty;
|
||||
|
||||
// Release property store so it no longer holds the file open
|
||||
Marshal.FinalReleaseComObject(propertyStore);
|
||||
}
|
||||
|
||||
public string Path { get; init; }
|
||||
|
||||
public IPropertyStore PropertyStore => _propertyStore.Value;
|
||||
public uint? Width { get; init; }
|
||||
|
||||
public uint? Height { get; init; }
|
||||
|
||||
public ulong FileSizeBytes { get; init; }
|
||||
|
||||
public string FileType { get; init; }
|
||||
|
||||
public async Task<IStorageItem?> GetStorageItemAsync()
|
||||
{
|
||||
|
@ -3,7 +3,9 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Windows.Storage;
|
||||
|
||||
@ -15,17 +17,26 @@ namespace Peek.Common.Models
|
||||
{
|
||||
private StorageFolder? storageFolder;
|
||||
|
||||
private Lazy<IPropertyStore> _propertyStore;
|
||||
|
||||
public FolderItem(string path)
|
||||
{
|
||||
Path = path;
|
||||
_propertyStore = new(() => PropertyStoreHelper.GetPropertyStoreFromPath(Path));
|
||||
var propertyStore = PropertyStoreHelper.GetPropertyStoreFromPath(Path);
|
||||
FileSizeBytes = propertyStore.TryGetULong(PropertyKey.FileSizeBytes) ?? 0;
|
||||
FileType = propertyStore.TryGetString(PropertyKey.FileType) ?? string.Empty;
|
||||
|
||||
// Release property store so it no longer holds the file open
|
||||
Marshal.FinalReleaseComObject(propertyStore);
|
||||
}
|
||||
|
||||
public string Path { get; init; }
|
||||
|
||||
public IPropertyStore PropertyStore => _propertyStore.Value;
|
||||
public uint? Width { get; init; }
|
||||
|
||||
public uint? Height { get; init; }
|
||||
|
||||
public ulong FileSizeBytes { get; init; }
|
||||
|
||||
public string FileType { get; init; }
|
||||
|
||||
public async Task<IStorageItem?> GetStorageItemAsync()
|
||||
{
|
||||
|
@ -22,7 +22,13 @@ namespace Peek.Common.Models
|
||||
|
||||
public string Path { get; init; }
|
||||
|
||||
public IPropertyStore PropertyStore { get; }
|
||||
public uint? Width { get; init; }
|
||||
|
||||
public uint? Height { get; init; }
|
||||
|
||||
public ulong FileSizeBytes { get; init; }
|
||||
|
||||
public string FileType { get; init; }
|
||||
|
||||
public Task<IStorageItem?> GetStorageItemAsync();
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using FileStream stream = File.OpenRead(Item.Path);
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
|
@ -12,11 +12,16 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
public static async Task<string> Read(string path)
|
||||
{
|
||||
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||
using var fs = OpenReadOnly(path);
|
||||
using var sr = new StreamReader(fs, Encoding.UTF8);
|
||||
|
||||
string content = await sr.ReadToEndAsync();
|
||||
return content;
|
||||
}
|
||||
|
||||
public static FileStream OpenReadOnly(string path)
|
||||
{
|
||||
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user