Format files
This commit is contained in:
parent
d41547d31f
commit
175f29eeb7
12 changed files with 1102 additions and 124 deletions
|
|
@ -1,29 +1,29 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Pastes from '../../db/txt.schema'
|
||||
import moment from 'moment-timezone'
|
||||
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)
|
||||
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 (paste) {
|
||||
// Check for an expired link
|
||||
if (moment().isAfter(moment(paste.expiresAt))) {
|
||||
await Pastes.findByIdAndDelete(paste._id);
|
||||
return res.status(404).send("PASTE EXPIRED");
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
import { Request, Response } from 'express'
|
||||
import Pastes from '../../db/txt.schema'
|
||||
import moment from 'moment-timezone'
|
||||
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(req.body.expiresAt).toDate();
|
||||
await paste.save()
|
||||
try {
|
||||
const paste = new Pastes();
|
||||
paste.paste = req.body.paste;
|
||||
paste.syntax = req.body.syntax;
|
||||
paste.expiresAt = moment(req.body.expiresAt).toDate();
|
||||
await paste.save();
|
||||
|
||||
return res.status(200).send(paste._id)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
}
|
||||
return res.status(200).send(paste._id);
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
return res.status(500).send({
|
||||
error: ex,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
import { Request, Response } from 'express'
|
||||
import URLs from '../../db/url.schema'
|
||||
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)
|
||||
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
|
||||
})
|
||||
if (url) {
|
||||
res.redirect(url.url);
|
||||
} else {
|
||||
res.status(404).send();
|
||||
}
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
return res.status(500).send({
|
||||
error: ex,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,25 +1,25 @@
|
|||
import { Request, Response } from 'express'
|
||||
import URLs from '../../db/url.schema'
|
||||
import { Request, Response } from "express";
|
||||
import URLs from "../../db/url.schema";
|
||||
|
||||
const isUrlValid = (url: string) => {
|
||||
return url.startsWith("http") || url.startsWith("www")
|
||||
}
|
||||
return url.startsWith("http") || url.startsWith("www");
|
||||
};
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!isUrlValid(req.body.url)) {
|
||||
return res.status(422).send("INVALID URL")
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
try {
|
||||
if (!isUrlValid(req.body.url)) {
|
||||
return res.status(422).send("INVALID URL");
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,31 +1,34 @@
|
|||
import { Schema, model, Document } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
import { Schema, model, Document } from "mongoose";
|
||||
import shortid from "shortid";
|
||||
|
||||
const PasteSchema = new Schema({
|
||||
const PasteSchema = new Schema(
|
||||
{
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
type: String,
|
||||
default: shortid.generate,
|
||||
},
|
||||
paste: { type: String },
|
||||
syntax: {
|
||||
type: String,
|
||||
default: null
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
expiresAt: {
|
||||
type: Date,
|
||||
expires: 0,
|
||||
default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)
|
||||
type: Date,
|
||||
expires: 0,
|
||||
default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
|
||||
},
|
||||
}, {
|
||||
timestamps: true
|
||||
})
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
export type PasteModel = Document & {
|
||||
_id: string,
|
||||
paste: string,
|
||||
syntax: string,
|
||||
expiresAt: Date,
|
||||
timestamps: Date
|
||||
}
|
||||
_id: string;
|
||||
paste: string;
|
||||
syntax: string;
|
||||
expiresAt: Date;
|
||||
timestamps: Date;
|
||||
};
|
||||
|
||||
export default model<PasteModel>('Paste', PasteSchema)
|
||||
export default model<PasteModel>("Paste", PasteSchema);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
import mongoose, { Document, Schema, Types } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
import mongoose, { Document, Schema, Types } from "mongoose";
|
||||
import shortid from "shortid";
|
||||
|
||||
const UrlSchema: Schema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
url: String
|
||||
})
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate,
|
||||
},
|
||||
url: String,
|
||||
});
|
||||
|
||||
export type UrlModel = Document & {
|
||||
_id: string,
|
||||
url: string
|
||||
}
|
||||
_id: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
export default mongoose.model<UrlModel>('urls', UrlSchema, 'urls')
|
||||
export default mongoose.model<UrlModel>("urls", UrlSchema, "urls");
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import mongoose from 'mongoose'
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export default async () => {
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Utils`, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useCreateIndex: true
|
||||
})
|
||||
}
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Utils`, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useCreateIndex: true,
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { config as LoadDotenv } from 'dotenv'
|
||||
import app from './app'
|
||||
import connectDB from './db'
|
||||
import { config as LoadDotenv } from "dotenv";
|
||||
import app from "./app";
|
||||
import connectDB from "./db";
|
||||
|
||||
if(process.env.NODE_ENV !== "production") {
|
||||
LoadDotenv()
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
LoadDotenv();
|
||||
}
|
||||
|
||||
connectDB()
|
||||
connectDB();
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.listen(port, () => console.log(`Listening at http://127.0.0.1:${port}`))
|
||||
app.listen(port, () => console.log(`Listening at http://127.0.0.1:${port}`));
|
||||
|
|
|
|||
Loading…
Reference in a new issue