Update main.py
Browse files
main.py
CHANGED
|
@@ -1,179 +1,181 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request, Form, Depends, HTTPException
|
| 2 |
-
from fastapi.templating import Jinja2Templates
|
| 3 |
-
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 5 |
-
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, Text
|
| 6 |
-
from sqlalchemy.ext.declarative import declarative_base
|
| 7 |
-
from sqlalchemy.orm import sessionmaker, Session
|
| 8 |
-
from datetime import datetime
|
| 9 |
-
import random
|
| 10 |
-
import string
|
| 11 |
-
import re
|
| 12 |
-
import os
|
| 13 |
-
import bcrypt
|
| 14 |
-
from typing import Optional
|
| 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 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
uvicorn
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form, Depends, HTTPException
|
| 2 |
+
from fastapi.templating import Jinja2Templates
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 5 |
+
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, Text
|
| 6 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 7 |
+
from sqlalchemy.orm import sessionmaker, Session
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import random
|
| 10 |
+
import string
|
| 11 |
+
import re
|
| 12 |
+
import os
|
| 13 |
+
import bcrypt
|
| 14 |
+
from typing import Optional
|
| 15 |
+
|
| 16 |
+
# Define your base URL
|
| 17 |
+
BASE_URL = "https://triflix-notepad.hf.space"
|
| 18 |
+
|
| 19 |
+
# Create the FastAPI app
|
| 20 |
+
app = FastAPI(title="Notepad App")
|
| 21 |
+
|
| 22 |
+
# Set up templates and static files directories
|
| 23 |
+
templates_dir = os.path.join(os.path.dirname(__file__), "templates")
|
| 24 |
+
templates = Jinja2Templates(directory=templates_dir)
|
| 25 |
+
|
| 26 |
+
# Create static directory if it doesn't exist
|
| 27 |
+
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
| 28 |
+
os.makedirs(static_dir, exist_ok=True)
|
| 29 |
+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 30 |
+
|
| 31 |
+
# Database setup
|
| 32 |
+
DATABASE_URL = "sqlite:///./notepad.db"
|
| 33 |
+
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
| 34 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 35 |
+
Base = declarative_base()
|
| 36 |
+
|
| 37 |
+
# Define the Note model
|
| 38 |
+
class Note(Base):
|
| 39 |
+
__tablename__ = "notes"
|
| 40 |
+
|
| 41 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 42 |
+
code = Column(String, unique=True, index=True)
|
| 43 |
+
content = Column(Text)
|
| 44 |
+
is_private = Column(Boolean, default=False)
|
| 45 |
+
password = Column(String, nullable=True)
|
| 46 |
+
created_at = Column(DateTime, default=datetime.utcnow)
|
| 47 |
+
|
| 48 |
+
# Create the database tables
|
| 49 |
+
Base.metadata.create_all(bind=engine)
|
| 50 |
+
|
| 51 |
+
# Dependency to get the database session
|
| 52 |
+
def get_db():
|
| 53 |
+
db = SessionLocal()
|
| 54 |
+
try:
|
| 55 |
+
yield db
|
| 56 |
+
finally:
|
| 57 |
+
db.close()
|
| 58 |
+
|
| 59 |
+
# Function to generate a random code for the note URL
|
| 60 |
+
def generate_random_code(length=6):
|
| 61 |
+
characters = string.ascii_letters + string.digits
|
| 62 |
+
return ''.join(random.choice(characters) for _ in range(length))
|
| 63 |
+
|
| 64 |
+
# Function to detect image URLs in text and convert them to HTML img tags
|
| 65 |
+
def process_content(content):
|
| 66 |
+
# Regular expression to find image URLs
|
| 67 |
+
image_pattern = r'(https?://\S+\.(?:jpg|jpeg|png|gif|webp|svg))'
|
| 68 |
+
|
| 69 |
+
# Replace image URLs with img tags
|
| 70 |
+
processed_content = re.sub(
|
| 71 |
+
image_pattern,
|
| 72 |
+
r'<img src="\1" class="my-2 max-w-full h-auto rounded" alt="Image">',
|
| 73 |
+
content
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Convert newlines to <br> tags for proper HTML rendering
|
| 77 |
+
processed_content = processed_content.replace('\n', '<br>')
|
| 78 |
+
|
| 79 |
+
return processed_content
|
| 80 |
+
|
| 81 |
+
# Home page route
|
| 82 |
+
@app.get("/", response_class=HTMLResponse)
|
| 83 |
+
async def home(request: Request):
|
| 84 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 85 |
+
|
| 86 |
+
# Create note route
|
| 87 |
+
@app.post("/create", response_class=HTMLResponse)
|
| 88 |
+
async def create_note(
|
| 89 |
+
request: Request,
|
| 90 |
+
content: str = Form(...),
|
| 91 |
+
is_private: bool = Form(False),
|
| 92 |
+
password: Optional[str] = Form(None),
|
| 93 |
+
db: Session = Depends(get_db)
|
| 94 |
+
):
|
| 95 |
+
# Generate a unique random code
|
| 96 |
+
code = generate_random_code()
|
| 97 |
+
while db.query(Note).filter(Note.code == code).first():
|
| 98 |
+
code = generate_random_code()
|
| 99 |
+
|
| 100 |
+
# Hash password if provided
|
| 101 |
+
hashed_password = None
|
| 102 |
+
if is_private and password:
|
| 103 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 104 |
+
|
| 105 |
+
# Create new note
|
| 106 |
+
new_note = Note(
|
| 107 |
+
code=code,
|
| 108 |
+
content=content,
|
| 109 |
+
is_private=is_private,
|
| 110 |
+
password=hashed_password
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
db.add(new_note)
|
| 114 |
+
db.commit()
|
| 115 |
+
|
| 116 |
+
# Redirect to the note view page using the BASE_URL
|
| 117 |
+
return RedirectResponse(url=f"{BASE_URL}/{code}", status_code=303)
|
| 118 |
+
|
| 119 |
+
# View note route
|
| 120 |
+
@app.get("/{code}", response_class=HTMLResponse)
|
| 121 |
+
async def view_note(request: Request, code: str, db: Session = Depends(get_db)):
|
| 122 |
+
# Find the note by code
|
| 123 |
+
note = db.query(Note).filter(Note.code == code).first()
|
| 124 |
+
|
| 125 |
+
if not note:
|
| 126 |
+
raise HTTPException(status_code=404, detail="Note not found")
|
| 127 |
+
|
| 128 |
+
# If the note is private, show password prompt
|
| 129 |
+
if note.is_private:
|
| 130 |
+
return templates.TemplateResponse(
|
| 131 |
+
"password.html",
|
| 132 |
+
{"request": request, "code": code}
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Process content to render images
|
| 136 |
+
processed_content = process_content(note.content)
|
| 137 |
+
|
| 138 |
+
return templates.TemplateResponse(
|
| 139 |
+
"view.html",
|
| 140 |
+
{"request": request, "note": note, "content": processed_content}
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Verify password route
|
| 144 |
+
@app.post("/{code}/verify", response_class=HTMLResponse)
|
| 145 |
+
async def verify_password(
|
| 146 |
+
request: Request,
|
| 147 |
+
code: str,
|
| 148 |
+
password: str = Form(...),
|
| 149 |
+
db: Session = Depends(get_db)
|
| 150 |
+
):
|
| 151 |
+
# Find the note by code
|
| 152 |
+
note = db.query(Note).filter(Note.code == code).first()
|
| 153 |
+
|
| 154 |
+
if not note:
|
| 155 |
+
raise HTTPException(status_code=404, detail="Note not found")
|
| 156 |
+
|
| 157 |
+
# Verify the password
|
| 158 |
+
if not note.is_private or not note.password:
|
| 159 |
+
return RedirectResponse(url=f"{BASE_URL}/{code}", status_code=303)
|
| 160 |
+
|
| 161 |
+
is_valid = bcrypt.checkpw(password.encode('utf-8'), note.password.encode('utf-8'))
|
| 162 |
+
|
| 163 |
+
if not is_valid:
|
| 164 |
+
return templates.TemplateResponse(
|
| 165 |
+
"password.html",
|
| 166 |
+
{"request": request, "code": code, "error": "Invalid password"}
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Process content to render images
|
| 170 |
+
processed_content = process_content(note.content)
|
| 171 |
+
|
| 172 |
+
return templates.TemplateResponse(
|
| 173 |
+
"view.html",
|
| 174 |
+
{"request": request, "note": note, "content": processed_content}
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Run the application
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
import uvicorn
|
| 180 |
+
# Running the app on host 0.0.0.0 and port 7860.
|
| 181 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|