Skip to content

Instantly share code, notes, and snippets.

@wavedevgit
Last active May 5, 2024 17:23
Show Gist options
  • Save wavedevgit/515d637d718f7cc9a9cbd6ab6032f4a9 to your computer and use it in GitHub Desktop.
Save wavedevgit/515d637d718f7cc9a9cbd6ab6032f4a9 to your computer and use it in GitHub Desktop.
Nginx cdn mapper generator

NGINX CDN mapping generator

This program generates a cdn mappings using regex, it is built using javascript.

Leave a if you like this code.

How it works?

  1. it get's the html of the index folder
  2. it runs a regex pattern on it
  const filesPattern =
    /<a href="(?<path>.+?)">.+?<\/a>[ ]+(?<date>\d+-\w+-\d+ \d+:\d+)[ ]+(?<size>.+)/;
  1. it maps through the valid ones and modifies the groups to fit with the excepted result
  2. it maps through each folder to get it's children files/folders

Example result:

[
    {
        "date": "2024-05-05T16:06:00.000Z",
        "size": 0,
        "url": "http://localhost/Isos/pk/",
        "type": "dir",
        "name": "pk/",
        "children": [
            {
                "date": "2024-05-05T16:06:00.000Z",
                "size": 0,
                "url": "http://localhost/Isos/pk/test/",
                "type": "dir",
                "name": "test/",
                "children": [
                    {
                        "date": "2024-05-05T16:07:00.000Z",
                        "size": 0,
                        "url": "http://localhost/Isos/pk/test/test/",
                        "type": "dir",
                        "name": "test/",
                        "children": [
                            {
                                "date": "2024-05-05T16:06:00.000Z",
                                "size": 0,
                                "url": "http://localhost/Isos/pk/test/test/test/",
                                "type": "dir",
                                "name": "test/",
                                "children": [
                                    {
                                        "date": "2024-05-05T16:06:00.000Z",
                                        "size": 0,
                                        "url": "http://localhost/Isos/pk/test/test/test/test/",
                                        "type": "dir",
                                        "name": "test/",
                                        "children": []
                                    }
                                ]
                            },
                            {
                                "date": "2024-05-05T16:06:00.000Z",
                                "size": 186,
                                "url": "http://localhost/Isos/pk/test/test/HTML%20File.html",
                                "type": "file",
                                "name": "HTML File.html"
                            }
                        ]
                    },
                    {
                        "date": "2024-05-05T16:06:00.000Z",
                        "size": 186,
                        "url": "http://localhost/Isos/pk/test/HTML%20File.html",
                        "type": "file",
                        "name": "HTML File.html"
                    }
                ]
            },
            {
                "date": "2024-05-05T16:06:00.000Z",
                "size": 186,
                "url": "http://localhost/Isos/pk/HTML%20File.html",
                "type": "file",
                "name": "HTML File.html"
            },
            {
                "date": "2024-05-05T16:06:00.000Z",
                "size": 0,
                "url": "http://localhost/Isos/pk/LibreOffice%20Impress.odp",
                "type": "file",
                "name": "LibreOffice Impress.odp"
            }
        ]
    },
    {
        "date": "2024-04-08T17:49:00.000Z",
        "size": 2344615936,
        "url": "http://localhost/Isos/Bliss-v16.9.4-x86_64-OFFICIAL-gapps-20240220.iso",
        "type": "file",
        "name": "Bliss-v16.9.4-x86_64-OFFICIAL-gapps-20240220.iso"
    },
    {
        "date": "2023-10-15T08:55:00.000Z",
        "size": 658505728,
        "url": "http://localhost/Isos/Debian%2012.2.0%20x64%20netinst.iso",
        "type": "file",
        "name": "Debian 12.2.0 x64 netinst.iso"
    },
]
const BASE_URL = "http://localhost/Isos";
const result = [];
Array.prototype.map_pattern = function (pattern) {
return this.map(item=>item.match(pattern))
}
Array.prototype.filter_valids = function (pattern) {
return this.filter(item=>item)
}
Array.prototype.map_groups = function (replacer=null) {
return this.map(item=>{
let { groups } = item
if (replacer) replacer(groups)
return groups
} )
}
async function generateMappings(path) {
const filesPattern =
/<a href="(?<path>.+?)">.+?<\/a>[ ]+(?<date>\d+-\w+-\d+ \d+:\d+)[ ]+(?<size>.+)/;
let html = await (await fetch(BASE_URL + path)).text();
const result = []
const files = html
.split("\n")
.map_pattern(filesPattern)
.filter_valids()
.map_groups(groups => {
groups.url = BASE_URL + path + groups.path;
groups.type = groups.path.endsWith("/") ? "dir" : "file";
groups.name = decodeURIComponent(groups.path);
delete groups.path;
groups.date = new Date(groups.date)
groups.size = groups.size === "-" ? 0 : Number(groups.size)
});
for (let file of files) {
if (file.type === "dir") {
file.children = await generateMappings(file.url.replace(BASE_URL, ""))
}
}
result.push(...files)
return result
}
async function main(){
const res = await generateMappings("/");
console.log(JSON.stringify(res,null,4))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment