Skip to content

Commit

Permalink
Merge pull request #36 from Vetlix/master
Browse files Browse the repository at this point in the history
make code go more brrrr
  • Loading branch information
diced authored Sep 29, 2020
2 parents c1c096c + d0615d6 commit 7d68dc7
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 51 deletions.
18 changes: 13 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typex",
"version": "2.1.3",
"version": "2.1.4",
"scripts": {
"build": "tsc -p .",
"start": "node out/src"
Expand All @@ -9,20 +9,21 @@
"@ayanaware/logger": "^2.2.1",
"@overnightjs/core": "^1.6.15",
"@types/bcrypt": "^3.0.0",
"@types/centra": "^2.2.0",
"@types/cookie-parser": "^1.4.2",
"@types/express-session": "^1.17.0",
"@types/mime": "^2.0.1",
"@types/multer": "^1.4.3",
"@types/semver": "^7.3.1",
"bcrypt": "^5.0.0",
"centra": "^2.4.2",
"cookie-parser": "^1.4.5",
"ejs": "^3.0.2",
"express": "^4.17.1",
"express-session": "^1.17.1",
"http-status-codes": "^1.4.0",
"mime": "^2.4.4",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
"semver": "^7.3.2",
"spectre.css": "^0.5.8",
"typeorm": "^0.2.24"
Expand Down
1 change: 0 additions & 1 deletion src/controllers/APIController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Request, Response } from 'express';
import { ORMHandler } from '..';
import { randomId, getImage, findFile, getShorten, hashPassword } from '../util';
import { createReadStream, createWriteStream, unlinkSync, existsSync, mkdirSync, readFileSync } from 'fs'
import { getExtension } from 'mime';
import { User } from '../entities/User';
import { sep } from 'path';
import { cookiesForAPI } from '../middleware/cookiesForAPI';
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Image {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Note.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Note {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Shorten.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Shorten {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { randomId, findFile } from "../util";
import Logger from "@ayanaware/logger";
import { readFileSync } from 'fs';
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import "./core/Console";
import {
Repository,
Connection,
createConnection,
ConnectionOptions
createConnection
} from "typeorm";
import { User } from "./entities/User";
import { ZiplineServer } from "./server";
Expand All @@ -17,7 +16,6 @@ import { notes } from "./interval";
import { GitHub } from "./structures/GitHub";
import { compare, } from 'semver';
import chalk from 'chalk';
import { execFileSync } from 'child_process';

if (!findFile('config.json', process.cwd())) {
Logger.get('FS').error(`No config.json exists in ${__dirname}, exiting...`)
Expand Down Expand Up @@ -59,6 +57,7 @@ const pk = JSON.parse(readFileSync(findFile('package.json', process.cwd()), 'utf
note: connection.getRepository(Note)
},
};

if (orm.connection.isConnected)
Logger.get(Connection).info(
`Successfully initialized database type: ${config.database.type}`
Expand Down
2 changes: 0 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as bodyParser from "body-parser";
import { Server } from "@overnightjs/core";
import { Connection } from "typeorm";
import { ORMHandler } from ".";
import Logger from "@ayanaware/logger";
import * as express from "express";
Expand All @@ -12,7 +11,6 @@ import cookies from "cookie-parser";
import { APIController } from "./controllers/APIController";
import { IndexController } from "./controllers/IndexController";
import { findFile } from "./util";
import { ImageUtil } from "./structures/ImageUtil";

if (!findFile('config.json', process.cwd())) {
Logger.get('FS').error(`No config.json exists in the ${__dirname}, exiting...`)
Expand Down
58 changes: 26 additions & 32 deletions src/structures/DiscordWebhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'node-fetch';
import req from 'centra';
import { User } from '../entities/User';
import { Shorten } from '../entities/Shorten';
import { Imaged } from './ImageUtil';
Expand All @@ -11,44 +11,38 @@ export class DiscordWebhook {
this.checkExists();
}
async checkExists(): Promise<boolean> {
const json = await (await fetch(this.url)).json();
const json = await (await req(this.url).send()).json();
if (json.code === 10015) throw new Error('Unknown Webhook')
else if (json.code === 50027) throw new Error('Invalid Webhook Token')
else if (json.code) throw new Error(`DiscordAPIError[${json.code}]: ${json.message}`);
return json.code ? false : true;
}
async sendImageUpdate(user: User, image: Imaged, config: any) {
try {
await (await fetch(this.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: config.discordWebhook.username,
avatar_url: config.discordWebhook.avatarURL,
content: `New image uploaded to <${image.origin}> by ${user.username} (${user.id}). ${image.url})`
})
}));
} catch (e) {
throw new Error(`Coulndn't send webhook: ${e.message}`)
}
const res = await req(this.url, 'POST')
.header({
'Content-Type': 'application/json'
})
.body({
user: config.discordWebhook.username,
avatar_url: config.discordWebhook.avatarURL,
content: `New image uploaded to <${image.origin}> by ${user.username} (${user.id}). ${image.url})`
})
.send();

if (res.statusCode !== 200) throw new Error(`Couldn't send webhook. (Status: ${res.statusCode})`);
}
async sendShortenUpdate(user: User, shorten: Shorten, ex: Shortened, config: any) {
try {
await (await fetch(this.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: config.discordWebhook.username,
avatar_url: config.discordWebhook.avatarURL,
content: `New shortened url added to <${ex.origin}> by ${user.username} (${user.id}). <${shorten.origin}> -> <${shorten.url}>`
})
}));
} catch (e) {
throw new Error(`Coulndn't send webhook: ${e.message}`)
}
const res = await req(this.url, 'POST')
.header({
'Content-Type': 'application/json'
})
.body({
user: config.discordWebhook.username,
avatar_url: config.discordWebhook.avatarURL,
content: `New shortened url added to <${ex.origin}> by ${user.username} (${user.id}). <${shorten.origin}> -> <${shorten.url}>`
})
.send();

if (res.statusCode !== 200) throw new Error(`Couldn't send webhook. (Status: ${res.statusCode})`);
}
}
4 changes: 2 additions & 2 deletions src/structures/GitHub.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fetch from 'node-fetch';
import req from 'centra';

export class GitHub {
public static async getFile(filePath: string) {
const res = await fetch('https://raw.githubusercontent.com/ZiplineProject/Zipline/master/' + filePath);
const res = await req(`https://raw.githubusercontent.com/ZiplineProject/Zipline/master/${filePath}`).send();
return res.text();
}
}

0 comments on commit 7d68dc7

Please sign in to comment.