diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 9d8d1d0..fe7a416 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -204,7 +204,7 @@ export class PostsController { - // Personnal movies + // Personnal movies posts case PostKind.POST_KIND_MOVIE: const movieID = h.postInt("movieID"); @@ -216,7 +216,7 @@ export class PostsController { break; - // Web links + // Web links posts case PostKind.POST_KIND_WEBLINK: const url = h.postURL("url"); @@ -231,6 +231,24 @@ export class PostsController { break; + + + // PDF posts + case PostKind.POST_KIND_PDF: + + if(!h.hasFile("pdf")) + h.error(401, "Missing PDF in 'pdf'!"); + + const pdf_path = await h.savePostFile("pdf", "post_pdf", "pdf", "application/pdf"); + + newPost.file = new PostFile({ + path: pdf_path, + type: lookup(pathUserData(pdf_path, true)), + size: statSync(pathUserData(pdf_path, true)).size + }); + + break; + default: h.error(500, "Unsupported kind of post!"); diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 80c32b4..cca0641 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -13,6 +13,7 @@ import { checkVirtualDirectory } from "../utils/VirtualDirsUtils"; import { FriendsHelper } from "../helpers/FriendsHelper"; import { PostsHelper } from "../helpers/PostsHelper"; import { PostAccessLevel } from "./Post"; +import { writeFileSync } from "fs"; /** * Response to a request @@ -411,6 +412,32 @@ export class RequestHandler { return targetFilePath; } + /** + * Save the file uploaded by a user + * + * @param postField The name of the POST field containing the file + * @param mime_type Required mime type + * @param ext Required file extension + * @param folder The target user folder + */ + public async savePostFile(postField: string, folder: string, ext:string, mime_type: string) : Promise { + + const file = this.getUploadedFile(postField); + if(file == undefined) + this.error(400, "File '"+postField+"' is missing in the request !"); + + if(file.mimetype != mime_type) + this.error(400, "Invalid file mime type (required: " + mime_type+")") + + const targetUserDataFolder = prepareFileCreation(this.getUserId(), folder); + const targetFilePath = generateNewUserDataFileName(targetUserDataFolder, ext); + const targetSysPath = pathUserData(targetFilePath, true); + + writeFileSync(targetSysPath, file.data, "binary"); + + return targetFilePath; + } + /** * Validate API tokens *