All Projects → mbourqui → django-constrainedfilefield

mbourqui / django-constrainedfilefield

Licence: BSD-3-Clause license
This Django app adds a new field, ConstrainedFileField, which adds the capability of checking the document size and type

Projects that are alternatives of or similar to django-constrainedfilefield

odin
Open-source, cross-platform, hassle-free file sharing with AES-256 encryption made with Flutter & Dart.
Stars: ✭ 114 (+338.46%)
Mutual labels:  file-upload
jQuery-File-Upload-for-java
Used klaalo / jQuery-File-Upload-Java and modified (https://github.com/klaalo/jQuery-File-Upload-Java)
Stars: ✭ 29 (+11.54%)
Mutual labels:  file-upload
react-native-tus-client
React Native client for the tus resumable upload protocol.
Stars: ✭ 38 (+46.15%)
Mutual labels:  file-upload
cheryl
PHP web based file manager
Stars: ✭ 44 (+69.23%)
Mutual labels:  file-upload
s3upload
Upload multiple files to AWS S3, make them public, and get their URLs easily from the command line.
Stars: ✭ 24 (-7.69%)
Mutual labels:  file-upload
crocofile
A webbased file upload manager to share files by sharing an account
Stars: ✭ 40 (+53.85%)
Mutual labels:  file-upload
Yii2 File Upload Widget
BlueImp File Upload Widget for Yii2
Stars: ✭ 251 (+865.38%)
Mutual labels:  file-upload
Transfer.sh
Easy file sharing from your Android device!
Stars: ✭ 14 (-46.15%)
Mutual labels:  file-upload
Django-WebApp
This is a web-app created using Python, Django. By using this user can login, upload files and also can view and download files uploaded by other users.
Stars: ✭ 285 (+996.15%)
Mutual labels:  file-upload
simple-file-uploader
A file uploader written using HTML5 and Node.js. It can upload both to a local directory on the server or to an AWS S3 server.
Stars: ✭ 85 (+226.92%)
Mutual labels:  file-upload
react-file-upload
react file uploader
Stars: ✭ 14 (-46.15%)
Mutual labels:  file-upload
fastify-file-upload
Fastify plugin for uploading files
Stars: ✭ 68 (+161.54%)
Mutual labels:  file-upload
djfiles
DJFiles is a simple Django app for manage static files of your project via admin panel.
Stars: ✭ 17 (-34.62%)
Mutual labels:  file-upload
sgfs
🚀Simple http file server. A open source file server, implement by golang that can be used to upload and download files. Simple to deploy, simple to use. 中文介绍:
Stars: ✭ 51 (+96.15%)
Mutual labels:  file-upload
django-mapbox-location-field
Simple in use location model and form field with MapInput widget for picking some location. Uses mapbox gl js, flexible map provider API. Fully compatible with bootstrap framework. Can be used with spatial or plain databases.
Stars: ✭ 60 (+130.77%)
Mutual labels:  django-field
Sharex
ShareX is a free and open source program that lets you capture or record any area of your screen and share it with a single press of a key. It also allows uploading images, text or other types of files to many supported destinations you can choose from.
Stars: ✭ 18,143 (+69680.77%)
Mutual labels:  file-upload
local-cloud
Turn any computer at home into a cloud for easy sharing of files across your devices.
Stars: ✭ 70 (+169.23%)
Mutual labels:  file-upload
IphpFileStoreBundle
Symfony 2 file upload for doctrine entities
Stars: ✭ 51 (+96.15%)
Mutual labels:  file-upload
bin
highly opinionated, minimal pastebin
Stars: ✭ 97 (+273.08%)
Mutual labels:  file-upload
kodbox
kodbox is a file manager for web. It is a newly designed product based on kodexplorer. It is also a web code editor, which allows you to develop websites directly within the web browser.You can run kodbox either online or locally,on Linux, Windows or Mac based platforms
Stars: ✭ 1,188 (+4469.23%)
Mutual labels:  file-upload

Python Django License PyPIv PyPIs Build Status Coverage Status Downloads Code style: black

ConstrainedFileField for Django

This Django app adds a new field type, ConstrainedFileField, that has the capability of checking the file size and type. Also provides a javascript checker for the form field.

Features

  • File size limitation
  • File type limitation
  • Javascript file size checker

Requirements

  • Python >= 3.5
  • Django>= 2.2.28
  • python-magic >= 0.4.2 iff you want to check the file type

Installation

Using PyPI

  1. Run
    • pip install django-constrainedfilefield, or
    • pip install django-constrainedfilefield[filetype] to ensure python-magic is installed.
  2. For windows, you must download the dll files and .magic file at https://github.com/pidydx/libmagicwin64 (32-bit version: http://gnuwin32.sourceforge.net/packages/file.htm)), add them to C:\Windows\System32 (or to a folder in your PATH), and set MAGIC_FILE_PATH="..." to the path of your .magic file in your settings.py. For more information about the files to download, go to: https://github.com/ahupp/python-magic/blob/43df08c5ed63d7aad839695f311ca1be2eeb1ecb/README.md#dependencies

Using the source code

  1. Make sure Pandoc is installed
  2. Run ./pypi_packager.sh
  3. Run pip install dist/django_constrainedfilefield-x.y.z-[...].wheel, where x.y.z must be replaced by the actual version number and [...] depends on your packaging configuration
  4. For windows, you must download the dll files and .magic file at https://github.com/pidydx/libmagicwin64 (32-bit version: http://gnuwin32.sourceforge.net/packages/file.htm)), add them to C:\Windows\System32 (or to a folder in your PATH), and set MAGIC_FILE_PATH="..." to the path of your .magic file in your settings.py. For more information about the files to download, go to: https://github.com/ahupp/python-magic/blob/43df08c5ed63d7aad839695f311ca1be2eeb1ecb/README.md#dependencies

Usage

Validate single file

The field can be used in forms or model forms like a normal FileField. If a user tries to upload a file which is too large or without a valid type, a form validation error will occur.

Note that the validation does not occur on the field itself (on save()), but when validated through a form.

Creating form from model

Create a model and add a field of type ConstrainedFileField. You can add a maximum size in bytes and a list of valid mime types that will be allowed. The list of all mime types is available here: http://www.iana.org/assignments/media-types/index.html. Setting none of the above, it behaves like a regular FileField.

from django.db import models
from constrainedfilefield.fields import ConstrainedFileField

class TestModel(models.Model):
    the_file = ConstrainedFileField(
                            null=True,
                            blank=True,
                            upload_to='testfile',
                            content_types=['image/png'],
                            max_upload_size=10240
                                    )
from django import forms
from myproject.models import TestModel

class TestModelForm(forms.ModelForm):
    class Meta:
        model = TestModel
        fields = ['the_file']

Building a form

from django import forms
from constrainedfilefield.fields import ConstrainedFileField

class TestNoModelForm(forms.Form):
    the_file = ConstrainedFileField(
                            null=True,
                            blank=True,
                            upload_to='testfile',
                            content_types=['image/png'],
                            max_upload_size=10240
                                    ).formfield()

Javascript file size validation

Additionally, to prevent user uploading too large files, a javascript checker can be set to the form field. In order to achieve that, you need to

  1. Add constrainedfilefield to the INSTALLED_APPS. This will load the javascripts from the static files.

  2. Activate this feature by setting js_checker=True when instantiating the ConstrainedFileField.

  3. Include the javascript in the template where the form field is used

    {% load static %}
    <script src="{% static 'constrainedfilefield/js/file_checker.js' %}"></script>

Validate single image

Same as above, using ConstrainedImageFileField instead.

The ConstrainedImageField offers additional constraints:

  • [min|max]_upload_[width|height] to define min/max dimensions, respectively width and height.

Note on DOS attacks

Important note: the check of the file size is made by Django once the whole file has been uploaded to the server and stored in a temp directory (or in memory if the file is small). Thus, this is useful to guarantee the quota of the users, for example, but will not stop an attacking user that wants to block the server by sending huge files (e. g. of several Gb).

To avoid this, you need to configure your front end to limit the size of uploaded files. How to do it depends on the software you are using. For example, if you use apache, you should use LimitRequestBody directive.

This is a complementary measure, because you'll usually want normal users that exceed the size by a reasonable amount to get a friendly form validation message, while attacking users will see how their connection is abruptly cut before the file finishes uploading. So the recommended setting is to give max_upload_size a small value (e.g. 5Mb) and LimitRequestBody a higher one (e.g. 100Mb).

Credits

This is a fork of django-validated-file from Kaleidos.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].