summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/layouts/Layout.astro4
-rw-r--r--src/pages/api/blobs.ts29
-rw-r--r--src/pages/blobs/_components/NewShape.tsx4
-rw-r--r--src/pages/blobs/index.astro15
-rw-r--r--src/utils.ts2
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;
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
- <link rel="preload" as="font" type="font/woff2" href={interFont400} />
- <link rel="preload" as="font" type="font/woff2" href={interFont800} />
+ <link rel="preload" as="font" type="font/woff2" href={interFont400} crossorigin />
+ <link rel="preload" as="font" type="font/woff2" href={interFont800} crossorigin />
</head>
<body class="antialiased text-white bg-[#355C7D]">
<div class="flex flex-col min-h-screen px-6 bg-grid-pattern sm:px-12">
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) {
<button className="btn btn-primary" onClick={randomizeBlob}>
Randomize
</button>
- <button className="btn btn-primary" onClick={uploadBlob} disabled={wasUploaded || !blobData}>
+ <button className="btn btn-primary" onClick={uploadBlob} disabled={uploadDisabled || wasUploaded || !blobData}>
Upload
</button>
</div>
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:
+<a href="https://app.netlify.com/start/deploy?repository=https://github.com/netlify-templates/astro-platform-starter">
+<img src="https://www.netlify.com/img/deploy/button.svg" style="display: inline;" alt="Deploy to Netlify" />
+</a>
+`;
---
<Layout title="Blobs">
+ <ContextAlert
+ addedChecksFunction={() => {
+ return uploadDisabled ? uploadDisabledText : null;
+ }}
+ class="mb-6"
+ />
<h1 class="mb-8">Blobs x Blobs</h1>
<Markdown content={explainer} class="mb-12" />
<ShapeEditor client:load />
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<str
if (cacheTags?.length > 0) headers['Cache-Tag'] = cacheTags.join(',');
return headers;
}
+
+export const uploadDisabled = import.meta.env.PUBLIC_DISABLE_UPLOADS?.toLowerCase() === 'true';