Coder Social home page Coder Social logo

simple-flask-file-server's Introduction

Simple python flask file server

Task assignment

  • implement in python (use of any libraries and DB services is optional)
  • upload, download, delete file
  • list files available on file server (with filtering options)
  • statistics of files available on file server
  • uploaded file has following metadata:
    • name
    • extension type
    • size
    • upload datetime

Prerequisites

  • small files - max. 100 MiB, but most of the arround 1 MiB
  • a lot of files
  • used mainly via JS from web browser
  • authentication and authorization is not needed
  • more reading than writing

Dependencies

  • solution is dockerized, so you need docker installed on your system
  • curl for manual tests

How to run

Extract provided ZIP file.

cd PROJECT_FOLDER
docker build -t file-server .
docker run -p 5000:5000 file-server

Now you can play with file server in another terminal window, web browser, or elsewhere :).

Files are not persistent. Restarted docker service will start empty.

Endpoints

Upload file - /upload - POST

  • curl -X POST -F "file=@PATH_TO_YOUR_FILE" http://localhost:5000/file
  • returns:
{
    "file_info":
        {
            "datetime_uploaded": ISO DATETIME as STRING,
            "name": STRING,
            "size_bytes": INT,
            "type": STRING
        },
    "message": STRING
}

Upload of new file with same name will overwrite the old one. Max upload size of one file is 100 MiB.

Download file - /file - GET

  • curl -o OUTPUT_FILE_NAME http://localhost:5000/file/TARGET_FILE_NAME

Delete file - /file - DELETE

  • curl -X DELETE http://localhost:5000/file/TARGET_FILE_NAME
  • returns:
{
    "message": STRING
}

List files - /file - GET

  • curl http://localhost:5000/file
    • with type filtering curl http://localhost:5000/file?type=.md
    • with name ordering ascending curl http://localhost:5000/file?sort_by_size=asc
    • with name ordering descending curl http://localhost:5000/file?sort_by_size=desc
    • or you can combine it like this curl "http://localhost:5000/file?type=.md&sort_by_size=asc"
  • returns:
{
    "files":
        [
            {
                "datetime_uploaded": ISO DATETIME as STRING,
                "name": STRING,
                "size_bytes": INT,
                "type": STRING
            }
        ]
}

Statistics - /file/statistics - GET

  • curl http://localhost:5000/file/statistics
  • return:
{
    "avg_size_bytes": INT,
    "median_size_bytes": INT,
    "num_of_files": INT,
    "total_size_bytes": INT
}

Notes to discuss

  • how to handle file persistence
  • README documents only happy path responses
  • possible authorization / authentification
  • how to not overwrite files with same name
  • I deliberately avoided all comments - code is short and straightforward
    • when possible - put all info into code, not into comments
    • in production code I like to use google style docstrings - github example
  • I do not follow PEP 8 line length
    • usually I like to use limit around ~ 120 characters per line - but it is all about consensus in team
    • integration of static type checking, linters and code formatter, I like to use following:
    • mypy - github link
    • ruff - github link

simple-flask-file-server's People

Contributors

tokrathesecond avatar

Watchers

 avatar

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.