-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add upload function & disable email sending for now
- Loading branch information
1 parent
a2d6807
commit 633f93d
Showing
8 changed files
with
3,962 additions
and
3,040 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
<script lang="ts"> | ||
import { toast } from 'svelte-sonner'; | ||
export let onFileAdded: (file: File) => void; | ||
let form: HTMLFormElement; | ||
function handleFileAdded(event: Event) { | ||
const file = (event.target as HTMLInputElement).files?.[0]; | ||
if (file?.type !== 'message/rfc822') { | ||
toast.error('Invalid file type. Please upload a .eml file'); | ||
form.reset(); | ||
return; | ||
} | ||
if (file) onFileAdded(file); | ||
} | ||
</script> | ||
|
||
<div | ||
class="border-input bg-background ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring flex min-h-32 w-full rounded-md border px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" | ||
role="form"> | ||
<form | ||
class="flex w-full flex-col gap-1" | ||
bind:this={form}> | ||
<h2 class="font-bold">Drop a file here to upload</h2> | ||
<input | ||
type="file" | ||
class="hidden" /> | ||
</div> | ||
accept="message/rfc822" | ||
on:change={handleFileAdded} | ||
class="border-input bg-background ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring flex min-h-32 w-full rounded-md border border-dashed px-3 py-2 text-sm file:hidden focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { db } from '@lib/db'; | ||
import { emails, emailStorage } from '@lib/db/schema'; | ||
import { nanoid } from 'nanoid'; | ||
import { error, json, type RequestHandler } from '@sveltejs/kit'; | ||
import verifyTurnstileToken from '@lib/turnstile'; | ||
import { TURNSTILE_SECRET_KEY } from '$env/static/private'; | ||
import { simpleParser } from 'mailparser'; | ||
|
||
export const POST: RequestHandler = async ({ | ||
cookies, | ||
request, | ||
getClientAddress | ||
}) => { | ||
const body = await request.formData(); | ||
const cloudflareToken = body.get('token'); | ||
|
||
if (typeof cloudflareToken !== 'string' || !cloudflareToken) { | ||
error(400, 'Missing turnstile response'); | ||
} | ||
const valid = await verifyTurnstileToken({ | ||
response: cloudflareToken, | ||
secretKey: TURNSTILE_SECRET_KEY, | ||
remoteIp: getClientAddress() | ||
}); | ||
if (!valid) { | ||
error(400, 'Invalid turnstile response'); | ||
} | ||
const email = body.get('file'); | ||
if (!(email instanceof File)) { | ||
error(400, 'Missing email file'); | ||
} | ||
|
||
const rawEmail = await email.text(); | ||
|
||
const parsedEmail = await simpleParser(rawEmail).catch((err) => { | ||
if (err instanceof Error) { | ||
return { error: err.message }; | ||
} else { | ||
return { error: 'Unknown error' }; | ||
} | ||
}); | ||
|
||
if ('error' in parsedEmail) { | ||
error(400, parsedEmail.error); | ||
} | ||
console.log(parsedEmail); | ||
|
||
const bodyHtml = parsedEmail.html || parsedEmail.textAsHtml; | ||
if (!bodyHtml) { | ||
error(400, 'Email has no body'); | ||
} | ||
|
||
const token = nanoid(); | ||
const insert = await db.insert(emails).values({ | ||
emailAddress: 'uploaded-email', | ||
token, | ||
emailReceived: true | ||
}); | ||
|
||
await db.insert(emailStorage).values({ | ||
emailId: Number(insert.lastInsertRowid ?? 0), | ||
emailFrom: parsedEmail.from?.value[0].address || 'unknown', | ||
emailContent: bodyHtml | ||
}); | ||
|
||
cookies.set('token', token, { path: '/' }); | ||
return json({ success: true }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.