From 9c3375cbd0ae2785adebdb96d95c85698e110891 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sun, 26 Feb 2023 01:15:17 -0800 Subject: [PATCH] Add shell script for uploading files to drift --- .../post/[id]/file/raw/[title]/route.ts | 2 - src/app/components/cmdk/pages/home.tsx | 1 - src/pages/api/post/index.ts | 13 ++- tools/upload.sh | 87 +++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100755 tools/upload.sh diff --git a/src/app/(drift)/(posts)/post/[id]/file/raw/[title]/route.ts b/src/app/(drift)/(posts)/post/[id]/file/raw/[title]/route.ts index acce5581..49a7fcf8 100644 --- a/src/app/(drift)/(posts)/post/[id]/file/raw/[title]/route.ts +++ b/src/app/(drift)/(posts)/post/[id]/file/raw/[title]/route.ts @@ -13,7 +13,6 @@ export async function GET( } } ) { - console.log("GET /api/file/raw/[fileId]/route.ts") const id = params.fileId const download = new URL(req.url).searchParams.get("download") === "true" @@ -34,7 +33,6 @@ export async function GET( const { title, content: contentBuffer } = file const content = contentBuffer.toString("utf-8") - console.log("title", title) let headers: HeadersInit = { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "s-maxage=86400" diff --git a/src/app/components/cmdk/pages/home.tsx b/src/app/components/cmdk/pages/home.tsx index 68980b37..693a3aa2 100644 --- a/src/app/components/cmdk/pages/home.tsx +++ b/src/app/components/cmdk/pages/home.tsx @@ -41,7 +41,6 @@ export default function HomePage({ { - console.log("toggle theme", resolvedTheme) setTheme(resolvedTheme === "dark" ? "light" : "dark") }} icon={resolvedTheme === "dark" ? : } diff --git a/src/pages/api/post/index.ts b/src/pages/api/post/index.ts index fd2cced9..1227a3f1 100644 --- a/src/pages/api/post/index.ts +++ b/src/pages/api/post/index.ts @@ -8,12 +8,15 @@ import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file" import { verifyApiUser } from "@lib/server/verify-api-user" async function handlePost(req: NextApiRequest, res: NextApiResponse) { + console.log("Handling post request") try { const userId = await verifyApiUser(req, res) if (!userId) { return res.status(401).json({ error: "Unauthorized" }) } + console.log("User is authenticated") + const files = req.body.files as (Omit & { content: string html: string @@ -23,9 +26,10 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) { if (missingTitles.length > 0) { throw new Error("All files must have a title") } + console.log("All files have titles") if (files.length === 0) { - throw new Error("You must submit at least one file") + throw new Error("You must submit at lea st one file") } let hashedPassword = "" @@ -80,12 +84,17 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) { }) return res.json(post) } catch (error) { + console.error(error) return res.status(500).json(error) } } const handler = async (req: NextApiRequest, res: NextApiResponse) => { - return await handlePost(req, res) + try { + return await handlePost(req, res) + } catch (error) { + return res.status(500).json(error) + } } export default withMethods(["POST"], handler) diff --git a/tools/upload.sh b/tools/upload.sh new file mode 100755 index 00000000..9bc3142c --- /dev/null +++ b/tools/upload.sh @@ -0,0 +1,87 @@ +#!/bin/bash +url="http://localhost:3000" +# Generated at /settings +TOKEN="" + +set -e # Exit on error + +visibility="unlisted" +title="Untitled" +password="" +description="" + +# Parse command line arguments +while getopts ":t:d:v:p:" opt; do + case ${opt} in + t) + title="$OPTARG" + ;; + d) + DEBUG=1 + ;; + v) + visibility="$OPTARG" + ;; + p) + password="$OPTARG" + ;; + # debug option with -D + \?) + echo "Invalid option -$OPTARG" 1>&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument" 1>&2 + exit 1 + ;; + esac +done +shift $((OPTIND - 1)) + +# Set the API endpoint URL +# {"id":"clel2nl7b0003p0scejnggjar","title":"test","visibility":"unlisted","password":"","createdAt":"2023-02-26T07:30:48.215Z","updatedAt":"2023-02-26T07:30:48.215Z","deletedAt":null,"expiresAt":null,"parentId":null,"description":"","authorId":"clc4babr80000p0gasef3i5ij"}⏎ +# Set the bearer token + +header="Authorization: Bearer $TOKEN" + +# Set the JSON payload +json=$( + cat <