Add shell script for uploading files to drift
This commit is contained in:
parent
806b173d22
commit
9c3375cbd0
4 changed files with 98 additions and 5 deletions
|
@ -13,7 +13,6 @@ export async function GET(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
console.log("GET /api/file/raw/[fileId]/route.ts")
|
|
||||||
const id = params.fileId
|
const id = params.fileId
|
||||||
const download = new URL(req.url).searchParams.get("download") === "true"
|
const download = new URL(req.url).searchParams.get("download") === "true"
|
||||||
|
|
||||||
|
@ -34,7 +33,6 @@ export async function GET(
|
||||||
const { title, content: contentBuffer } = file
|
const { title, content: contentBuffer } = file
|
||||||
const content = contentBuffer.toString("utf-8")
|
const content = contentBuffer.toString("utf-8")
|
||||||
|
|
||||||
console.log("title", title)
|
|
||||||
let headers: HeadersInit = {
|
let headers: HeadersInit = {
|
||||||
"Content-Type": "text/plain; charset=utf-8",
|
"Content-Type": "text/plain; charset=utf-8",
|
||||||
"Cache-Control": "s-maxage=86400"
|
"Cache-Control": "s-maxage=86400"
|
||||||
|
|
|
@ -41,7 +41,6 @@ export default function HomePage({
|
||||||
<Item
|
<Item
|
||||||
shortcut="T"
|
shortcut="T"
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
console.log("toggle theme", resolvedTheme)
|
|
||||||
setTheme(resolvedTheme === "dark" ? "light" : "dark")
|
setTheme(resolvedTheme === "dark" ? "light" : "dark")
|
||||||
}}
|
}}
|
||||||
icon={resolvedTheme === "dark" ? <Sun /> : <Moon />}
|
icon={resolvedTheme === "dark" ? <Sun /> : <Moon />}
|
||||||
|
|
|
@ -8,12 +8,15 @@ import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file"
|
||||||
import { verifyApiUser } from "@lib/server/verify-api-user"
|
import { verifyApiUser } from "@lib/server/verify-api-user"
|
||||||
|
|
||||||
async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
||||||
|
console.log("Handling post request")
|
||||||
try {
|
try {
|
||||||
const userId = await verifyApiUser(req, res)
|
const userId = await verifyApiUser(req, res)
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return res.status(401).json({ error: "Unauthorized" })
|
return res.status(401).json({ error: "Unauthorized" })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("User is authenticated")
|
||||||
|
|
||||||
const files = req.body.files as (Omit<ServerFile, "content" | "html"> & {
|
const files = req.body.files as (Omit<ServerFile, "content" | "html"> & {
|
||||||
content: string
|
content: string
|
||||||
html: string
|
html: string
|
||||||
|
@ -23,9 +26,10 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
||||||
if (missingTitles.length > 0) {
|
if (missingTitles.length > 0) {
|
||||||
throw new Error("All files must have a title")
|
throw new Error("All files must have a title")
|
||||||
}
|
}
|
||||||
|
console.log("All files have titles")
|
||||||
|
|
||||||
if (files.length === 0) {
|
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 = ""
|
let hashedPassword = ""
|
||||||
|
@ -80,12 +84,17 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
||||||
})
|
})
|
||||||
return res.json(post)
|
return res.json(post)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
return res.status(500).json(error)
|
return res.status(500).json(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
|
try {
|
||||||
return await handlePost(req, res)
|
return await handlePost(req, res)
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withMethods(["POST"], handler)
|
export default withMethods(["POST"], handler)
|
||||||
|
|
87
tools/upload.sh
Executable file
87
tools/upload.sh
Executable 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"
|
Loading…
Reference in a new issue