picture.js 569 B

123456789101112131415161718192021222324252627
  1. let sharp = require('sharp')
  2. module.exports = (sequelize, DataTypes) => {
  3. let ProfilePicture = sequelize.define('ProfilePicture', {
  4. file: DataTypes.BLOB('long'),
  5. mimetype: DataTypes.STRING
  6. }, {
  7. classMethods: {
  8. associate (models) {
  9. ProfilePicture.belongsTo(models.User)
  10. }
  11. },
  12. hooks: {
  13. beforeUpdate (profilePicture, options, cb) {
  14. sharp(profilePicture.file)
  15. .resize(300, 300, { fit: 'cover' })
  16. .toBuffer((err, buff) => {
  17. profilePicture.file = buff
  18. cb(err || null, options)
  19. })
  20. }
  21. }
  22. })
  23. return ProfilePicture
  24. }