Last active
February 24, 2019 21:51
-
-
Save thblckjkr/6813034dd948ff69da73388dd74adaac to your computer and use it in GitHub Desktop.
Creates an object from a html string obtained from an index of apache directory. Translates an APACHE default directory listing, to javascript object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Requires JQuery >= 1.8 | |
* @param {info} str string-based data of the | |
* Author: @thblckjkr | Theo G | |
* Ported to JQuery from https://github.com/juliangruber/parse-apache-directory-index | |
* Usage: | |
* $.get("/a/directory/path", function(data, status){ | |
* var files = directoryparse(data); | |
* console.log(files); | |
* }) | |
*/ | |
function bytes(str){ | |
var m = /^\s*([0-9.]+)([A-Z]*)\s*$/.exec(str); | |
if (!m) return null; | |
var num = Number(m[1]); | |
var suf = m[2]; | |
return suf === 'K' ? num * 1024 | |
: suf === 'M' ? num * 1024 * 1024 | |
: suf === 'G' ? num * 1024 * 1024 * 1024 | |
: num; | |
}; | |
function directoryparse(src){ | |
var dir = '/'; | |
var files = []; | |
// The fifth element is always the table? | |
$document = $.parseHTML(src); | |
$table = $($document[5]); | |
$rows = $table.find('tr').toArray(); | |
// Figure out the order of the columns, by looking at the header row. | |
// eg { 'Name': 0, 'Last modified': 1, 'Size': 2 } | |
var fieldCols = $($rows[0]) | |
.children('th') | |
.toArray() | |
.reduce((fieldCols, th, i) => | |
Object.assign(fieldCols, { | |
[$(th).text().trim()]: i | |
}), | |
{}); | |
// Make sure we at least found a "Name" column | |
if (fieldCols['Name'] === undefined) { | |
throw new Error('Unable to parse apache index html: cannot identify "Name" column.'); | |
} | |
// Parse fields | |
$rows | |
// Ignore the header row | |
.slice(1) | |
.forEach((tr) => { | |
var $tds = $(tr).find('td'); | |
var getCol = label => fieldCols[label] === undefined ? null : $tds.eq(fieldCols[label]); | |
var getColText = label => getCol(label) && getCol(label).text().trim(); | |
var path = getCol('Name').children().eq(0).attr('href'); | |
var name = getColText('Name'); | |
// Ignore 'Parent Directory' row | |
if (name === 'Parent Directory' || !name) return; | |
files.push({ | |
type: path.endsWith('/') | |
? 'directory' | |
: 'file', | |
name: name, | |
path: dir + path, | |
lastModified: getCol('Last modified') && new Date(getColText('Last modified')), | |
size: getCol('Size') && bytes(getColText('Size')), | |
description: getColText('Description') | |
}); | |
}); | |
return { dir, files }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment