diff options
author | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-08-13 19:34:21 +0300 |
---|---|---|
committer | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-08-13 19:34:21 +0300 |
commit | d8b83d5de25bb7aa6ca34fa662caa1c376029fa3 (patch) | |
tree | 76af8e54132455f60bf6e12ab420ea98c00a9254 /UVtools.Core | |
parent | 323e64d5eec1ffde0a8992a6c07acc65120e7c07 (diff) |
Add CanProcess method to allow multiple formats with same extension
Based on: https://github.com/tslater2006/UVtools/commit/ec2727ddc13c65cde2092e45ac0ba7fed796cbaa
Diffstat (limited to 'UVtools.Core')
-rw-r--r-- | UVtools.Core/FileFormats/ChituboxFile.cs | 20 | ||||
-rw-r--r-- | UVtools.Core/FileFormats/FileExtension.cs | 2 | ||||
-rw-r--r-- | UVtools.Core/FileFormats/FileFormat.cs | 52 | ||||
-rw-r--r-- | UVtools.Core/Operations/OperationLayerImport.cs | 2 | ||||
-rw-r--r-- | UVtools.Core/Operations/OperationRedrawModel.cs | 2 |
5 files changed, 67 insertions, 11 deletions
diff --git a/UVtools.Core/FileFormats/ChituboxFile.cs b/UVtools.Core/FileFormats/ChituboxFile.cs index 0915d02..323490d 100644 --- a/UVtools.Core/FileFormats/ChituboxFile.cs +++ b/UVtools.Core/FileFormats/ChituboxFile.cs @@ -9,6 +9,7 @@ // https://github.com/cbiffle/catibo/blob/master/doc/cbddlp-ctb.adoc using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; @@ -1646,6 +1647,25 @@ namespace UVtools.Core.FileFormats LayerDefinitions = null; } + public override bool CanProcess(string fileFullPath) + { + if (!base.CanProcess(fileFullPath)) return false; + + try + { + using var fs = new BinaryReader(new FileStream(fileFullPath, FileMode.Open, FileAccess.Read)); + var magic = fs.ReadUInt32(); + + return magic is MAGIC_CBDDLP or MAGIC_CBT or MAGIC_CBTv4; + } + catch (Exception e) + { + Debug.WriteLine(e); + } + + return false; + } + public void SanitizeProperties() { if (IsCbtFile) diff --git a/UVtools.Core/FileFormats/FileExtension.cs b/UVtools.Core/FileFormats/FileExtension.cs index c27473c..693a450 100644 --- a/UVtools.Core/FileFormats/FileExtension.cs +++ b/UVtools.Core/FileFormats/FileExtension.cs @@ -116,7 +116,7 @@ namespace UVtools.Core.FileFormats public FileFormat GetFileFormat(bool createNewInstance = false) => FileFormatType is null - ? FileFormat.FindByExtension(Extension, false, createNewInstance) + ? FileFormat.FindByExtensionOrFilePath(Extension, createNewInstance) : FileFormat.FindByType(FileFormatType, createNewInstance); public static FileExtension Find(string extension) => diff --git a/UVtools.Core/FileFormats/FileFormat.cs b/UVtools.Core/FileFormats/FileFormat.cs index 0024761..40a73db 100644 --- a/UVtools.Core/FileFormats/FileFormat.cs +++ b/UVtools.Core/FileFormats/FileFormat.cs @@ -336,19 +336,46 @@ namespace UVtools.Core.FileFormats /// <summary> /// Find <see cref="FileFormat"/> by an extension /// </summary> - /// <param name="extension">Extension name to find</param> - /// <param name="isFilePath">True if <see cref="extension"/> is a file path rather than only a extension name</param> + /// <param name="extensionOrFilePath"> name to find</param> /// <param name="createNewInstance">True to create a new instance of found file format, otherwise will return a pre created one which should be used for read-only purpose</param> /// <returns><see cref="FileFormat"/> object or null if not found</returns> - public static FileFormat FindByExtension(string extension, bool isFilePath = false, bool createNewInstance = false) + public static FileFormat FindByExtensionOrFilePath(string extensionOrFilePath, bool createNewInstance = false) { - if (isFilePath) + if (string.IsNullOrWhiteSpace(extensionOrFilePath)) return null; + + bool isFilePath = false; + // Test for ext first + var fileFormats = AvailableFormats.Where(fileFormat => fileFormat.IsExtensionValid(extensionOrFilePath)).ToArray(); + if (fileFormats.Length == 0) // Extension not found, can be filepath, try to find it { - GetFileNameStripExtensions(extension, out extension); + GetFileNameStripExtensions(extensionOrFilePath, out var extension); + if (string.IsNullOrWhiteSpace(extension)) return null; + + fileFormats = AvailableFormats.Where(fileFormat => fileFormat.IsExtensionValid(extension)).ToArray(); + if (fileFormats.Length == 0) return null; + isFilePath = true; // Was a file path + } + + if (fileFormats.Length == 1 || !isFilePath) + return createNewInstance + ? (FileFormat)Activator.CreateInstance(fileFormats[0].GetType()) + : fileFormats[0]; + + // Multiple instances using Check for valid candidate + foreach (var fileFormat in fileFormats) + { + if (fileFormat.CanProcess(extensionOrFilePath)) + { + return createNewInstance + ? (FileFormat)Activator.CreateInstance(fileFormat.GetType()) + : fileFormat; + } } - if (string.IsNullOrWhiteSpace(extension)) return null; - return (from fileFormat in AvailableFormats where fileFormat.IsExtensionValid(extension) select createNewInstance ? (FileFormat) Activator.CreateInstance(fileFormat.GetType()) : fileFormat).FirstOrDefault(); + // Try this in a far and not probable attempt + return createNewInstance + ? (FileFormat)Activator.CreateInstance(fileFormats[0].GetType()) + : fileFormats[0]; } public static FileExtension FindExtension(string extension) @@ -1930,13 +1957,22 @@ namespace UVtools.Core.FileFormats } } + public virtual bool CanProcess(string fileFullPath) + { + if (fileFullPath is null) return false; + if (!File.Exists(fileFullPath)) return false; + + return true; + } + + /// <summary> /// Validate if a file is a valid <see cref="FileFormat"/> /// </summary> /// <param name="fileFullPath">Full file path</param> public void FileValidation(string fileFullPath) { - if (fileFullPath is null) throw new ArgumentNullException(nameof(FileFullPath), "fullFilePath can't be null."); + if (string.IsNullOrWhiteSpace(fileFullPath)) throw new ArgumentNullException(nameof(FileFullPath), "FileFullPath can't be null nor empty."); if (!File.Exists(fileFullPath)) throw new FileNotFoundException("The specified file does not exists.", fileFullPath); if (IsExtensionValid(fileFullPath, true)) diff --git a/UVtools.Core/Operations/OperationLayerImport.cs b/UVtools.Core/Operations/OperationLayerImport.cs index 6ecc629..c2997ee 100644 --- a/UVtools.Core/Operations/OperationLayerImport.cs +++ b/UVtools.Core/Operations/OperationLayerImport.cs @@ -261,7 +261,7 @@ namespace UVtools.Core.Operations _files[i].ValueAsString.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || _files[i].ValueAsString.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) continue; - var fileFormat = FileFormat.FindByExtension(_files[i].ValueAsString, true, true); + var fileFormat = FileFormat.FindByExtensionOrFilePath(_files[i].ValueAsString, true); if (fileFormat is null) continue; fileFormat.FileFullPath = _files[i].ValueAsString; fileFormats.Add(fileFormat); diff --git a/UVtools.Core/Operations/OperationRedrawModel.cs b/UVtools.Core/Operations/OperationRedrawModel.cs index 7017f64..abcc4be 100644 --- a/UVtools.Core/Operations/OperationRedrawModel.cs +++ b/UVtools.Core/Operations/OperationRedrawModel.cs @@ -156,7 +156,7 @@ namespace UVtools.Core.Operations #region Methods public FileFormat IsFileValid(bool returnNewInstance = false) => - FileFormat.FindByExtension(_filePath, true, returnNewInstance); + FileFormat.FindByExtensionOrFilePath(_filePath, returnNewInstance); protected override bool ExecuteInternally(OperationProgress progress) { |