Skip to content

Commit

Permalink
Merge pull request #250 from WilliamDavidHarrison/dev/0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tycrek authored Dec 22, 2023
2 parents 3f685ef + f53bb10 commit f0dd773
Show file tree
Hide file tree
Showing 6 changed files with 16,902 additions and 3 deletions.
5 changes: 4 additions & 1 deletion backend/UserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { UserConfiguration, UserConfigTypeChecker, PostgresConfiguration } from
import fs from 'fs-extra';
import { path } from '@tycrek/joint';
import { log } from './log.js';
import { validate } from 'william.js';

const FILEPATH = path.join('.ass-data/userconfig.json');

Expand Down Expand Up @@ -40,6 +41,7 @@ const Checkers: UserConfigTypeChecker = {
idSize: numChecker,
gfySize: numChecker,
maximumFileSize: numChecker,
discordWebhook: (val) => validate.discord.webhook(val),

s3: {
endpoint: basicStringChecker,
Expand Down Expand Up @@ -96,6 +98,7 @@ export class UserConfig {
if (!Checkers.idSize(config.idSize)) throw new Error('Invalid ID size');
if (!Checkers.gfySize(config.gfySize)) throw new Error('Invalid Gfy size');
if (!Checkers.maximumFileSize(config.maximumFileSize)) throw new Error('Invalid maximum file size');
if (!Checkers.discordWebhook(config.discordWebhook)) throw new Error('Invalid Discord webhook');

// * Optional S3 config
if (config.s3 != null) {
Expand All @@ -118,7 +121,7 @@ export class UserConfig {
if (!Checkers.sql.mySql.port(config.database.options.port)) throw new Error('Invalid database port');
if (config.database.kind == 'postgres') {
if (!Checkers.sql.postgres.port((config.database.options as PostgresConfiguration).port)) {
throw new Error("Invalid database port");
throw new Error('Invalid database port');
}
}
} else throw new Error('Database options missing');
Expand Down
15 changes: 14 additions & 1 deletion backend/routers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BusBoyFile, AssFile } from 'ass';

import axios from 'axios';
import fs from 'fs-extra';
import bb from 'express-busboy';
import crypto from 'crypto';
Expand Down Expand Up @@ -88,7 +89,19 @@ router.post('/', rateLimiterMiddleware("upload", UserConfig.config?.rateLimit?.u
data.put('files', assFile.fakeid, assFile);

log.debug('File saved to', !s3 ? assFile.save.local! : 'S3');
return res.type('json').send({ resource: `${req.ass.host}/${assFile.fakeid}` });
await res.type('json').send({ resource: `${req.ass.host}/${assFile.fakeid}` });

// Send to Discord webhook
try {
await axios.post(UserConfig.config.discordWebhook, {
body: JSON.stringify({
content: `New upload: ${req.ass.host}/${assFile.fakeid}`
})
})
} catch (err) {
log.warn('Failed to send request to Discord webhook');
console.error(err);
}
} catch (err) {
log.error('Failed to upload file', bbFile.filename);
console.error(err);
Expand Down
3 changes: 3 additions & 0 deletions backend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DateTime } from 'luxon';
import { id } from 'william.js';

export const customId = (length: number, alphabet: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') => id(length, 1, alphabet);

export const randomHexColour = () => { // From: https://www.geeksforgeeks.org/javascript-generate-random-hex-codes-color/
const letters = '0123456789ABCDEF';
Expand Down
2 changes: 2 additions & 0 deletions common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ declare module 'ass' {
idSize: number;
gfySize: number;
maximumFileSize: number;
discordWebhook: string;

s3?: S3Configuration;
database?: DatabaseConfiguration;
Expand Down Expand Up @@ -154,6 +155,7 @@ declare module 'ass' {
idSize: (val: any) => boolean;
gfySize: (val: any) => boolean;
maximumFileSize: (val: any) => boolean;
discordWebhook: (val: any) => boolean;
s3: {
endpoint: (val: any) => boolean;
bucket: (val: any) => boolean;
Expand Down
Loading

0 comments on commit f0dd773

Please sign in to comment.