-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
140 lines (124 loc) · 6.58 KB
/
Copy pathparse.js
File metadata and controls
140 lines (124 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require("fs");
const path = require("path");
const sh = require("shorthash");
const mm = require("music-metadata");
const util = require("util");
const walk = (dir, done) => {
let results = {
"songs":{},
"albums":{},
"artists":{},
"playlists":{},
"genres":{},
"all":{}
};
fs.readdir(dir, (err, list) => {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, (err, res) => {
for(let cat in res) {
if(cat !== "all") {
if(cat !== "playlists") {
for(let index in res[cat]) {
if(results[cat][index] === undefined)
results[cat][index] = [];
results[cat][index] = results[cat][index].concat(res[cat][index]);
}
}
} else {
for(let song in res[cat]) {
results[cat][song] = res[cat][song];
}
}
}
if (!--pending) done(null, results);
});
} else {
let parent = path.dirname(file).split(path.sep).pop()
let ext = path.extname(file);
if(ext === ".mp3" || ext === ".flac") {
mm.parseFile(file, {native: true})
.then(function (metadata) {
let tags = metadata.common;
let entry = {};
let fileName = path.basename(
file, path.extname(file));
if(tags.title === undefined) {
entry.title = fileName;
} else {
entry.title = tags.title;
}
let newname = fileName.replace(/\s+/g,'-');
newname = fileName.replace(/[^a-zA-Z0-9\-.]/g,'-') + ext;
//newname = newname.replace('-mkv', '.mkv');
fs.rename(file, dir + "/" + newname, function(err){
if(err) throw err;
});
if(tags.picture) {
let binaryData = new Buffer(tags.picture[0].data, 'base64').toString('binary');
let coverName = (tags.album) ?
tags.album : entry.title;
coverName = coverName.replace(/\s+/g,'-');
coverName = coverName.replace(/[^a-zA-Z0-9\-.]/g,'-');
let coverExt = tags.picture[0].format;
let fileName = "\\" + coverName + "." + coverExt;
let coverPath = "client\\public\\covers" + fileName;
fs.writeFile(
coverPath,
binaryData,
"binary",
function(err){
console.log("No album art");
});
entry.cover = fileName;
} else {
entry.cover = "";
}
let musicPath = path.relative(process.cwd(), dir + "/" + newname);
musicPath = musicPath.split('client\\public\\music')[1];
entry.path = musicPath; //path.relative(process.cwd(), dir + "/" + newname);
entry.artist = (tags.artist === undefined) ?
"None" : tags.artist;
entry.album = (tags.album === undefined) ?
"None" : tags.album;
entry.genre = (tags.genre === undefined) ?
[] : tags.genre;
const id = sh.unique(musicPath) // Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10);
results["all"][id] = entry;
if(results["songs"][parent] === undefined)
results["songs"][parent] = [];
if(results["artists"][entry.artist] === undefined)
results["artists"][entry.artist] = [];
if(results["albums"][entry.album] === undefined)
results["albums"][entry.album] = [];
results["songs"][parent].push(id);
results["artists"][entry.artist].push(id);
results["albums"][entry.album].push(id);
for(let genre in entry.genre) {
if(results["genres"][entry.genre[genre]]
=== undefined)
results["genres"][entry.genre[genre]] = [];
results["genres"][entry.genre[genre]]
.push(id);
}
if (!--pending) done(null, results);
})
.catch(function (err) {
console.error(err);
});
} else {
if (!--pending) done(null, results);
}
}
});
});
});
}
module.exports = {
walk
}