node package manager

hapi-fenixedu-drive

Hapi.js Plugin for FenixEdu Drive

Add the dependency to your project.

Yarn:

yarn add hapi-fenixedu-drive

NPM:

npm install --save hapi-fenixedu-drive

Register your plugin.

server.register({
        register: require('hapi-fenixedu-drive'),
        options: {
            url: 'your-drive-url',
            appId: 'your-drive-app-id',
            appSecret: 'your-drive-app-secret',
            username: 'your-drive-username'
        }
    }, (err) => {
        if (err) {
            console.error('Failed to load plugin:', err);
        }
});

Store a file in the configured FenixEdu Drive.

api.route({
    method: 'POST',
    path: '/upload',
    config: {
     payload: {
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data'
        }
    },
    handler: function (request, reply) {
        request.storeFile({
            filename: filename,
            file: request.payload.file,
            path: path
        }).then((fileInfo) => {
            //Store fileInfo in a database for later retrieval... 
        });
    }
});

Redirect a file stream to the client is a simple as:

api.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
     const fileId = ... // Retrieve this fileId from a database for example... 
 
        reply.fileFromDrive({
            fileId: fileId
        }, (err, fileStream) => {
            //handle error if exists 
            reply(fileStream);
        });
    }
});