Skip to content

Commit

Permalink
Merge pull request #3 from Mr-Sunglasses/main
Browse files Browse the repository at this point in the history
fix: minors
  • Loading branch information
Mr-Sunglasses committed Dec 10, 2023
2 parents 918381e + b07e382 commit 7c14f1b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN pip install pdm

# copy files
COPY pyproject.toml pdm.lock README.md /project/
COPY data/ project/
COPY data/ project/data
COPY src/ /project/src


Expand Down
32 changes: 20 additions & 12 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pathlib import Path
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from .schema import Data
from .utils import generate_uuid

app = FastAPI(title="paste.py 🐍")
Expand All @@ -30,41 +29,50 @@ def post_as_a_file(file: UploadFile = File(...)):
print(large_uuid_storage)
except Exception:
# return {"message": "There was an error uploading the file"}
raise HTTPException(detail="There was an error uploading the file", status_code=status.HTTP_403_FORBIDDEN)
raise HTTPException(detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN)
finally:
file.file.close()

return PlainTextResponse(uuid, status_code=status.HTTP_201_CREATED)

@app.get("/{uuid}/")

@app.get("/paste/{uuid}")
def post_as_a_text(uuid):
path = f"data/{uuid}"
try:
with open(path, 'rb') as f:
return PlainTextResponse(f.read())
except Exception as e:
print(e)
raise HTTPException(detail="404: The Requested Resource is not found",status_code=status.HTTP_404_NOT_FOUND)
raise HTTPException(detail="404: The Requested Resource is not found",
status_code=status.HTTP_404_NOT_FOUND)


@app.get("/", response_class=HTMLResponse)
def indexpage(request: Request):
return templates.TemplateResponse("index.html", {"request": request})

@app.delete("/{uuid}/", response_class=PlainTextResponse)

@app.delete("/paste/{uuid}", response_class=PlainTextResponse)
def delete_paste(uuid):
path = f"data/{uuid}"
try:
os.remove(path)
return PlainTextResponse(f"File successfully deleted {uuid}")
except FileNotFoundError:
raise HTTPException(detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND)
raise HTTPException(detail="File Not Found",
status_code=status.HTTP_404_NOT_FOUND)
except Exception as e:
raise HTTPException(detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT )
raise HTTPException(
detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT)


@app.get("/web", response_class=HTMLResponse)
def web(request: Request):
return templates.TemplateResponse("web.html", {"request": request})


@app.post("/web", response_class=PlainTextResponse)
def web_post(content: str = Form(...)):
# print(content)
Expand All @@ -78,10 +86,10 @@ def web_post(content: str = Form(...)):
with open(path, 'wb') as f:
f.write(file_content)
large_uuid_storage.append(uuid)
print(large_uuid_storage)
except Exception as e:
# return {"message": "There was an error uploading the file"}
print(e)
raise HTTPException(detail="There was an error uploading the file", status_code=status.HTTP_403_FORBIDDEN)

return RedirectResponse(f"http://paste.fosscu.org/{uuid}/", status_code=status.HTTP_303_SEE_OTHER)
raise HTTPException(detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN)

return RedirectResponse(f"http://paste.fosscu.org/paste/{uuid}", status_code=status.HTTP_303_SEE_OTHER)
14 changes: 7 additions & 7 deletions src/paste/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

API USAGE

POST https://paste.fosscu.org/
POST https://paste.fosscu.org/paste

Send the raw data along. Will respond with a link to the paste.

Expand All @@ -21,28 +21,28 @@
uploaded. If the response code is anything else, an error has
occurred. Pasting is heavily rate limited.

GET https://paste.fosscu.org/<id&gt/;
GET https://paste.fosscu.org/paste/<id>

Retrieve the paste with the given id as plain-text.


DELETE https://paste.fosscu.org/<id&gt/;
DELETE https://paste.fosscu.org/paste/<id>

Delete the paste with the given id.

EXAMPLES

Paste a file named 'file.txt' using cURL:

curl -X POST -f "file=@file.txt" https://paste.fosscu.org/
curl -X POST -f "file=@file.txt" https://paste.fosscu.org/file

Paste from stdin using cURL:

echo "Hello, world." | curl -X POST -f "file=@-" https://paste.fosscu.org/
echo "Hello, world." | curl -X POST -f "file=@-" https://paste.fosscu.org/file

Delete an existing paste with id <id> using cURL:

curl -X DELETE https://paste.fosscu.org/<id&gt/;
curl -X DELETE https://paste.fosscu.org/paste/<id>

A shell function that can be added to `.bashrc` or `.bash_profle` for
quick pasting from the command line. The command takes a filename or reads
Expand All @@ -51,7 +51,7 @@

function paste() {
local file=${1:-/dev/stdin}
curl -X POST -f "file=@${file}" https://paste.fosscu.org
curl -X POST -f "file=@${file}" https://paste.fosscu.org/file
}
</body>
</html>
Empty file removed src/paste/test.py
Empty file.
4 changes: 2 additions & 2 deletions src/paste/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import string
import os


def generate_uuid():
# Combine uppercase letters, lowercase letters, and digits
characters = string.ascii_letters + string.digits
Expand All @@ -11,8 +12,7 @@ def generate_uuid():

return random_code


def extract_extension(file_name):
_, extension = os.path.splitext(file_name)
return extension

print(extract_extension("file.txt"))

0 comments on commit 7c14f1b

Please sign in to comment.