2018-06-25 19:08:16 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:21 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2018-06-25 19:08:16 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Filters.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Filters.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Enumerates all filters and instantiates the correct one.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
2018-06-25 19:22:42 +01:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
// the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2018-06-25 19:08:16 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:28 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2018-06-25 19:08:16 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-02-27 00:33:15 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.Console;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
2020-02-27 00:33:15 +00:00
|
|
|
namespace Aaru.CommonTypes
|
2018-06-25 19:08:16 +01:00
|
|
|
{
|
|
|
|
|
public class FiltersList
|
|
|
|
|
{
|
|
|
|
|
public SortedDictionary<string, IFilter> Filters;
|
|
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
/// <summary>Fills the list of all known filters</summary>
|
2018-06-25 19:08:16 +01:00
|
|
|
public FiltersList()
|
|
|
|
|
{
|
2020-02-27 14:01:52 +00:00
|
|
|
Assembly assembly = Assembly.Load("Aaru.Filters");
|
2018-06-25 19:08:16 +01:00
|
|
|
Filters = new SortedDictionary<string, IFilter>();
|
|
|
|
|
|
|
|
|
|
foreach(Type type in assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IFilter))))
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-03 01:41:48 +00:00
|
|
|
var filter = (IFilter)type.GetConstructor(Type.EmptyTypes)?.Invoke(new object[]
|
|
|
|
|
{ });
|
|
|
|
|
|
|
|
|
|
if(filter != null &&
|
|
|
|
|
!Filters.ContainsKey(filter.Name.ToLower()))
|
2018-06-25 19:08:16 +01:00
|
|
|
Filters.Add(filter.Name.ToLower(), filter);
|
|
|
|
|
}
|
2019-11-03 01:41:48 +00:00
|
|
|
catch(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Exception {0}", exception);
|
|
|
|
|
}
|
2018-06-25 19:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
/// <summary>Gets the filter that allows to read the specified path</summary>
|
2018-06-25 19:08:16 +01:00
|
|
|
/// <param name="path">Path</param>
|
|
|
|
|
/// <returns>The filter that allows reading the specified path</returns>
|
|
|
|
|
public IFilter GetFilter(string path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IFilter noFilter = null;
|
2019-11-03 01:41:48 +00:00
|
|
|
|
2018-06-25 19:08:16 +01:00
|
|
|
foreach(IFilter filter in Filters.Values)
|
|
|
|
|
if(filter.Id != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
|
|
|
|
|
{
|
2019-11-03 01:41:48 +00:00
|
|
|
if(!filter.Identify(path))
|
|
|
|
|
continue;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
var foundFilter = (IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke(new object[]
|
|
|
|
|
{ });
|
2018-06-25 19:08:16 +01:00
|
|
|
|
|
|
|
|
foundFilter?.Open(path);
|
|
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
if(foundFilter?.IsOpened() == true)
|
|
|
|
|
return foundFilter;
|
2018-06-25 19:08:16 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
noFilter = filter;
|
|
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
if(!noFilter?.Identify(path) == true)
|
|
|
|
|
return noFilter;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
|
|
|
|
noFilter?.Open(path);
|
|
|
|
|
|
|
|
|
|
return noFilter;
|
|
|
|
|
}
|
2019-11-03 01:41:48 +00:00
|
|
|
catch(IOException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-06-25 19:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-03 01:41:48 +00:00
|
|
|
/// <summary>Gets all known filters</summary>
|
2018-06-25 19:08:16 +01:00
|
|
|
/// <returns>Known filters</returns>
|
2018-12-31 13:17:27 +00:00
|
|
|
public SortedDictionary<string, IFilter> GetFiltersList() => Filters;
|
2018-06-25 19:08:16 +01:00
|
|
|
}
|
|
|
|
|
}
|