Skip to content

Commit

Permalink
⚡️ Upload doc (reworkd#1205)
Browse files Browse the repository at this point in the history
* ✨ Upload doc block

* ✨ Shorten url
  • Loading branch information
asim-shrestha authored Aug 7, 2023
1 parent 8b85d8e commit f4c1910
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
23 changes: 23 additions & 0 deletions next/src/services/workflow/node-block-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ const TextInputWebhookBlockDefinition: NodeBlockDefinition = {
],
};

const UploadDocBlockDefinition: NodeBlockDefinition = {
name: "Upload Doc",
type: "UploadDoc",
description: "Securely upload a .docx to Amazon S3",
image_url: "/tools/web.png",
icon: FaBook,
input_fields: [
{
name: "text",
description: "The text to upload",
type: "string",
},
],
output_fields: [
{
name: "file_url",
description: "The URL to access the doc",
type: "string",
},
],
};

const DiffDocBlockDefinition: NodeBlockDefinition = {
name: "Diff Doc",
type: "DiffDoc",
Expand Down Expand Up @@ -356,6 +378,7 @@ export const getNodeBlockDefinitions = (): NodeBlockDefinition[] => {
APITriggerBlockDefinition,
SlackWebhookBlockDefinition,
DiffDocBlockDefinition,
UploadDocBlockDefinition,
TextInputWebhookBlockDefinition,
FileUploadBlockDefinition,
IfBlockDefinition,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import io
import urllib
import urllib.parse
from typing import Any

import aiohttp
from docx import Document

from reworkd_platform.schemas.workflow.base import Block, BlockIOBase
from reworkd_platform.services.aws.s3 import SimpleStorageService
from reworkd_platform.services.sockets import websockets
from reworkd_platform.settings import settings


class UploadDocInput(BlockIOBase):
text: str


class UploadDocOutput(BlockIOBase):
file_url: str


class UploadDoc(Block):
type = "UploadDoc"
description = "Securely upload a .docx to Amazon S3"
input: UploadDocInput

async def run(self, workflow_id: str, **kwargs: Any) -> UploadDocOutput:
with io.BytesIO() as pdf_file:
websockets.log(workflow_id, "Creating doc")
doc = Document()
doc.add_paragraph(self.input.text)
doc.save(pdf_file)

websockets.log(workflow_id, "Uploading doc to S3")
s3_service = SimpleStorageService(settings.s3_bucket_name)
s3_service.upload_to_bucket(
object_name=f"docs/{workflow_id}/{self.id}.docx",
file=pdf_file,
)
file_url = s3_service.create_presigned_download_url(
object_name=f"docs/{workflow_id}/{self.id}.docx",
)

websockets.log(workflow_id, f"Doc successfully uploaded to S3")
tiny_url = await get_shortened_url(file_url)
websockets.log(workflow_id, f"Download the doc via: {tiny_url}")

return UploadDocOutput(file_url=tiny_url)


async def get_shortened_url(url: str) -> str:
ISGD_URL = "https://is.gd/create.php"

# Create the full API URL with the URL parameter encoded
api_url = ISGD_URL + "?" + urllib.parse.urlencode({"url": url, "format": "simple"})

async with aiohttp.ClientSession() as session:
async with session.get(api_url) as response:
if response.status == 200:
return await response.text()
else:
raise ValueError(
f"Failed to shorten the URL. Status: {response.status}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from reworkd_platform.schemas.workflow.blocks.conditions.if_condition import IfCondition
from reworkd_platform.schemas.workflow.blocks.do_nothing import DoNothingBlock
from reworkd_platform.schemas.workflow.blocks.pdf.diff_doc import DiffDoc
from reworkd_platform.schemas.workflow.blocks.pdf.upload_doc import UploadDoc
from reworkd_platform.schemas.workflow.blocks.slack.slack_bot import SlackMessageBlock
from reworkd_platform.schemas.workflow.blocks.text_input_webhook import TextInputWebhook
from reworkd_platform.schemas.workflow.blocks.triggers.api_trigger import (
Expand All @@ -31,6 +32,8 @@
def get_block_runner(block: Block) -> Block:
if block.type == "IfCondition":
return IfCondition(**block.dict())
if block.type == "UploadDoc":
return UploadDoc(**block.dict())
if block.type == "DiffDoc":
return DiffDoc(**block.dict())
if block.type == "WebInteractionAgent":
Expand Down

0 comments on commit f4c1910

Please sign in to comment.