Metadata-Version: 2.1
Name: python-magic-file
Version: 0.0.4
Summary: Small Python module to aid developers in getting file extensions from files securely.
Home-page: https://github.com/vremes/python-magic-file
Author: Valtteri Remes
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: python-magic (==0.4.27) ; platform_system != "Windows"
Requires-Dist: python-magic-bin (==0.4.14) ; platform_system == "Windows"

# python-magic-file
Small Python module to aid developers in getting file extensions from files.

### Usage
```py
from python_magic_file import MagicFile

# HTML file.
with open('path/to/file.html', 'rb') as f:
    magic_file = MagicFile(f)
    print(magic_file.get_extension()) # .html

# File with unknown file extension.
with open('path/to/file.m4v', 'rb') as f:
    magic_file = MagicFile(f)

    # UserWarning: File extension for mimetype "video/x-m4v" is None, consider adding an extension for this mimetype using MagicFile.add_type_to_mimetypes_module or mimetypes.add_type call.
    print(magic_file.get_extension()) # None

# File with unknown file extension
# but the extension is registered manually.
MagicFile.add_type_to_mimetypes_module('video/x-m4v', 'm4v')

with open('path/to/file.m4v', 'rb') as f:
    magic_file = MagicFile(f)
    print(magic_file.get_extension()) # .m4v

```

### Usage with Flask
```py
from python_magic_file import MagicFile
from flask import Flask, request

app = Flask(__name__)

@app.post('/upload')
def upload_file():
    uploaded_file = request.files.get('file')

    if not uploaded_file:
        return 'Missing file from request body!'

    magic_file = MagicFile(uploaded_file.stream)

    file_extension = magic_file.get_extension()

    if not file_extension:
        return 'File extension not supported!'

if __name__ == '__main__':
    app.run()

```
