Giter VIP home page Giter VIP logo

vremes / shrpy Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 4.0 262 KB

ShareX custom uploader/destination server written in Python - includes optional use of password, URL shortener, whitelisted file extensions, deletion URLs and Discord webhooks.

License: MIT License

Python 100.00%
flask sharex python3 custom-uploader sharex-server sharex-upload sharex-uploader flask-sharex python selfhosted

shrpy's Introduction

License: MIT CodeQL

ShareX custom uploader/destination server written in Python (Flask).

I created this mostly for my personal use, but if you have any suggestions, ideas or improvements feel free to open a new issue (pull requests are also welcome).

Endpoints

Route HTTP method Description
/ GET Index page with some text just to make sure this application works.
/uploads/<filename> GET Route to serve a given file from uploads directory.
/url/<token> GET Redirects you to the URL for given short URL token.
/api/sharex/upload GET ShareX custom uploader configuration for files, you can import this to ShareX from Destinations -> Custom uploader settings -> Import -> From URL
/api/sharex/shorten GET ShareX custom uploader configuration for short URLs, you can import this to ShareX from Destinations -> Custom uploader settings -> Import -> From URL
/api/upload POST Route for file uploads.
/api/shorten POST Route for URL shortening.
/api/delete-short-url/<hmac_hash>/<token> GET ShareX deletion URL for short URLs.
/api/delete-file/<hmac_hash>/<filename> GET ShareX deletion URL for files.

Setup

Below you'll find two examples on how to setup this application.

Development

  1. Clone the repository
git clone https://github.com/vremes/shrpy.git
  1. Move to cloned repository directory and install requirements
cd shrpy
pip3 install -r requirements.txt
  1. Setup .env file, see Configuration for additional information
cp .env_template .env
nano .env
  • You must set FLASK_SECRET to something, good way to generate secrets is the following command
    python -c "from secrets import token_urlsafe; print(token_urlsafe(64))"
  1. Run Flask built-in development server
python3 wsgi.py

Production

  1. Install NGINX and Supervisor
apt install nginx supervisor
  1. Install Gunicorn and Gevent
pip3 install gunicorn gevent
  1. Clone the repository to /var/www/
git clone https://github.com/vremes/shrpy.git /var/www/shrpy
  1. Move to cloned repository directory and install requirements
cd /var/www/shrpy/
pip3 install -r requirements.txt
  1. Setup .env file, see Configuration for additional information
cp .env_template .env
nano .env
  • You must set FLASK_SECRET to something, good way to generate secrets is the following command
    python -c "from secrets import token_urlsafe; print(token_urlsafe(64))"
  1. Configure Supervisor to run Gunicorn, see Gunicorn Documentation for additional information
nano /etc/supervisor/conf.d/shrpy.conf
  • Example configuration:
    [program:shrpy]
    directory=/var/www/shrpy
    command=gunicorn --bind=127.0.0.1:8000 --worker-class=gevent wsgi:application
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/shrpy.err.log
    stdout_logfile=/var/log/shrpy.out.log
    
  1. Update Supervisor configuration and configure NGINX
supervisorctl update
nano /etc/nginx/sites-available/shrpy.conf
  • Example configuration:
    server {
        listen 80;
        server_name example.com; # <==== Change to your domain name
        client_max_body_size 16M;
    
        location / {
            include proxy_params;
            proxy_pass http://127.0.0.1:8000;
        }
    
        location /uploads {
            alias /var/www/shrpy/app/uploads/;
        }
    }
  1. Enable NGINX configuration and restart NGINX
ln -s /etc/nginx/sites-available/shrpy.conf /etc/nginx/sites-enabled/
service nginx restart
  1. Visit the root (/) path on your domain and it should be running:
{
    "message": "It works! Beep boop."
}

Configuration

shrpy looks for config values from OS environment variables.

You can set these environment variables in .env_template and then rename the .env_template to .env.

Key Type Default value Description
FLASK_SECRET str None Secret key for Flask application, see https://flask.palletsprojects.com/en/2.0.x/config/#SECRET_KEY
UPLOAD_DIR str /app/uploads/ Path for uploaded files.
ALLOWED_EXTENSIONS str png;jpg;jpeg;gif;webm;mp4;webp;txt;m4v Allowed file extensions separated by semicolon.
CUSTOM_EXTENSIONS str video/x-m4v=m4v,image/webp=webp Additional mimetype=extension pairs for Python mimetypes module
UPLOAD_PASSWORD str None The password to protect /api/upload and /api/shorten endpoints.
DISCORD_WEBHOOKS str None Discord webhook URLs separated by semicolon.
DISCORD_WEBHOOK_TIMEOUT int 5 Timeout for Discord webhook requests in seconds.
MAGIC_BUFFER_BYTES int 2048 The amount of bytes python-magic will read from uploaded file to determine its extension.
URL_TOKEN_BYTES int 6 The amount of bytes secrets.token_urlsafe will use to generate shortened URLs.
USE_ORIGINAL_FILENAME bool True If saved files should include original filename.

HTTP Headers

Name Example value Description
Authorization hunter2 The plaintext password for file uploads and URL shortening, simply ignore this header if you don't use a password.

shrpy's People

Contributors

dependabot[bot] avatar vinchethescript avatar vremes avatar

Stargazers

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

Watchers

 avatar  avatar

shrpy's Issues

Missing file shrpy.db

When following the instructions for setting up production in the README, gunicorn raises sqlite3.OperationalError: unable to open database file. This repo does not contain a shrpy.db, which I assume is required for the application. I do see a CREATE TABLE IF NOT EXISTS in utils.py though, maybe add an empty .db file to the repo or add a note in the README to create one?

AttributeError: 'NoneType' object has no attribute 'encode'

Hiya, fairly sure I've done all the steps correctly but when trying to post images I'm getting 500 errors and the following log

2022-01-28 11:10:29,797 | app.log_exception | ERROR | Exception on /api/upload [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/var/www/shrpy/app/core/utils.py", line 45, in decorated_function
    return f(*args, **kwargs)
  File "/var/www/shrpy/app/blueprints/api.py", line 18, in upload
    return FileService.create()
  File "/var/www/shrpy/app/core/services.py", line 37, in create
    hmac_hash = create_hmac_hash(f.filename, current_app.secret_key)
  File "/var/www/shrpy/app/core/utils.py", line 74, in create_hmac_hash
    hmac_secret_key.encode('utf-8'),
AttributeError: 'NoneType' object has no attribute 'encode'

Any help is appreciated

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.