cryptgeon/client/src/lib/views/Create.svelte

196 lines
4.0 KiB
Svelte
Raw Normal View History

2021-05-02 03:08:30 +02:00
<script lang="ts">
2021-12-22 13:10:08 +01:00
import { create, Note, PayloadToLargeError } from '$lib/api'
import { encrypt, getKeyFromString, getRandomBytes, Hex } from '$lib/crypto'
2021-05-02 12:31:32 +02:00
import Button from '$lib/ui/Button.svelte'
2021-12-21 00:15:04 +01:00
import FileUpload from '$lib/ui/FileUpload.svelte'
2021-12-22 13:10:08 +01:00
import MaxSize from '$lib/ui/MaxSize.svelte'
2021-05-02 12:31:32 +02:00
import Switch from '$lib/ui/Switch.svelte'
import TextArea from '$lib/ui/TextArea.svelte'
import TextInput from '$lib/ui/TextInput.svelte'
2021-12-22 13:10:08 +01:00
import { blur } from 'svelte/transition'
2021-05-02 03:08:30 +02:00
let note: Note = {
2021-05-02 14:02:40 +02:00
contents: '',
2021-12-21 00:15:04 +01:00
meta: { type: 'text' },
2021-05-02 03:08:30 +02:00
views: 1,
expiration: 60,
}
let result: { password: string; id: string } | null = null
let advanced = false
2021-12-21 00:15:04 +01:00
let file = false
2021-05-02 03:08:30 +02:00
let type = false
let message = ''
2021-05-02 14:41:08 +02:00
let loading = false
let error: string | null = null
2021-05-02 03:08:30 +02:00
$: if (!advanced) {
note.views = 1
type = false
}
$: {
let fraction: string
fraction = type ? `${note.expiration} minutes` : `${note.views} views`
message = 'the note will expire and be destroyed after ' + fraction
}
2021-12-21 00:15:04 +01:00
$: note.meta.type = file ? 'file' : 'text'
2021-12-22 13:10:08 +01:00
$: if (!file) {
note.contents = ''
}
2021-05-02 03:08:30 +02:00
async function submit() {
try {
2021-05-02 14:41:08 +02:00
error = null
2021-05-02 03:08:30 +02:00
loading = true
2021-05-03 12:21:51 +02:00
const password = Hex.encode(getRandomBytes(32))
const key = await getKeyFromString(password)
2021-05-02 03:08:30 +02:00
const data: Note = {
2021-05-03 12:21:51 +02:00
contents: await encrypt(note.contents, key),
2021-12-21 00:15:04 +01:00
meta: note.meta,
2021-05-02 03:08:30 +02:00
}
// @ts-ignore
if (type) data.expiration = parseInt(note.expiration)
// @ts-ignore
else data.views = parseInt(note.views)
const response = await create(data)
result = {
password: password,
id: response.id,
}
2021-05-10 09:58:13 +02:00
} catch (e) {
2021-12-20 18:22:10 +01:00
if (e instanceof PayloadToLargeError) {
error = 'could not create not. note is to big'
} else {
error = 'could not create note. please try again.'
}
2021-05-02 03:08:30 +02:00
} finally {
loading = false
}
}
function reset() {
window.location.reload()
}
</script>
{#if result}
2021-05-03 12:21:51 +02:00
<TextInput
type="text"
readonly
2021-05-08 21:46:33 +02:00
label="share link"
2021-05-16 11:16:25 +02:00
value="{window.location.origin}/note/{result.id}#{result.password}"
2021-05-03 12:21:51 +02:00
copy
2021-05-10 09:58:13 +02:00
data-testid="note-share-link"
2021-05-03 12:21:51 +02:00
/>
2021-05-02 03:08:30 +02:00
<br />
2021-05-08 21:46:33 +02:00
<p>
<b>availability:</b>
<br />
the note is not guaranteed to be stored as everything is kept in ram, if it fills up the oldest notes
will be removed.
<br />
(you probably will be fine, just be warned.)
</p>
<br />
<Button on:click={reset}>new note</Button>
2021-05-02 03:08:30 +02:00
{:else}
2021-12-30 22:36:28 +01:00
<p>
Easily send <i>fully encrypted</i>, secure notes or files with one click. Just create a note and
share the link.
</p>
2021-05-02 03:08:30 +02:00
<form on:submit|preventDefault={submit}>
<fieldset disabled={loading}>
2021-12-21 00:15:04 +01:00
{#if file}
<FileUpload label="file" on:file={(f) => (note.contents = f.detail)} />
{:else}
<TextArea
label="note"
bind:value={note.contents}
placeholder="..."
data-testid="input-note"
/>
{/if}
2021-05-02 03:08:30 +02:00
<div class="bottom">
2021-12-21 00:15:04 +01:00
<Switch class="file" label="file" bind:value={file} />
2021-05-02 03:08:30 +02:00
<Switch label="advanced" bind:value={advanced} />
2021-12-21 00:15:04 +01:00
<div class="grow" />
2021-12-22 13:10:08 +01:00
<div class="tr">
<small>max: <MaxSize /> </small>
<br />
<Button type="submit" data-testid="button-create">create</Button>
</div>
2021-05-02 03:08:30 +02:00
</div>
2021-05-02 14:41:08 +02:00
{#if error}
<div class="error-text">{error}</div>
{/if}
2021-05-08 21:46:33 +02:00
<p>
<br />
{#if loading}
loading...
{:else}
{message}
{/if}
</p>
2021-05-02 03:08:30 +02:00
2021-12-22 13:10:08 +01:00
{#if advanced}
<div transition:blur={{ duration: 250 }}>
<br />
<div class="fields">
<TextInput
type="number"
label="views"
bind:value={note.views}
disabled={type}
max={100}
/>
<div class="middle-switch">
<Switch label="mode" bind:value={type} color={false} />
</div>
<TextInput
type="number"
label="minutes"
bind:value={note.expiration}
disabled={!type}
max={360}
/>
2021-05-02 03:08:30 +02:00
</div>
</div>
2021-12-22 13:10:08 +01:00
{/if}
2021-05-02 03:08:30 +02:00
</fieldset>
</form>
{/if}
<style>
.bottom {
display: flex;
align-items: flex-end;
margin-top: 0.5rem;
}
2021-12-21 00:15:04 +01:00
.bottom :global(.file) {
margin-right: 0.5rem;
}
.grow {
flex: 1;
}
2021-05-02 03:08:30 +02:00
.middle-switch {
margin: 0 1rem;
}
2021-12-20 18:22:10 +01:00
.error-text {
margin-top: 0.5rem;
}
2021-12-22 13:10:08 +01:00
.fields {
display: flex;
}
2021-05-02 03:08:30 +02:00
</style>