S3 file upload support
This commit is contained in:
parent
fb98314bf4
commit
778b629fde
15 changed files with 539 additions and 404 deletions
|
|
@ -1,10 +1,15 @@
|
|||
import { config as LoadDotenv } from "dotenv";
|
||||
|
||||
import { json, urlencoded } from "body-parser";
|
||||
import cors from "cors";
|
||||
import express from "express";
|
||||
import winston from "winston";
|
||||
import Routes from "./app/controllers";
|
||||
import AccessLog from "./app/middleware/AccessLog";
|
||||
import UserInjection from "./app/middleware/UserInjection";
|
||||
import { UserModel } from "./app/models/User";
|
||||
import { LOGGER } from "./app/util/Logger";
|
||||
import Multify from "./multer";
|
||||
|
||||
interface AuthenticatableRequest {
|
||||
user?: UserModel;
|
||||
|
|
@ -16,6 +21,15 @@ declare global {
|
|||
}
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
LoadDotenv();
|
||||
LOGGER.add(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.simple(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
|
|
@ -24,6 +38,8 @@ app.use(urlencoded({ extended: true }));
|
|||
app.use(UserInjection());
|
||||
app.use(AccessLog());
|
||||
|
||||
console.log("-------------------------------------------------");
|
||||
Multify(app);
|
||||
Routes.forEach(({ path, httpMethod, handler }) => {
|
||||
switch (httpMethod) {
|
||||
case "GET":
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { Request, Response } from "express";
|
||||
import Products from "../../../models/Product";
|
||||
import Product from "../../../models/Product";
|
||||
import { LOGGER } from "../../../util/Logger";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
|
|
@ -8,7 +8,7 @@ export default async (req: Request, res: Response) => {
|
|||
return res.status(403).send();
|
||||
}
|
||||
|
||||
const tmp = new Products(req.body);
|
||||
const tmp = new Product(req.body);
|
||||
await tmp.save();
|
||||
return res.status(200).json(tmp);
|
||||
} catch (ex) {
|
||||
|
|
|
|||
9
backend/src/app/models/S3FileLink.ts
Normal file
9
backend/src/app/models/S3FileLink.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Document, model, Schema } from "mongoose";
|
||||
|
||||
export type FileModel = Document & { key: string };
|
||||
|
||||
const FileSchema = new Schema<FileModel>({
|
||||
key: { type: String, required: true },
|
||||
});
|
||||
|
||||
export default model<FileModel>("files", FileSchema, "files");
|
||||
54
backend/src/multer.ts
Normal file
54
backend/src/multer.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import aws from "aws-sdk";
|
||||
import { Express } from "express";
|
||||
import moment from "moment";
|
||||
import multer from "multer";
|
||||
import multers3 from "multer-s3";
|
||||
import S3FileLink from "./app/models/S3FileLink";
|
||||
|
||||
export default (app: Express) => {
|
||||
const s3 = new aws.S3({
|
||||
region: process.env.AWS_REGION,
|
||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||
apiVersion: "2006-03-01",
|
||||
signatureVersion: "v4",
|
||||
});
|
||||
|
||||
const uploader = multer({
|
||||
storage: multers3({
|
||||
s3,
|
||||
bucket: process.env.AWS_BUCKET,
|
||||
key: function (req, file, cb) {
|
||||
cb(null, `${moment().toISOString()}-${file.originalname}`);
|
||||
},
|
||||
metadata: function (req, file, cb) {
|
||||
cb(null, { fieldName: file.fieldname });
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
// Special endpoint for the image upload
|
||||
app.post("/api/v1/fileupload", uploader.single("file"), async (req, res) => {
|
||||
const f = new S3FileLink({ key: (req.file as any).key });
|
||||
await f.save();
|
||||
return res.status(200).json({ output: f.key });
|
||||
});
|
||||
|
||||
app.get("/api/v1/fileupload/:key", async (req, res) => {
|
||||
if (!req.params.key) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
const signedUrl = await s3.getSignedUrlPromise("getObject", {
|
||||
Bucket: process.env.AWS_BUCKET,
|
||||
Key: req.params.key,
|
||||
Expires: 604800,
|
||||
});
|
||||
|
||||
return res.redirect(signedUrl);
|
||||
});
|
||||
|
||||
console.log("GET /api/v1/fileupload/:key");
|
||||
console.log("-------------------------------------------------");
|
||||
console.log("POST /api/v1/fileupload");
|
||||
};
|
||||
|
|
@ -1,18 +1,7 @@
|
|||
import { config as LoadDotenv } from "dotenv";
|
||||
import winston from "winston";
|
||||
import app from "./app";
|
||||
import { LOGGER } from "./app/util/Logger";
|
||||
import ConnectDb from "./db";
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
LoadDotenv();
|
||||
LOGGER.add(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.simple(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
ConnectDb();
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
|
|
|
|||
Loading…
Reference in a new issue