Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var fs = require('fs');
- var path = require('path');
- var async = require('async');
- var archiver = require('archiver');
- const FileDirectory = "./";
- var port = process.argv[2] || 8888;
- var dirs = {};
- dirs = [".//png//", ".//jpg//", ".//html//", ".//js//", ".//gif//"];
- exts = [".png", ".jpg", ".html", ".js", ".gif"];
- var getFilesFromDir = function(myDir, zipArchiver, myCallback) {
- var results = [];
- fs.readdir(myDir, function processContent(error, files) { //czytacz asynchroniczny
- if (error) {
- console.log(error);
- return myCallback(error);
- }
- else {
- var filesLeft = files.length;
- console.log('Found files: ' + filesLeft + ' ' + myDir);
- if(!filesLeft) { //directory is empty
- console.log('Dir is empty ' + myDir);
- return myCallback(null, results);
- }
- for(file in files)
- {
- file = path.join(myDir,files[file]);
- if(fs.statSync(file).isDirectory())
- {
- console.log('Entering directory ' + file);
- getFilesFromDir(file, zipArchiver, function subdirectoryDone(error, internalResult) {
- console.log("callback hell" + file + ' files left: ' + (filesLeft-1));
- if (!--filesLeft) {
- console.log('Last dir in ' + file);
- return myCallback(null,"last file callback");
- }
- });
- }
- else { //normal file
- var fileName = path.basename(file);
- console.log(fileName);
- zipArchiver.append(fs.createReadStream(file), {name: fileName});
- console.log("File: " + file + ' items left: ' + (filesLeft-1));
- if (!--filesLeft) {
- console.log('Last file:' + file);
- return myCallback(null,"last file callback");
- }
- }
- }
- }
- });
- };
- var myServer = http.createServer(function (req, res) {
- console.log('request starting:' + req.method + " " + req.url);
- var dirName = path.dirname(req.url).toLowerCase();
- var baseName = path.basename(req.url).toLowerCase();
- var extName = path.extname(req.url).toLowerCase();
- console.log(dirName + " " + baseName + " " + extName);
- if (req.method.toLowerCase() === 'get' && (extName === '.html' || extName === '.js')) {
- fs.readFile(FileDirectory + req.url, function (error, content) {
- if (error) {
- res.writeHead(500);
- res.end();
- }
- else {
- res.writeHead(200, { 'Content-Type': 'text/html' });
- res.end(content, 'utf-8');
- }
- });
- return;
- }
- else if (req.method.toLowerCase() === 'get' && (extName === '')) {
- var myDir = FileDirectory + req.url;
- var zipArchiver = archiver('zip');
- res.setHeader("Content-Disposition", "attachment; filename=superZip.zip");
- zipArchiver.pipe(res);
- getFilesFromDir(myDir, zipArchiver, function ImCalledWhenDone(error, result) {
- if (error) {
- console.log("error getfilesfromdir");
- }
- else {
- console.log("everything went fine");
- zipArchiver.finalize(function (error) {
- console.log(error);
- });
- }
- });
- };
- return;
- });
- myServer.listen(parseInt(port, 10));
- console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement