const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
    Self.remoteMethodCtx('downloadFile', {
        description: 'Download a document',
        accessType: 'READ',
        accepts: [
            {
                arg: 'id',
                type: 'Number',
                description: 'The document id',
                http: {source: 'path'}
            }
        ],
        returns: [
            {
                arg: 'body',
                type: 'file',
                root: true
            }, {
                arg: 'Content-Type',
                type: 'String',
                http: {target: 'header'}
            }, {
                arg: 'Content-Disposition',
                type: 'String',
                http: {target: 'header'}
            }
        ],
        http: {
            path: `/:id/downloadFile`,
            verb: 'GET'
        },
        accessScopes: ['DEFAULT', 'read:multimedia']
    });

    Self.downloadFile = async function(ctx, id) {
        if (!await Self.checkRole(ctx, id))
            throw new UserError(`You don't have enough privileges`);
        return await Self.getFile(id);
    };
};