11import sodium from 'libsodium-wrappers-sumo' ;
2- import { get } from "svelte/store" ;
3- import { localDb } from "./dexie" ;
4- import { authStore } from "./stores" ;
2+ import { localDb } from './dexie' ;
3+ import { authStore } from './stores' ;
4+ import { get } from 'svelte/store' ;
5+ import { _ } from '$lib/i18n' ;
56
67export async function deletePaste ( pasteId : string , accessKey ?: string ) {
7- if ( accessKey ) {
8- await fetch ( `/api/paste/${ pasteId } ` , {
9- method : 'DELETE' ,
10- headers : {
11- Authorization : `Bearer ${ accessKey } `
12- }
13- } ) ;
14- }
15- await localDb . pastes . delete ( pasteId ) ;
8+ if ( accessKey ) {
9+ await fetch ( `/api/paste/${ pasteId } ` , {
10+ method : 'DELETE' ,
11+ headers : {
12+ Authorization : `Bearer ${ accessKey } `
13+ }
14+ } ) ;
15+ }
16+ await localDb . pastes . delete ( pasteId ) ;
1617}
1718
1819export async function savePaste (
19- pasteId : string ,
20- accessKey : string ,
21- masterKey : string ,
22- created ?: Date ,
23- name ?: string
20+ pasteId : string ,
21+ accessKey : string ,
22+ masterKey : string ,
23+ created ?: Date ,
24+ name ?: string
2425) {
26+ const auth = get ( authStore ) ;
2527
26- const auth = get ( authStore ) ;
28+ if ( ! auth ) {
29+ await localDb . pastes . add ( {
30+ id : pasteId ,
31+ accessKey : accessKey ,
32+ masterKey : masterKey ,
33+ created : created ?? new Date ( ) ,
34+ name : name ?? 'Unknown'
35+ } ) ;
36+ } else {
37+ await sodium . ready ;
2738
28- if ( ! auth ) {
29- await localDb . pastes . add ( {
30- id : pasteId ,
31- accessKey : accessKey ,
32- masterKey : masterKey ,
33- created : created ?? new Date ( ) ,
34- name : name ?? 'Unknown'
35- } ) ;
36- } else {
37- await sodium . ready ;
39+ const rawEncryptionKey = sodium . from_base64 ( auth . encryptionKey ) ;
3840
39- const rawEncryptionKey = sodium . from_base64 ( auth . encryptionKey ) ;
41+ const encryptedPasteNonce = sodium . randombytes_buf ( sodium . crypto_secretbox_NONCEBYTES ) ;
42+ const encryptedPaste = sodium . crypto_secretbox_easy (
43+ sodium . from_base64 ( masterKey ) ,
44+ encryptedPasteNonce ,
45+ rawEncryptionKey
46+ ) ;
4047
41- const encryptedPasteNonce = sodium . randombytes_buf ( sodium . crypto_secretbox_NONCEBYTES ) ;
42- const encryptedPaste = sodium . crypto_secretbox_easy (
43- sodium . from_base64 ( masterKey ) ,
44- encryptedPasteNonce ,
45- rawEncryptionKey
46- ) ;
48+ const encryptedAccessNonce = sodium . randombytes_buf ( sodium . crypto_secretbox_NONCEBYTES ) ;
49+ const encryptedAccessKey = sodium . crypto_secretbox_easy (
50+ sodium . from_base64 ( accessKey ) ,
51+ encryptedAccessNonce ,
52+ rawEncryptionKey
53+ ) ;
4754
48- const encryptedAccessNonce = sodium . randombytes_buf ( sodium . crypto_secretbox_NONCEBYTES ) ;
49- const encryptedAccessKey = sodium . crypto_secretbox_easy (
50- sodium . from_base64 ( accessKey ) ,
51- encryptedAccessNonce ,
52- rawEncryptionKey
53- ) ;
55+ const savePastePayload = new FormData ( ) ;
56+ savePastePayload . append ( 'encryptedPasteNonce' , sodium . to_base64 ( encryptedPasteNonce ) ) ;
57+ savePastePayload . append ( 'encryptedPasteKey' , sodium . to_base64 ( encryptedPaste ) ) ;
5458
55- const savePastePayload = new FormData ( ) ;
56- savePastePayload . append ( 'encryptedPasteNonce' , sodium . to_base64 ( encryptedPasteNonce ) ) ;
57- savePastePayload . append ( 'encryptedPasteKey' , sodium . to_base64 ( encryptedPaste ) ) ;
59+ savePastePayload . append ( 'encryptedAccessNonce' , sodium . to_base64 ( encryptedAccessNonce ) ) ;
60+ savePastePayload . append ( 'encryptedAccessKey' , sodium . to_base64 ( encryptedAccessKey ) ) ;
5861
59- savePastePayload . append ( 'encryptedAccessNonce' , sodium . to_base64 ( encryptedAccessNonce ) ) ;
60- savePastePayload . append ( 'encryptedAccessKey' , sodium . to_base64 ( encryptedAccessKey ) ) ;
62+ await fetch ( `/api/account/paste/${ pasteId } ` , { method : 'POST' , body : savePastePayload } ) ;
63+ }
64+ }
6165
62- await fetch (
63- `/api/account/paste/${ pasteId } ` ,
64- { method : 'POST' , body : savePastePayload }
65- ) ;
66- }
67- }
66+ export function pasteDeletionTimes ( ) {
67+ // Must be used in func due to how states work
68+ return [
69+ { value : - 2 , label : get ( _ ) ( 'paste_actions.expire.periods.never' ) } ,
70+ { value : - 1 , label : get ( _ ) ( 'paste_actions.expire.periods.being_viewed' ) } ,
71+ {
72+ value : 0.08333 ,
73+ label : `5 ${ get ( _ ) ( 'paste_actions.expire.periods.minutes' ) } `
74+ } ,
75+ { value : 0.25 , label : `15 ${ get ( _ ) ( 'paste_actions.expire.periods.minutes' ) } ` } ,
76+ { value : 0.5 , label : `30 ${ get ( _ ) ( 'paste_actions.expire.periods.minutes' ) } ` } ,
77+ { value : 1 , label : `1 ${ get ( _ ) ( 'paste_actions.expire.periods.hour' ) } ` } ,
78+ { value : 2 , label : `2 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
79+ { value : 3 , label : `3 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
80+ { value : 4 , label : `4 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
81+ { value : 5 , label : `5 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
82+ { value : 6 , label : `6 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
83+ { value : 7 , label : `7 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
84+ { value : 8 , label : `8 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
85+ { value : 9 , label : `9 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
86+ { value : 10 , label : `10 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
87+ { value : 11 , label : `11 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
88+ { value : 12 , label : `12 ${ get ( _ ) ( 'paste_actions.expire.periods.hours' ) } ` } ,
89+ { value : 24 , label : `1 ${ get ( _ ) ( 'paste_actions.expire.periods.day' ) } ` } ,
90+ { value : 48 , label : `2 ${ get ( _ ) ( 'paste_actions.expire.periods.days' ) } ` } ,
91+ { value : 72 , label : `3 ${ get ( _ ) ( 'paste_actions.expire.periods.days' ) } ` } ,
92+ { value : 96 , label : `4 ${ get ( _ ) ( 'paste_actions.expire.periods.days' ) } ` } ,
93+ { value : 120 , label : `5 ${ get ( _ ) ( 'paste_actions.expire.periods.days' ) } ` } ,
94+ { value : 144 , label : `6 ${ get ( _ ) ( 'paste_actions.expire.periods.days' ) } ` } ,
95+ { value : 168 , label : `1 ${ get ( _ ) ( 'paste_actions.expire.periods.week' ) } ` } ,
96+ { value : 336 , label : `2 ${ get ( _ ) ( 'paste_actions.expire.periods.weeks' ) } ` } ,
97+ { value : 504 , label : `3 ${ get ( _ ) ( 'paste_actions.expire.periods.weeks' ) } ` } ,
98+ { value : 730 , label : `1 ${ get ( _ ) ( 'paste_actions.expire.periods.month' ) } ` } ,
99+ { value : 1461 , label : `2 ${ get ( _ ) ( 'paste_actions.expire.periods.months' ) } ` } ,
100+ { value : 2192 , label : `3 ${ get ( _ ) ( 'paste_actions.expire.periods.months' ) } ` }
101+ ] ;
102+ }
0 commit comments