Skip to content

Commit

Permalink
Merge pull request #55 from ambujraj/dev
Browse files Browse the repository at this point in the history
added webhook user endpoint
  • Loading branch information
ambujraj authored Feb 26, 2024
2 parents b926660 + 505fc3f commit 872927e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
14 changes: 13 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ provider "aws" {

provider "aws" {
alias = "r2"
region = "us-east-2"
region = "auto"

access_key = var.r2_access_key
secret_key = var.r2_secret_key
Expand Down Expand Up @@ -81,6 +81,18 @@ resource "aws_dynamodb_table" "byteshare-upload-metadata" {
}
}

resource "aws_dynamodb_table" "byteshare-user" {
provider = aws.aws
name = "byteshare-user"
billing_mode = "PAY_PER_REQUEST"
hash_key = "user_id"

attribute {
name = "user_id"
type = "S"
}
}

resource "aws_dynamodb_table" "byteshare-subscriber" {
provider = aws.aws
name = "byteshare-subscriber"
Expand Down
3 changes: 2 additions & 1 deletion middleware/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
WEB_BASE_URL=http://localhost:3000
R2_ACCOUNT_ID=
R2_ACCESS_KEY=
R2_SECRET_KEY=
R2_SECRET_KEY=
RESEND_API_KEY=
19 changes: 15 additions & 4 deletions middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
resend.api_key = str(os.getenv("RESEND_API_KEY"))

# Storage
BUCKET_NAME = "byteshare-blob"
BUCKET_NAME = "byteshare-blob"
storage = CloudflareR2Manager(BUCKET_NAME)

# DynamoDB
table_name = "byteshare-upload-metadata"
user_table_name = "byteshare-user"
subscriber_table_name = "byteshare-subscriber"
dynamodb = DynamoDBManager(table_name)
user_dynamodb = DynamoDBManager(user_table_name)
subscriber_dynamodb = DynamoDBManager(subscriber_table_name)


Expand Down Expand Up @@ -66,11 +68,11 @@ class ContinueUpload(BaseModel):
class PostUpload(BaseModel):
file_names: list


class AddUser(BaseModel):
name: str
registration: str
email: str
emailVerification: bool


@app.get("/health")
Expand Down Expand Up @@ -317,6 +319,7 @@ def post_upload_return_link_qr(body: PostUpload, upload_id: str):
"downloads_allowed": str(upload_metadata["max_download"]),
}


@app.post("/user")
def webhook_post_user_send_email(body: AddUser):
"""
Expand All @@ -327,12 +330,18 @@ def webhook_post_user_send_email(body: AddUser):
- name: name of the user
- registration: registeration date
- email: email address of user
- emailVerification: is email verified
Returns:
- Sends a welcome email to the user.
"""

user = {
"user_id": uuid.uuid4().hex,
"email": body.email,
"created_at": body.registration,
}
user_dynamodb.create_item(user)

params = {
"from": "ByteShare <hello@byteshare.io>",
"to": [body.email],
Expand All @@ -352,6 +361,7 @@ def webhook_post_user_send_email(body: AddUser):
<p>I'm always happy to help and read our customers' suggestions.</p>
<p>Thanks</p>
<p>Ambuj Raj<br>
ByteShare.io</p>
Expand All @@ -363,6 +373,7 @@ def webhook_post_user_send_email(body: AddUser):

email = resend.Emails.send(params)


@app.get("/download/{upload_id}")
def get_file_url_return_name_link(upload_id: str):
"""
Expand Down Expand Up @@ -403,7 +414,7 @@ def get_file_url_return_name_link(upload_id: str):
download_expiration_time = 21600 # 6 hours
# Generate share download link
file_url = storage.generate_download_url(file_path, download_expiration_time)
if(upload_metadata["share_email_as_source"]):
if upload_metadata["share_email_as_source"]:
file_data["user_email"] = upload_metadata["creator_email"]
else:
file_data["user_email"] = None
Expand Down

0 comments on commit 872927e

Please sign in to comment.