NodeJS 有没有一种方法来计算一个pdf文件的页数使用multer上传?

mbskvtky  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(173)

我正在开发一个功能来定义要上传的PDF文件的限制大小。这部分是可以的,但我还需要定义一个限制多少页的pdf文件,我找不到任何答案。我正在使用NestJS,到目前为止,我的代码块看起来像这样:

@UseInterceptors(
FilesInterceptor('files', 10, {
  limits: { fileSize: 10242880 },
  fileFilter: (req, file, callback) => {
    if (!file.originalname.match(/\.(jpg|jpeg|png|pdf)$/)) {
      req.fileValidationError =
        'Invalid file type provided. Valid types: [jpg, jpeg, png, pdf]';
      //TODO: Add validation to check if the file is pdf, and if so, define a limit of pages for the file. (max * pages)
      callback(null, false);
    }

    const fileSize = parseInt(req.headers['content-length']);
    if (fileSize > 10242880) {
      req.fileValidationError = 'File size exceeds the maximum limit [10mb]';
      callback(null, false);
    }
    callback(null, true);
    if (!file.originalname.match(/\.(pdf)$/)) {
      //what to do here??
      
    }
  },
})

字符串
界面中可用的多个选项如下。也许他们中的任何一个人都能给我提供这样的信息,但我真的很感激有人能帮我一把。谢啦,谢啦

export interface MulterOptions {
dest?: string;
/** The storage engine to use for uploaded files. */
storage?: any;
/**
 * An object specifying the size limits of the following optional properties. This object is passed to busboy
 * directly, and the details of properties can be found on https://github.com/mscdex/busboy#busboy-methods
 */
limits?: {
    /** Max field name size (Default: 100 bytes) */
    fieldNameSize?: number;
    /** Max field value size (Default: 1MB) */
    fieldSize?: number;
    /** Max number of non- file fields (Default: Infinity) */
    fields?: number;
    /** For multipart forms, the max file size (in bytes)(Default: Infinity) */
    fileSize?: number;
    /** For multipart forms, the max number of file fields (Default: Infinity) */
    files?: number;
    /** For multipart forms, the max number of parts (fields + files)(Default: Infinity) */
    parts?: number;
    /** For multipart forms, the max number of header key=> value pairs to parse Default: 2000(same as node's http). */
    headerPairs?: number;
};
/** Keep the full path of files instead of just the base name (Default: false) */
preservePath?: boolean;
fileFilter?(req: any, file: {
    /** Field name specified in the form */
    fieldname: string;
    /** Name of the file on the user's computer */
    originalname: string;
    /** Encoding type of the file */
    encoding: string;
    /** Mime type of the file */
    mimetype: string;
    /** Size of the file in bytes */
    size: number;
    /** The folder to which the file has been saved (DiskStorage) */
    destination: string;
    /** The name of the file within the destination (DiskStorage) */
    filename: string;
    /** Location of the uploaded file (DiskStorage) */
    path: string;
    /** A Buffer of the entire file (MemoryStorage) */
    buffer: Buffer;
}, callback: (error: Error | null, acceptFile: boolean) => void): void;


}

0h4hbjxa

0h4hbjxa1#

不,穆特一个人不会数“不”。因为它只是一个“multipart/form-data”解析器。
您需要使用PDF解析器来计数否。关于pdf-lib,请参阅example
顺便说一句,我有一些建议,以您的多重验证码。
1.你应该验证mime类型的文件,而不是它的文件扩展名。
1.通过content-length头确定文件大小是一个非常糟糕的主意,因为它可以被最终用户(使用curl或netcat)操纵。使用fileFilter args提供的file.size。由于您已经设置了limits.fileSize选项,因此在这里验证文件大小变得毫无意义。

const INVALID_PDF_ERROR_MSG = 'Invalid file type provided. Valid types: [jpg, jpeg, png, pdf]'

FilesInterceptor('files', 10, {
  limits: {
    fileSize: 10 * 1024 * 1024, // 10Mb in bytes
  },
  fileFilter: (req, file, callback) => {
    if (file.mime !== 'application/pdf') {
      req.fileValidationError = INVALID_PDF_ERROR_MSG
      callback(null, false) // ```callback(new Error(INVALID_PDF_ERROR_MSG), false)``` is right way to create errors but I leave it you
      return
    }
    // // point less since limits.fileSize is provided
    // if (file.size > 10242880) callback(new Error('File size exceeds the maximum limit [10mb]'), false)

    // I recommend not counting pages here, instead do inside of route handler.
    callback(null, true)
  },
})

字符串

相关问题