Skip to content

Commit

Permalink
updated submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 11, 2024
1 parent 4cc8031 commit add010b
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 41 deletions.
2 changes: 1 addition & 1 deletion FileUploaderBot
Submodule FileUploaderBot updated 1 files
+38 −39 main.py
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The file uploader is implemented in the Python programming language using FastAP
## API Documentation

The API is currently hosted at [fu.andcool.ru](https://fu.andcool.ru/).
Page redirection is handled through the nginx proxy server. The API consists of 2 pathes:
Page redirection is handled through the nginx proxy server. The API consists of 2 paths:

- `/file/` – Endpoint where all files are located.
- `/api/` – Main API endpoint
Expand Down Expand Up @@ -95,7 +95,7 @@ On successful execution, the API returns a `200` HTTP code along with a JSON res
| 400 | No file uploaded | No file is given in the request body |
| 413 | File size exceeds the limit (100MB) | The file size exceeds 100MB |
| 400 | Invalid group id | `group_id` parameter contains non-numerical value |
| 404 | Group not found | The group wuth provided `group_id` not found |
| 404 | Group not found | The group with provided `group_id` not found |
| 403 | You are not in the group | Group is exists, but you are has not permissions to upload files in this group |

### Delete a file
Expand Down Expand Up @@ -158,7 +158,7 @@ Successful execution returns a `200` HTTP code, indicating successful registrati
| 3 | 403 | Wrong password | Incorrect password |
| 4 | 404 | User not found | Username not found |

### Refreshe the token
### Refresh the token
`POST /api/refresh_token`
Request limit per minute: 10 times.
The request body includes the `accessToken` field containing only the token (without `Bearer`).
Expand Down Expand Up @@ -227,7 +227,7 @@ Errors described in section `1.1` may occur as well.
| Error Code | message | Possible Reasons |
| ---------- | ------------------------ | ------------------------------------------------------------------------------ |
| 400 | Invalid group id | `group_id` parameter contains non-numerical value |
| 404 | Group not found | The group wuth provided `group_id` not found |
| 404 | Group not found | The group with provided `group_id` not found |
| 403 | You are not in the group | Group is exists, but you are has not permissions to upload files in this group |


Expand Down Expand Up @@ -258,7 +258,7 @@ It takes the `Authorization` header containing the access token.
```json
{
"status": "success",
"message": "transfered",
"message": "transferred",
"unsuccess": [
{
"file_url": "4yn-8yjhsR",
Expand Down Expand Up @@ -331,7 +331,7 @@ Errors described in section `1.1` may occur as well.
| Error Code | message | Possible Reasons |
| ---------- | -------------------------------------------------- | -------------------------------------- |
| 404 | Group not found | Group with passed `group_id` not found |
| 403 | You dont have any permissions to delete this group | You are not owner of this group |
| 403 | You don't have any permissions to delete this group | You are not owner of this group |

### Generate a new invite link
`GET /api/generate_invite/{group_id}`
Expand All @@ -355,7 +355,7 @@ Errors described in section `1.1` may occur as well.
| Error Code | message | Possible Reasons |
| ---------- | ----------------------------- | -------------------------------------- |
| 404 | Group not found | Group with passed `group_id` not found |
| 403 | You dont have any permissions | You are not owner of this group |
| 403 | You don't have any permissions | You are not owner of this group |

### Join to a group
`POST /api/join/{invite_link}`
Expand Down
55 changes: 24 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
created by AndcoolSystems, 2023-2024
"""

from fastapi import FastAPI, UploadFile, Request, Header
from fastapi.responses import JSONResponse, FileResponse, Response, RedirectResponse
from typing import Annotated, Union
import uvicorn
from config import filetypes, default, accessLifeTime, accessLifeTimeBot, pattern
import aiohttp
import utils
from slowapi.errors import RateLimitExceeded
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from fastapi import FastAPI, UploadFile, Request, Header
from fastapi.middleware.cors import CORSMiddleware
import time
import aiofiles
from prisma import Prisma
import uuid
import os
from datetime import datetime
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from typing import Annotated, Union
from dotenv import load_dotenv
import jwt
from datetime import datetime
from prisma import Prisma
import aiofiles
import uvicorn
import aiohttp
import bcrypt
import random
import utils
import time
import uuid
import json
import jwt
import os
import re


Expand Down Expand Up @@ -80,7 +80,7 @@ async def api(request: Request):
)


async def check_token(Authorization, user_agent):
async def check_token(Authorization: str, user_agent: str):
if not Authorization: # If token doesn't provided
return None, {"message": "No Authorization header provided", "errorId": -1}

Expand All @@ -92,15 +92,11 @@ async def check_token(Authorization, user_agent):
}

try:
token = jwt.decode(
token_header[1], "accessTokenSecret", algorithms=["HS256"]
) # Decode token
token = jwt.decode(token_header[1], "accessTokenSecret", algorithms=["HS256"]) # Decode token
except jwt.exceptions.DecodeError:
return None, {"message": "Invalid access token", "errorId": -4}

token_db = await db.token.find_first(
where={"accessToken": token_header[1]}, include={"user": True}
) # Find token in db
token_db = await db.token.find_first(where={"accessToken": token_header[1]}, include={"user": True}) # Find token in db

if not token_db: # If not found
return None, {"message": "Token not found", "errorId": -5}
Expand Down Expand Up @@ -311,11 +307,8 @@ async def delete_file(url: str, key: str = ""):
where={"id": result.id}
) # Delete file record from database

async with aiohttp.ClientSession(
"https://api.cloudflare.com"
) as session: # Clear file cache from CloudFlare
async with session.post(
f"/client/v4/zones/{os.getenv('ZONE_ID')}/purge_cache",
async with aiohttp.ClientSession("https://api.cloudflare.com") as session: # Clear file cache from CloudFlare
async with session.post(f"/client/v4/zones/{os.getenv('ZONE_ID')}/purge_cache",
json={"files": ["https://fu.andcool.ru/file/" + result.url]},
headers={"Authorization": "Bearer " + os.getenv("KEY")}):
pass
Expand Down Expand Up @@ -396,7 +389,7 @@ async def getFiles(
"ext": file.ext,
"user_filename": user_filename,
"creation_date": file.created_date,
"craeted_at": file.craeted_at,
"created_at": file.craeted_at,
"size": utils.calculate_size(file.size),
"username": usr,
"synced": True,
Expand All @@ -416,7 +409,7 @@ async def getFiles(
)


@app.post("/api/register") # Registartion handler
@app.post("/api/register") # Registration handler
@limiter.limit(dynamic_limit_provider)
async def register(request: Request, bot: bool = False, user_agent: Union[str, None] = Header(default=None)):
try:
Expand Down Expand Up @@ -444,7 +437,7 @@ async def register(request: Request, bot: bool = False, user_agent: Union[str, N
where={"username": body["username"]}
) # Find same username in db

if user: # If iser already exists
if user: # If user already exists
return JSONResponse(
{
"status": "error",
Expand Down Expand Up @@ -485,7 +478,7 @@ async def register(request: Request, bot: bool = False, user_agent: Union[str, N
"status": "success",
"accessToken": access,
"username": body["username"],
"message": "registred",
"message": "registered",
},
status_code=200,
)
Expand Down Expand Up @@ -789,7 +782,7 @@ async def transfer(
except Exception:
non_success.append(requested_file)

return {"status": "success", "message": "transfered", "unsuccess": non_success}
return {"status": "success", "message": "transferred", "unsuccess": non_success}


# --------------------------------------Groups------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fastapi
slowapi
python-dotenv
prisma
aiofiles
uvicorn
aiohttp
bcrypt
PyJWT
13 changes: 13 additions & 0 deletions web/404/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
</body>
<script>
const path = window.location.pathname.split('/').reverse()[0];
window.location.replace('https://fu.andcool.ru/file/' + path);
</script>
</html>
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>File uploader</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Manrope:wght@800&family=Roboto+Mono&display=swap" rel="stylesheet">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="manifest" href="/res/manifest.webmanifest">
<link rel="stylesheet" href="style.css">
<link rel="preload" as="image" href="./res/globe_on.png">
<link rel="preload" as="image" href="./res/globe_off.png">
Expand Down
20 changes: 20 additions & 0 deletions web/res/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "File Uploader",
"short_name": "File Upload",
"start_url": "/",
"display": "standalone",
"background_color": "#222222",
"theme_color": "#222222",
"icons": [
{
"src": "/res/pag.png",
"sizes": "626x626",
"type": "image/png"
}
],
"orientation": "natural",
"dir": "auto",
"lang": "en",
"id": "fu.andcool",
"description": "Simple File Uploader"
}
Binary file added web/res/pag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion web/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,3 @@ async function upload(file) {
}

}

0 comments on commit add010b

Please sign in to comment.