repl.it
@badvillain01/

webp_flask

Python

No description

fork
loading
Files
  • main.py
  • templates
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, render_template, request, Response, redirect
import pymongo, datetime, secrets, random, os, string

# get_year gets current year
def get_year():
  y = datetime.datetime.today().year
  return y

# random_id generate random id
def random_id(n=8):
  randomid = "".join([random.choice(string.ascii_letters + string.digits) for x in range(n)])
  return randomid

def allowed_file(filename):
  return '.' in filename and filename.rsplit('.', 1)[1].lower() == "webp"


client = pymongo.MongoClient(os.getenv("DB_URI")).get_database("my_db")
db = client.get_collection("Data")

app = Flask(__name__)
app.config["SECRET_KEY"] = secrets.token_hex(random.randint(10, 50))

@app.route("/", methods=["GET"])
def home():
  return render_template("index.html", year=get_year())


@app.route("/upload", methods=["POST"])
def upload():
  webp_file = request.files["webp_thevillainer"]

  if webp_file.filename == '':
    return "Invalid file"

  if webp_file and allowed_file(webp_file.filename):
    file_id = random_id()

    print(file_id)

    data = {
      "_id": file_id,
      "originalname": webp_file.filename,
      "created": datetime.datetime.today().strftime('%m/%d/%Y %H:%M'),
      "filedata": webp_file.read()
    }

    db.insert_one(data)
    
    return redirect(f"https://{request.host}/{file_id}")


  return "Invalid File"  


@app.route("/<file_id>")
def display(file_id):
  data = db.find_one({"_id": file_id})
  return Response(data["filedata"], mimetype="image/webp")


    

@app.route("/s/uptimerobot")
def uptimerobot():
  return "Hello uptimerobot"

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=8080)
?