Skip to content

Other Tree Format

Rhilip edited this page Jan 23, 2024 · 3 revisions

By default, Call $fileTree = $torrent->getFileTree(?$sortType = TorrentFile::FILETREE_SORT_NORMAL); will return the array like:

[
    "torrentname" => [
        "directory" => [
            "filename2" => 2345
        ],
        "filename1" => 123
   ]
]

However, You can trans to other format by other code outsize the Bencode Library.

jsTree Format

Tree Format Example:

{
   "id": 0,
   "text": "title",
   "children": [],
   "size": 0
}

Example Javascript Transform Code:

function transToJsTree (rawData) {
    let i = 0;

    function transChildren (childrenData) {
        return Object.entries(childrenData).map(([name, value]) => {
            let p = {
                id: i++,
                text: name,
                size: 0
            };
        
            if (typeof value === 'object') {
                p.children = transChildren(value)
                p.size += p.children.map(x => x.size).reduce((partialSum, a) => partialSum + a, 0);  // sum children size
            } else {
                p.size = value
            }
            return p
        })
    }

    return transChildren(rawData)[0]
}

Clone this wiki locally