Skip to content

Commit 9f16ad8

Browse files
committed
fix: merge conflicts
2 parents 3ba0cfc + f0c7d3c commit 9f16ad8

File tree

12 files changed

+178
-54
lines changed

12 files changed

+178
-54
lines changed

β€Ž.github/CODEOWNERSβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This is a comment.
2+
3+
# Each line is a file pattern followed by one or more owners.
4+
5+
# These owners will be the default owners for everything in
6+
7+
# the repo. Unless a later match takes precedence,
8+
9+
# @global-owner1 and @global-owner2 will be requested for
10+
11+
# review when someone opens a pull request.
12+
13+
* @Mr-Sunglasses

β€Ž.github/workflows/mypy.ymlβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: mypy paste.py 🐍
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Setup PDM
14+
uses: pdm-project/setup-pdm@v3
15+
with:
16+
python-version: 3.11
17+
cache: true
18+
cache-dependency-path: "**/pdm.lock"
19+
- name: Install dependencies
20+
run: pdm install
21+
- name: Run mypy
22+
run: pdm mypy

β€Ž.github/workflows/sarthi.ymlβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Sarthi Preview Environments
2+
on:
3+
pull_request_target:
4+
types: [ opened, closed, reopened, synchronize ]
5+
pull_request:
6+
types: [ opened, closed, reopened, synchronize ]
7+
push:
8+
# delete preview environments when branches are deleted
9+
delete:
10+
11+
jobs:
12+
sarthi_job:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v2
17+
18+
- name: Set up Sarthi
19+
uses: tushar5526/sarthi-deploy@main
20+
with:
21+
compose_file: docker-compose.yaml # override this with the compose file name
22+
sarthi_server_url: ${{ secrets.SARTHI_SERVER_URL }}
23+
sarthi_secret: ${{ secrets.SARTHI_SECRET }} # Secret text generate while setting up the server

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pyenv install 3.11.3
7171
- **Run the project using docker-compose**:
7272

7373
```bash
74-
docker-compose up -d
74+
docker-compose up
7575
```
7676

7777
## Local setup πŸ› οΈ without Docker 🐳

β€Žmypy.iniβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
ignore_missing_imports = True
3+
disallow_untyped_defs = True

β€Žpdm.lockβ€Ž

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žpyproject.tomlβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ license = {text = "MIT"}
1919

2020
[tool.pdm.scripts]
2121
start = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --workers 4"
22+
dev = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --reload"
2223
test = "pytest"
24+
mypy = "mypy src/paste"
2325

