From 98216ceecf205b2d7552de3b9827405e969335ef Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 13 Jul 2020 00:01:53 -0400 Subject: [PATCH] API changes and new database models w/ new utils --- src/api/loc.sh/routes/root.ts | 13 +++++++++++++ src/class/Client.ts | 6 +++--- src/class/LocalStorage.ts | 4 ++-- src/class/Moderation.ts | 2 +- src/class/RichEmbed.ts | 22 +++++++++++----------- src/class/Util.ts | 8 ++++++++ src/models/File.ts | 21 +++++++++++++++++++++ src/models/index.ts | 1 + 8 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/models/File.ts diff --git a/src/api/loc.sh/routes/root.ts b/src/api/loc.sh/routes/root.ts index 110ba2e..086d1a2 100644 --- a/src/api/loc.sh/routes/root.ts +++ b/src/api/loc.sh/routes/root.ts @@ -23,5 +23,18 @@ export default class Root extends Route { return res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR }); } }); + + this.router.get('/m/:id', async (req, res) => { + try { + const id = req.params.id.split('.')[0]; + const file = await this.server.client.db.File.findOne({ identifier: id }); + if (file.downloaded >= file.maxDownloads) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND }); + res.contentType(file.mimeType); + res.status(200).send(file.data); + return await file.updateOne({ $inc: { downloaded: 1 } }); + } catch (err) { + return this.handleError(err, res); + } + }); } } diff --git a/src/class/Client.ts b/src/class/Client.ts index 1c83c3b..8a8eabb 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -2,7 +2,7 @@ import eris from 'eris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.'; -import { Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface } from '../models'; +import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface } from '../models'; export default class Client extends eris.Client { public config: { token: string, prefix: string, guildID: string, mongoDB: string, emailPass: string, }; @@ -17,14 +17,14 @@ export default class Client extends eris.Client { public serverManagement: ServerManagement; - public db: { Member: mongoose.Model, Moderation: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, local: { muted: LocalStorage } }; + public db: { File: mongoose.Model, Member: mongoose.Model, Moderation: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, local: { muted: LocalStorage } }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); - this.db = { Member, Moderation, PagerNumber, Rank, Redirect, local: { muted: new LocalStorage('muted') } }; + this.db = { File, Member, Moderation, PagerNumber, Rank, Redirect, local: { muted: new LocalStorage('muted') } }; } public async loadDatabase() { diff --git a/src/class/LocalStorage.ts b/src/class/LocalStorage.ts index 4827d07..7203050 100644 --- a/src/class/LocalStorage.ts +++ b/src/class/LocalStorage.ts @@ -17,8 +17,8 @@ export default class LocalStorage { private locked: boolean = false; - constructor(dbName: string) { - this.storagePath = `${__dirname}/../../localstorage/${dbName}.json.gz`; + constructor(dbName: string, dir = `${__dirname}/../../localstorage`) { + this.storagePath = `${dir}/${dbName}.json.gz`; this.init(); } diff --git a/src/class/Moderation.ts b/src/class/Moderation.ts index 4b457e6..9b3f302 100644 --- a/src/class/Moderation.ts +++ b/src/class/Moderation.ts @@ -189,7 +189,7 @@ export default class Moderation { return mod.save(); } - public async kick(user: Member|User, moderator: Member, reason?: string): Promise { + public async kick(user: Member | User, moderator: Member, reason?: string): Promise { if (reason && reason.length > 512) throw new Error('Kick reason cannot be longer than 512 characters'); await this.client.guilds.get(this.client.config.guildID).kickMember(user.id, reason); const logID = randomBytes(2).toString('hex'); diff --git a/src/class/RichEmbed.ts b/src/class/RichEmbed.ts index b0e4b72..fda660e 100644 --- a/src/class/RichEmbed.ts +++ b/src/class/RichEmbed.ts @@ -45,7 +45,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the title of this embed. */ - setTitle(title: string) { + public setTitle(title: string) { if (typeof title !== 'string') throw new TypeError('RichEmbed titles must be a string.'); if (title.length > 256) throw new RangeError('RichEmbed titles may not exceed 256 characters.'); this.title = title; @@ -55,7 +55,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the description of this embed. */ - setDescription(description: string) { + public setDescription(description: string) { if (typeof description !== 'string') throw new TypeError('RichEmbed descriptions must be a string.'); if (description.length > 2048) throw new RangeError('RichEmbed descriptions may not exceed 2048 characters.'); this.description = description; @@ -65,7 +65,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the URL of this embed. */ - setURL(url: string) { + public setURL(url: string) { if (typeof url !== 'string') throw new TypeError('RichEmbed URLs must be a string.'); if (!url.startsWith('http://') && !url.startsWith('https://')) url = `https://${url}`; this.url = encodeURI(url); @@ -75,7 +75,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the color of this embed. */ - setColor(color: string | number) { + public setColor(color: string | number) { if (typeof color === 'string' || typeof color === 'number') { if (typeof color === 'string') { const regex = /[^a-f0-9]/gi; @@ -92,7 +92,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the author of this embed. */ - setAuthor(name: string, icon_url?: string, url?: string) { + public setAuthor(name: string, icon_url?: string, url?: string) { if (typeof name !== 'string') throw new TypeError('RichEmbed Author names must be a string.'); if (url && typeof url !== 'string') throw new TypeError('RichEmbed Author URLs must be a string.'); if (icon_url && typeof icon_url !== 'string') throw new TypeError('RichEmbed Author icons must be a string.'); @@ -103,7 +103,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the timestamp of this embed. */ - setTimestamp(timestamp = new Date()) { + public setTimestamp(timestamp = new Date()) { if (Number.isNaN(timestamp.getTime())) throw new TypeError('Expecting ISO8601 (Date constructor)'); this.timestamp = timestamp; return this; @@ -112,7 +112,7 @@ export default class RichEmbed implements EmbedOptions { /** * Adds a field to the embed (max 25). */ - addField(name: string, value: string, inline = false) { + public addField(name: string, value: string, inline = false) { if (typeof name !== 'string') throw new TypeError('RichEmbed Field names must be a string.'); if (typeof value !== 'string') throw new TypeError('RichEmbed Field values must be a string.'); if (typeof inline !== 'boolean') throw new TypeError('RichEmbed Field inlines must be a boolean.'); @@ -128,14 +128,14 @@ export default class RichEmbed implements EmbedOptions { /** * Convenience function for `.addField('\u200B', '\u200B', inline)`. */ - addBlankField(inline = false) { + public addBlankField(inline = false) { return this.addField('\u200B', '\u200B', inline); } /** * Set the thumbnail of this embed. */ - setThumbnail(url: string) { + public setThumbnail(url: string) { if (typeof url !== 'string') throw new TypeError('RichEmbed Thumbnail URLs must be a string.'); this.thumbnail = { url }; return this; @@ -144,7 +144,7 @@ export default class RichEmbed implements EmbedOptions { /** * Set the image of this embed. */ - setImage(url: string) { + public setImage(url: string) { if (typeof url !== 'string') throw new TypeError('RichEmbed Image URLs must be a string.'); if (!url.startsWith('http://') || !url.startsWith('https://')) url = `https://${url}`; this.image = { url }; @@ -154,7 +154,7 @@ export default class RichEmbed implements EmbedOptions { /** * Sets the footer of this embed. */ - setFooter(text: string, icon_url?: string) { + public setFooter(text: string, icon_url?: string) { if (typeof text !== 'string') throw new TypeError('RichEmbed Footers must be a string.'); if (icon_url && typeof icon_url !== 'string') throw new TypeError('RichEmbed Footer icon URLs must be a string.'); if (text.length > 2048) throw new RangeError('RichEmbed footer text may not exceed 2048 characters.'); diff --git a/src/class/Util.ts b/src/class/Util.ts index d1a096d..184e55d 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -131,6 +131,14 @@ export default class Util { return array; } + public splitArray(array: T[], count: number) { + const finalArray: T[][] = []; + while (array.length) { + finalArray.push(array.splice(0, count)); + } + return finalArray; + } + public decimalToHex(int: number): string { const hex = int.toString(16); return '#000000'.substring(0, 7 - hex.length) + hex; diff --git a/src/models/File.ts b/src/models/File.ts new file mode 100644 index 0000000..f2fa685 --- /dev/null +++ b/src/models/File.ts @@ -0,0 +1,21 @@ +import { Document, Schema, model } from 'mongoose'; + +export interface FileInterface extends Document { + name: string, + identifier: string, + mimeType: string, + data: Buffer, + downloaded: number, + maxDownloads: number, +} + +const File: Schema = new Schema({ + name: String, + identifier: String, + mimeType: String, + data: Buffer, + downloaded: Number, + maxDownloads: Number, +}); + +export default model('File', File); diff --git a/src/models/index.ts b/src/models/index.ts index c296c03..8bf8622 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,4 @@ +export { default as File, FileInterface } from './File'; export { default as Member, MemberInterface } from './Member'; export { default as Moderation, ModerationInterface } from './Moderation'; export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber';