keystonejs / keystone-storage-adapter-s3

⚠️ Archived - Legacy S3 Storage Adapter for KeystoneJS
MIT License
17 stars 55 forks source link

schema option to presave image dimensions #28

Open eliataylor opened 7 years ago

eliataylor commented 7 years ago

more of an Enhancement request, but might there be plans to allow saving image dimensions in the schema?

schema { dimensions:true,...

eliataylor commented 6 years ago

found this is easily done by adding the fields to the schema:

@ https://github.com/keystonejs/keystone-storage-adapter-s3/blob/master/index.js#L65

S3Adapter.SCHEMA_FIELD_DEFAULTS = {
    filename: true,
    bucket: false,
    path: false,
    etag: false,
    width:false,
    height:false,
    wUnits:false,
    hUnits:false
};

then I added a post save method:

then I implemented the updates with:

const Probe = require('probe-image-size');
Content.schema.post('save',(doc,next)=>{

    if (doc.image && doc.image.url) {
        Probe(doc.image.url, function (err, result) {
            if (err) console.log(err);
          console.log(doc.image, result);
            doc.image.width = result.width;
            doc.image.height = result.height;
            doc.image.wUnits = result.wUnits;
            doc.image.hUnits = result.hUnits;
            console.log('UPDATING IMAGE', doc.image);
            return keystone.list('Content').model.update({'_id': doc._id}, {image: doc.image}).exec();
        });
    }
});