diff --git a/.gitignore b/.gitignore index bc22080..524acd4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /dataBase.db /.env /tg -/web_nextjs +/out \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c456616 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "FileUploaderBot"] + path = FileUploaderBot + url = https://github.com/kwixie1/FileUploaderBot.git diff --git a/FileUploaderBot b/FileUploaderBot new file mode 160000 index 0000000..2c0f5d9 --- /dev/null +++ b/FileUploaderBot @@ -0,0 +1 @@ +Subproject commit 2c0f5d9c7f5660ea101321c52f41197dfa552e91 diff --git a/README.md b/README.md index 18ea244..f25c4cb 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,13 @@ This section will be referred to as `1.1` in the documentation. **List of errors:** -| errorId | message | Reasons | -| ------- | ----------------------------------------------------- | --------------------------------------------------- | -| -1 | No Authorization header provided | The request is missing the `Authorization` header | +| errorId | message | Reasons | +| ------- | ------------------------------------------------------ | -------------------------------------------------- | +| -1 | No Authorization header provided | The request is missing the `Authorization` header | | -2 | Authorization header must have `Bearer ` format | The `Authorization` header has an incorrect format | -| -3 | Access token expired | The token has expired | -| -4 | Invalid access token | The token cannot be decrypted | -| -5 | Token not found | The token is not found | +| -3 | Access token expired | The token has expired | +| -4 | Invalid access token | The token cannot be decrypted | +| -5 | Token not found | The token is not found | ### 1.2 Basic API @@ -56,9 +56,9 @@ If the file type cannot be determined, the API returns the file in download mode #### Possible Errors -| Error Code | Description | Possible Reasons | -| ---------- | ----------------------------- | -----------------------------------------------| -| 404 | File not found | The file referenced by the code does not exist | +| Error Code | Description | Possible Reasons | +| ---------- | -------------- | ---------------------------------------------- | +| 404 | File not found | The file referenced by the code does not exist | ### Upload a file to the server `POST /api/upload?include_ext=false` @@ -93,11 +93,10 @@ On successful execution, the API returns a `200` HTTP code along with a JSON res ``` #### Possible Errors -| Error Code | Description | Possible Reasons | -| ---------- | ------------------------------ | ---------------------------------------- | -| 400 | No file uploaded | No file is given in the request body | -| 400 | Bad file extension | The file does not have an extension | -| 413 | File size exceeds the limit (100MB) | The file size exceeds 100MB | +| Error Code | Description | Possible Reasons | +| ---------- | ----------------------------------- | ------------------------------------ | +| 400 | No file uploaded | No file is given in the request body | +| 413 | File size exceeds the limit (100MB) | The file size exceeds 100MB | ### Delete a file `GET /api/delete/?key=` @@ -105,10 +104,10 @@ Successful execution returns a `200` status code, removing the file from the ser #### Possible Errors -| Error Code | Description | Possible Reasons | -| ---------- | -------------------------- | ---------------------------------- | -| 404 | File not found | The file for deletion is not found | -| 400 | Invalid unique key | The provided unique key is invalid | +| Error Code | Description | Possible Reasons | +| ---------- | ------------------ | ---------------------------------- | +| 404 | File not found | The file for deletion is not found | +| 400 | Invalid unique key | The provided unique key is invalid | ### 1.2 Authorization API ### Login and register @@ -142,22 +141,22 @@ Successful execution returns a `200` HTTP code, indicating successful registrati **Common for both requests:** -| errorId | HTTP code |message | Reasons | -| ------- | ----------|---------------------------------| --------------------------------------------------- | -| 2 | 400 | No username/password provided | Username/password fields are missing in the request | +| errorId | HTTP code | message | Reasons | +| ------- | --------- | ----------------------------- | --------------------------------------------------- | +| 2 | 400 | No username/password provided | Username/password fields are missing in the request | **Errors for /register:** -| errorId | HTTP code | message | Reasons | -| ------- | ----------|------------------------------------------------| ----------------------------------------------| -| 1 | 400 |An account with this name is already registered | A user with the given username already exists | +| errorId | HTTP code | message | Reasons | +| ------- | --------- | ----------------------------------------------- | --------------------------------------------- | +| 1 | 400 | An account with this name is already registered | A user with the given username already exists | **Errors for /login:** -| errorId | HTTP code | message | Reasons | -| ------- | ----------|----------------------| -----------------------| -| 3 | 400 |Wrong password | Incorrect password | -| 4 | 404 |User not found | Username not found | +| errorId | HTTP code | message | Reasons | +| ------- | --------- | -------------- | ------------------ | +| 3 | 400 | Wrong password | Incorrect password | +| 4 | 404 | User not found | Username not found | ### Refreshe the token `POST /api/refresh_token` @@ -167,9 +166,9 @@ Successful execution returns a `200` HTTP code along with the `accessToken` fiel #### Possible Errors -| errorId | HTTP code | message | Reasons | -| ------- | ----------|-----------------------------| ------------------------------------------------- | -| 5 | 400 | No access token provided | The `accessToken` field is missing in the request | +| errorId | HTTP code | message | Reasons | +| ------- | --------- | ------------------------ | ------------------------------------------------- | +| 5 | 400 | No access token provided | The `accessToken` field is missing in the request | Errors described in section `1.1` may also occur. diff --git a/main.py b/main.py index 6f1bd8f..96db780 100644 --- a/main.py +++ b/main.py @@ -93,7 +93,7 @@ async def check_token(Authorization): @app.get("/invite/{group_id}") # invite page handler async def invite(group_id: str, request: Request): async with aiofiles.open("accept_invite.html", mode="rb") as f: - return Response(await f.read(), media_type="text/html", status_code=200) + return Response(await f.read(), media_type="text/html", status_code=200) @app.post("/api/upload/{group_id}") # File upload handler @@ -114,12 +114,6 @@ async def upload_file( content={"status": "error", "message": "No file uploaded"}, status_code=400 ) - if file.filename.find(".") == -1: # Check, if the file has a extension - return JSONResponse( - content={"status": "error", "message": "Bad file extension"}, - status_code=400, - ) - if file.size > 100 * 1024 * 1024: # 100MB limit return JSONResponse( content={ @@ -145,7 +139,8 @@ async def upload_file( if group_id != "private": if not token_db: # If token is not valid - return JSONResponse(content={ + return JSONResponse( + content={ "status": "error", "message": "Auth error", "auth_error": auth_error, @@ -153,23 +148,29 @@ async def upload_file( status_code=401, ) if not group_id.isnumeric(): - return JSONResponse(content={"status": "error", "message": "Invalid group id"}, - status_code=400) + return JSONResponse( + content={"status": "error", "message": "Invalid group id"}, + status_code=400, + ) - group = await db.group.find_first(where={"group_id": group_id}, include={"members": True} + group = await db.group.find_first( + where={"group_id": group_id}, include={"members": True} ) if not group: - return JSONResponse(content={"status": "error", "message": "Group not found"}, - status_code=404 + return JSONResponse( + content={"status": "error", "message": "Group not found"}, + status_code=404, ) if token_db.user not in group.members: - return JSONResponse(content={"status": "error", "message": "You are not in the group"}, - status_code=400) + return JSONResponse( + content={"status": "error", "message": "You are not in the group"}, + status_code=400, + ) else: group_id = -1 key = str(uuid.uuid4()) # Generate unique delete key - ext = "." + file.filename.split(".")[-1].lower() # Get file extension + ext = ("." + file.filename.split(".")[-1].lower()) if file.filename.find(".") != -1 else "" # Get file extension fid = utils.generate_token(10) + (ext if include_ext else "") # Generate file url fn = str(uuid.uuid4()) + ext # Generate file name @@ -188,9 +189,7 @@ async def upload_file( "last_watched": time.time(), "key": key, "type": ( - filetypes.get(ext[1:], default) - if ext.lower()[1:] in filetypes - else "download" + filetypes.get(ext[1:], default) if ext and ext.lower()[1:] in filetypes else "download" ), "ext": ext, "size": file.size, @@ -212,7 +211,7 @@ async def upload_file( "ext": created.ext, "size": utils.calculate_size(file.size), "user_filename": user_filename, - "username": None if not token_db else token_db.user.username, + "username": None if not token_db and group_id != "private" else token_db.user.username, "craeted_at": created.craeted_at, "synced": saved_to_account, "auth_error": auth_error, @@ -324,8 +323,9 @@ async def getFiles( ) # Get user files from db if group_id == "private": - files = await db.file.find_many(where={"user_id": user.id, "group_id": -1} - ) # Get all user files from db + files = await db.file.find_many( + where={"user_id": user.id, "group_id": -1} + ) # Get all user files from db else: if not group_id.isnumeric(): return JSONResponse( @@ -354,7 +354,7 @@ async def getFiles( user_filename = file.user_filename[:50] + ( "..." if len(file.user_filename) > 50 else "" ) - usr = await db.user.find_first(where={"id": file.user_id}) + usr = (await db.user.find_first(where={"id": file.user_id})).username if group_id != "private" else None files_response.append( { "file_url": file.url, @@ -365,7 +365,7 @@ async def getFiles( "creation_date": file.created_date, "craeted_at": file.craeted_at, "size": utils.calculate_size(file.size), - "username": (usr.username if usr else None) if group_id != "private" else None, + "username": usr, "synced": True, } ) @@ -374,7 +374,9 @@ async def getFiles( "status": "success", "message": "messages got successfully", "username": user.username, - "is_group_owner": None if group_id == "private" else group.admin_id == token_db.user_id, + "is_group_owner": ( + None if group_id == "private" else group.admin_id == token_db.user_id + ), "data": files_response, }, status_code=200, @@ -754,12 +756,15 @@ async def generate_invite( status_code=403, ) - invite = await db.invitements.create(data={"data": utils.generate_token(15), - "group":{ - 'connect':{"id": group.id} - }}) + invite = await db.invitements.create( + data={"data": utils.generate_token(15), "group": {"connect": {"id": group.id}}} + ) - return {"status": "success", "message": "created", "invite_link": f"https://fu.andcool.ru/invite/{invite.data}"} + return { + "status": "success", + "message": "created", + "invite_link": f"https://fu.andcool.ru/invite/{invite.data}", + } @app.post("/api/join/{invite_link}") # join handler @@ -791,7 +796,9 @@ async def delete_group( {"status": "error", "message": "Invite link not found"}, status_code=404 ) - group = await db.group.find_first(where={"id": invite.group_id}, include={"members": True}) + group = await db.group.find_first( + where={"id": invite.group_id}, include={"members": True} + ) if token_db.user in group.members: return JSONResponse( {"status": "error", "message": "You are already in the group"}, @@ -846,7 +853,6 @@ async def delete_group( {"status": "error", "message": "Invite link not found"}, status_code=404 ) - return { "status": "success", "message": "Info got successfully", @@ -928,8 +934,8 @@ async def get_groups( "group_id": group.group_id, } ) - return {"status": "success", "message": "groups got successfully", "groups": groups} + if __name__ == "__main__": # Start program uvicorn.run("main:app", reload=True, port=8080) diff --git a/web/index.html b/web/index.html index 20502bb..e7b86a6 100644 --- a/web/index.html +++ b/web/index.html @@ -9,7 +9,7 @@ - + @@ -94,7 +94,7 @@

File uploader