Add shell script for uploading files to drift

This commit is contained in:
Max Leiter 2023-02-26 01:15:17 -08:00
parent 806b173d22
commit 9c3375cbd0
4 changed files with 98 additions and 5 deletions

View file

@ -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"

View file

@ -41,7 +41,6 @@ export default function HomePage({
<Item
shortcut="T"
onSelect={() => {
console.log("toggle theme", resolvedTheme)
setTheme(resolvedTheme === "dark" ? "light" : "dark")
}}
icon={resolvedTheme === "dark" ? <Sun /> : <Moon />}

View file

@ -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<unknown>) {
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<ServerFile, "content" | "html"> & {
content: string
html: string
@ -23,9 +26,10 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
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<unknown>) {
})
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)

87
tools/upload.sh Executable file
View file

@ -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 <<EOF
{
"title": "$title",
"description": "$description",
"visibility": "$visibility",
"password": "$password",
"files": [
EOF
)
# Loop through each file argument and add it to the JSON payload
for file in "$@"; do
title=$(basename "$file")
content=$(cat "$file")
json=$(
cat <<EOF
$json
{
"title": "$title",
"content": $(jq -Rs . <<<"$content")
},
EOF
)
done
# Close the JSON payload: remove just the trailing comma and add the closing bracket
json=$(echo "$json" | sed '$s/,$//')"]}"
# Send the POST request to the API endpoint
response=$(curl -s -X POST -H "$header" -H "Content-Type: application/json" -d "$json" "$url/api/post")
# Extract the ID from the response using jq
id=$(echo "$response" | jq -r '.id')
# Construct the URL with the ID
url_with_id="$url/post/$id"
# Print the URL
echo "$url_with_id"