如何在撇号 3 CMS 附件模块中设置其他文件组?

How do I set additional file groups in Apostrophe 3 CMS attachment module?

提问人:Julian Vogels 提问时间:8/31/2022 更新时间:9/1/2022 访问量:32

问:

撇号 3 CMS 附件模块中的文件组指定哪些文件可以在文件上传表单中上传。默认情况下,撇号 3 提供两个文件组:和 。officeimage

我想再添加两个文件组:(仅)和(用于存档)。如自定义文件组中建议的那样:pdfzip

除了 office 和 image 之外,开发人员还可以使用 @apostrophecms/attachment 模块的 fileGroups 选项来配置文件类型组。

不幸的是,没有关于如何做到这一点的示例,所以我参考了撇号 2 文档 fileGroups。本文档提到了默认配置:

[
  {
    name: 'images',
    label: 'Images',
    extensions: [ 'gif', 'jpg', 'png' ],
    extensionMaps: {
      jpeg: 'jpg'
    },
    // uploadfs should treat this as an image and create scaled versions
    image: true
  },
  {
    name: 'office',
    label: 'Office',
    extensions: [ 'txt', 'rtf', 'pdf', 'xls', 'ppt', 'doc', 'pptx', 'sldx', 'ppsx', 'potx', 'xlsx', 'xltx', 'csv', 'docx', 'dotx' ],
    extensionMaps: {},
    // uploadfs should just accept this file as-is
    image: false
  }
]

我扩展了配置并将其添加到@apostrophecms/附件模块中:

// modules/@apostrophecms/attachment/index.js
module.exports = {
  options: {
    '@apostrophecms/attachment': {
      options: {
        fileGroups: [
          {
            name: 'pdf',
            label: 'PDF',
            extensions: ['pdf'],
            extensionMaps: {},
            // uploadfs should just accept this file as-is
            image: false
          },
          {
            name: 'zip',
            label: 'ZIP',
            extensions: ['zip'],
            extensionMaps: {},
            // uploadfs should just accept this file as-is
            image: false
          },
          {
            name: 'image',
            label: 'Image',
            extensions: ['gif', 'jpg', 'png', 'svg', 'txt', 'rtf', 'pdf', 'xls', 'ppt', 'doc', 'pptx', 'sldx', 'ppsx', 'potx', 'xlsx', 'xltx', 'csv', 'docx', 'dotx', 'jpeg'],
            extensionMaps: {
              jpeg: 'jpg'
            },
            // uploadfs should treat this as an image and create scaled versions
            image: true
          },
          {
            name: 'office',
            label: 'Office',
            extensions: [ 'txt', 'rtf', 'pdf', 'xls', 'ppt', 'doc', 'pptx', 'sldx', 'ppsx', 'potx', 'xlsx', 'xltx', 'csv', 'docx', 'dotx' ],
            extensionMaps: {},
            // uploadfs should just accept this file as-is
            image: false
          }
        ]
      }
    }
  }
};

在小部件中,我配置了一个字段以仅允许文件:zip

file: {
  label: 'File',
  type: 'attachment',
  fileGroup: 'zip'
}

上传有效的“存档.zip”文件时,我看到以下错误消息:

“ 不接受文件类型。允许的扩展名:”

Error dialogue screenshot

如何为撇号 3 正确配置自定义文件组?

撇号-CMS 撇号

评论


答:

1赞 Bob Means 9/1/2022 #1

这是我们在 a3 中知道的限制。附件模块的配置方式不会通过文件扩展 fileGroups。相反,您必须在同一文件中“手动”执行此操作。 因此,在您的情况下,它将是这样的:modules/@apostrophe/attachment/index.js

module.exports = {
  init(self) {
    const newGroups = [
      {
        name: 'pdf',
        label: 'PDF',
        extensions: ['pdf'],
        extensionMaps: {},
        // uploadfs should just accept this file as-is
        image: false
      },
      {
        name: 'zip',
        label: 'ZIP',
        extensions: ['zip'],
        extensionMaps: {},
        // uploadfs should just accept this file as-is
        image: false
      }
    ];
    self.fileGroups = [ ...self.fileGroups, ...newGroups ];
  }
};

您可以使用相同类型的模式来扩展现有组。

...
    const group = self.fileGroups.find((group) => group.name === 'office');
    group.extensions = [ ...group.extensions, 'woff2' ];
...

希望这能为您解决!