Bryan

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  37 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks
HPE Enovia connector usually generates a lot of xml files in temp folder and It often takes time to list them on linux and we can use this small app to list all the files for a folder in a json format by only using the browser. and we can also get the content of a xml file.

functions to provide and how to use :
1. list the content files in a folder, for example /var/user/oemdev/HPEIDOL/IDOLServer/Enovia_1ABackfillCatalog/temp
http://frmrszvrd006.dc-m.alcatel-lucent.com:8099/listfiles?base_folder=/var/user/oemdev/HPEIDOL/IDOLServer/Enovia_1ABackfillCatalog/temp&page=1&page_size=100

2. get the content of a file, we should provide the base folder which the file locates
http://frmrszvrd006.dc-m.alcatel-lucent.com:8099/getcontent?base_folder=/var/user/oemdev/HPEIDOL/IDOLServer/Enovia_1ABackfillCatalog/temp&file=1479950661_45valy5v.xml


var http = require('http');
var url = require('url');
var path = require('path');
fs = require('fs')

//load fileslist in a specified folder
function load_files(base_folder, page, page_size, callback) {
    fs.readdir(base_folder, (err, files) => {
        if (err) {
            console.log(err);
            if (err.code == "ENOENT") {
                callback(make_error("no_such_folder", "the specified folder " + base_folder + " doesnot exist"));
            } else {
                callback(make_error("file_error", JSON.stringify(err)));
            }
            return;
        }

        var only_files = [];
        var folder_name = path.basename(base_folder);

        var iterator = (index) => {
            if (index == files.length) {
                var ps;
                var start = (page - 1) * page_size;
                if (start < 0) {
                    start = 0;
                }
                ps = only_files.slice(start, start + page_size);
                var obj = {
                    short_name: folder_name,
                    files_lists: ps
                };
                callback(null, obj);
                return;
            }

            fs.stat(base_folder  +"/" + files[index], (err, stats) => {
                if (err) {
                    callback(make_error("file_error", JSON.stringify(err)));
                    return;
                }

                if (stats.isFile()) {
                    var obj = {
                        filename: files[index],
                        desc: files[index],
                        stats: stats
                    };
                    only_files.push(obj);
                }
                iterator(index + 1);
            });
        }
        iterator(0);
    });

}

//process the http requests that comes, for example http://frmrszvrd006.dc-m.alcatel-lucent.com:8099/listfiles
function handle_incoming_request(req, res) {

    req.parsed_url = url.parse(req.url, true);
    var core_url = req.parsed_url.pathname;
    console.log("Incoming requests" + req.method + " " + req.url);
    //list all the files in a folder
    if (core_url.substr(0, 10) == '/listfiles') {
        handle_get_files_list(req, res);
        //get the content of a file
    } else if (core_url.substr(0, 11) == '/getcontent') {
        handle_get_content(req, res);
    } else {
        send_failture(res, 404, invalid_resource());
    }
}

//get all the files for a folder, and pagination is supported in the way of ?page=1&page_size=100
function handle_get_files_list(req, res) {

    var getp = req.parsed_url.query;
    var page_num = getp.page ? parseInt(getp.page) : 1;
    var page_size = getp.page_size ? parseInt(getp.page_size) : 1000;
    var base_folder = getp.base_folder ? getp.base_folder : "/var";

    if (isNaN(parseInt(page_num))) {
        page_num = 1;
    }

    if (isNaN(parseInt(page_size))) {
        page_size = 1000;
    }

    var core_url = req.parsed_url.pathname;

    //core.url -"/listfiles/".length
    load_files(base_folder, page_num, page_size, (err, folder_contents) => {
        if (err && err.error == "no_such_folder") {
            send_failture(res, 404, err);
        } else if (err) {
            send_failture(res, 500, err);
        } else {
            send_success(res, {
                folder_data: folder_contents
            });
        }
    });
}

function handle_get_content(req, res) {

    var getp = req.parsed_url.query;
    var base_folder = getp.base_folder;
    var file = getp.file;
    fs.readFile(base_folder + "/" + file, "utf8", (err, data) => {
        if (err) {
            send_failture(res, 404, err);
            return;
        }
        console.log(data);

        send_success(res, {
            file_content: data
        });
    });

}

function send_failture(res, server_code, err) {
    var code = (err.code) ? err.code : err.name
    res.writeHead(server_code, {
        "Content-Type": "application/json"
    });
    res.end(JSON.stringify({
        error: code,
        message: err.message
    }) + "\n");
}

function send_success(res, data) {
    res.writeHead(200, {
        'Content-type': 'application/json'
    });
    var output = {
        error: null,
        data: data
    };
    res.end(JSON.stringify(output) + "\n");
}

function make_error(err, msg) {
    var e = new Error(msg);
    e.code = err;
    return e;
}

function invalid_resource() {
    return make_error("invalid resource", "the requested resource doesnot exist");
}

function no_such_folder() {
    return make_error("no_such_folder", "the specified folder doesnot exist");
}

function bad_json() {
    return make_error("bad jason", "the jason format is not correct");
}

function missing_data(name) {
    return make_error(name + " is missing", "missing data");
}

var s = http.createServer(handle_incoming_request);
s.listen(8099);


Reference
Learning Node.js 2nd Edition
https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
https://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs/
posted on 2017-08-19 20:07 Life is no respector of any genius. 阅读(264) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: