|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const url = require('url'); |
| 4 | +const Model = require('hof').model; |
| 5 | +const jimp = require('jimp'); |
| 6 | +const uuid = require('uuid').v4; |
| 7 | +const fs = require('fs'); |
| 8 | +const noPreview = 'data:image/png;base64,' + fs.readFileSync('assets/images/no-preview.png', {encoding: 'base64'}); |
| 9 | +const config = require('../../../config'); |
| 10 | + |
| 11 | +module.exports = class UploadModel extends Model { |
| 12 | + constructor(...args) { |
| 13 | + super(...args); |
| 14 | + this.set('id', uuid()); |
| 15 | + } |
| 16 | + |
| 17 | + save() { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const attributes = { |
| 20 | + url: config.upload.hostname |
| 21 | + }; |
| 22 | + const reqConf = url.parse(this.url(attributes)); |
| 23 | + reqConf.formData = { |
| 24 | + document: { |
| 25 | + value: this.get('data'), |
| 26 | + options: { |
| 27 | + filename: this.get('name'), |
| 28 | + contentType: this.get('mimetype') |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + reqConf.method = 'POST'; |
| 33 | + this.request(reqConf, (err, data) => { |
| 34 | + if (err) { |
| 35 | + return reject(err); |
| 36 | + } |
| 37 | + resolve(data); |
| 38 | + }); |
| 39 | + }) |
| 40 | + .then(result => { |
| 41 | + return this.set({ url: result.url }); |
| 42 | + }) |
| 43 | + .then(() => { |
| 44 | + return this.thumbnail(); |
| 45 | + }) |
| 46 | + .then(() => { |
| 47 | + return this.unset('data'); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + thumbnail() { |
| 52 | + return jimp.read(this.get('data')) |
| 53 | + .then(image => { |
| 54 | + image.resize(300, jimp.AUTO); |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + image.getBase64(this.get('mimetype'), (e, data) => { |
| 57 | + return e ? reject(e) : resolve(data); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }) |
| 61 | + .then(data => { |
| 62 | + this.set('thumbnail', data); |
| 63 | + }) |
| 64 | + .catch(() => { |
| 65 | + this.set('thumbnail', noPreview); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + auth() { |
| 70 | + if (!config.keycloak.token) { |
| 71 | + // eslint-disable-next-line no-console |
| 72 | + console.error('keycloak token url is not defined'); |
| 73 | + return Promise.resolve({ |
| 74 | + bearer: 'abc123' |
| 75 | + }); |
| 76 | + } |
| 77 | + const tokenReq = { |
| 78 | + url: config.keycloak.token, |
| 79 | + form: { |
| 80 | + username: config.keycloak.username, |
| 81 | + password: config.keycloak.password, |
| 82 | + grant_type: 'password', |
| 83 | + client_id: config.keycloak.clientId, |
| 84 | + client_secret: config.keycloak.secret |
| 85 | + }, |
| 86 | + method: 'POST' |
| 87 | + }; |
| 88 | + |
| 89 | + return new Promise((resolve, reject) => { |
| 90 | + this._request(tokenReq, (err, response) => { |
| 91 | + const body = JSON.parse(response.body); |
| 92 | + |
| 93 | + if (err || body.error) { |
| 94 | + return reject(err || new Error(`${body.error} - ${body.error_description}`)); |
| 95 | + } |
| 96 | + |
| 97 | + resolve({ |
| 98 | + bearer: JSON.parse(response.body).access_token |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + } |
| 103 | +}; |
0 commit comments