Added external embeds
This commit is contained in:
parent
5972139552
commit
516a054c31
6 changed files with 70 additions and 16 deletions
|
@ -74,6 +74,9 @@
|
||||||
<p>
|
<p>
|
||||||
Progress: {progress * 100}%
|
Progress: {progress * 100}%
|
||||||
</p>
|
</p>
|
||||||
|
{#if form.success}
|
||||||
|
<p>{form.success}</p>
|
||||||
|
{/if}
|
||||||
<p>
|
<p>
|
||||||
<Button class="upload-btn" clickFunc={ () => fileInput.click() }>Upload</Button>
|
<Button class="upload-btn" clickFunc={ () => fileInput.click() }>Upload</Button>
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<style>
|
<style>
|
||||||
img {
|
img, video {
|
||||||
max-width: 100px;
|
max-width: 100px;
|
||||||
margin: 5px;
|
margin: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
img.only-img {
|
.only-img {
|
||||||
max-width: 450px;
|
max-width: 450px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,9 +34,19 @@
|
||||||
{:else}
|
{:else}
|
||||||
<img src={elem.url} alt='Image preview'>
|
<img src={elem.url} alt='Image preview'>
|
||||||
{/if}
|
{/if}
|
||||||
|
{:else if elem.type == 'video'}
|
||||||
|
{#if line.filter(x => x.type == 'video').length < 2}
|
||||||
|
<video class='only-img' alt='Video preview' controls>
|
||||||
|
<source src={elem.url}>
|
||||||
|
</video>
|
||||||
|
{:else}
|
||||||
|
<video alt='Video preview' controls>
|
||||||
|
<source src={elem.url}>
|
||||||
|
</video>
|
||||||
|
{/if}
|
||||||
{:else if elem.type == 'link'}
|
{:else if elem.type == 'link'}
|
||||||
<a href={elem.url}>{elem.display + ' '}</a>
|
<a href={elem.url}>{elem.display + ' '}</a>
|
||||||
{:else}
|
{:else if !elem.type}
|
||||||
{elem + ' '}
|
{elem + ' '}
|
||||||
{/if}
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
|
|
|
@ -14,7 +14,7 @@ const LEGAL_SORTS = [
|
||||||
|
|
||||||
const FILE_SIZE_LIMIT = 1024*1024*16;
|
const FILE_SIZE_LIMIT = 1024*1024*16;
|
||||||
|
|
||||||
const VALID_EXTENSIONS = ['png','jpg','jpeg','gif','svg']
|
const VALID_EXTENSIONS = ['png','jpg','jpeg','gif','svg', 'mp4'];
|
||||||
|
|
||||||
var ridArray = {};
|
var ridArray = {};
|
||||||
|
|
||||||
|
@ -325,13 +325,12 @@ backend.fileCreate = async({img, extension,id, last }) => {
|
||||||
const extensionSafe = safePath(extension);
|
const extensionSafe = safePath(extension);
|
||||||
|
|
||||||
if (VALID_EXTENSIONS.indexOf(extensionSafe) == -1)
|
if (VALID_EXTENSIONS.indexOf(extensionSafe) == -1)
|
||||||
return { success: 'Illegal file extension.' };
|
return { success: 'Illegal file extension. Permitted file extensions are: ' + VALID_EXTENSIONS.join(', ') };
|
||||||
|
|
||||||
writeFile(`${process.cwd()}/db/post-${imgHash}.${extensionSafe}`,imgData,{encoding: 'base64'});
|
writeFile(`${process.cwd()}/db/post-${imgHash}.${extensionSafe}`,imgData,{encoding: 'base64'});
|
||||||
|
|
||||||
return { success: 'Successfully uploaded file.', 'href': `/img/${imgHash}.${extensionSafe}`};
|
return { success: 'Successfully uploaded file.', 'href': `/img/${imgHash}.${extensionSafe}`};
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
backendProxy,
|
backendProxy,
|
||||||
backend
|
backend
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
const EXTENSION_MAP = {
|
||||||
|
'png': 'img',
|
||||||
|
'jpg': 'img',
|
||||||
|
'jpeg': 'img',
|
||||||
|
'gif': 'img',
|
||||||
|
'svg': 'img',
|
||||||
|
'mp4': 'video'
|
||||||
|
}
|
||||||
|
|
||||||
let checkLength = function(string, field, lowerBound, upperBound) {
|
let checkLength = function(string, field, lowerBound, upperBound) {
|
||||||
if (string.length < lowerBound) {
|
if (string.length < lowerBound) {
|
||||||
if (string.length == 0) {
|
if (string.length == 0) {
|
||||||
|
@ -74,7 +83,11 @@ let formatPost = function(post) {
|
||||||
|
|
||||||
if (cap1 == 'img') {
|
if (cap1 == 'img') {
|
||||||
var matchCleaned = safePath(splitPost[1]);
|
var matchCleaned = safePath(splitPost[1]);
|
||||||
splitPost = {'type': 'img', 'url': `/img/${matchCleaned}`};
|
var extension = matchCleaned.split('.').pop().toLowerCase();
|
||||||
|
|
||||||
|
extension = safeName(extension);
|
||||||
|
|
||||||
|
splitPost = {'type': EXTENSION_MAP[extension] || 'none', 'url': `/img/${matchCleaned}`};
|
||||||
|
|
||||||
return splitPost;
|
return splitPost;
|
||||||
}
|
}
|
||||||
|
@ -85,12 +98,28 @@ let formatPost = function(post) {
|
||||||
|
|
||||||
splitPost = {'type': 'link', 'display': subPost, 'subtype': type, 'url': `/${type}/${subPostIn}`};
|
splitPost = {'type': 'link', 'display': subPost, 'subtype': type, 'url': `/${type}/${subPostIn}`};
|
||||||
|
|
||||||
|
return splitPost;
|
||||||
|
} else if (subPost.startsWith('https://')) {
|
||||||
|
var url;
|
||||||
|
var extension = subPost.split('.').pop().toLowerCase();
|
||||||
|
|
||||||
|
if (EXTENSION_MAP[extension]) {
|
||||||
|
url = `/embed?url=${encodeURIComponent(subPost)}`;
|
||||||
|
splitPost = [
|
||||||
|
{'type': 'link', 'display': subPost, 'url': url},
|
||||||
|
{'type': EXTENSION_MAP[extension] || 'link', 'display': subPost, 'url': url}
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
url = subPost;
|
||||||
|
splitPost = {'type': EXTENSION_MAP[extension] || 'link', 'display': subPost, 'url': url};
|
||||||
|
}
|
||||||
|
|
||||||
return splitPost;
|
return splitPost;
|
||||||
}
|
}
|
||||||
|
|
||||||
return subPost;
|
return subPost;
|
||||||
})
|
})
|
||||||
return line;
|
return line.flat();
|
||||||
});
|
});
|
||||||
|
|
||||||
return post;
|
return post;
|
||||||
|
|
|
@ -2,29 +2,31 @@ import { backend, backendProxy } from '../../../lib/db/db.js';
|
||||||
|
|
||||||
|
|
||||||
/** @type {import('./$types').RequestHandler} */
|
/** @type {import('./$types').RequestHandler} */
|
||||||
export async function GET({ url, cookies, params }) {
|
export async function GET({ url, cookies, params, fetch }) {
|
||||||
const formEntries = url.searchParams;
|
const formEntries = url.searchParams;
|
||||||
return await handleReq({
|
return await handleReq({
|
||||||
cookies,
|
cookies,
|
||||||
params: formEntries,
|
params: formEntries,
|
||||||
route: params.route
|
route: params.route,
|
||||||
|
fetch
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {import('./$types').RequestHandler} */
|
/** @type {import('./$types').RequestHandler} */
|
||||||
export async function POST({ cookies, request, params }) {
|
export async function POST({ cookies, request, params, fetch }) {
|
||||||
|
|
||||||
const formEntries = (await request.formData()).entries();
|
const formEntries = (await request.formData()).entries();
|
||||||
|
|
||||||
return await handleReq({
|
return await handleReq({
|
||||||
cookies,
|
cookies,
|
||||||
params: formEntries,
|
params: formEntries,
|
||||||
route: params.route
|
route: params.route,
|
||||||
|
fetch
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleReq({ cookies, params, route }) {
|
async function handleReq({ cookies, params, route, fetch }) {
|
||||||
var backendParams = {cookies};
|
var backendParams = {cookies,fetch};
|
||||||
|
|
||||||
for (const [key, value] of params) {
|
for (const [key, value] of params) {
|
||||||
backendParams[key] = value + '';
|
backendParams[key] = value + '';
|
||||||
|
@ -33,7 +35,7 @@ async function handleReq({ cookies, params, route }) {
|
||||||
return await mainApi({backendParams, route: route});
|
return await mainApi({backendParams, route: route});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mainApi({backendParams, route}) {
|
async function mainApi({backendParams, route, fetch}) {
|
||||||
if (Object.keys(backend).indexOf(route) == -1) {
|
if (Object.keys(backend).indexOf(route) == -1) {
|
||||||
return new Response(JSON.stringify({success: 'route doesn\'t exist'}));
|
return new Response(JSON.stringify({success: 'route doesn\'t exist'}));
|
||||||
}
|
}
|
||||||
|
|
11
src/routes/embed/+server.js
Normal file
11
src/routes/embed/+server.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
/** @type {import('./$types').RequestHandler} */
|
||||||
|
export async function GET({ url, fetch }) {
|
||||||
|
const urlParam = url.searchParams.get('url');
|
||||||
|
|
||||||
|
const output = await fetch(urlParam);
|
||||||
|
|
||||||
|
return new Response(await output.blob(), {'headers': {
|
||||||
|
'Content-Type': output.headers.get("Content-Type")
|
||||||
|
}});
|
||||||
|
}
|
Loading…
Reference in a new issue