--
This commit is contained in:
parent
3cd5d356cb
commit
25f973e2b3
37 changed files with 812 additions and 470 deletions
20
backend/src/app/controllers/img/getImageHandler.ts
Normal file
20
backend/src/app/controllers/img/getImageHandler.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Images from '../../db/img.schema'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const img = await Images.findById(req.params.slug)
|
||||
|
||||
if (img) {
|
||||
res.contentType(img.img.contentType)
|
||||
res.send(img.img.data)
|
||||
} else {
|
||||
res.status(404).end()
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
37
backend/src/app/controllers/img/putImageHandler.ts
Normal file
37
backend/src/app/controllers/img/putImageHandler.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Images from '../../db/img.schema'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const data = fs.readFileSync(req.file.path)
|
||||
fs.unlinkSync(req.file.path)
|
||||
|
||||
const images = await Images.find()
|
||||
|
||||
for (const img of images) {
|
||||
if (img.img.data.compare(data) === 0) {
|
||||
/* If some other IP uploads, we want to add them too */
|
||||
img.uploaders.push(req.clientIp as string)
|
||||
await img.save()
|
||||
return res.status(200).send(img._id);
|
||||
}
|
||||
}
|
||||
|
||||
const img = new Images()
|
||||
|
||||
img.img.contentType = req.file.mimetype
|
||||
img.img.data = data
|
||||
img.uploaders = req.clientIp ? [req.clientIp as string] : []
|
||||
|
||||
await img.save()
|
||||
|
||||
return res.status(200).send(img._id);
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
34
backend/src/app/controllers/txt/getTxtHandler.ts
Normal file
34
backend/src/app/controllers/txt/getTxtHandler.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Pastes from '../../db/txt.schema'
|
||||
import moment from 'moment-timezone'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const paste = await Pastes.findById(req.params.slug)
|
||||
|
||||
if (paste) {
|
||||
// Check for an expired link
|
||||
if (moment().isAfter(moment(paste.expiresAt))) {
|
||||
await Pastes.findByIdAndDelete(paste._id)
|
||||
return res.status(404).send("PASTE_EXPIRED")
|
||||
}
|
||||
|
||||
if (req.query && paste.syntax === null) {
|
||||
paste.syntax = Object.keys(req.query)[0]
|
||||
await paste.save()
|
||||
}
|
||||
|
||||
return res.json({
|
||||
paste: paste.paste,
|
||||
syntax: paste.syntax
|
||||
})
|
||||
} else {
|
||||
return res.status(404).end()
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
20
backend/src/app/controllers/txt/putTxtHandler.ts
Normal file
20
backend/src/app/controllers/txt/putTxtHandler.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Pastes from '../../db/txt.schema'
|
||||
import moment from 'moment-timezone'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const paste = new Pastes()
|
||||
paste.paste = req.body.paste
|
||||
paste.syntax = req.body.syntax
|
||||
paste.expiresAt = moment().add(req.body.expiry).toDate()
|
||||
await paste.save()
|
||||
|
||||
return res.status(200).send(paste._id)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
19
backend/src/app/controllers/url/getUrlHandler.ts
Normal file
19
backend/src/app/controllers/url/getUrlHandler.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { Request, Response } from 'express'
|
||||
import URLs from '../../db/url.schema'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const url = await URLs.findById(req.params.slug)
|
||||
|
||||
if (url) {
|
||||
res.redirect(url.url);
|
||||
} else {
|
||||
res.status(404).send()
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
17
backend/src/app/controllers/url/putUrlHandler.ts
Normal file
17
backend/src/app/controllers/url/putUrlHandler.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { Request, Response } from 'express'
|
||||
import URLs from '../../db/url.schema'
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const url = new URLs()
|
||||
url.url = req.body.url
|
||||
await url.save()
|
||||
|
||||
return res.status(200).send(url._id);
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import moment from 'moment'
|
||||
import { Document, model, Schema } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const ImgSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: moment().toISOString()
|
||||
},
|
||||
uploaders: [String],
|
||||
img: {
|
||||
contentType: String,
|
||||
data: Buffer,
|
||||
}
|
||||
})
|
||||
|
||||
export type ImgModel = Document & {
|
||||
_id: string,
|
||||
createdAt: Date,
|
||||
uploaders: string[],
|
||||
img: {
|
||||
contentType: string,
|
||||
data: Buffer
|
||||
}
|
||||
}
|
||||
|
||||
export default model<ImgModel>("images", ImgSchema, "images")
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import { Schema, model, Document } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const PasteSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
paste: { type: String },
|
||||
syntax: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
expiresAt: {
|
||||
type: Date,
|
||||
expires: 0,
|
||||
default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)
|
||||
},
|
||||
}, {
|
||||
timestamps: true
|
||||
})
|
||||
|
||||
export type PasteModel = Document & {
|
||||
_id: string,
|
||||
paste: string,
|
||||
syntax: string,
|
||||
expiresAt: Date,
|
||||
timestamps: Date
|
||||
}
|
||||
|
||||
export default model<PasteModel>('Paste', PasteSchema)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import mongoose, { Document, Schema, Types } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const UrlSchema: Schema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
url: String
|
||||
})
|
||||
|
||||
export type UrlModel = Document & {
|
||||
_id: string,
|
||||
url: string
|
||||
}
|
||||
|
||||
export default mongoose.model<UrlModel>('urls', UrlSchema, 'urls')
|
||||
Loading…
Reference in a new issue