Added file uploads

This commit is contained in:
Xodrium 2023-02-07 19:36:32 -05:00
parent 911d17637f
commit b2ce13eea1
10 changed files with 169 additions and 40 deletions

View file

@ -1,11 +1,13 @@
<script>
import Area from '$lib/components/Area.svelte';
import {formatPost} from '$lib/util.js';
export let success, username, content, upvotes, downvotes, id;
let query = (id) ? `/post/${id}` : '';
let contentSplit = content.split('\n');
let contentSplit = formatPost(content || '');
let fData;
@ -38,6 +40,10 @@
.vote-area {
margin-right: 30px;
}
img {
max-width: 250px;
}
</style>
{#if success}
@ -61,7 +67,11 @@
</span>
<span slot="main">
{#each contentSplit as line}
<p>{line}</p>
{#if line && line.type == 'img'}
<img src={line.url} alt='Image preview'>
{:else}
<p>{line}</p>
{/if}
{/each}
</span>
<span slot="footer">

View file

@ -2,14 +2,15 @@ const rowCount = 5;
const AUTH_ACTIONS = [
'postCreate',
'fileCreate',
'vote'
];
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
import { hash, compare } from 'bcrypt'
import { randomBytes } from 'node:crypto';
import { randomBytes, createHash } from 'node:crypto';
import { readFile, writeFile } from 'node:fs/promises';
import { calcVote, calcVoteUser, checkLength, checkRegex } from '../util.js';
var db;
@ -128,7 +129,7 @@ backend.login = async ({user, pass, cookies}) => {
return { success: 'Successfully logged into account.', data: token, location: '/'};
}
backend.postCreate = async ({content}) => {
backend.postCreate = async ({content, user}) => {
var lengthCheck = checkLength(content,'Post content',1,10240);
if (lengthCheck)
@ -145,7 +146,7 @@ backend.postCreate = async ({content}) => {
calcVote(0,0)
])
return {'success': 'Your post has been broadcasted!' };
return {'success': 'Your post has been broadcasted!', 'href': `/post/${id}` };
}
backend.postGet = async ({id}) => {
@ -244,6 +245,24 @@ backend.token = async ({cookies}) => {
return {data: existingAccounts[0].username};
}
backend.fileCreate = async({img, extension}) => {
const imgHash = createHash('md5').update(img).digest('hex');
let lengthCheck = checkLength(img,'Image',0,1024*1024*5);
if (lengthCheck)
return lengthCheck;
const extensionSafe = extension.replace(/(\s+)/g, '\\$1');
if (extensionSafe != 'png' && extensionSafe != 'jpg' && extensionSafe != 'svg' )
return { success: 'Illegal file extension.' };
writeFile(`${process.cwd()}/db/post-${imgHash}.${extensionSafe}`,img,{encoding: 'base64'});
return { success: 'Successfully uploaded file.', 'href': `/img/${imgHash}.${extensionSafe}`};
}
export {
backendProxy,
backend

View file

@ -59,10 +59,33 @@ let handleSubmit = async e => {
}).then(x => x.text());
}
let formatPost = function(post) {
post = post.split('\n');
post = post.map(subPost => {
var splitPost = subPost.split('||');
if (splitPost.length > 1) {
var cap1 = splitPost[0];
if (cap1 == 'img') {
var matchCleaned = splitPost[1].replace(/(\s+)/g, '\\$1');
splitPost = {'type': 'img', 'url': `/img/${matchCleaned}`};
return splitPost;
}
}
return subPost;
});
return post;
}
export {
checkLength,
checkRegex,
calcVote,
handleSubmit,
calcVoteUser
calcVoteUser,
formatPost
};