Compare commits
No commits in common. "29ed8c1073308d84395c0c8ace65855697c9be47" and "66596f5ecccbe16dbd89bab4c5d953703565f7e1" have entirely different histories.
29ed8c1073
...
66596f5ecc
|
@ -168,28 +168,11 @@
|
||||||
|
|
||||||
<h4 class="h4 mt-4 mb-2">Examples</h4>
|
<h4 class="h4 mt-4 mb-2">Examples</h4>
|
||||||
|
|
||||||
<pre class="bg-gray-900 py-4 px-4 mb-2 block rounded-lg whitespace-normal">
|
<code class="bg-gray-900 py-2 px-4 mb-2 block rounded-lg">curl -X POST -H "Content-Type: application/json" -d '{'{'}"contents": "paste contents"{'}'}' https://0x45.st/api/pastes</code>
|
||||||
<div class="text-sm font-medium text-gray-500 mb-2">JSON Request</div>
|
|
||||||
<code class="whitespace-normal">
|
|
||||||
$ curl -X POST -H "Content-Type: application/json" -d '{'{'}"contents": "paste contents"{'}'}' https://0x45.st/api/pastes
|
|
||||||
</code>
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<pre class="bg-gray-900 py-4 px-4 mb-2 block rounded-lg whitespace-normal">
|
<code class="bg-gray-900 py-2 px-4 mb-2 block rounded-lg">curl -X POST -F "contents=paste contents" https://0x45.st/api/pastes</code>
|
||||||
<div class="text-sm font-medium text-gray-500 mb-2">Form Request</div>
|
|
||||||
<code class="whitespace-normal">
|
|
||||||
$ curl -X POST -F "contents=paste contents" https://0x45.st/api/pastes<br />
|
|
||||||
# or, with a file<br />
|
|
||||||
$ curl -X POST -F "contents=@file-name.txt" https://0x45.st/api/pastes
|
|
||||||
</code>
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<pre class="bg-gray-900 py-4 px-4 mb-2 block rounded-lg whitespace-normal">
|
<code class="bg-gray-900 py-2 px-4 mb-2 block rounded-lg">curl -X POST -d "paste contents" https://0x45.st/api/pastes</code>
|
||||||
<div class="text-sm font-medium text-gray-500 mb-2">Plaintext Request</div>
|
|
||||||
<code class="whitespace-normal">
|
|
||||||
$ curl -X POST -H "Content-Type: text/plain" -d "paste contents" https://0x45.st/api/pastes
|
|
||||||
</code>
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<p class="mb-4 mt-4">If the paste was successfully created, the API will respond with the following JSON:</p>
|
<p class="mb-4 mt-4">If the paste was successfully created, the API will respond with the following JSON:</p>
|
||||||
|
|
||||||
|
|
|
@ -28,26 +28,18 @@ const isTrue = (value: string | boolean | undefined): boolean => {
|
||||||
// Extract the options from the form data.
|
// Extract the options from the form data.
|
||||||
const extractOptionsFromForm = async (req: Request): Promise<PasteOptions> => {
|
const extractOptionsFromForm = async (req: Request): Promise<PasteOptions> => {
|
||||||
const form = await req.formData();
|
const form = await req.formData();
|
||||||
const contents = form.get('contents');
|
const contents = form.get('contents') as string;
|
||||||
const language = form.get('language') as string;
|
const language = form.get('language') as string;
|
||||||
const password = form.get('password') as string;
|
const password = form.get('password') as string;
|
||||||
const burnAfterReading = form.get('burnAfterReading') as string;
|
const burnAfterReading = form.get('burnAfterReading') as string;
|
||||||
const raw = form.get('raw') as string;
|
const raw = form.get('raw') as string;
|
||||||
|
|
||||||
let text: string;
|
if (!contents || contents.length === 0) {
|
||||||
// Check if contents is a file, if so read it.
|
|
||||||
if (contents instanceof File) {
|
|
||||||
text = await contents.text();
|
|
||||||
} else {
|
|
||||||
text = contents as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!text || text.length === 0) {
|
|
||||||
throw error(400, 'No contents provided for your paste.')
|
throw error(400, 'No contents provided for your paste.')
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
contents: text,
|
contents,
|
||||||
language,
|
language,
|
||||||
password,
|
password,
|
||||||
burnAfterReading: isTrue(burnAfterReading),
|
burnAfterReading: isTrue(burnAfterReading),
|
||||||
|
@ -58,6 +50,7 @@ const extractOptionsFromForm = async (req: Request): Promise<PasteOptions> => {
|
||||||
// Extract the options from the JSON body.
|
// Extract the options from the JSON body.
|
||||||
const extractOptionsFromJSON = async (req: Request): Promise<PasteOptions> => {
|
const extractOptionsFromJSON = async (req: Request): Promise<PasteOptions> => {
|
||||||
const { contents, language, password, burnAfterReading, raw } = await req.json();
|
const { contents, language, password, burnAfterReading, raw } = await req.json();
|
||||||
|
console.log(contents, language, password, burnAfterReading, raw);
|
||||||
|
|
||||||
if (!contents || contents.length === 0) {
|
if (!contents || contents.length === 0) {
|
||||||
throw error(400, 'No contents provided for your paste.')
|
throw error(400, 'No contents provided for your paste.')
|
||||||
|
@ -77,7 +70,9 @@ const extractOptionsFromQuery = async (req: Request): Promise<PasteOptions> => {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const query = url.searchParams;
|
const query = url.searchParams;
|
||||||
|
|
||||||
const contents = await req.text();
|
const reader = req.body?.getReader();
|
||||||
|
const body = await reader?.read();
|
||||||
|
const contents = body?.value?.toString();
|
||||||
|
|
||||||
if (!contents) {
|
if (!contents) {
|
||||||
throw error(400, 'No contents provided for your paste.')
|
throw error(400, 'No contents provided for your paste.')
|
||||||
|
@ -137,6 +132,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
id,
|
id,
|
||||||
|
url: `${env.SITE_URL}/${id}.${highlight}`,
|
||||||
highlight,
|
highlight,
|
||||||
encrypted: !!password,
|
encrypted: !!password,
|
||||||
contents: pasteContents,
|
contents: pasteContents,
|
||||||
|
@ -147,20 +143,15 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
const pastes = await Mongo.getNamedCollection("pastes");
|
const pastes = await Mongo.getNamedCollection("pastes");
|
||||||
const res = await pastes.insertOne(data);
|
const res = await pastes.insertOne(data);
|
||||||
|
|
||||||
const url = `${env.SITE_URL}/${id}.${highlight}`;
|
|
||||||
|
|
||||||
if (!res.acknowledged) {
|
if (!res.acknowledged) {
|
||||||
throw error(500, 'Failed to create paste');
|
throw error(500, 'Failed to create paste');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (raw) {
|
if (raw) {
|
||||||
return json({
|
return json(data, {
|
||||||
...data,
|
|
||||||
url,
|
|
||||||
}, {
|
|
||||||
status: 201,
|
status: 201,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return text(url + '\n');
|
return text(data.url);
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in New Issue