Giter VIP home page Giter VIP logo

Comments (12)

RetainPhil avatar RetainPhil commented on July 19, 2024

Just for a bit more context, I end up writing something like this as a work-around:

    /// <summary>
    /// Just for exposing FileTypeValidator.FileTypes
    /// </summary>
    private abstract class TypeValidator : FileTypeValidator
    {
        public static new IReadOnlyCollection<IFileType> FileTypes
            => FileTypeValidator.FileTypes;
    }

    private bool IsSignatureBitsValid(IFormFile file, string extension)
    {
        if (!TypeValidator.FileTypes.Any(t => t.Extension == extension))
        {
            return true; //or false
        }

        using var stream = file.OpenReadStream();
        if (!FileTypeValidator.IsTypeRecognizable(stream))
        {
            return false;
        }

        var fileType = FileTypeValidator.GetFileType(stream);
        return fileType.Extension == extension;
    }

from filetypechecker.

AJMitev avatar AJMitev commented on July 19, 2024

@RetainPhil You are doing basically the same thing that the IsTypeRecognizable method does with the only difference that the IsTypeRecognizable method is comparing the bytes while your code compares two strings. This library is supporting a large variety of types. If the type you are looking for is not supported you can implement it easily.

The expression TypeValidator.FileTypes.Any(t => t.Extension == extension) will return the same result as FileTypeValidator.IsTypeRecognizable(stream) every time.

from filetypechecker.

RetainPhil avatar RetainPhil commented on July 19, 2024

EDIT: Yeh I see what you're saying. Well I guess I was thinking that maybe someone might want to handle the cases where a file claims to be of a type which has a validator, but the validation fails, and a file which doesn't even have a validator to begin with. (and might or might not be valid).

In the end I suppose you'd have to verify that the extension is something that the system supports (and has a validator) before checking the signature bits, either way there's a hard-coded list of 'supported types' you'd have to manage, whether that's the types supported out of the box by the library already or not.

from filetypechecker.

pylvr avatar pylvr commented on July 19, 2024

quick question guys , does filesteam.IsImage , incluse Webp images ?

from filetypechecker.

AJMitev avatar AJMitev commented on July 19, 2024

At the moment no, but I am planning to include it in the next release. If you want your code to recognize it you can implement a custom type

from filetypechecker.

pylvr avatar pylvr commented on July 19, 2024

@AJMitev thanks, and great work btw 😉

from filetypechecker.

pylvr avatar pylvr commented on July 19, 2024

@AJMitev sorry to bother you again , can you say if this code is safe enought to test for webp ?

 public class WebPFileType : FileType
    {
        private static readonly string name = "WebPacl Custom Type 1.0";
        private static readonly string extension = "webp";
        private static readonly byte[] magicBytes = new byte[] { 0x52, 0x49 };

        public WebPFileType() : base(name, extension, magicBytes) { }
    }

from filetypechecker.

AJMitev avatar AJMitev commented on July 19, 2024

It's looking good :)

from filetypechecker.

pylvr avatar pylvr commented on July 19, 2024

@AJMitev sorry to bother you again , can you say if this code is safe enought to test for webp ?

 public class WebPFileType : FileType
    {
        private static readonly string name = "WebPacl Custom Type 1.0";
        private static readonly string extension = "webp";
        private static readonly byte[] magicBytes = new byte[] { 0x52, 0x49 };

        public WebPFileType() : base(name, extension, magicBytes) { }
    }

@AJMitev can you help me out please ? i created my custom type , and when a test it with a wepb img ( i double checked the header for magic bytes ) but the code 'fileStream.Is<WebPFileType>()' always return false , any ideas why?

from filetypechecker.

AJMitev avatar AJMitev commented on July 19, 2024

Try this:

  using (var fileStream = File.OpenRead("myFileLocation"))
  {
      var isRecognizableType = FileTypeValidator.IsTypeRecognizable(fileStream);
  }

If isRecognizableType is false then you are not registered your type correctly. And you should read this.

from filetypechecker.

pylvr avatar pylvr commented on July 19, 2024

@AJMitev i did registered in the startup class
and isRecognizableType return true

from filetypechecker.

AJMitev avatar AJMitev commented on July 19, 2024

Well, this is easy to debug mate, just retrieve the type and see what you will receive.

 var fileType = FileTypeValidator.GetFileType(fileStream);

Now when I saw the bytes you are defined in your type, I bet it will return AVI instead of WebP, this is because the beginning of both types are the same and for now FindMaxScore returns the first type that scores the highest. In that case AVI.

For now I can recommend you to set more specific byte definition. In near future I will improve the behavior.

from filetypechecker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.