Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在保存ZIP文件之前解压缩它';穆特的内容_Node.js_Multer - Fatal编程技术网

Node.js 在保存ZIP文件之前解压缩它';穆特的内容

Node.js 在保存ZIP文件之前解压缩它';穆特的内容,node.js,multer,Node.js,Multer,我有一个类似于RESTAPI的express,并在multer中启用了文件上传 我的文件上传相关逻辑都在multer中解决,我希望保持这种方式。 最近,一个新的文件类型将被添加到此端点(.zip),但我真的不想保存它。我希望能够重新使用multer已经实现的逻辑,但添加了一个先例步骤,将文件传递给multer,以便它可以保存它们并像往常一样完成流程 到目前为止,我所拥有的: routes.post( '/upload', multer(multerConfig).single('file

我有一个类似于RESTAPI的express,并在multer中启用了文件上传

我的文件上传相关逻辑都在multer中解决,我希望保持这种方式。 最近,一个新的文件类型将被添加到此端点(.zip),但我真的不想保存它。我希望能够重新使用multer已经实现的逻辑,但添加了一个先例步骤,将文件传递给multer,以便它可以保存它们并像往常一样完成流程

到目前为止,我所拥有的:

routes.post(
  '/upload',
  multer(multerConfig).single('file'),
  async (req, res) => {
    const { path: filePath } = req.file

    const file = xlsx.readFile(filePath)
    const cells = Object.values(file.Sheets).reduce((accumulator, sheet) => {
      const sheetCellsKeys = Object.keys(sheet)

      const sortedCellsKeys = sheetCellsKeys
        .sort((previousCell, currentCell) => (
          previousCell.localeCompare(currentCell, 'en', { numeric: true })
        ))

      const validCellsKeys = sortedCellsKeys.filter((cellKey) => (
        /^[A-Z]+[0-9]+$/.test(cellKey)
      ))

      const grouppedCellsKeys = groupBy(validCellsKeys, (cellKey) => (
        cellKey.replace(/\D/g, '')
      ))

      return [
        ...accumulator,
        ...Object.values(grouppedCellsKeys)
          .reduce((cellsAccumulator, cellsKeys) => ([
            ...cellsAccumulator,
            cellsKeys.map((cellKey) => (
              sheet[cellKey].v
            )),
          ]), []),
      ]
    }, [])

    res.send(cells)
  },
)

我想解压缩我的zip文件,并将其传递给multer,就像它的内容被发送到/process端点一样。这可能吗?

我不知道如何更新multer的值。但在这种情况下,我将创建一个中间件函数,并在multer middleware的后面设置它,在这个中间件中我将提取zip文件,并将zip文件的内容保存到存储文件夹,最后通过
req.file
对象将新的文件路径传递给下一个进程(因为在最后一个进程中,您只需要excel文件的路径)

下面的代码只是我想法的一个示例:

新中间件

const unzipMiddleware = (req, res, next) => {
  const { path: zipFilePath, mimetype } = req.file // zipFilePath is './public/uploads/file.zip'
  if (mimetype !== 'application/gzip') { // You would want to check that, I think so
    return next() // do nothing
  }
  // get content of the zipFilePath [1.xlsx, 2.xlsx, 3.xlsx,....]
  // extract the zip file to storage path (maybe the same with multer setting - ./public/uploads/)

  // update the value of the file info
  req.file = {
    path: './public/uploads/3.xlsx' // example, you just use the last file
  }

  next() // continue, important line
}
routes.post(
  '/upload',
  multer(multerConfig).single('file'),
  unzipMiddleware, // use new middleware here, after multer middleware
  async (req, res) => {
    const { path: filePath } = req.file // now filePath will be './public/uploads/3.xlsx', instead of './public/uploads/file.zip'
    //... your logic
  }
)
新中间件的使用

const unzipMiddleware = (req, res, next) => {
  const { path: zipFilePath, mimetype } = req.file // zipFilePath is './public/uploads/file.zip'
  if (mimetype !== 'application/gzip') { // You would want to check that, I think so
    return next() // do nothing
  }
  // get content of the zipFilePath [1.xlsx, 2.xlsx, 3.xlsx,....]
  // extract the zip file to storage path (maybe the same with multer setting - ./public/uploads/)

  // update the value of the file info
  req.file = {
    path: './public/uploads/3.xlsx' // example, you just use the last file
  }

  next() // continue, important line
}
routes.post(
  '/upload',
  multer(multerConfig).single('file'),
  unzipMiddleware, // use new middleware here, after multer middleware
  async (req, res) => {
    const { path: filePath } = req.file // now filePath will be './public/uploads/3.xlsx', instead of './public/uploads/file.zip'
    //... your logic
  }
)

我本来打算采用这种方法,但我想知道如何“更改”我的请求,以便multer中间件只接收解压后的文件,如果在multer中间件之前使用解压后的文件,也许可以?为了将已经清理过的数据传递给multer,操纵我的请求是否安全?