如何显示使用 Types.File 存储的 KeystoneJS thumbnail/preview 图像?和架构 url 与 publicPath

How to display KeystoneJS thumbnail/preview images stored with Types.File? and schema url vs. publicPath

我正在使用 Types.File 存储本地图像 我有 2 个问题,如果有人可以帮助我!

1) 我在 'fs' 中定义了我的 publicPath 并将架构 url 设置为 true, 我期待 url 然后 return 包含 public 路径的东西, 但是它得到了 /public/ "filename",我定义的 publicPath 是不是错了?还是有别的用途?

2) 我尝试使用 Types.File 和 Types.Url 的 属性 格式将图像标签 return 作为 thumbnail/preview,但没有成功对于他们两个,我注意到格式已被删除并将在 Types.File 中重新实现,它还没有吗?还是我这里也做错了??

 var myStorage = new keystone.Storage({
     adapter: keystone.Storage.Adapters.FS,
     fs: {
         path: keystone.expandPath('./public/postimages'), // required; path where the files should be stored
         publicPath: '/postimages', // path where files will be served
     },
     schema: {
         url: true
     }
 });

 Post.add({
      *...*
      image: {
        type: Types.File,
         storage: myStorage,
         /* format: function(item, file) {
             return '<img src="/files/' + file.filename + '" style="max-width: 300px">';
         } */
       },
     image_url: {
         type: String,
        noedit: true,
     }
      *...*
 });  

 Post.schema.pre('save', function(next) {
     this.image_url = this.image ? this.image.url : '';
     next(); 
 });

对于你的第一期,我有同样的事情。结果是 FS 适配器使用 https://nodejs.org/api/url.html#url_url_resolve_from_to 创建 url 字符串(下面的代码取自 keystone 的 FS 适配器):

/**
    Gets the public path of a stored file by combining the publicPath option
    with the filename in the field value
*/
FSAdapter.prototype.getFileURL = function (file) {
    var publicPath = this.options.publicPath;
    if (!publicPath) return null; // No URL.

    return url.resolve(publicPath, file.filename);
};

正如您从 nodejs 文档页面上的示例中看到的那样:

url.resolve('/one/two/three', 'four')         // '/one/two/four'

所以只要在 /postimages 后面加一个“/”就可以了。

publicPath: '/postimages/'

此后应该可以正常工作了。