Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement of the tree structure build #3

Open
qtipee opened this issue Nov 4, 2019 · 1 comment
Open

Enhancement of the tree structure build #3

qtipee opened this issue Nov 4, 2019 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@qtipee
Copy link
Collaborator

qtipee commented Nov 4, 2019

The buildFilesTree function is working ; it builds a map for each folder that can contain other maps (sub-folders) or files (the audio files). However, I'm note sure this is the better way of doing it.

The goal of this function is to build a tree to retrieve the hierarchical structure of the uploaded content, because when uploading a folder from a file input (with webkitdirectory tag), we only get the files and not the folders, but we can retrieve them via the webkitRelativePath property of each file.

This tree structure is important ; it is used to add the folders and audio files to the zip file (generated application) and to build the HTML tree to display the audio files.

So, is there a better way of building the tree structure ? Or maybe another way of uploading the files, or by using another data container for the folders and files than the Map. This is important to use as less libraries as possible and to do the maximum with vanilla Javascript.

// Builds a tree structure with the uploaded files
function buildFilesTree(filesList)
{
let filesTree = new Map();
// Iterates over each uploaded file
for (let i = 0; i < filesList.length; i++)
{
let file = filesList[i];
// Filters the audio files
if (file.type.includes('audio'))
{
let filename = file.name;
let relativePath = file.webkitRelativePath;
// This file parent folders (array)
let fileParents = relativePath.split('/').slice(0, -1);
let currentFolder = filesTree;
// Iterates over each parent folder of the current file
for (let f = 0; f < fileParents.length; f++)
{
// This folder is not in the files tree yet
if (!currentFolder.has(fileParents[f]))
{
currentFolder.set(fileParents[f], new Map());
}
currentFolder = currentFolder.get(fileParents[f]);
}
// Adds this file to its first parent folder
currentFolder.set(filename, file);
}
}
return filesTree;
}

A test example result :

Screenshot 2019-11-04 at 22 18 21

@qtipee qtipee added the enhancement New feature or request label Nov 4, 2019
@grunenwald
Copy link
Member

Should you add a "help wanted" label to this issue ?
https://help.github.com/en/github/managing-your-work-on-github/about-labels

@qtipee qtipee added help wanted Extra attention is needed and removed enhancement New feature or request labels Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants