From 6eb143cda881ba4dd26ab84bd85673f0fe94c1b8 Mon Sep 17 00:00:00 2001 From: Elad Rosenheim Date: Tue, 23 Apr 2024 19:48:09 +0300 Subject: Support disabling uploads + fonts fix (#11) * Inline Netlify logo, preload fonts * Support disabling blobs upload + fonts fix --- src/layouts/Layout.astro | 4 ++-- src/pages/api/blobs.ts | 29 +++++++++++++++++++++-------- src/pages/blobs/_components/NewShape.tsx | 4 ++-- src/pages/blobs/index.astro | 15 +++++++++++++++ src/utils.ts | 2 ++ 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index a105d7b..56d0c95 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -23,8 +23,8 @@ const { title } = Astro.props; - - + +
diff --git a/src/pages/api/blobs.ts b/src/pages/api/blobs.ts index d11aa58..6c2901b 100644 --- a/src/pages/api/blobs.ts +++ b/src/pages/api/blobs.ts @@ -1,9 +1,12 @@ import type { APIRoute } from 'astro'; import { getStore } from '@netlify/blobs'; +import { uploadDisabled } from '../../utils'; export const prerender = false; export const POST: APIRoute = async ({ request }) => { + if (uploadDisabled) throw new Error('Sorry, uploads are disabled'); + const parameters = await request.json(); const blobStore = getStore('shapes'); const key = parameters.name; @@ -16,12 +19,22 @@ export const POST: APIRoute = async ({ request }) => { }; export const GET: APIRoute = async ({ request }) => { - const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); - const data = await blobStore.list(); - const keys = data.blobs.map(({ key }) => key); - return new Response( - JSON.stringify({ - keys - }) - ); + try { + const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); + const data = await blobStore.list(); + const keys = data.blobs.map(({ key }) => key); + return new Response( + JSON.stringify({ + keys + }) + ); + } catch (e) { + console.error(e); + return new Response( + JSON.stringify({ + keys: [], + error: 'Failed listing blobs' + }) + ); + } }; diff --git a/src/pages/blobs/_components/NewShape.tsx b/src/pages/blobs/_components/NewShape.tsx index 05f9e6f..a141177 100644 --- a/src/pages/blobs/_components/NewShape.tsx +++ b/src/pages/blobs/_components/NewShape.tsx @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import type { Dispatch, SetStateAction } from 'react'; import ShapePreview from './ShapePreview.tsx'; -import { generateBlob } from '../../../utils'; +import { generateBlob, uploadDisabled } from '../../../utils'; import type { BlobProps } from '../../../types.ts'; interface Props { @@ -51,7 +51,7 @@ export default function NewShape(props: Props) { -
diff --git a/src/pages/blobs/index.astro b/src/pages/blobs/index.astro index 5630d6b..11aa98a 100644 --- a/src/pages/blobs/index.astro +++ b/src/pages/blobs/index.astro @@ -2,6 +2,8 @@ import Layout from '../../layouts/Layout.astro'; import Markdown from '../../components/Markdown.astro'; import ShapeEditor from './_components/ShapeEditor.tsx'; +import { uploadDisabled } from '../../utils'; +import ContextAlert from '../../components/ContextAlert.astro'; const explainer = ` [Netlify Blobs](https://docs.netlify.com/blobs/overview/) provides an object store for any kind of data, be it JSON, binary, @@ -9,9 +11,22 @@ or [really](https://mk.gg/projects/chalkstream) anything else ([really!](https:/ Below is an Astro island with a React component for editing, uploading and viewing blobs to the blob store 🙂. `; + +const uploadDisabledText = ` +User uploads are disabled in this site. To run your own and try it out: + +Deploy to Netlify + +`; --- + { + return uploadDisabled ? uploadDisabledText : null; + }} + class="mb-6" + />

Blobs x Blobs

diff --git a/src/utils.ts b/src/utils.ts index 7a32b7d..59f3ce1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -53,3 +53,5 @@ export function cacheHeaders(maxAgeDays = 365, cacheTags?: string[]): Record 0) headers['Cache-Tag'] = cacheTags.join(','); return headers; } + +export const uploadDisabled = import.meta.env.PUBLIC_DISABLE_UPLOADS?.toLowerCase() === 'true'; -- cgit v1.2.3