2426
[tool.pdm.dev-dependencies]
2527
test = [
@@ -33,3 +35,10 @@ lint = [
3335
hooks = [
3436
"pre-commit>=3.6.0",
3537
]
38+
typing = [
39+
"mypy>=1.8.0",
40+
]
41+
42+
[tool.ruff]
43+
line-length = 160
44+
exclude = ["data/*", ".git"]

β€Žsrc/paste/__init__.pyβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__version__: str = "2.0.0"
2+
__author__: str = "fosscu"
3+
__contact__: str = "[email protected]"
4+
__url__: str = "https://fosscu.org"

β€Žsrc/paste/main.pyβ€Ž

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
from fastapi import File, UploadFile, HTTPException, status, Request, Form, FastAPI, Header
2-
from fastapi.responses import PlainTextResponse, HTMLResponse, RedirectResponse, JSONResponse
1+
from fastapi import (
2+
File,
3+
UploadFile,
4+
HTTPException,
5+
status,
6+
Request,
7+
Form,
8+
FastAPI,
9+
Header,
10+
Response,
11+
)
12+
from fastapi.responses import (
13+
PlainTextResponse,
14+
HTMLResponse,
15+
RedirectResponse,
16+
JSONResponse,
17+
)
318
import shutil
419
import os
520
import json
621
from pathlib import Path
7-
from fastapi import FastAPI
822
from fastapi.templating import Jinja2Templates
923
from fastapi.middleware.cors import CORSMiddleware
1024
from slowapi.errors import RateLimitExceeded
@@ -16,10 +30,25 @@
1630
from pygments.lexers import get_lexer_by_name, guess_lexer
1731
from pygments.formatters import HtmlFormatter
1832
from pygments.util import ClassNotFound
19-
from typing import List, Optional, Any
33+
from typing import List, Optional
34+
from . import __version__, __author__, __contact__, __url__
35+
36+
description: str = "paste.py 🐍 - A pastebin written in python."
2037

2138
limiter = Limiter(key_func=get_remote_address)
22-
app: FastAPI = FastAPI(title="paste.py 🐍")
39+
app: FastAPI = FastAPI(
40+
title="paste.py 🐍",
41+
version=__version__,
42+
contact=dict(
43+
name=__author__,
44+
url=__url__,
45+
email=__contact__,
46+
),
47+
license_info=dict(name="MIT", url="https://opensource.org/license/mit/"),
48+
openapi_url=None,
49+
docs_url=None,
50+
redoc_url=None,
51+
)
2352
app.state.limiter = limiter
2453
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
2554

@@ -41,8 +70,7 @@
4170

4271
BASE_DIR: Path = Path(__file__).resolve().parent
4372

44-
templates: Jinja2Templates = Jinja2Templates(
45-
directory=str(Path(BASE_DIR, "templates")))
73+
templates: Jinja2Templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates")))
4674

4775

4876
@app.post("/file")
@@ -54,16 +82,17 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai
5482
uuid = generate_uuid()
5583
# Extract file extension from the filename
5684
try:
57-
file_extension: str = Path(file.filename).suffix[1:]
58-
path: str = f"data/{uuid}.{file_extension}"
85+
file_extension: Optional[str] = None
86+
if file.filename is not None:
87+
file_extension = Path(file.filename).suffix[1:]
88+
path: str = f"data/{uuid}{file_extension}"
5989
except Exception:
6090
path = f"data/{uuid}"
6191
finally:
6292
val: str = "/".join(path.split("/")[1:])
6393
with open(path, "wb") as f:
6494
shutil.copyfileobj(file.file, f)
6595
large_uuid_storage.append(uuid)
66-
print(large_uuid_storage)
6796
except Exception:
6897
raise HTTPException(
6998
detail="There was an error uploading the file",
@@ -75,7 +104,7 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai
75104

76105

77106
@app.get("/paste/{uuid}")
78-
async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Any:
107+
async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Response:
79108
path: str = f"data/{uuid}"
80109
try:
81110
with open(path, "rb") as f:
@@ -97,10 +126,8 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
97126
try:
98127
lexer = get_lexer_by_name(file_extension, stripall=True)
99128
except ClassNotFound:
100-
lexer = get_lexer_by_name(
101-
"text", stripall=True) # Default lexer
102-
formatter = HtmlFormatter(
103-
style="colorful", full=True, linenos="inline", cssclass='code')
129+
lexer = get_lexer_by_name("text", stripall=True) # Default lexer
130+
formatter = HtmlFormatter(style="colorful", full=True, linenos="inline", cssclass="code")
104131
highlighted_code: str = highlight(content, lexer, formatter)
105132
# print(highlighted_code)
106133
custom_style = """
@@ -194,19 +221,16 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
194221
</script>
195222
</html>
196223
"""
197-
return HTMLResponse(
198-
content=response_content
199-
)
200-
except Exception as e:
201-
print(e)
224+
return HTMLResponse(content=response_content)
225+
except Exception:
202226
raise HTTPException(
203227
detail="404: The Requested Resource is not found",
204228
status_code=status.HTTP_404_NOT_FOUND,
205229
)
206230

207231

208232
@app.get("/", response_class=HTMLResponse)
209-
async def indexpage(request: Request) -> HTMLResponse:
233+
async def indexpage(request: Request) -> Response:
210234
return templates.TemplateResponse("index.html", {"request": request})
211235

212236

@@ -217,25 +241,19 @@ async def delete_paste(uuid: str) -> PlainTextResponse:
217241
os.remove(path)
218242
return PlainTextResponse(f"File successfully deleted {uuid}")
219243
except FileNotFoundError:
220-
raise HTTPException(
221-
detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND
222-
)
244+
raise HTTPException(detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND)
223245
except Exception as e:
224-
raise HTTPException(
225-
detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT
226-
)
246+
raise HTTPException(detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT)
227247

228248

229249
@app.get("/web", response_class=HTMLResponse)
230-
async def web(request: Request) -> HTMLResponse:
250+
async def web(request: Request) -> Response:
231251
return templates.TemplateResponse("web.html", {"request": request})
232252

233253

234254
@app.post("/web", response_class=PlainTextResponse)
235255
@limiter.limit("100/minute")
236-
async def web_post(
237-
request: Request, content: str = Form(...), extension: Optional[str] = Form(None)
238-
) -> RedirectResponse:
256+
async def web_post(request: Request, content: str = Form(...), extension: Optional[str] = Form(None)) -> RedirectResponse:
239257
try:
240258
file_content: bytes = content.encode()
241259
uuid: str = generate_uuid()
@@ -249,16 +267,13 @@ async def web_post(
249267
with open(path, "wb") as f:
250268
f.write(file_content)
251269
large_uuid_storage.append(uuid_)
252-
except Exception as e:
253-
print(e)
270+
except Exception:
254271
raise HTTPException(
255272
detail="There was an error uploading the file",
256273
status_code=status.HTTP_403_FORBIDDEN,
257274
)
258275

259-
return RedirectResponse(
260-
f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER
261-
)
276+
return RedirectResponse(f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER)
262277

263278

264279
@app.get("/health", status_code=status.HTTP_200_OK)

β€Žsrc/paste/templates/index.htmlβ€Ž

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<title>paste.py 🐍</title>
5-
<meta charset="UTF-8"/>
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7-
<meta name="og:title" content="paste.py 🐍"/>
8-
<meta name="og:site_name" content="paste.py"/>
9-
<meta name="og:description" content="A simple pastebin powered by FastAPI."/>
10-
<meta name="og:type" content="website"/>
11-
<meta name="og:url" content="https://paste.fosscu.org"/>
12-
<meta name="og:locale" content="en_US"/>
4+
<title>paste.py 🐍</title>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<meta name="og:title" content="paste.py 🐍" />
8+
<meta name="og:site_name" content="paste.py" />
9+
<meta
10+
name="og:description"
11+
content="A simple pastebin powered by FastAPI."
12+
/>
13+
<meta name="og:type" content="website" />
14+
<meta name="og:url" content="https://paste.fosscu.org" />
15+
<meta name="og:locale" content="en_US" />
1316
<style>
1417
body {
1518
font-family: sans-serif;
@@ -109,7 +112,7 @@ <h3>EXAMPLES</h3>
109112
A shell function that can be added to <strong>.bashrc</strong> or <strong>.bash_profle</strong> or <strong>.zshrc</strong> for
110113
quick pasting from the command line. The command takes a filename or reads
111114
from stdin if none was supplied and outputs the URL of the paste to
112-
stdout: <pre>paste file.txt</pre> or <pre>echo "hi" | paste</pre></p>
115+
stdout: <pre>paste file.txt</pre> <!-- or <pre>echo "hi" | paste</pre></p> -->
113116

114117

115118
</pre>

0 commit comments

Comments
Β (0)