I have a file structure like this:
root
|_ fruits
|___ apple
|______images
|________ apple001.jpg
|________ apple002.jpg
|_ animals
|___ cat
|______images
|________ cat001.jpg
|________ cat002.jpg
I would like to, using Javascript and Node.js, listen to this root directory and all sub directories and create a JSON which mirrors this directory structure, each node contains type, name, path, and children:
data = [
{
type: "folder",
name: "animals",
path: "/animals",
children: [
{
type: "folder",
name: "cat",
path: "/animals/cat",
children: [
{
type: "folder",
name: "images",
path: "/animals/cat/images",
children: [
{
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat001.jpg"
}, {
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat002.jpg"
}
]
}
]
}
]
}
];
Here's a coffeescript JSON:
data =
[
type: "folder"
name: "animals"
path: "/animals"
children :
[
type: "folder"
name: "cat"
path: "/animals/cat"
children:
[
type: "folder"
name: "images"
path: "/animals/cat/images"
children:
[
type: "file"
name: "cat001.jpg"
path: "/animals/cat/images/cat001.jpg"
,
type: "file"
name: "cat001.jpg"
path: "/animals/cat/images/cat002.jpg"
]
]
]
]
how to get this json data format in django views?(python)
Here's a sketch. Error handling is left as an exercise for the reader.
var fs = require('fs'),
path = require('path')
function dirTree(filename) {
var stats = fs.lstatSync(filename),
info = {
path: filename,
name: path.basename(filename)
};
if (stats.isDirectory()) {
info.type = "folder";
info.children = fs.readdirSync(filename).map(function(child) {
return dirTree(filename + '/' + child);
});
} else {
// Assuming it's a file. In real life it could be a symlink or
// something else!
info.type = "file";
}
return info;
}
if (module.parent == undefined) {
// node dirTree.js ~/foo/bar
var util = require('util');
console.log(util.inspect(dirTree(process.argv[2]), false, null));
}
there's an NPM Module for it
https://www.npmjs.com/package/directory-tree
Creates an object representing a directory tree.
From:
photos
├── summer
│ └── june
│ └── windsurf.jpg
└── winter
└── january
├── ski.png
└── snowboard.jpg
To:
{
"path": "",
"name": "photos",
"type": "directory",
"children": [
{
"path": "summer",
"name": "summer",
"type": "directory",
"children": [
{
"path": "summer/june",
"name": "june",
"type": "directory",
"children": [
{
"path": "summer/june/windsurf.jpg",
"name": "windsurf.jpg",
"type": "file"
}
]
}
]
},
{
"path": "winter",
"name": "winter",
"type": "directory",
"children": [
{
"path": "winter/january",
"name": "january",
"type": "directory",
"children": [
{
"path": "winter/january/ski.png",
"name": "ski.png",
"type": "file"
},
{
"path": "winter/january/snowboard.jpg",
"name": "snowboard.jpg",
"type": "file"
}
]
}
]
}
]
}
var tree = directoryTree('/some/path');
And you can also filter by extensions:
var filteredTree = directoryTree('/some/path', ['.jpg', '.png']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With