1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-26 07:19:23 +00:00

Add PDF support

This commit is contained in:
Pierre HUBERT 2020-03-20 12:10:48 +01:00
parent 0a1626dc47
commit 5c8c3be9ae
2 changed files with 47 additions and 2 deletions

View File

@ -204,7 +204,7 @@ export class PostsController {
// Personnal movies // Personnal movies posts
case PostKind.POST_KIND_MOVIE: case PostKind.POST_KIND_MOVIE:
const movieID = h.postInt("movieID"); const movieID = h.postInt("movieID");
@ -216,7 +216,7 @@ export class PostsController {
break; break;
// Web links // Web links posts
case PostKind.POST_KIND_WEBLINK: case PostKind.POST_KIND_WEBLINK:
const url = h.postURL("url"); const url = h.postURL("url");
@ -232,6 +232,24 @@ export class PostsController {
break; 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: <string>lookup(pathUserData(pdf_path, true)),
size: statSync(pathUserData(pdf_path, true)).size
});
break;
default: default:
h.error(500, "Unsupported kind of post!"); h.error(500, "Unsupported kind of post!");
} }

View File

@ -13,6 +13,7 @@ import { checkVirtualDirectory } from "../utils/VirtualDirsUtils";
import { FriendsHelper } from "../helpers/FriendsHelper"; import { FriendsHelper } from "../helpers/FriendsHelper";
import { PostsHelper } from "../helpers/PostsHelper"; import { PostsHelper } from "../helpers/PostsHelper";
import { PostAccessLevel } from "./Post"; import { PostAccessLevel } from "./Post";
import { writeFileSync } from "fs";
/** /**
* Response to a request * Response to a request
@ -411,6 +412,32 @@ export class RequestHandler {
return targetFilePath; 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<string> {
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 * Validate API tokens
* *