Skip to content
Snippets Groups Projects
Commit 87aaab15 authored by Rafael László's avatar Rafael László :speech_balloon:
Browse files

custom card background image

parent 5f5c6314
No related branches found
No related tags found
3 merge requests!24Auth, Profile, News, Entry Card, File management,!21update feature/news_api branch,!19Profile and Entry Card
Pipeline #5598 passed
import multer from "multer";
import path from "path";
export const cardImageStorage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "uploads/card_image/");
},
filename: function (req, file, callback) {
callback(null, Date.now() + "-" + file.originalname);
},
});
export default cardImageStorage;
import { NextFunction, Request, Response } from "express";
import CardImage from "../../../models/CardImageSchema";
import File from "../../../models/FileSchema";
import Profile from "../../../models/ProfileSchema";
import fs from "fs";
const getCardImage = () => async (
req: Request,
res: Response,
next: NextFunction
) => {
CardImage.findOne({}, async (error, cardImage) => {
if (error) {
console.warn(error);
res.status(400);
} else {
if (!!cardImage) {
const file = await File.findById(cardImage!.imageId).exec();
res.status(200);
res.data.cardImage = cardImage;
}
next();
}
});
};
export default getCardImage;
import { NextFunction, Request, Response } from "express";
import Card from "../../models/CardSchema";
import File from "../../models/FileSchema";
import Profile from "../../models/ProfileSchema";
import Card from "../../../models/CardSchema";
import File from "../../../models/FileSchema";
import Profile from "../../../models/ProfileSchema";
import fs from "fs";
const getUserCard = () => async (
......@@ -26,9 +26,16 @@ const getUserCard = () => async (
"base64"
);
const bgPicture =
let bgPicture;
if (!res.data.cardImage)
bgPicture =
"data:image/png;base64," +
fs.readFileSync("src/utils/card/background.png", "base64");
else {
const bgFile = await File.findById(res.data.cardImage.imageId).exec();
bgPicture =
"data:image/png;base64," + fs.readFileSync(bgFile!.path, "base64");
}
const svg = fs.readFileSync("src/utils/card/template.svg", "utf8");
......
import { NextFunction, Request, Response, response } from "express";
import File from "../../../models/FileSchema";
const responseCardImage = () => async (req: Request, res: Response) => {
if (!res.data.cardImage) {
res.status(404).json({ message: "Card Image not found" });
} else {
const cardImage = await File.findById(res.data.cardImage.imageId).exec();
return res.sendFile(cardImage!.path, { root: "." });
}
};
export default responseCardImage;
import { NextFunction, Request, Response } from "express";
import CardImage from "../../../models/CardImageSchema";
import File from "../../../models/FileSchema";
import Profile from "../../../models/ProfileSchema";
import { fileFilter } from "../fileFilter";
import fs from "fs";
const uploadCardImage = () => async (
req: Request,
res: Response,
next: NextFunction
) => {
const uploadedFile = req.file;
CardImage.findOne({}, async (error, cardImage) => {
if (error) {
console.warn(error);
res.status(400);
} else {
if (!!cardImage) {
const oldFile = await File.findById(cardImage.imageId).exec();
fs.unlink(oldFile!.path, (err) => {
if (err) {
console.error(err);
}
});
await File.deleteOne({ _id: oldFile?.id }).exec();
}
const newFile = new File();
newFile.originalName = uploadedFile.originalname;
newFile.uploadDate = new Date();
newFile.path = uploadedFile.path;
newFile.mimeType = uploadedFile.mimetype;
await newFile.save((err) => {
if (err) {
return res.status(400);
}
});
if (!cardImage) {
const cardImage = new CardImage();
cardImage.imageId = newFile.id;
await cardImage.save((err) => {
if (err) return res.status(400);
});
} else {
await CardImage.updateOne(
{ _id: cardImage?.id },
{ imageId: newFile.id }
).exec();
}
return res.status(200).send(true);
}
});
};
export default uploadCardImage;
......@@ -3,13 +3,14 @@ import { NextFunction, Request, Response } from "express";
import Profile from "../../models/ProfileSchema";
const getUser = () => (req: Request, res: Response, next: NextFunction) => {
console.log(res);
Profile.findById(req.params.id, (error, profile) => {
if (error) {
console.warn(error);
res.status(400);
} else {
res.status(200);
res.data = { profile };
res.data.profile = profile;
}
next();
});
......
import { Document, Schema, model } from "mongoose";
export interface ICardImage extends Document {
imageId: Number;
}
const CardImageSchema = new Schema({
imageId: { type: Schema.Types.ObjectId, ref: "File", required: false },
});
export default model<ICardImage>("CardImage", CardImageSchema);
import { Application } from "express";
import authenticated from "../middlewares/auth/authenticated";
import cardImageStorage from "../middlewares/files/card/cardImageStorage";
import { fileFilter } from "../middlewares/files/fileFilter";
import getOwnProfilePicture from "../middlewares/files/getOwnProfilePicture";
import getCardImage from "../middlewares/files/card/getCardImage";
import getOwnProfilePicture from "../middlewares/files/profile/getOwnProfilePicture";
import handleFileValidationError from "../middlewares/files/handleFileValidationError";
import multer from "multer";
import profilePictureStorage from "../middlewares/files/profilePictureStorage";
import uploadProfilePicture from "../middlewares/files/uploadProfilePicture";
import profilePictureStorage from "../middlewares/files/profile/profilePictureStorage";
import responseCardImage from "../middlewares/files/card/responseCardImage";
import uploadCardImage from "../middlewares/files/card/uploadCardImage";
import uploadProfilePicture from "../middlewares/files/profile/uploadProfilePicture";
const profilePictureUpload = multer({
storage: profilePictureStorage,
fileFilter,
});
const CardImageUpload = multer({
storage: cardImageStorage,
fileFilter,
});
const fileRoute = (app: Application): void => {
app.post(
"/api/v1/files/profile",
......@@ -21,6 +30,20 @@ const fileRoute = (app: Application): void => {
uploadProfilePicture()
);
app.get("/api/v1/files/profile", authenticated(), getOwnProfilePicture());
app.post(
"/api/v1/files/card",
authenticated(),
CardImageUpload.single("card_image"),
handleFileValidationError(),
uploadCardImage()
);
app.get(
"/api/v1/files/card",
authenticated(),
getCardImage(),
responseCardImage()
);
};
export default fileRoute;
......@@ -5,9 +5,10 @@ import authenticated from "../middlewares/auth/authenticated";
import deleteUser from "../middlewares/user/deleteUser";
import deleteWarning from "../middlewares/user/deleteWarning";
import emptyResponse from "../middlewares/utils/emptyResponse";
import getCardImage from "../middlewares/files/card/getCardImage";
import getOwnUser from "../middlewares/user/getOwnUser";
import getUser from "../middlewares/user/getUser";
import getUserCard from "../middlewares/files/getUserCard";
import getUserCard from "../middlewares/files/card/getUserCard";
import getUsersList from "../middlewares/user/getUsersList";
import getWarning from "../middlewares/user/getWarning";
import getWarningsList from "../middlewares/user/getWarningsList";
......@@ -27,12 +28,19 @@ const usersRoute = (app: Application): void => {
"/api/v1/users/me/card",
authenticated(),
getOwnUser(),
getCardImage(),
getUserCard()
);
app.get("/api/v1/users/:id", getUser(), responseUser());
app.get("/api/v1/users/:id/card", authenticated(), getUser(), getUserCard());
app.get(
"/api/v1/users/:id/card",
authenticated(),
getUser(),
getCardImage(),
getUserCard()
);
app.get("/api/v1/users/:id/warnings", getWarningsList());
......
......@@ -2,6 +2,7 @@ import { INews } from "../../models/NewsSchema";
import { IProfile } from "../../models/ProfileSchema";
import { IFile } from "../../models/FileSchema";
import { ICard } from "../../models/CardSchema";
import { ICardImage } from "../../models/CardImageSchema";
declare global {
namespace Express {
......@@ -12,6 +13,7 @@ declare global {
profile?: IProfile | null;
card?: ICard | null;
cards?: ICard[] | null;
cardImage?: ICardImage | null;
profiles?: IProfile[] | null;
error?: string | null;
files?: IFile[] | null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment