如何显示与Types.File一起存储的KeystoneJS缩略图/预览图像?和架构url与publicPath

时间:2017-02-17 20:03:54

标签: keystonejs

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

1)我在'fs'中定义了我的publicPath并将架构url设置为true, 我期待url然后会返回包含publicPath的东西, 但它取而代之的是/ public /“filename”,我是否定义了publicPath错误?或者它意味着什么呢?

2)我尝试使用Types.File和Types.Url的format属性将图像标记作为缩略图/预览返回,但两者都没有运气,我注意到格式已被删除并将重新实现在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(); 
 });

1 个答案:

答案 0 :(得分:0)

对于你的第一期,我有同样的事情。事实证明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'

所以只需添加&#34; /&#34;在你/ postimages之后,它应该工作。

publicPath: '/postimages/'

事情应该在此之后发挥作用。