Best Flask open-source libraries and packages

Flfm

Flask File Manager. View files & upload files with style.
Updated 1 year ago

flfm

Flask File Manager. View files & upload files with style.

Flask File Manager (flfm) is a WSGI application written in Python.

Language grade: Python Language grade: JavaScript

flfm_new_gif

Features:

  • Private sharing between Accounts Registered users can share the contents of their folders with other users & vice versa!
  • Account Management. Do you want to serve things publicly or do you want users (or you) to be able to upload & save files privately? The choice is yours!
  • Built-in viewer for files. Currently Supported: Text Files, Image Files, Video Files
  • Configurable. Define where users can snoop around and upload (or not) files.
  • SLIDESHOWS! For image files, you can watch a slideshow of all images in a folder or manually sift through them.
  • Uploading of files.
  • STREAM VIDEO CONTENT The built-in viewer can play videos with a properly configured nginx.

Table of Contents

Configuring flfm

flfm is configured via a couple of files. The .env file and the rules file. We'll go over both of these here.

dotenv file

First, refer to the .env_example file. You'll see the following:

# Example .env file
# Refer to config.py

# Allow Account Registration
ACCOUNT_REGISTRATION_ENABLED=True

# Where to create users' home folders at
USERS_HOME_FOLDERS=/var/flfm/homes

# Rules file location
RULES_FILE=/etc/flfm.d/rules

# Where to store session files
SESSION_FILE_DIR=/var/cache/flfm/

# Where to sideload videos for streaming thru the viewer
VIEWER_VIDEO_DIRECTORY=/var/cache/videos

# Uncomment to change Application Root
##APPLICATION_ROOT=/flfm

# Customize the banner
##BANNER="My Cool Custom Banner"
# Or use one from a file (limit one line)
##BANNER_TYPE="file"
##BANNER=/etc/flfm.d/banner

# DATABASE
DB_SCHEMA="mysql"
DB_USERNAME="flfm_user"
DB_PASSWORD="PASSWORD"
DB_HOST="localhost"
DB_DATABASE="flfm"

Explanation:
RULES_FILE - the location of the rules file It should be in a secure location such as /etc and also be read-only. SESSION_FILE_DIR - storage for flask-session files See here for more information

rules file

The rules file is plain text and does not support comments. Here is an example:

Allowed=/var/www/public
Disallowed=/var/www/public/private
AllowUploads=/var/www/public/incoming

By default, all paths are non-traversable. If you want a path on your server to be accessible, you'll need to add an Allowed rule.

You can explicitly restrict subdirectories within an allowed directory by specifying a Disallowed rule with the fully-qualified path to that subdirectory.

Nesting:

When it comes to the nesting of rules, do not nest an Allowed rule in a Disallowed rule's directory tree. There's no reason to do this, for Flask File Manager will not allow access to files or directories by default in the absence of rules.

Please note that all of these rules apply only to directories & files that are accessible by the WSGI server's uid/gid.

Explanation:
Allowed=/path/to/foo - give flfm access to this dir
Disallowed,DisAllowed=/path/to/foo - restrict flfm's access to this dir
AllowUpload,AllowUploads=/path/to/foo - allow a user to upload files in the given directory

Before Using flfm

Configuring the rules and creating a dot-env file is not quite enough to get started with flfm. Before using flfm, you'll need to generate the secret key. This is easily accomplished with running make in the project root directory (requires bash & openssl).

You'll also want to setup the database, create initial accounts (optional), and deploy.

MySQL Configuration

MySQL is used to store user credentials. Before you can run migrations, perform the initial setup on your MySQL server. To illustrate:

CREATE DATABASE flfm;
CREATE USER 'flfm_user'@'localhost' IDENTIFIED WITH mysql_native_password BY '<PASSWORD_HERE>';
GRANT ALL ON flfm.* TO 'flfm_user'@'localhost';

If you're going to run tests, then also:

CREATE DATABASE flfm_tests;
GRANT ALL ON flfm_tests.* TO 'flfm_user'@'localhost';

Once this is done, you can run migrations to create the table schemas and what-all for flfm.

Other SQL Backends:

Hate MySQL? Think it's overkill?? While there's no guarantee of compatibility, you can easily change the SQL backend. Look at the DATABASE section of the .env file.

Deploying flfm

systemd Service

Refer to the example systemd service file in systemd/flfm.service.

You'll be able to start flfm as a service with systemctl.

For a tutorial concerning this, check out this guide.

nginx Configuration

Checkout the example nginx config file in nginx/example_nginx.conf.

There are two ways to configure nginx as a reverse-proxy for flfm.

  • From the root, or /
  • From a location, such as /flfm

Let's look at how our location directives would look for the first method.

        location / {
                proxy_pass http://127.0.0.1:<port>/;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }

Now, let's look at how our location directives would look for the second method. Whatever location you pick must be both in the proxy_pass directive and also set as the APPLICATION_ROOT variable in the .env file.

         location ^~ /flfm {
                proxy_pass http://127.0.0.1:<port>/flfm;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }

REMEMBER: APPLICATION_ROOT == Location == End of proxy_pass.

Playing videos

Make an alias to the VIEWER_VIDEO_DIRECTORY variable.

        location /videos/ {
            alias <VIEWER_VIDEO_DIRECTORY>;
            mp4;
            mp4_buffer_size 2M;
            mp4_max_buffer_size 10M;
        }

Then, set up socket.io in nginx, Click Here To See How.

Running Tests

If you wish to run the tests for yourself, ensure that you have created a .env file pointing to a sample rules file.

You'll also want to ensure that you have the appropriate directory tree structure to run the tests against as well.

Then, from the project root directory, simply execute:

python tests.py

Creating Initial Accounts

FLFM supports account sessions. By default, account registration is disabled, but you can modify that in your .env file.

To create accounts, use the management script like so:

python manage.py createuser

Follow the interactive prompts to create the needed account(s) for use with FLFM.

Special Thanks

Special thanks go out to the following javascript libraries not listed in the project's dependencies: