mirror of
https://github.com/cupcakearmy/cryptgeon.git
synced 2026-09-26 20:41:45 +00:00
first v3
This commit is contained in:
@@ -1,8 +1,25 @@
|
||||
import { API, type Status } from 'cryptgeon/shared'
|
||||
import { status as apiStatus } from '@cryptgeon/shared'
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
export const status = writable<null | Status>(null)
|
||||
export type StatusInfo = {
|
||||
version: string
|
||||
max_size: number
|
||||
max_views: number
|
||||
max_expiration: number
|
||||
allow_advanced: boolean
|
||||
allow_files: boolean
|
||||
imprint_url: string
|
||||
imprint_html: string
|
||||
theme_image: string
|
||||
theme_text: string
|
||||
theme_page_title: string
|
||||
theme_favicon: string
|
||||
theme_new_note_notice: boolean
|
||||
theme_home_link: boolean
|
||||
}
|
||||
|
||||
export const status = writable<null | StatusInfo>(null)
|
||||
|
||||
export async function init() {
|
||||
status.set(await API.status())
|
||||
}
|
||||
status.set((await apiStatus()) as StatusInfo)
|
||||
}
|
||||
@@ -4,10 +4,9 @@
|
||||
import { status } from '$lib/stores/status'
|
||||
import Switch from '$lib/ui/Switch.svelte'
|
||||
import TextInput from '$lib/ui/TextInput.svelte'
|
||||
import type { Note } from 'cryptgeon/shared'
|
||||
|
||||
interface Props {
|
||||
note: Note
|
||||
note: { views: number; expiration: number }
|
||||
timeExpiration?: boolean
|
||||
customPassword?: string | null
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import Button from '$lib/ui/Button.svelte'
|
||||
import MaxSize from '$lib/ui/MaxSize.svelte'
|
||||
import type { FileDTO } from 'cryptgeon/shared'
|
||||
import type { FileDTO } from '@cryptgeon/shared'
|
||||
|
||||
interface Props {
|
||||
label?: string
|
||||
@@ -16,9 +16,9 @@
|
||||
async function fileToDTO(file: File): Promise<FileDTO> {
|
||||
return {
|
||||
name: file.name,
|
||||
mime: file.type,
|
||||
size: file.size,
|
||||
type: file.type,
|
||||
contents: new Uint8Array(await file.arrayBuffer()),
|
||||
data: new Uint8Array(await file.arrayBuffer()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { t } from 'svelte-intl-precompile'
|
||||
import Button from '$lib/ui/Button.svelte'
|
||||
import type { FileDTO } from 'cryptgeon/shared'
|
||||
import type { FileDTO } from '@cryptgeon/shared'
|
||||
|
||||
interface Props {
|
||||
files: FileDTO[]
|
||||
@@ -12,7 +12,7 @@
|
||||
let previewUrls: string[] = $state([])
|
||||
|
||||
$effect(() => {
|
||||
const urls = files.map((f) => URL.createObjectURL(new Blob([f.contents], { type: f.type })))
|
||||
const urls = files.map((f) => URL.createObjectURL(new Blob([f.data.slice(0)], { type: f.mime })))
|
||||
previewUrls = urls
|
||||
return () => {
|
||||
for (const url of urls) URL.revokeObjectURL(url)
|
||||
@@ -36,12 +36,12 @@
|
||||
<div class="files-grid">
|
||||
{#each files as entry, index}
|
||||
<div class="file-preview">
|
||||
{#if isImage(entry.type)}
|
||||
{#if isImage(entry.mime)}
|
||||
<img src={previewUrls[index]} class="preview-img" alt={entry.name} />
|
||||
{:else}
|
||||
<div class="file-icon">
|
||||
<div class="file-extension">
|
||||
{entry.name.split('.').pop()?.toUpperCase() || entry.type}
|
||||
{entry.name.split('.').pop()?.toUpperCase() || entry.mime}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<script lang="ts" module>
|
||||
export type DecryptedNote = Omit<NotePublic, 'contents'> & { contents: any }
|
||||
export type DecryptedNote = {
|
||||
meta: { type: 'text' | 'file' }
|
||||
contents: any
|
||||
}
|
||||
|
||||
function saveAs(file: File) {
|
||||
const url = window.URL.createObjectURL(file)
|
||||
@@ -20,7 +23,7 @@
|
||||
|
||||
import Button from '$lib/ui/Button.svelte'
|
||||
import { copy } from '$lib/utils'
|
||||
import type { FileDTO, NotePublic } from 'cryptgeon/shared'
|
||||
import type { FileDTO } from '@cryptgeon/shared'
|
||||
|
||||
interface Props {
|
||||
note: DecryptedNote
|
||||
@@ -31,10 +34,9 @@
|
||||
const RE_URL = /[A-Za-z]+:\/\/([A-Z a-z0-9\-._~:\/?#\[\]@!$&'()*+,;%=])+/g
|
||||
let files: FileDTO[] = $state([])
|
||||
|
||||
async function downloadFile(file: FileDTO) {
|
||||
// @ts-ignore
|
||||
const f = new File([file.contents], file.name, {
|
||||
type: file.type,
|
||||
async function downloadFile(file: FileDTO) {
|
||||
const f = new File([file.data.slice(0)], file.name, {
|
||||
type: file.mime,
|
||||
})
|
||||
saveAs(f)
|
||||
}
|
||||
@@ -78,12 +80,12 @@
|
||||
<button onclick={() => downloadFile(file)}>
|
||||
<b>↓ {file.name}</b>
|
||||
</button>
|
||||
<small> {file.type} - {prettyBytes(file.size)}</small>
|
||||
<small> {file.mime} - {prettyBytes(file.size ?? file.data.length)}</small>
|
||||
</div>
|
||||
{#if file.type.startsWith('image/')}
|
||||
{#if file.mime.startsWith('image/')}
|
||||
{#key file.name}
|
||||
<img
|
||||
src={URL.createObjectURL(new File([file.contents], file.name, { type: file.type }))}
|
||||
src={URL.createObjectURL(new File([file.data.slice(0)], file.name, { type: file.mime }))}
|
||||
alt={file.name}
|
||||
class="preview"
|
||||
/>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import Icon from '$lib/ui/Icon.svelte'
|
||||
import { copy as copyFN } from '$lib/utils'
|
||||
import { getRandomBytes, Hex } from 'occulto'
|
||||
import { randomBytes, bytesToHex } from '@cryptgeon/shared'
|
||||
import type { HTMLInputAttributes } from 'svelte/elements'
|
||||
|
||||
interface Props {
|
||||
@@ -35,7 +35,7 @@
|
||||
}
|
||||
|
||||
async function randomFN() {
|
||||
value = Hex.encode(await getRandomBytes(32))
|
||||
value = bytesToHex(randomBytes(32))
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { AES, Hex } from 'occulto'
|
||||
import {
|
||||
deriveKey, generateKey, encrypt, randomBytes,
|
||||
bytesToHex, encode,
|
||||
create as apiCreate,
|
||||
type FileDTO, type ServerNote
|
||||
} from '@cryptgeon/shared'
|
||||
import { t } from 'svelte-intl-precompile'
|
||||
import { blur } from 'svelte/transition'
|
||||
|
||||
@@ -14,14 +19,8 @@
|
||||
import Result, { type NoteResult } from '$lib/ui/NoteResult.svelte'
|
||||
import Switch from '$lib/ui/Switch.svelte'
|
||||
import TextArea from '$lib/ui/TextArea.svelte'
|
||||
import { Adapters, API, PayloadToLargeError, type FileDTO, type Note } from 'cryptgeon/shared'
|
||||
|
||||
let note: Note = $state({
|
||||
contents: '',
|
||||
meta: { type: 'text' },
|
||||
views: 1,
|
||||
expiration: 60,
|
||||
})
|
||||
let note: { views: number; expiration: number } = $state({ views: 1, expiration: 60 })
|
||||
let files: FileDTO[] = $state([])
|
||||
let result: NoteResult | null = $state(null)
|
||||
let advanced = $state(false)
|
||||
@@ -31,6 +30,7 @@
|
||||
let description = $state('')
|
||||
let loading: string | null = $state(null)
|
||||
let isPasting = $state(false)
|
||||
let textContent = $state('')
|
||||
|
||||
$effect(() => {
|
||||
if (!advanced) {
|
||||
@@ -50,13 +50,7 @@
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
note.meta.type = isFile ? 'file' : 'text'
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
if (!isFile) {
|
||||
note.contents = ''
|
||||
}
|
||||
if (!isFile) textContent = ''
|
||||
})
|
||||
|
||||
async function handlePaste(e: ClipboardEvent) {
|
||||
@@ -98,11 +92,12 @@
|
||||
const name =
|
||||
file.name || `pasted-file-${Date.now()}-${Math.round(Math.random() * 1000)}${ext}`
|
||||
const renamed = new File([file], name, { type: file.type })
|
||||
const data = new Uint8Array(await renamed.arrayBuffer())
|
||||
return {
|
||||
name: renamed.name,
|
||||
mime: renamed.type,
|
||||
size: renamed.size,
|
||||
type: renamed.type,
|
||||
contents: new Uint8Array(await renamed.arrayBuffer()),
|
||||
data,
|
||||
}
|
||||
})
|
||||
)
|
||||
@@ -122,40 +117,42 @@
|
||||
try {
|
||||
loading = $t('common.encrypting')
|
||||
|
||||
const derived = customPassword && (await AES.derive(customPassword))
|
||||
const key = derived ? derived[0] : await AES.generateKey()
|
||||
const salt = customPassword ? randomBytes(16) : null
|
||||
const key = customPassword
|
||||
? deriveKey(customPassword, salt!)
|
||||
: generateKey()
|
||||
|
||||
const data: Note = {
|
||||
contents: '',
|
||||
meta: note.meta,
|
||||
}
|
||||
if (derived) data.meta.derivation = derived[1]
|
||||
let inner: Uint8Array
|
||||
if (isFile) {
|
||||
if (files.length === 0) throw new EmptyContentError()
|
||||
data.contents = await Adapters.Files.encrypt(files, key)
|
||||
inner = encode({ type: 'files', data: files })
|
||||
} else {
|
||||
if (note.contents === '') throw new EmptyContentError()
|
||||
data.contents = await Adapters.Text.encrypt(note.contents, key)
|
||||
if (textContent === '') throw new EmptyContentError()
|
||||
inner = encode({ type: 'text', data: textContent })
|
||||
}
|
||||
|
||||
const data = encrypt(inner, key)
|
||||
const extra = customPassword
|
||||
? encode({ salt: salt!, N: 32768, r: 8, p: 1 })
|
||||
: new Uint8Array()
|
||||
const serverNote: ServerNote = {
|
||||
meta: {
|
||||
...(timeExpiration ? { expiration: parseInt(note.expiration as any) } : { views: parseInt(note.views as any) }),
|
||||
extra,
|
||||
},
|
||||
data,
|
||||
}
|
||||
if (timeExpiration) data.expiration = parseInt(note.expiration as any)
|
||||
else data.views = parseInt(note.views as any)
|
||||
|
||||
loading = $t('common.uploading')
|
||||
const response = await API.create(data)
|
||||
const response = await apiCreate(serverNote)
|
||||
result = {
|
||||
id: response.id,
|
||||
password: customPassword ? undefined : Hex.encode(key),
|
||||
password: customPassword ? undefined : bytesToHex(key),
|
||||
}
|
||||
notify.success($t('home.messages.note_created'))
|
||||
} catch (e) {
|
||||
if (e instanceof PayloadToLargeError) {
|
||||
notify.error($t('home.errors.note_too_big'))
|
||||
} else if (e instanceof EmptyContentError) {
|
||||
notify.error($t('home.errors.empty_content'))
|
||||
} else {
|
||||
console.error(e)
|
||||
notify.error($t('home.errors.note_error'))
|
||||
}
|
||||
console.error(e)
|
||||
notify.error($t('home.errors.note_error'))
|
||||
} finally {
|
||||
loading = null
|
||||
}
|
||||
@@ -183,7 +180,7 @@
|
||||
<TextArea
|
||||
data-testid="text-field"
|
||||
label={$t('common.note')}
|
||||
bind:value={note.contents}
|
||||
bind:value={textContent}
|
||||
placeholder="..."
|
||||
/>
|
||||
{/if}
|
||||
@@ -284,4 +281,4 @@
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { AES, Hex } from 'occulto'
|
||||
import { deriveKey, hexToBytes, decrypt, decode, info, get as apiGet, type FileDTO } from '@cryptgeon/shared'
|
||||
import { onMount } from 'svelte'
|
||||
import { t } from 'svelte-intl-precompile'
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
import Loader from '$lib/ui/Loader.svelte'
|
||||
import ShowNote, { type DecryptedNote } from '$lib/ui/ShowNote.svelte'
|
||||
import TextInput from '$lib/ui/TextInput.svelte'
|
||||
import { Adapters, API, type NoteMeta } from 'cryptgeon/shared'
|
||||
import type { PageData } from './$types'
|
||||
|
||||
interface Props {
|
||||
@@ -20,7 +19,7 @@
|
||||
let password: string | null = $state<string | null>(null)
|
||||
let note: DecryptedNote | null = $state(null)
|
||||
let exists = $state(false)
|
||||
let meta: NoteMeta | null = $state(null)
|
||||
let hasExtra = $state(false)
|
||||
|
||||
let loading: string | null = $state(null)
|
||||
let error: string | null = $state(null)
|
||||
@@ -28,13 +27,16 @@
|
||||
let valid = $derived(!!password?.length)
|
||||
|
||||
onMount(async () => {
|
||||
// Check if note exists
|
||||
try {
|
||||
loading = $t('common.loading')
|
||||
password = window.location.hash.slice(1)
|
||||
const note = await API.info(id)
|
||||
meta = note.meta
|
||||
exists = true
|
||||
const meta = await info(id)
|
||||
if (meta) {
|
||||
hasExtra = !!meta.extra?.length
|
||||
exists = true
|
||||
} else {
|
||||
exists = false
|
||||
}
|
||||
} catch {
|
||||
exists = false
|
||||
} finally {
|
||||
@@ -42,9 +44,6 @@
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* Get the actual contents of the note and decrypt it.
|
||||
*/
|
||||
async function show(e: SubmitEvent) {
|
||||
e.preventDefault()
|
||||
try {
|
||||
@@ -53,26 +52,43 @@
|
||||
return
|
||||
}
|
||||
|
||||
// Load note
|
||||
error = null
|
||||
loading = $t('common.downloading')
|
||||
const data = await API.get(id)
|
||||
const serverNote = await apiGet(id)
|
||||
if (!serverNote) {
|
||||
error = $t('show.errors.not_found')
|
||||
return
|
||||
}
|
||||
|
||||
loading = $t('common.decrypting')
|
||||
const derived = meta?.derivation && (await AES.derive(password!, meta.derivation))
|
||||
const key = derived ? derived[0] : Hex.decode(password!)
|
||||
switch (data.meta.type) {
|
||||
let key: Uint8Array
|
||||
if (hasExtra && serverNote.meta.extra && serverNote.meta.extra.length > 0) {
|
||||
const derivation = decode(serverNote.meta.extra) as any
|
||||
key = deriveKey(password!, new Uint8Array(derivation.salt))
|
||||
} else {
|
||||
key = hexToBytes(password!)
|
||||
}
|
||||
|
||||
const decrypted = decrypt(serverNote.data, key)
|
||||
const content = decode(decrypted) as any
|
||||
|
||||
switch (content.type) {
|
||||
case 'text':
|
||||
note = {
|
||||
meta: { type: 'text' },
|
||||
contents: await Adapters.Text.decrypt(data.contents, key),
|
||||
}
|
||||
break
|
||||
case 'file':
|
||||
note = {
|
||||
meta: { type: 'file' },
|
||||
contents: await Adapters.Files.decrypt(data.contents, key),
|
||||
contents: content.data,
|
||||
}
|
||||
break
|
||||
case 'files':
|
||||
const files = (content.data as any[]).map((f: any) => ({
|
||||
...f,
|
||||
data: f.data instanceof Uint8Array ? f.data : new Uint8Array(f.data as any),
|
||||
}))
|
||||
note = {
|
||||
meta: { type: 'file' },
|
||||
contents: files,
|
||||
}
|
||||
break
|
||||
default:
|
||||
error = $t('show.errors.unsupported_type')
|
||||
return
|
||||
@@ -94,7 +110,7 @@
|
||||
<form onsubmit={show}>
|
||||
<fieldset>
|
||||
<p>{$t('show.explanation')}</p>
|
||||
{#if meta?.derivation}
|
||||
{#if hasExtra}
|
||||
<TextInput
|
||||
data-testid="show-note-password"
|
||||
type="password"
|
||||
@@ -129,4 +145,4 @@
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Reference in New Issue
Block a user