├── .gitignore ├── .jshintrc ├── README.md └── server ├── .gitignore ├── config └── config.js ├── controller └── uploadDownload.js ├── package.json ├── routes.js └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | client/src/bower_components 14 | node_modules 15 | npm-debug.log 16 | /.idea 17 | server/public 18 | .jshintrc 19 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "strict": true, 4 | "indent": 2, 5 | "latedef": true, 6 | "undef": true, 7 | "unused": true, 8 | "camelcase": true, 9 | "curly": true, 10 | "eqeqeq": true, 11 | "newcap": true, 12 | "quotmark": "single" 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hapi-file-upload-download 2 | ======================== 3 | 4 | The purpose of this app is to upload and download the file of any extension. You don't need to worry about file extension, is going to work for all types of file. 5 | 6 | ### Install an app 7 | 8 | Run the following command in root directory of an app in command prompt. 9 | 10 | ###### *Install node packages* 11 | 12 | server/ npm install 13 | 14 | ### Run an app 15 | 16 | ###### *Run Server* 17 | 18 | Run the following command in root directory of an app in command prompt. 19 | 20 | server/ node server.js 21 | 22 | You can see the port number in command prompt after sucessfull run 23 | 24 | [Express-file-upload-download] (https://github.com/Cron-J/Express-file-upload-download) 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /server/.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | -------------------------------------------------------------------------------- /server/config/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | server: { 3 | 4 | host: '0.0.0.0', 5 | port: 8000 6 | }, 7 | publicFolder: './public', 8 | uploadFolder: '/uploads', 9 | MixFolder: './public/uploads', 10 | MixInsideFolder: './public/uploads/' 11 | }; 12 | -------------------------------------------------------------------------------- /server/controller/uploadDownload.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | multiparty = require('multiparty'), 3 | walk = require('walk'), 4 | Config = require('../config/config'); 5 | /* 6 | * Display upload form 7 | */ 8 | 9 | exports.display_form = { 10 | handler: function(requestuest, reply) { 11 | reply( 12 | '
' + 13 | '' + 14 | '' + 15 | '
' 16 | ); 17 | } 18 | }; 19 | 20 | /* 21 | * upload file 22 | */ 23 | 24 | exports.uploadFile = { 25 | payload: { 26 | maxBytes: 209715200, 27 | output: 'stream', 28 | parse: false 29 | }, 30 | handler: function(requset, reply) { 31 | var form = new multiparty.Form(); 32 | form.parse(requset.payload, function(err, fields, files) { 33 | if (err) return reply(err); 34 | else upload(files, reply); 35 | }); 36 | } 37 | }; 38 | 39 | /* 40 | * upload file function 41 | */ 42 | 43 | var upload = function(files, reply) { 44 | fs.readFile(files.file[0].path, function(err, data) { 45 | checkFileExist(); 46 | fs.writeFile(Config.MixInsideFolder + files.file[0].originalFilename, data, function(err) { 47 | if (err) return reply(err); 48 | else return reply('File uploaded to: ' + Config.MixInsideFolder + files.file[0].originalFilename); 49 | 50 | }); 51 | }); 52 | }; 53 | 54 | /* 55 | * Check File existence and create if not exist 56 | */ 57 | 58 | var checkFileExist = function() { 59 | fs.exists(Config.publicFolder, function(exists) { 60 | if (exists === false) fs.mkdirSync(Config.publicFolder); 61 | 62 | fs.exists(Config.MixFolder, function(exists) { 63 | if (exists === false) fs.mkdirSync(Config.MixFolder); 64 | }); 65 | }); 66 | }; 67 | 68 | /** 69 | * get file 70 | */ 71 | 72 | exports.getFile = { 73 | handler: function(request, reply) { 74 | var file = request.params.file, 75 | path = Config.publicFolder + Config.uploadFolder + "/" + file, 76 | ext = file.substr(file.lastIndexOf('.') + 1); 77 | fs.readFile(path, function(error, content) { 78 | if (error) return reply("file not found"); 79 | var contentType; 80 | switch (ext) { 81 | case "pdf": 82 | contentType = 'application/pdf'; 83 | break; 84 | case "ppt": 85 | contentType = 'application/vnd.ms-powerpoint'; 86 | break; 87 | case "pptx": 88 | contentType = 'application/vnd.openxmlformats-officedocument.preplyentationml.preplyentation'; 89 | break; 90 | case "xls": 91 | contentType = 'application/vnd.ms-excel'; 92 | break; 93 | case "xlsx": 94 | contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; 95 | break; 96 | case "doc": 97 | contentType = 'application/msword'; 98 | break; 99 | case "docx": 100 | contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; 101 | break; 102 | case "csv": 103 | contentType = 'application/octet-stream'; 104 | break; 105 | default: 106 | reply.file(path); 107 | } 108 | 109 | reply(content).header('Content-Type', contentType).header("Content-Disposition", "attachment; filename=" + file); 110 | 111 | }); 112 | } 113 | }; 114 | 115 | /** 116 | *get fileList 117 | */ 118 | 119 | exports.fileList = { 120 | handler: function(request, reply) { 121 | var files = []; 122 | // Walker options 123 | var walker = walk.walk(Config.publicFolder + Config.uploadFolder, { 124 | followLinks: false 125 | }); 126 | 127 | walker.on('file', function(root, stat, next) { 128 | // Add this file to the list of files 129 | files.push(stat.name); 130 | next(); 131 | }); 132 | 133 | walker.on('end', function() { 134 | return reply(files); 135 | }); 136 | } 137 | }; -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-file-upload-download", 3 | "description": "hapi-file-upload-download", 4 | "version": "0.1.0", 5 | "main": "server", 6 | "engines": { 7 | "node": ">=0.10.22" 8 | }, 9 | "dependencies": { 10 | "hapi": "7.0.0", 11 | "multiparty": "^4.1.1", 12 | "walk": "^2.3.9" 13 | }, 14 | "keywords": [ 15 | "hapi", 16 | "file upload", 17 | "file download", 18 | "file list" 19 | ], 20 | "author": { 21 | "name": "sonipandey", 22 | "email": "sonipandey.71@gmail.com" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /server/routes.js: -------------------------------------------------------------------------------- 1 | // Load modules 2 | 3 | var UD = require('./controller/uploadDownload'); 4 | 5 | // API Server Endpoints 6 | exports.endpoints = [ 7 | 8 | { method: 'GET', path: '/', config: UD.display_form}, 9 | { method: 'POST', path: '/uploadFile', config: UD.uploadFile}, 10 | { method: 'GET', path: '/getFile/{file}', config: UD.getFile}, 11 | { method: 'GET', path: '/fileList', config: UD.fileList} 12 | ]; -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | var Hapi = require('hapi'), 2 | Routes = require('./routes'), 3 | Config = require('./config/config'); 4 | 5 | 6 | var app = {}; 7 | app.config = Config; 8 | 9 | var server = Hapi.createServer(app.config.server.host, app.config.server.port, {cors: true}); 10 | 11 | server.route(Routes.endpoints); 12 | 13 | server.start(function () { 14 | console.log('Server started ', server.info.uri); 15 | }); --------------------------------------------------------------------------------