mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
@@ -6,6 +6,7 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ImageResizer.Properties;
|
||||
@@ -14,6 +15,8 @@ namespace ImageResizer.Models
|
||||
{
|
||||
public class ResizeBatch
|
||||
{
|
||||
private readonly IFileSystem _fileSystem = new FileSystem();
|
||||
|
||||
public string DestinationDirectory { get; set; }
|
||||
|
||||
public ICollection<string> Files { get; } = new List<string>();
|
||||
@@ -71,7 +74,7 @@ namespace ImageResizer.Models
|
||||
catch (Exception ex)
|
||||
#pragma warning restore CA1031 // Do not catch general exception types
|
||||
{
|
||||
errors.Add(new ResizeError { File = Path.GetFileName(file), Error = ex.Message });
|
||||
errors.Add(new ResizeError { File = _fileSystem.Path.GetFileName(file), Error = ex.Message });
|
||||
}
|
||||
|
||||
Interlocked.Increment(ref completed);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
@@ -12,11 +13,14 @@ using System.Windows.Media.Imaging;
|
||||
using ImageResizer.Properties;
|
||||
using ImageResizer.Utilities;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem;
|
||||
|
||||
namespace ImageResizer.Models
|
||||
{
|
||||
internal class ResizeOperation
|
||||
{
|
||||
private readonly IFileSystem _fileSystem = new System.IO.Abstractions.FileSystem();
|
||||
|
||||
private readonly string _file;
|
||||
private readonly string _destinationDirectory;
|
||||
private readonly Settings _settings;
|
||||
@@ -31,7 +35,7 @@ namespace ImageResizer.Models
|
||||
public void Execute()
|
||||
{
|
||||
string path;
|
||||
using (var inputStream = File.OpenRead(_file))
|
||||
using (var inputStream = _fileSystem.File.OpenRead(_file))
|
||||
{
|
||||
var decoder = BitmapDecoder.Create(
|
||||
inputStream,
|
||||
@@ -87,8 +91,8 @@ namespace ImageResizer.Models
|
||||
}
|
||||
|
||||
path = GetDestinationPath(encoder);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
using (var outputStream = File.Open(path, FileMode.CreateNew, FileAccess.Write))
|
||||
_fileSystem.Directory.CreateDirectory(_fileSystem.Path.GetDirectoryName(path));
|
||||
using (var outputStream = _fileSystem.File.Open(path, FileMode.CreateNew, FileAccess.Write))
|
||||
{
|
||||
encoder.Save(outputStream);
|
||||
}
|
||||
@@ -96,13 +100,13 @@ namespace ImageResizer.Models
|
||||
|
||||
if (_settings.KeepDateModified)
|
||||
{
|
||||
File.SetLastWriteTimeUtc(path, File.GetLastWriteTimeUtc(_file));
|
||||
_fileSystem.File.SetLastWriteTimeUtc(path, _fileSystem.File.GetLastWriteTimeUtc(_file));
|
||||
}
|
||||
|
||||
if (_settings.Replace)
|
||||
{
|
||||
var backup = GetBackupPath();
|
||||
File.Replace(path, _file, backup, ignoreMetadataErrors: true);
|
||||
_fileSystem.File.Replace(path, _file, backup, ignoreMetadataErrors: true);
|
||||
FileSystem.DeleteFile(backup, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
|
||||
}
|
||||
}
|
||||
@@ -179,11 +183,11 @@ namespace ImageResizer.Models
|
||||
|
||||
private string GetDestinationPath(BitmapEncoder encoder)
|
||||
{
|
||||
var directory = _destinationDirectory ?? Path.GetDirectoryName(_file);
|
||||
var originalFileName = Path.GetFileNameWithoutExtension(_file);
|
||||
var directory = _destinationDirectory ?? _fileSystem.Path.GetDirectoryName(_file);
|
||||
var originalFileName = _fileSystem.Path.GetFileNameWithoutExtension(_file);
|
||||
|
||||
var supportedExtensions = encoder.CodecInfo.FileExtensions.Split(',');
|
||||
var extension = Path.GetExtension(_file);
|
||||
var extension = _fileSystem.Path.GetExtension(_file);
|
||||
if (!supportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
extension = supportedExtensions.FirstOrDefault();
|
||||
@@ -199,11 +203,11 @@ namespace ImageResizer.Models
|
||||
_settings.SelectedSize.Height,
|
||||
encoder.Frames[0].PixelWidth,
|
||||
encoder.Frames[0].PixelHeight);
|
||||
var path = Path.Combine(directory, fileName + extension);
|
||||
var path = _fileSystem.Path.Combine(directory, fileName + extension);
|
||||
var uniquifier = 1;
|
||||
while (File.Exists(path))
|
||||
while (_fileSystem.File.Exists(path))
|
||||
{
|
||||
path = Path.Combine(directory, fileName + " (" + uniquifier++ + ")" + extension);
|
||||
path = _fileSystem.Path.Combine(directory, fileName + " (" + uniquifier++ + ")" + extension);
|
||||
}
|
||||
|
||||
return path;
|
||||
@@ -211,15 +215,15 @@ namespace ImageResizer.Models
|
||||
|
||||
private string GetBackupPath()
|
||||
{
|
||||
var directory = Path.GetDirectoryName(_file);
|
||||
var fileName = Path.GetFileNameWithoutExtension(_file);
|
||||
var extension = Path.GetExtension(_file);
|
||||
var directory = _fileSystem.Path.GetDirectoryName(_file);
|
||||
var fileName = _fileSystem.Path.GetFileNameWithoutExtension(_file);
|
||||
var extension = _fileSystem.Path.GetExtension(_file);
|
||||
|
||||
var path = Path.Combine(directory, fileName + ".bak" + extension);
|
||||
var path = _fileSystem.Path.Combine(directory, fileName + ".bak" + extension);
|
||||
var uniquifier = 1;
|
||||
while (File.Exists(path))
|
||||
while (_fileSystem.File.Exists(path))
|
||||
{
|
||||
path = Path.Combine(directory, fileName + " (" + uniquifier++ + ")" + ".bak" + extension);
|
||||
path = _fileSystem.Path.Combine(directory, fileName + " (" + uniquifier++ + ")" + ".bak" + extension);
|
||||
}
|
||||
|
||||
return path;
|
||||
|
||||
Reference in New Issue
Block a user