Coder Social home page Coder Social logo

file-validator / file-validator Goto Github PK

View Code? Open in Web Editor NEW
63.0 2.0 2.0 11.17 MB

The first Python validation package uses type, mime, extension, magic numbers, and size to validate files. ✅

Home Page: https://file-validator.github.io/

License: MIT License

Makefile 2.29% Python 97.30% HTML 0.24% Dockerfile 0.16%
django file file-validation filetype python python-magic validation validator mime files

file-validator's Introduction

0000-0010:  89 50 4e 47-0d 0a 1a 0a-00 00 00 0d-49 48 44 52  .PNG.... ....IHDR
0000-0020:  00 00 04 7b-00 00 04 dc-08 06 00 00-00 14 48 89  ...{.... ......H.
0000-0030:  b8 00 00 20-00 49 44 41-54 78 9c ec-dd dd 6f 9b  .....IDA Tx....o.
0000-0040:  e7 79 c7 f1-4b 22 25 ca-2c d9 06 89-e2 84 69 d2  .y..K"%. ,.....i.
0000-0050:  ed a0 62 57-0c cb 10 ef-d8 f0 b1 e1-3f 70 c0 fe  ..bW.... ....?p..
0000-0060:  0f c3 c7 86-cf 5b ac c3-b0 56 ca 41-d3 bc 28 49  .....[.. .V.A..(I
0000-0070:  e5 34 0b 19-5a 6f b4 86-9b b3 8b b5-4b 03 c7 a6  .4..Zo.. ....K...
0000-0080:  24 ea a7 cf-07 20 22 39-00 f9 f0 b9-1f 09 d0 17  $....."9 ........
0000-0090:  d7 73 73 6d-32 99 9c 15-00 00 00 00-11 d6 2d 23  .ssm2... ......-#
0000-00a0:  00 00 00 40-0e b1 07 00-00 00 20 88-d8 03 00 00  ...@.... ........
0000-00b0:  00 10 44 ec-01 00 00 00-08 22 f6 00-00 00 00 04  ..D..... ."......
0000-00c0:  11 7b 00 00-00 00 82 88-3d 00 00 00-00 41 c4 1e  .{...... =....A..
0000-00d0:  00 00 00 80-20 62 0f 00-00 00 40 10-b1 07 00 00  .....b.. ..@.....
0000-00e0:  00 20 88 d8-03 00 00 00-10 44 ec 01-00 00 00 08  ........ .D......
0000-00f0:  22 f6 00 00-00 00 04 11-7b 00 00 00-00 82 88 3d  "....... {......=
0000-0100:  00 00 00 00-41 c4 1e 00-00 00 80 20-62 0f 00 00  ....A... ....b...
0000-0110:  00 40 10 b1-07 00 00 00-20 88 d8 03-00 00 00 10  .@...... ........

pypi made with python license Build status Coverage Status python-versions Downloads djangopackages pre-commit.ci status DeepSource codecov Codacy Badge CodeFactor Percentage of issues still open ci

What Is File Validator? ✅

It is a Python library for file validation based on MIME, size, type and magic numbers that supports Django.

Why should we use this library? 🧐

  • File validation based on MIME, type, extension, magic numbers and size

  • File validation using the filetype library

  • File validation using the python-magic library

  • File validation using the mimetypes library

  • File validation using the puremagic library

  • File validation simultaneously with all libraries

  • File Size validation

  • Provided ValidatedFilefield and FileValidator for file validation in Django

  • Provided ValidatedFilefield in forms with support accept, multiple and custom css attributes

  • Supporting for all mimes

Where to report if we found a bug? 🪲

Can report the problem through this link

Where should I ask if I had any questions❓

Can you ask your questions through this link

Where to read the documentation? 📄

Can you read the documentation through this link

Contributors

file-validator's People

Contributors

deepsource-io[bot] avatar deepsourcebot avatar dependabot[bot] avatar lgtm-migrator avatar pre-commit-ci[bot] avatar restyled-commits avatar rzashakeri avatar snyk-bot avatar sondrelg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

file-validator's Issues

Bug with python-magic-bin==0.4.14

Describe the bug
I am trying to install file_validator in my python-3.11 inside a docker container. I added file_validator==0.3 in my requirements.txt file. Ran pip install -r requirements.txt inside the container and ran into this error message:

ERROR: Could not find a version that satisfies the requirement python-magic-bin==0.4.14 (from file-validator) (from versions: none)
#0 79.16 ERROR: No matching distribution found for python-magic-bin==0.4.14

To Reproduce
Steps to reproduce the behavior:

  1. Create a dockerfile from python:3.11-slim as the base image
  2. Install libmagic1 inside the container via: RUN apt-get update -y && apt-get install libmagic1 libmagic-dev -y
  3. Add file_validator==0.3 to the requirements.txt
  4. Rebuild the container so that newly added requirement is installed.
  5. See error

Expected behavior
Requirements being installed without any issue.

Screenshots
Screenshot 2023-02-28 at 8 26 05 PM

Desktop (please complete the following information):

  • OS: Docker Desktop (Mac OS Monterey 12.6.3)
  • Base Image: python:3.11-slim

Additional context
So, I do not know if this is the real issue, but found a workaround.

  1. Fork this repo.

  2. Comment out these lines of code in setup.py:

    PYTHON_MAGIC_BIN = "python-magic-bin==0.4.14"
    OS_NAME = platform.system()
    
    if OS_NAME == 'Windows':
        requirements.append(PYTHON_MAGIC_BIN)
    else:
        requirements.append(PYTHON_MAGIC)```
    
    
  3. Append "python-magic==0.4.27" in the requirements list in setup.py

  4. Voila! it works. It is very strange. Somehow the if condition is not being respected. As soon as i comment it out, it starts working. I ssh'ed into the docker container and checked the result of platform.system(). It was 'Linux'. So i have no clue why is if condition being evaluated as true.

  5. This is my personal fork see the changes. These work for me.

Is this a Django-only application? If not, Django dependency should be moved to optional dependencies.

Is your feature request related to a problem? Please describe.
ATM, Django is a required dependency. It's OK if this app is a Django-only.

Describe the solution you'd like
Only if this is a non-Django app or app that is used broader than Django-only, it's best to move the Django from required dependencies to optional dependencies.

Additionally, it would make sense to make it really clear in the documentation.

Can't work with Django and DRF at the same time.

Describe the bug
So, this library is very good, but there is a small issue with it. It only works if files are uploaded via admin site. It doesn't work well with DRF via POST request even when the correct parsers (MultipartParser) are set.

To Reproduce
Steps to reproduce the behavior:

  1. Install Django=4.17 & DRF 3.14 to your project.
  2. Create a simple model with FileField and FileValidator(part of this package) as the validator with the Filefield.
  3. Make migrations, and migrate.
  4. Go to admin site and try to upload any file.
  5. Upload will work just fine.
  6. Now try to create a CreateAPIView from DRF generics.
  7. Add parser_classes = (MultiPartParser, ) to the view
  8. Try to hit the POST endpoint with formdata containing a file.
  9. AttributeError: '_io.BufferedRandom' object has no attribute 'size'

Expected behavior
File should be uploaded as it had been in case of the Admin site.

Screenshots
If applicable, add screenshots to help explain your problem.

Versions (please complete the following information):

  • OS: Docker Desktop v4.17.0
  • Django 4.17
  • DRF 3.14
  • file_validator 0.3.4

Narrowed Issue

  • Go to file_validator/models
  • Change line 178 to current_file = value
  • Now the DRF will start working and the Admin site upload will fail.
  • So either Django admin site file upload works or DRF file upload works at a time, but not both, together.

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.