This commit is contained in:
split / May 2024-05-23 22:11:39 -07:00
parent dc53b2abad
commit 9c2f73210f
16 changed files with 109 additions and 167 deletions

View file

@ -4,30 +4,12 @@ import { readFile, writeFile } from "fs/promises"
import { FileVisibility } from "./files.js";
import { AccountSchemas } from "./schemas/index.js";
import { z } from "zod"
import DbFile from "./dbfile.js";
// this is probably horrible
// but i don't even care anymore
export let Accounts: Account[] = []
/*
export interface Account {
id : string
username : string
email? : string
password : {
hash : string
salt : string
}
files : string[]
admin : boolean
defaultFileVisibility : FileVisibility
customCSS? : string
embed? : {
color? : string
largeImage? : boolean
}
}*/
export let Db = new DbFile<Account[]>("accounts",[])
export type Account = z.infer<typeof AccountSchemas.Account>
@ -42,7 +24,7 @@ export type Account = z.infer<typeof AccountSchemas.Account>
export async function create(username:string,pwd:string,admin:boolean=false):Promise<string> {
let accId = crypto.randomBytes(12).toString("hex")
Accounts.push(
Db.data.push(
{
id: accId,
username: username,
@ -54,7 +36,7 @@ export async function create(username:string,pwd:string,admin:boolean=false):Pro
}
)
await save()
await Db.save()
return accId
}
@ -64,7 +46,7 @@ export async function create(username:string,pwd:string,admin:boolean=false):Pro
* @returns An Account, if it exists
*/
export function getFromUsername(username:string) {
return Accounts.find(e => e.username == username)
return Db.data.find(e => e.username == username)
}
/**
@ -73,7 +55,7 @@ export function getFromUsername(username:string) {
* @returns An Account, if it exists
*/
export function getFromId(id:string) {
return Accounts.find(e => e.id == id)
return Db.data.find(e => e.id == id)
}
/**
@ -92,8 +74,8 @@ export function getFromToken(token:string) {
* @param id The target account's ID
*/
export function deleteAccount(id:string) {
Accounts.splice(Accounts.findIndex(e => e.id == id),1)
return save()
Db.data.splice(Db.data.findIndex(e => e.id == id),1)
return Db.save()
}
export namespace password {
@ -121,11 +103,11 @@ export namespace password {
*/
export function set(id:string,password:string) {
let acc = Accounts.find(e => e.id == id)
let acc = Db.data.find(e => e.id == id)
if (!acc) return
acc.password = hash(password)
return save()
return Db.save()
}
@ -135,7 +117,7 @@ export namespace password {
* @param password Password to check
*/
export function check(id:string,password:string) {
let acc = Accounts.find(e => e.id == id)
let acc = Db.data.find(e => e.id == id)
if (!acc) return
return acc.password.hash == hash(password,acc.password.salt).hash
@ -153,12 +135,12 @@ export namespace files {
// maybe replace with a obj like
// { x:true }
// for faster lookups? not sure if it would be faster
let acc = Accounts.find(e => e.id == accountId)
let acc = Db.data.find(e => e.id == accountId)
if (!acc) return
if (acc.files.find(e => e == fileId)) return
acc.files.push(fileId)
if (!noWrite) return save()
if (!noWrite) return Db.save()
}
/**
@ -169,31 +151,19 @@ export namespace files {
* @returns A Promise which resolves when accounts.json finishes writing, if `noWrite` is `false`
*/
export function deindex(accountId:string,fileId:string, noWrite:boolean=false) {
let acc = Accounts.find(e => e.id == accountId)
let acc = Db.data.find(e => e.id == accountId)
if (!acc) return
let fi = acc.files.findIndex(e => e == fileId)
if (fi >= 0) {
acc.files.splice(fi,1)
if (!noWrite) return save()
if (!noWrite) return Db.save()
}
}
}
/**
* @description Saves accounts.json
* @returns A promise which resolves when accounts.json finishes writing
*/
export function save() {
return writeFile(`${process.cwd()}/.data/accounts.json`,JSON.stringify(Accounts))
.catch((err) => console.error(err))
}
readFile(`${process.cwd()}/.data/accounts.json`)
.then((buf) => {
Accounts = JSON.parse(buf.toString())
}).catch(err => console.error(err))
.finally(() => {
if (!Accounts.find(e => e.admin)) {
Db.read()
.then(() => {
if (!Db.data.find(e => e.admin)) {
create("admin","admin",true)
}
})

View file

@ -4,13 +4,15 @@ import type { Context } from "hono"
import { readFile, writeFile } from "fs/promises"
import { z } from "zod"
import { AuthSchemas } from "./schemas/index.js"
export let AuthTokens: AuthToken[] = []
import DbFile from "./dbfile.js"
export let AuthTokenTO: { [key: string]: NodeJS.Timeout } = {}
export type TokenPermission = z.infer<typeof AuthSchemas.Scope>
export type TokenType = z.infer<typeof AuthSchemas.TokenType>
export type AuthToken = z.infer<typeof AuthSchemas.AuthToken>
export const Db = new DbFile<AuthToken[]>("tokens", [])
export function create(
id: string,
expire: number | null = 24 * 60 * 60 * 1000,
@ -28,10 +30,10 @@ export function create(
type != "User" ? tokenPermissions || ["user"] : undefined,
})
AuthTokens.push(token)
Db.data.push(token)
tokenTimer(token)
save()
Db.save()
return token.token
}
@ -46,7 +48,7 @@ export function tokenFor(ctx: Context) {
}
function getToken(token: string) {
return AuthTokens.find(
return Db.data.find(
(e) => e.token == token && (e.expire == null || Date.now() < e.expire)
)
}
@ -84,23 +86,14 @@ export function invalidate(token: string) {
clearTimeout(AuthTokenTO[token])
}
AuthTokens.splice(
AuthTokens.findIndex((e) => e.token == token),
Db.data.splice(
Db.data.findIndex((e) => e.token == token),
1
)
save()
Db.save()
}
export function save() {
writeFile(
`${process.cwd()}/.data/tokens.json`,
JSON.stringify(AuthTokens)
).catch((err) => console.error(err))
}
readFile(`${process.cwd()}/.data/tokens.json`)
Db.read()
.then((buf) => {
AuthTokens = JSON.parse(buf.toString())
AuthTokens.forEach((e) => tokenTimer(e))
Db.data.forEach((e) => tokenTimer(e))
})
.catch((err) => console.error(err))

View file

@ -23,7 +23,7 @@ class Activity {
}
write() {
this.lastWrt = Date.now();
this.lastWrite = Date.now();
return this._write()
}
@ -39,13 +39,15 @@ class Activity {
if (this.goal == this.clock.lastGoal)
this.startPreciseClock()
else if (Date.now()-this.lastWrt < 15000)
else
this.clock.lastGoal = this.goal
if (Date.now()-this.lastWrite > 15000)
this.write()
}
stopClock() {
if (!this.clock) return
else clearTimeout(this.clock.id)
if (this.clock) clearTimeout(this.clock.id)
}
startTickClock() {
@ -67,8 +69,8 @@ class Activity {
set() {
this.goal = Date.now()+5000
if (!this.clock || this.clock.type != "precise")
this.startPreciseClock()
if (!this.clock || this.clock.type != "tick")
this.startTickClock()
}
}
@ -101,7 +103,7 @@ export default class DbFile<Structure extends ({}|[])> {
private async write() {
let data = JSON.stringify(this.data)
for (let x in this.files)
for (let x of this.files)
await writeFile(x, data)
}
@ -137,13 +139,13 @@ export default class DbFile<Structure extends ({}|[])> {
}
private async tryRead(path: string) {
return JSON.parse(readFile(path).toString())
return JSON.parse((await readFile(path)).toString())
}
async read() {
let availFiles = await this.findAvailable()
for (let x in availFiles) {
let data = await this.tryRead(x).catch(e => null)
for (let x of availFiles) {
let data = await this.tryRead(x).catch(_ => null)
if (data !== null) {
this.data = data
break

View file

@ -11,7 +11,7 @@ import * as Accounts from "./accounts.js"
import { z } from "zod"
import * as schemas from "./schemas/files.js"
import { issuesToMessage } from "./middleware.js"
import file from "../routes/api/v1/file/index.js"
import DbFile from "./dbfile.js"
export let alphanum = Array.from(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
@ -435,9 +435,9 @@ export class UploadStream extends Writable {
if (!this.uploadId) this.setUploadId(generateFileId())
let ogf = this.files.files[this.uploadId!]
let ogf = this.files.db.data[this.uploadId!]
this.files.files[this.uploadId!] = {
this.files.db.data[this.uploadId!] = {
filename: this.name,
mime: this.mime,
messageids: this.messages,
@ -459,7 +459,7 @@ export class UploadStream extends Writable {
delete this.files.locks[this.uploadId!]
if (ogf?.messageids) await this.files.api.deleteMessages(ogf.messageids)
await this.files.write()
await this.files.db.save()
if (this.owner) Accounts.files.index(this.owner, this.uploadId!)
return this.uploadId
}
@ -508,7 +508,7 @@ export class UploadStream extends Writable {
if (!check.success)
return this.destroy(new WebError(400, issuesToMessage(check.error.issues)))
if (this.files.files[id] && this.files.files[id].owner != this.owner)
if (this.files.db.data[id] && this.files.db.data[id].owner != this.owner)
return this.destroy(new WebError(403, "you don't own this file"))
if (this.files.locks[id])
@ -587,50 +587,27 @@ export class UploadStream extends Writable {
export default class Files {
config: Configuration
api: API
files: { [key: string]: FilePointer } = {}
data_directory: string = `${process.cwd()}/.data`
db = new DbFile<{ [key: string]: FilePointer }>("files", {})
locks: Record<string, boolean> = {} // I'll, like, do something more proper later
constructor(config: Configuration) {
this.config = config
this.api = new API(config.discordToken!, config)
readFile(this.data_directory + "/files.json")
.then((buf) => {
this.files = JSON.parse(buf.toString() || "{}")
})
.catch(console.error)
this.db.read()
}
createWriteStream(owner?: string) {
return new UploadStream(this, owner)
}
// fs
/**
* @description Saves file database
*
*/
async write(): Promise<void> {
await writeFile(
this.data_directory + "/files.json",
JSON.stringify(
this.files,
null,
process.env.NODE_ENV === "development" ? 4 : undefined
)
)
}
/**
* @description Update a file from monofile 1.x to 2.x
* @param uploadId Target file's ID
*/
async update(uploadId: string) {
let target_file = this.files[uploadId]
let target_file = this.db.data[uploadId]
let attachments: APIAttachment[] = []
for (let message of target_file.messageids) {
@ -668,8 +645,8 @@ export default class Files {
uploadId: string,
range?: { start: number; end: number }
): Promise<ReadStream> {
if (this.files[uploadId]) {
let file = this.files[uploadId]
if (this.db.data[uploadId]) {
let file = this.db.data[uploadId]
if (!file.sizeInBytes || !file.chunkSize)
await this.update(uploadId)
return new ReadStream(this, file, range)
@ -684,7 +661,7 @@ export default class Files {
* @param noWrite Whether or not the change should be written to disk. Enable for bulk deletes
*/
async unlink(uploadId: string, noWrite: boolean = false): Promise<void> {
let target = this.files[uploadId]
let target = this.db.data[uploadId]
if (!target) return
await this.api.deleteMessages(target.messageids)
@ -693,14 +670,14 @@ export default class Files {
let id = files.deindex(target.owner, uploadId, noWrite)
if (id) await id
}
delete this.files[uploadId]
delete this.db.data[uploadId]
if (!noWrite)
return this.write()
return this.db.save()
}
async chown(uploadId: string, newOwner?: string, noWrite: boolean = false) {
let target = this.files[uploadId]
let target = this.db.data[uploadId]
if (target.owner) {
let i = files.deindex(target.owner, uploadId, Boolean(newOwner && noWrite))
if (i) await i
@ -713,24 +690,24 @@ export default class Files {
}
if (!noWrite)
return this.write()
return this.db.save()
}
async mv(uploadId: string, newId: string, noWrite: boolean = false) {
let target = this.files[uploadId]
let target = this.db.data[uploadId]
if (target.owner) {
let owner = Accounts.getFromId(target.owner)
if (owner) {
owner.files.splice(owner.files.indexOf(uploadId), 1, newId)
if (!noWrite)
await Accounts.save()
await Accounts.Db.save()
}
}
this.files[newId] = target
delete this.files[uploadId]
this.db.data[newId] = target
delete this.db.data[uploadId]
if (!noWrite)
return this.write()
return this.db.save()
}
}

View file

@ -40,7 +40,7 @@ export default function (files: Files) {
}
Accounts.password.set(targetAccount.id, body.password)
auth.AuthTokens.filter((e) => e.account == targetAccount?.id).forEach(
auth.Db.data.filter((e) => e.account == targetAccount?.id).forEach(
(v) => {
auth.invalidate(v.token)
}
@ -70,7 +70,7 @@ export default function (files: Files) {
return ctx.text("not found", 404)
}
Accounts.save()
Accounts.Db.save()
return ctx.text("OK")
})
@ -80,7 +80,7 @@ export default function (files: Files) {
return ctx.text("not found", 404)
}
let targetFile = files.files[body.target]
let targetFile = files.db.data[body.target]
if (!targetFile) {
return ctx.text("not found", 404)
@ -106,7 +106,7 @@ export default function (files: Files) {
let accId = targetAccount.id
auth.AuthTokens.filter((e) => e.account == accId).forEach((v) => {
auth.Db.data.filter((e) => e.account == accId).forEach((v) => {
auth.invalidate(v.token)
})
@ -140,7 +140,7 @@ export default function (files: Files) {
return writeFile(
process.cwd() + "/.data/files.json",
JSON.stringify(files.files)
JSON.stringify(files.db.data)
).then(cpl)
} else return cpl()
})
@ -151,7 +151,7 @@ export default function (files: Files) {
return ctx.text("not found", 404)
}
let targetFile = files.files[body.target]
let targetFile = files.db.data[body.target]
if (!targetFile) {
return ctx.text("not found", 404)
}
@ -172,8 +172,8 @@ export default function (files: Files) {
}
targetFile.owner = newOwner ? newOwner.id : undefined
return files
.write()
return files.db
.save()
.then(() => ctx.text("ok", 200))
.catch(() => ctx.text("error", 500))
})
@ -184,12 +184,12 @@ export default function (files: Files) {
return ctx.text("inappropriate body", 400)
}
let targetFile = files.files[body.target]
let targetFile = files.db.data[body.target]
if (!targetFile) {
return ctx.text("not found", 404)
}
if (files.files[body.new]) {
if (files.db.data[body.new]) {
return ctx.status(400)
}
@ -197,14 +197,14 @@ export default function (files: Files) {
Accounts.files.deindex(targetFile.owner, body.target)
Accounts.files.index(targetFile.owner, body.new)
}
delete files.files[body.target]
files.files[body.new] = targetFile
delete files.db.data[body.target]
files.db.data[body.new] = targetFile
return files
.write()
return files.db
.save()
.then(() => ctx.status(200))
.catch(() => {
files.files[body.target] = body.new
files.db.data[body.target] = body.new
if (targetFile.owner) {
Accounts.files.deindex(targetFile.owner, body.new)

View file

@ -150,7 +150,7 @@ export default function (files: Files) {
)
) {
acc.defaultFileVisibility = body.defaultFileVisibility
Accounts.save()
Accounts.Db.save()
return ctx.text(
`dfv has been set to ${acc.defaultFileVisibility}`
)
@ -170,7 +170,7 @@ export default function (files: Files) {
const body = await ctx.req.json()
let accId = acc.id
auth.AuthTokens.filter((e) => e.account == accId).forEach((v) => {
auth.Db.data.filter((e) => e.account == accId).forEach((v) => {
auth.invalidate(v.token)
})
@ -187,7 +187,7 @@ export default function (files: Files) {
return writeFile(
process.cwd() + "/.data/files.json",
JSON.stringify(files.files)
JSON.stringify(files.db.data)
).then(cpl)
} else cpl()
}
@ -235,7 +235,7 @@ export default function (files: Files) {
}
acc.username = body.username
Accounts.save()
Accounts.Db.save()
if (acc.email) {
return sendMail(
@ -339,7 +339,7 @@ export default function (files: Files) {
ctx.req.param("code").toUpperCase() == vcode.code
) {
acc.email = vcode.email
Accounts.save()
Accounts.Db.save()
let e = verificationCodes.get(acc?.id || "")?.expiry
if (e) clearTimeout(e)
@ -361,7 +361,7 @@ export default function (files: Files) {
if (acc.email) {
delete acc.email
Accounts.save()
Accounts.Db.save()
return ctx.text("email detached")
} else return ServeError(ctx, 400, "email not attached")
}
@ -490,7 +490,7 @@ export default function (files: Files) {
Accounts.password.set(accId, body.password)
auth.AuthTokens.filter((e) => e.account == accId).forEach((v) => {
auth.Db.data.filter((e) => e.account == accId).forEach((v) => {
auth.invalidate(v.token)
})
@ -517,7 +517,7 @@ export default function (files: Files) {
let accId = acc.id
auth.AuthTokens.filter((e) => e.account == accId).forEach((v) => {
auth.Db.data.filter((e) => e.account == accId).forEach((v) => {
auth.invalidate(v.token)
})
@ -535,13 +535,13 @@ export default function (files: Files) {
let accId = acc.id
return ctx.json({
...acc,
sessionCount: auth.AuthTokens.filter(
sessionCount: auth.Db.data.filter(
(e) =>
e.type == "User" &&
e.account == accId &&
(e.expire == null || e.expire > Date.now())
).length,
sessionExpires: auth.AuthTokens.find(
sessionExpires: auth.Db.data.find(
(e) => e.token == sessionToken
)?.expire,
password: undefined,

View file

@ -30,7 +30,7 @@ export default function (files: Files) {
return ctx.json(
acc.files
.map((e) => {
let fp = files.files[e]
let fp = files.db.data[e]
if (!fp) {
Accounts.files.deindex(accId, e)
return null
@ -66,7 +66,7 @@ export default function (files: Files) {
body.target.forEach((e: string) => {
if (!acc.files.includes(e)) return
let fp = files.files[e]
let fp = files.db.data[e]
switch (body.action) {
case "delete":
@ -81,15 +81,15 @@ export default function (files: Files) {
)
)
return
files.files[e].visibility = body.value
files.db.data[e].visibility = body.value
modified++
break
case "setTag":
if (!body.value) delete files.files[e].tag
if (!body.value) delete files.db.data[e].tag
else {
if (body.value.toString().length > 30) return
files.files[e].tag = body.value
files.db.data[e].tag = body.value
.toString()
.toLowerCase()
}
@ -98,11 +98,11 @@ export default function (files: Files) {
}
})
return Accounts.save()
return Accounts.Db.save()
.then(() => {
writeFile(
process.cwd() + "/.data/files.json",
JSON.stringify(files.files)
JSON.stringify(files.db.data)
)
})
.then(() => ctx.text(`modified ${modified} files`))

View file

@ -38,7 +38,7 @@ type HonoEnv = {
const router = new Hono<HonoEnv>()
function getTargetToken(ctx: Context<HonoEnv, "/:token", BlankInput>) {
return auth.AuthTokens.find(
return auth.Db.data.find(
e =>
e.account == ctx.get("target").id
&& e.token == ctx.req.param("token")
@ -59,7 +59,7 @@ export default function (files: Files) {
router.get("/", async (ctx) => {
return ctx.json(
auth.AuthTokens.filter(e => e.account == ctx.get("target").id)
auth.Db.data.filter(e => e.account == ctx.get("target").id)
)
})
@ -73,7 +73,7 @@ export default function (files: Files) {
(c) => c.req.query("type")?.split(",")
),
async (ctx) => {
let targets = auth.AuthTokens.filter(
let targets = auth.Db.data.filter(
e =>
e.account == ctx.get("target").id
&& ctx.get("parsedScheme").has(e.type)

View file

@ -188,7 +188,7 @@ const validators: {
validator: (actor, target, params) => {
if (!actor.admin) return [400, "only admins can modify suspensions"]
if (params.suspension)
auth.AuthTokens
auth.Db.data
.filter(e => e.account == target.id)
.forEach(e => auth.invalidate(e.token))
return params.suspension || undefined
@ -320,7 +320,7 @@ export default function (files: Files) {
return [200, "OK"] as Message
})
await Accounts.save()
await Accounts.Db.save()
if (messages.length == 1)
return ctx.text(
@ -337,7 +337,7 @@ export default function (files: Files) {
if (actor == target && !verifyPoi(actor.id, ctx.req.query("poi")))
return ServeError(ctx, 403, "invalid proof of identity provided")
auth.AuthTokens.filter((e) => e.account == target?.id).forEach((token) => {
auth.Db.data.filter((e) => e.account == target?.id).forEach((token) => {
auth.invalidate(token.token)
})
@ -367,7 +367,7 @@ export default function (files: Files) {
auth.getPermissions(sessionToken)?.includes("email")
? acc.email
: undefined,
activeSessions: auth.AuthTokens.filter(
activeSessions: auth.Db.data.filter(
(e) =>
e.type == "User" &&
e.account == acc.id &&

View file

@ -99,7 +99,7 @@ export default function(files: Files) {
parser.on("field", async (k,v) => {
if (k == "uploadId") {
if (files.files[v] && ctx.req.method == "POST")
if (files.db.data[v] && ctx.req.method == "POST")
return file.destroy(new WebError(409, "file already exists"))
file.setUploadId(v)
// I'M GONNA KILL MYSELF!!!!

View file

@ -26,7 +26,7 @@ export default function(files: Files, apiRoot: Hono) {
let acc = ctx.get("account") as Accounts.Account
let file = files.files[fileId]
let file = files.db.data[fileId]
ctx.header("Accept-Ranges", "bytes")
ctx.header("Access-Control-Allow-Origin", "*")
ctx.header("Content-Security-Policy", "sandbox allow-scripts")

View file

@ -17,8 +17,8 @@ export default function(files: Files) {
router.get("/", async (ctx) =>
ctx.json({
version: pkg.version,
files: Object.keys(files.files).length,
totalSize: Object.values(files.files).filter(e => e.sizeInBytes).reduce((acc,cur)=>acc+cur.sizeInBytes!,0),
files: Object.keys(files.db.data).length,
totalSize: Object.values(files.db.data).filter(e => e.sizeInBytes).reduce((acc,cur)=>acc+cur.sizeInBytes!,0),
maxDiscordFiles: config.maxDiscordFiles,
maxDiscordFileSize: config.maxDiscordFileSize,
accounts: config.accounts,

View file

@ -61,7 +61,7 @@ export default function (files: Files) {
router.get("/", requiresAccount, ctx => {
let sessionToken = auth.tokenFor(ctx)
return ctx.json({
expiry: auth.AuthTokens.find(
expiry: auth.Db.data.find(
(e) => e.token == sessionToken
)?.expire,
})

View file

@ -30,7 +30,7 @@ export default function (files: Files) {
}
currentAccount.email = code.data
await Accounts.save()
await Accounts.Db.save()
return ctx.redirect('/')
} else return ServeError(ctx, 404, "code not found")

View file

@ -17,7 +17,7 @@ export default function (files: Files) {
let acc = ctx.get("account") as Accounts.Account
const fileId = ctx.req.param("fileId")
const host = ctx.req.header("Host")
const file = files.files[fileId]
const file = files.db.data[fileId]
if (file) {
if (file.visibility == "private" && acc?.id != file.owner) {
return ServeError(ctx, 403, "you do not own this file")

View file

@ -28,7 +28,7 @@ program
.alias("ls")
.description("List files in the database")
.action(() => {
Object.keys(files.files).forEach((e) => console.log(e))
Object.keys(files.db.data).forEach((e) => console.log(e))
})
program
@ -40,7 +40,7 @@ program
.action(async (id, options) => {
await new Promise<void>((resolve) => setTimeout(() => resolve(), 1000))
let fp = files.files[id]
let fp = files.db.data[id]
if (!fp) throw `file ${id} not found`