From 43a429e1067a01ac99a68c4f7cbae79994c1f3d0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 4 Jan 2020 18:45:29 +0100 Subject: [PATCH] Add support for image posts --- package-lock.json | 5 +++++ package.json | 1 + src/controllers/PostsController.ts | 29 ++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 5d3b714..e9d3517 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59,6 +59,11 @@ "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==", "dev": true }, + "@types/mime-types": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", + "integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=" + }, "@types/mysql": { "version": "2.15.8", "resolved": "https://registry.npmjs.org/@types/mysql/-/mysql-2.15.8.tgz", diff --git a/package.json b/package.json index 0248904..4e8fba8 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "", "license": "MIT", "dependencies": { + "@types/mime-types": "^2.1.0", "express": "^4.17.1", "express-fileupload": "^1.1.6", "mysql": "^2.17.1", diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 41bee1b..43bf2ef 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -1,7 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; import { PostsHelper } from "../helpers/PostsHelper"; -import { Post, PostVisibilityLevel, PostKind, PostAccessLevel, PostPageKind } from "../entities/Post"; +import { Post, PostVisibilityLevel, PostKind, PostAccessLevel, PostPageKind, PostFile } from "../entities/Post"; import { MoviesController } from "./MoviesController"; import { MoviesHelper } from "../helpers/MoviesHelper"; import { SurveyHelper } from "../helpers/SurveyHelper"; @@ -14,6 +14,9 @@ import { GroupsHelper } from "../helpers/GroupsHelper"; import { time } from "../utils/DateUtils"; import { findKey } from "../utils/ArrayUtils"; import { check_string_before_insert } from "../utils/StringUtils"; +import { pathUserData } from "../utils/UserDataUtils"; +import { statSync } from "fs"; +import { lookup } from "mime-types"; /** * Posts controller @@ -160,6 +163,30 @@ export class PostsController { h.error(400, "Specified post content is invalid!"); break; + + + + + // Image posts + case PostKind.POST_KIND_IMAGE: + + if(!h.hasFile("image")) + h.error(400, "An error occured while receiving the image!"); + + // Save image + const path = await h.savePostImage("image", "imgpost", 2000, 2000); + + newPost.file = new PostFile({ + path: path, + type: lookup(pathUserData(path, true)), + size: statSync(pathUserData(path, true)).size + }); + + break; + + + + default: h.error(500, "Unsupported kind of post!");