All files / src/services/storage storage.hooks.js

0% Statements 0/12
0% Branches 0/8
0% Functions 0/4
0% Lines 0/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                                                                                       
// import { getBase64DataURI } from 'dauria'
import { disallow, discard, iff } from 'feathers-hooks-common'
import { populateAttachmentResource, attachToResource, detachFromResource } from '../../hooks'
 
module.exports = {
  before: {
    all: [],
    find: [ disallow() ],
    get: [],
    create: [
      populateAttachmentResource,
      (hook) => {
        // If form multipart data transform to data buffer for blob service
        if (!hook.data.uri && hook.params.file) {
          // Before https://github.com/feathersjs-ecosystem/feathers-blob/releases/tag/v1.5.0 only data URI were supported
          // hook.data.uri = getBase64DataURI(hook.params.file.buffer, hook.params.file.mimetype)
          // Now raw buffers are
          hook.data.buffer = hook.params.file.buffer
          hook.data.contentType = hook.params.file.mimetype
        }
        // Makes uploaded files public when required
        if (hook.data.public) hook.params.s3 = { ACL: 'public-read' }
      }
    ],
    update: [ disallow() ],
    patch: [ disallow() ],
    remove: [ populateAttachmentResource ]
  },
 
  after: {
    all: [],
    find: [],
    get: [],
    // Let the attachment on the resource object occur only when resource has been found
    create: [
      (hook) => {
        // If form multipart data get filename
        if (hook.params.file) {
          hook.result.name = hook.params.file.originalname
        }
      },
      iff(hook => hook.params.resource, attachToResource), discard('uri')
    ],
    update: [],
    patch: [],
    // Let the detachment on the resource object occur only when resource has been found
    remove: [ iff(hook => hook.params.resource, detachFromResource), discard('uri') ]
  },
 
  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
}