Skip to content

Commit

Permalink
static content upload and serving
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 31, 2024
1 parent 1b33914 commit 36a8fdc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 2 deletions.
41 changes: 39 additions & 2 deletions routers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ def file_upload(form_data: FormData = fastapi_request_form):
return {"url": upload_file_from_bytes(filename, data, content_type)}


@gui.route(app, "/internal/file-upload/")
def zip_file_upload(request: Request):
from static_pages import StaticPageUpload

uploader = StaticPageUpload(request=request)

with page_wrapper(request):
uploader.render_file_upload()

return {
"meta": raw_build_meta_tags(
url=get_og_url_path(request),
title="Upload ZIP File",
description="Internal Page: Upload a ZIP file to extract its contents, to google cloud",
),
}


@gui.route(app, "/GuiComponents/")
def component_page(request: Request):
import components_doc
Expand Down Expand Up @@ -575,15 +593,34 @@ def chat_lib_route(request: Request, integration_id: str, integration_name: str
app,
"/{page_slug}/",
"/{page_slug}/{run_slug}/",
"/{page_slug}/{path:path}",
"/{page_slug}/{run_slug}-{example_id}/",
)
def recipe_page_or_handle(
request: Request, page_slug: str, run_slug: str = None, example_id: str = None
request: Request,
page_slug: str,
run_slug: str = None,
path: str = None,
example_id: str = None,
):
try:
handle = Handle.objects.get_by_name(page_slug)
except Handle.DoesNotExist:
return render_page(request, page_slug, RecipeTabs.run, example_id)
import static_pages

static_content = static_pages.serve(page_slug, path)
if not static_content:
# render recipe page
return render_page(request, page_slug, RecipeTabs.run, example_id)

if static_content.get("redirectUrl"):
return RedirectResponse(static_content.get("redirectUrl"))

if static_content.get("content"):
return Response(
content=static_content["content"],
)

else:
return render_page_for_handle(request, handle)

Expand Down
113 changes: 113 additions & 0 deletions static_pages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import gooey_gui as gui
from daras_ai_v2 import settings
import io
from starlette.requests import Request
from fastapi import HTTPException
import typing
from zipfile import ZipFile, is_zipfile
import urllib.request
from daras_ai_v2.base import BasePage


def gcs_bucket() -> "storage.storage.Bucket":
from firebase_admin import storage

return storage.bucket(settings.GS_BUCKET_NAME)


WEBSITE_FOLDER_PATH = "gooey-website"


def serve(page_slug: str, file_path: str):
from daras_ai_v2.all_pages import normalize_slug

if page_slug == "pricing":
page_slug = "index"

bucket = gcs_bucket()

blob_path = ""
if page_slug and not file_path:
# If page_slug is provided, then it's a page
blob_path = f"{WEBSITE_FOLDER_PATH}/{page_slug}.html"
elif file_path:
blob_path = f"{WEBSITE_FOLDER_PATH}/{file_path}"

if not (blob_path.endswith(".html") or blob_path.endswith(".css")):
return dict(
redirectUrl=f"https://storage.googleapis.com/{settings.GS_BUCKET_NAME}/{WEBSITE_FOLDER_PATH}/{file_path}"
)

static_file = bucket.get_blob(blob_path)
if not static_file:
return None

blob = static_file.download_as_string()
blob = blob.decode("utf-8")
content = io.StringIO(blob).read()

return dict(content=content)


class StaticPageUpload(BasePage):
def __init__(self, request: Request):
self.zip_file = None
self.extracted_files = []
self.request = request
self.is_uploading = False

def extract_zip_to_gcloud(self):
bucket = gcs_bucket()
if not self.zip_file:
return

# download zip file from gcloud (uppy)
zip_file = urllib.request.urlopen(self.zip_file)
archive = io.BytesIO()
archive.write(zip_file.read())

if archive and is_zipfile(archive):
with ZipFile(archive, "r") as z:
for file_info in z.infolist():
if not file_info.is_dir():
file_data = z.read(file_info)
file_name = file_info.filename # Maintain directory structure
blob_path = f"{WEBSITE_FOLDER_PATH}/{file_name}"
blob = bucket.blob(blob_path)
blob.upload_from_string(file_data)
self.extracted_files.append(blob.public_url)

def render_file_upload(self) -> None:
if not BasePage.is_current_user_admin(self):
raise HTTPException(status_code=404, detail="Not authorized")

with gui.div(
className="container d-flex align-items-center justify-content-center flex-column"
):
self.zip_file = gui.file_uploader(
label="Upload ZIP File",
accept=[".zip"],
value=self.zip_file,
)
pressed_save = gui.button(
"Extract ZIP File",
key="zip_file",
type="primary",
disabled=not self.zip_file or self.is_uploading,
)
gui.caption(
"After successful upload, extracted files will be displayed below.",
className="my-4 text-muted",
)
if pressed_save:
self.is_uploading = True
self.extract_zip_to_gcloud()

# render extracted files if any
if self.extracted_files and len(self.extracted_files) > 0:
gui.write("Extracted Files:")
with gui.tag("div", className="my-4 d-flex flex-column"):
for extracted_file in self.extracted_files:
with gui.tag("div", className="bg-light p-2 my-2"):
with gui.tag("a", href=extracted_file):
gui.html(extracted_file)

0 comments on commit 36a8fdc

Please sign in to comment.