Upgrade next and remove sqlite & react-admin.

main
Thomas Hintz 2 years ago
parent a976b03c40
commit 7b3587d7f1

1203
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -24,23 +24,18 @@
"eslint-config-next": "13.3",
"focus-visible": "^5.2.0",
"i": "^0.3.7",
"mp3-duration": "^1.1.0",
"next": "^13.4.10",
"next": "^13.4.13",
"next13-progressbar": "^0.1.2",
"nodemailer": "^6.9.3",
"podcast": "^2.0.1",
"postcss-focus-visible": "^6.0.4",
"ra-data-json-server": "^4.12.1",
"ra-data-simple-rest": "^4.12.1",
"ra-input-rich-text": "^4.12.1",
"react": "18.2.0",
"react-admin": "^4.12.1",
"react-aria": "^3.26.0",
"react-dom": "18.2.0",
"react-stately": "^3.24.0",
"sanitize-html": "^2.11.0",
"sharp": "^0.32.3",
"sqlite3": "^5.1.6",
"srtparsejs": "^1.0.8",
"uuid": "^9.0.0"
},

@ -2,7 +2,7 @@ import * as srtparsejs from "srtparsejs";
import fs from 'fs';
import path from 'path';
import db from '@/db';
// import db from '@/db';
import { extractFromXml } from '@extractus/feed-extractor'
@ -315,28 +315,28 @@ export async function getEpisodes() {
: feedEntries;
}
export async function getEpisodesLocal() {
const dbEpisodes = await db.all('select * from episodes order by number desc;');
return dbEpisodes.map(({ title, pub_date, summary: description, content, slug, duration, filename, number, episode_type, buzzsprout_id, buzzsprout_url, youtube_url, transcript_filename }) => {
const filepath = path.join(process.cwd(), 'public', 'files', 'episodes', filename);
return {
num: number,
id: buzzsprout_id,
title,
description,
content,
published: pub_date,
chapters: [`https://feeds.buzzsprout.com/1764837/${buzzsprout_id}/chapters.json`],
youtube: youtube_url,
slug,
transcript: transcript_filename ? srtparsejs.parse(fs.readFileSync(path.join(process.cwd(), 'src', 'data', transcript_filename)).toString()) : undefined,
audio: {
src: buzzsprout_url,
type: 'audio/mpeg'
},
};
});
}
/* export async function getEpisodesLocal() {
* const dbEpisodes = await db.all('select * from episodes order by number desc;');
* return dbEpisodes.map(({ title, pub_date, summary: description, content, slug, duration, filename, number, episode_type, buzzsprout_id, buzzsprout_url, youtube_url, transcript_filename }) => {
* const filepath = path.join(process.cwd(), 'public', 'files', 'episodes', filename);
* return {
* num: number,
* id: buzzsprout_id,
* title,
* description,
* content,
* published: pub_date,
* chapters: [`https://feeds.buzzsprout.com/1764837/${buzzsprout_id}/chapters.json`],
* youtube: youtube_url,
* slug,
* transcript: transcript_filename ? srtparsejs.parse(fs.readFileSync(path.join(process.cwd(), 'src', 'data', transcript_filename)).toString()) : undefined,
* audio: {
* src: buzzsprout_url,
* type: 'audio/mpeg'
* },
* };
* });
* } */
export async function getEpisode({ episodeSlug }) {
const episodes = await getEpisodes()

@ -1,8 +0,0 @@
import dynamic from "next/dynamic"
const App = dynamic(() => import("@/admin/App"), { ssr: false })
const AdminPage = () => {
return <App />;
};
export default AdminPage;

@ -1,14 +0,0 @@
const fs = require('fs');
export default async function handler(req, res) {
if (req.method === 'GET') {
const files = fs.readdirSync('./public/files/episodes', {withFileTypes: true})
.filter(item => !item.isDirectory())
.map(item => item.name);
files.sort();
files.reverse();
res.setHeader('Content-Range', files.length);
res.status(200).json(files.map((f, i) => { return { id: i, filename: f } }));
}
}

@ -1,30 +0,0 @@
import db from '@/db';
const COLS = {};
const COLS_PREFIXED = {};
const COLS_LIST = ['id', 'number', 'content', 'summary', 'slug', 'season', 'episode', 'duration', 'filename', 'title', 'episode_type', 'buzzsprout_id', 'buzzsprout_url', 'pub_date', 'youtube_url', 'transcript_filename', 'audio_url', 'audio_size'];
COLS_LIST.forEach((k) => COLS[k] = k)
COLS_LIST.forEach((k) => COLS_PREFIXED[k] = `$${k}`)
export default async function handler(req, res) {
const sessionId = req.cookies?.session;
if (!sessionId) { res.status(404).json({}); return; }
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
const { id } = req.query;
if (req.method === 'GET') {
const episode = await db.get('select * from episodes where id = ?', id);
res.status(200).json(episode)
} else if (req.method === 'PUT') {
const changes = req.body;
const changesForSQL = {};
Object.keys(changes).forEach((k) => changesForSQL[COLS_PREFIXED[k]] = changes[k]);
const { id } = await db.get(`update episodes set ${Object.keys(changes).map((k) => `${COLS[k]} = ${COLS_PREFIXED[k]}`).join(', ')} where id = $id returning id;`, changesForSQL);
const episode = await db.get('select * from episodes where id = ?', id);
res.status(200).json(episode)
} else if (req.method = 'DELETE') {
const episode = await db.get('select * from episodes where id = ?', id);
await db.run('delete from episodes where id = ?', id);
res.status(200).json(episode);
}
}

@ -1,42 +0,0 @@
import db from '@/db';
const SORT_MAP = {
'ASC': 'asc',
'DESC': 'desc'
};
const COLUMN_MAP = {
'id': 'id',
'number': 'number',
'episode': 'episode'
};
const COLS_LIST = ['number', 'content', 'summary', 'slug', 'season', 'episode', 'duration', 'filename', 'title', 'episode_type', 'buzzsprout_id', 'buzzsprout_url', 'pub_date', 'youtube_url', 'transcript_filename', 'audio_url', 'audio_size'];
export default async function handler(req, res) {
const sessionId = req.cookies?.session;
if (!sessionId) { res.status(404).json({}); return; }
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
if (req.method === 'GET') {
const { sort, range, filter } = req.query;
const [sortColumn, sortDirection] = sort ? JSON.parse(sort) : [false, false];
const [rangeStart, rangeEnd] = range ? JSON.parse(range) : [false, false];
let rows;
if (sort && range) {
rows = await db.all(`select * from episodes order by ${COLUMN_MAP[sortColumn]} ${SORT_MAP[JSON.parse(sort)[1]]} limit ? offset ?;`, rangeEnd - rangeStart, rangeStart);
} else if (filter) {
const filterParsed = JSON.parse(filter);
rows = await db.all(`select * from episodes where id in (${filterParsed['id'].map(x => '?').join(',')})`, filterParsed['id']);
}
const { count } = await db.get('select count(id) as count from episodes;');
res.setHeader('Content-Range', count);
res.status(200).json(rows)
} else if (req.method === 'POST') {
await db.run(`insert into episodes (${COLS_LIST.join(', ')}) values (${COLS_LIST.map(() => '?').join(', ')});`,
COLS_LIST.map((c) => req.body[c]));
const episode = await db.get('select * from episodes where number = ? and title = ? and slug = ?', req.body['number'], req.body['title'], req.body['slug']);
res.status(200).json(episode);
}
}

@ -1,14 +0,0 @@
import db from '@/db';
export default async function handler(req, res) {
const sessionId = req.cookies?.session;
if (!sessionId) { res.status(404).json({}); return; }
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
if (req.method === 'GET') {
const rows = await db.all('select id, user_id, uuid, started_date from subscriptions;');
res.setHeader('Content-Range', rows.length);
res.status(200).json(rows)
}
}

@ -1,15 +0,0 @@
const fs = require('fs');
export default async function handler(req, res) {
if (req.method === 'GET') {
const filesOrig = fs.readdirSync('./src/data', {withFileTypes: true})
.filter(item => !item.isDirectory())
.map(item => item.name);
const files = filesOrig.filter(f => f.includes('.srt'));
files.sort();
files.reverse();
res.setHeader('Content-Range', files.length);
res.status(200).json(files.map((f, i) => { return { id: i, filename: f } }));
}
}

@ -1,16 +0,0 @@
import db from '@/db';
export default async function handler(req, res) {
const sessionId = req.cookies?.session;
if (!sessionId) { res.status(404).json({}); return; }
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
if (req.method === 'GET') {
const rows = await db.all('select id, email from users;');
res.setHeader('Content-Range', rows.length);
res.status(200).json(rows)
}
}

@ -1,128 +0,0 @@
import path from 'path';
import fs from 'fs';
import db from '@/db';
import {
ROOT,
REACTORS_ACCOUNT,
accountUnsubscribeURL,
accountFeedURL,
podcastPage,
episodeFile
} from '@/paths';
import { Podcast } from 'podcast';
import { getEpisodes } from '@/data/episodes';
async function syncEpisodes() {
const episodes = await getEpisodes();
let newEpisodes = false;
const dbUpdates = episodes.map(async ({ title, published, description, content, slug, audio: { src, length }, num, id, youtube }) => {
const existsInDb = await db.get('select id from episodes where number=?', num);
if (!existsInDb) {
newEpisodes = true;
console.log('adding to db');
await db.run('insert into episodes (number, content, summary, slug, season, episode, audio_url, title, episode_type, buzzsprout_id, buzzsprout_url, pub_date, youtube_url) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
num,
content,
description,
slug,
1,
num,
'',
title,
'full',
id,
src,
published,
youtube);
console.log('added to db', num);
}
})
// if (newEpisodes) {
// TODO upsert: "insert into feed (last_build_date) VALUES(datetime('now'),datetime('now', 'localtime'));"
// }
await Promise.all(dbUpdates);
return newEpisodes;
};
export default async function handler(req, res) {
if (req.method === 'GET') {
// await syncEpisodes();
const { uuid: uuidRaw } = req.query;
const uuid = uuidRaw.split('.rss')[0];
const subExists = await db.get('select id from subscriptions where uuid=?', uuid);
if (subExists) {
const now = new Date();
const dbEpisodesRaw = await db.all('select * from episodes order by number desc;');
const dbEpisodes = dbEpisodesRaw.filter(e => new Date(e.pub_date) <= now);
const { last_build_date } = await db.get('select last_build_date from feed;');
const lastEpisode = dbEpisodes[0];
let lastBuilt = new Date(last_build_date);
if (lastBuilt < new Date(lastEpisode.pub_date)) {
console.log('rebuild!');
await db.run('update feed set last_build_date = ?;', now.toISOString());
lastBuilt = now;
}
const feed = new Podcast({
title: 'The React Show Premium: The Reactors',
description: `<p>Premium subscription to The React Show: thank you so much for your support!</p>
<p>Manage your subscription here: <a href="${REACTORS_ACCOUNT}">${REACTORS_ACCOUNT}</a></p>
<p>Unsubscribe here: <a href="${accountUnsubscribeURL(uuid)}">${accountUnsubscribeURL(uuid)}</a></p>
<p>Discussions about React, JavaScript, and web development by React experts with a focus on diving deep into learning React and discussing what it's like to work within the React industry.</p>`,
feedUrl: accountFeedURL(uuid),
siteUrl: ROOT,
imageUrl: 'https://storage.buzzsprout.com/variants/d1tds1rufs5340fyq9mpyzo491qp/5cfec01b44f3e29fae1fb88ade93fc4aecd05b192fbfbc2c2f1daa412b7c1921.jpg',
author: 'The React Show',
copyright: '© 2023 Owl Creek',
language: 'en',
categories: ['Technology','Education','Business'],
pubDate:lastBuilt,
ttl: 60,
itunesAuthor: 'The React Show',
itunesOwner: { name: 'The React Show' },
itunesExplicit: false,
itunesCategory: [{
text: 'Technology'
},
{
text: 'Education'
},
{
text: 'Business'
}],
itunesImage: 'https://storage.buzzsprout.com/variants/d1tds1rufs5340fyq9mpyzo491qp/5cfec01b44f3e29fae1fb88ade93fc4aecd05b192fbfbc2c2f1daa412b7c1921.jpg'
});
dbEpisodes.forEach(({ title, pub_date, summary: description, content, slug, duration, audio_url, number, episode_type }) => {
feed.addItem({
title,
description: content,
content,
url: podcastPage(slug),
date: pub_date,
itunesTitle: title,
itunesExplicit: false,
itunesSummary: description,
itunesDuration: duration,
itunesAuthor: 'The React Show',
itunesSeason: 1,
itunesEpisode: number,
itunesEpisodeType: episode_type,
enclosure : {
url: audio_url || ''
},
});
})
const xml = feed.buildXml();
res.setHeader('Content-Type', 'text/xml; charset=utf-8');
res.send(xml);
} else {
return res.status(404).send('Not found');
}
}
};

@ -1,19 +0,0 @@
import fs from 'fs';
export default async function handler(req, res) {
if (process.env.NODE_ENV !== 'development') {
return res.status(401).send('Unauthorized');
}
if (req.method === 'POST') {
try {
fs.copyFileSync('./test-db.sqlite3', './db.sqlite3');
res.status(200).end();
} catch (error) {
console.error(error);
res.status(500).send('Error copying file');
}
} else {
res.status(405).send('Method Not Allowed');
}
}

@ -1,63 +0,0 @@
import { setCookie } from 'cookies-next';
import { v4 as uuidv4 } from 'uuid';
import db from '@/db';
import { withRateLimiter } from '@/lib/rateLimiter';
import { scrypt, randomBytes, timingSafeEqual } from 'crypto';
import { promisify } from 'util';
const scryptPromise = promisify(scrypt);
async function verify(password, hash, salt, rounds = 64) {
const keyBuffer = Buffer.from(hash, 'hex');
const derivedKey = await scryptPromise(password, salt, rounds);
// Ensure both buffers have the same length
const keyBufferLength = keyBuffer.length;
const derivedKeyLength = derivedKey.length;
const maxLength = Math.max(keyBufferLength, derivedKeyLength);
const paddedKeyBuffer = keyBuffer.length < maxLength ?
Buffer.concat([Buffer.alloc(maxLength - keyBufferLength), keyBuffer]) : keyBuffer;
const paddedDerivedKey = derivedKey.length < maxLength ?
Buffer.concat([Buffer.alloc(maxLength - derivedKeyLength), derivedKey]) : derivedKey;
return timingSafeEqual(paddedKeyBuffer, paddedDerivedKey);
}
function makeMsg(email, text) {
return `/reactors/sign-in?msg=${encodeURIComponent(text)}&email=${encodeURIComponent(email)}`
};
async function handler(req, res) {
if (req.method === 'POST') {
const { email, password, remember_me: rememberMe } = req.body;
if (email && password) {
const queryRes = await db.get('select id, salt, password_hash from users where email=?;', email);
const { password_hash, salt, id: userId } = queryRes || { password_hash: '', salt: '', id: '' };
const verifyRes = await verify(password, password_hash, salt);
if (verifyRes) {
const sessionId = uuidv4();
const maxAge = 60 * 60 * 24 * 365;
const today = new Date();
const expiresDate = new Date(today.getTime() + (1000 * maxAge));
await db.run('insert into sessions (user_id, session_id, expires) values (?, ?, ?);', userId, sessionId, expiresDate.toISOString());
setCookie('session', sessionId, { req, res, maxAge: rememberMe ? maxAge : undefined, httpOnly: true, sameSite: true, secure: process.env.NODE_ENV === 'production' });
res.redirect(303, '/reactors/account')
} else {
res.redirect(303, makeMsg(email, 'Invalid password or account does not exist.'));
}
} else {
if (!email) {
res.redirect(303, makeMsg(email, 'Please enter an email address.'));
}
if (!password) {
res.redirect(303, makeMsg(email, 'Please enter a password.'));
}
}
} else {
res.status(405).json({ error: 'Method not allowed. Only POST method is supported.' });
}
}
export default withRateLimiter(handler, true);

@ -1,10 +0,0 @@
import { deleteCookie } from 'cookies-next';
export default async function handler(req, res) {
if (req.method === 'POST') {
deleteCookie('session', { req, res, httpOnly: true, sameSite: true, secure: process.env.NODE_ENV === 'production' });
return res.redirect(303, '/');
} else {
// Handle any other HTTP method
}
}
Loading…
Cancel
Save