Move BackingStorageType to StorageManager

This commit is contained in:
Ben Olden-Cooligan 2019-03-16 18:48:26 -04:00
parent 4ad5d5c1ed
commit a2f0933a97
5 changed files with 19 additions and 14 deletions

View File

@ -45,7 +45,7 @@ namespace NAPS2.DI
var recoveryFolderPath = Path.Combine(Paths.Recovery, Path.GetRandomFileName()); var recoveryFolderPath = Path.Combine(Paths.Recovery, Path.GetRandomFileName());
var rsm = new RecoveryStorageManager(recoveryFolderPath); var rsm = new RecoveryStorageManager(recoveryFolderPath);
FileStorageManager.Current = rsm; FileStorageManager.Current = rsm;
ScannedImage.ConfigureBackingStorage<FileStorage>(); StorageManager.ConfigureBackingStorage<FileStorage>();
StorageManager.ImageMetadataFactory = rsm; StorageManager.ImageMetadataFactory = rsm;
} }
} }

View File

@ -20,7 +20,7 @@ namespace NAPS2.Sdk.Samples
// This will put files in the system temp folder by default, which can be // This will put files in the system temp folder by default, which can be
// overriden by changing FileStorageManager.Current. // overriden by changing FileStorageManager.Current.
// TODO: Probably this should go on StorageManager? It's weird to have to remember what's where // TODO: Probably this should go on StorageManager? It's weird to have to remember what's where
ScannedImage.ConfigureBackingStorage<FileStorage>(); StorageManager.ConfigureBackingStorage<FileStorage>();
IScanDriver driver = new WiaScanDriver(); IScanDriver driver = new WiaScanDriver();
ScanDevice device = driver.GetDeviceList().First(); ScanDevice device = driver.GetDeviceList().First();

View File

@ -85,7 +85,7 @@ namespace NAPS2.Sdk.Tests.Worker
{ {
// TODO: More tests, verifying correct serialization and using RecoveryStorageManager // TODO: More tests, verifying correct serialization and using RecoveryStorageManager
// TODO: Also check correct error handling // TODO: Also check correct error handling
ScannedImage.ConfigureBackingStorage<FileStorage>(); StorageManager.ConfigureBackingStorage<FileStorage>();
try try
{ {
var twainWrapper = new TwainWrapperMockScanner var twainWrapper = new TwainWrapperMockScanner
@ -109,7 +109,7 @@ namespace NAPS2.Sdk.Tests.Worker
} }
finally finally
{ {
ScannedImage.ConfigureBackingStorage<GdiImage>(); StorageManager.ConfigureBackingStorage<GdiImage>();
} }
} }

View File

@ -12,13 +12,6 @@ namespace NAPS2.Images
{ {
public class ScannedImage : IDisposable public class ScannedImage : IDisposable
{ {
public static void ConfigureBackingStorage<TStorage>() where TStorage : IStorage
{
BackingStorageType = typeof(TStorage);
}
public static Type BackingStorageType { get; private set; } = typeof(IStorage);
private IImage thumbnail; private IImage thumbnail;
private int thumbnailState; private int thumbnailState;
private int transformState; private int transformState;
@ -32,21 +25,21 @@ namespace NAPS2.Images
public ScannedImage(IStorage storage, StorageConvertParams convertParams) public ScannedImage(IStorage storage, StorageConvertParams convertParams)
{ {
BackingStorage = StorageManager.Convert(storage, BackingStorageType, convertParams); BackingStorage = StorageManager.ConvertToBacking(storage, convertParams);
Metadata = StorageManager.ImageMetadataFactory.CreateMetadata(BackingStorage); Metadata = StorageManager.ImageMetadataFactory.CreateMetadata(BackingStorage);
Metadata.Commit(); Metadata.Commit();
} }
public ScannedImage(IStorage storage, IImageMetadata metadata, StorageConvertParams convertParams) public ScannedImage(IStorage storage, IImageMetadata metadata, StorageConvertParams convertParams)
{ {
BackingStorage = StorageManager.Convert(storage, BackingStorageType, convertParams); BackingStorage = StorageManager.ConvertToBacking(storage, convertParams);
Metadata = metadata; Metadata = metadata;
} }
public ScannedImage(IStorage storage, ScanBitDepth bitDepth, bool highQuality, int quality) public ScannedImage(IStorage storage, ScanBitDepth bitDepth, bool highQuality, int quality)
{ {
var convertParams = new StorageConvertParams { Lossless = highQuality, LossyQuality = quality }; var convertParams = new StorageConvertParams { Lossless = highQuality, LossyQuality = quality };
BackingStorage = StorageManager.Convert(storage, BackingStorageType, convertParams); BackingStorage = StorageManager.ConvertToBacking(storage, convertParams);
Metadata = StorageManager.ImageMetadataFactory.CreateMetadata(BackingStorage); Metadata = StorageManager.ImageMetadataFactory.CreateMetadata(BackingStorage);
// TODO: Is this stuff really needed in metadata? // TODO: Is this stuff really needed in metadata?
Metadata.BitDepth = bitDepth; Metadata.BitDepth = bitDepth;

View File

@ -28,6 +28,13 @@ namespace NAPS2.Images.Storage
public static Type ImageType { get; private set; } public static Type ImageType { get; private set; }
public static void ConfigureBackingStorage<TStorage>() where TStorage : IStorage
{
BackingStorageType = typeof(TStorage);
}
public static Type BackingStorageType { get; private set; } = typeof(IStorage);
public static IImageFactory ImageFactory => ImageFactories.Get(ImageType) ?? throw new InvalidOperationException($"No factory has been registered for the image type {ImageType.FullName}."); public static IImageFactory ImageFactory => ImageFactories.Get(ImageType) ?? throw new InvalidOperationException($"No factory has been registered for the image type {ImageType.FullName}.");
private static readonly Dictionary<Type, IImageFactory> ImageFactories = new Dictionary<Type, IImageFactory>(); private static readonly Dictionary<Type, IImageFactory> ImageFactories = new Dictionary<Type, IImageFactory>();
@ -63,6 +70,11 @@ namespace NAPS2.Images.Storage
return (IImage)Convert(storage, ImageType, convertParams); return (IImage)Convert(storage, ImageType, convertParams);
} }
public static IStorage ConvertToBacking(IStorage storage, StorageConvertParams convertParams)
{
return Convert(storage, BackingStorageType, convertParams);
}
public static TStorage Convert<TStorage>(IStorage storage) public static TStorage Convert<TStorage>(IStorage storage)
{ {
return (TStorage)Convert(storage, typeof(TStorage), new StorageConvertParams()); return (TStorage)Convert(storage, typeof(TStorage), new StorageConvertParams());