forked from bcollazo/seratojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
94 lines (83 loc) · 2.7 KB
/
util.js
File metadata and controls
94 lines (83 loc) · 2.7 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
const path = require("path");
const INVALID_CHARACTERS_REGEX = /[^A-Za-z0-9_. ]/gi;
const parse = function (contents) {
// Find all 'ptrk' ocurrances
const indices = [];
for (let i = 0; i < contents.length; i++) {
if (contents.slice(i, i + 4) === "ptrk") {
indices.push(i);
}
}
// Content in between these indices are the songs
const songs = [];
indices.forEach((value, index) => {
const start = value + 9; // + 9 to skip the 'ptrk' itself and the bytes for size
const isLast = index === indices.length - 1;
const end = isLast ? contents.length : indices[index + 1] - 8; // -8 to remove 'otrk' and size bytes
let filepath = contents.slice(start, end);
filepath = filepath.replace(/\0/g, ""); // remove null-termination bytes
songs.push(path.resolve("/", filepath));
});
return songs;
};
const toSeratoString = function (string) {
return "\0" + string.split("").join("\0");
};
const intToHexbin = function (number) {
const hex = number.toString(16).padStart(8, "0");
let ret = "";
for (let idx of [0, 2, 4, 6]) {
let bytestr = hex.slice(idx, idx + 2);
ret += String.fromCodePoint(parseInt(bytestr, 16));
}
return ret;
};
const sanitizeFilename = function (filename) {
return filename.replace(INVALID_CHARACTERS_REGEX, "-");
};
/** Second param for dependency injection testing */
function removeDriveRoot(absoluteSongPath, platformParam = null) {
const platform = platformParam || process.platform;
if (platform === "win32") {
return absoluteSongPath.substring(3); // remove the C: or D: or ...
} else {
if (isFromExternalDrive(absoluteSongPath, platform)) {
const externalDrive = selectExternalRoot(absoluteSongPath, platform);
return absoluteSongPath.substring(externalDrive.length);
} else {
return absoluteSongPath;
}
}
}
/**
* Assumes input is an external path
* @returns external volume prefix string for external drive paths
*
* e.g.
* /Volumes/SampleDrive/Some/Path.mp3 => /Volumes/SampleDrive
* D:\\Folder\\song.mp3 => D:\\
*/
function selectExternalRoot(externalSongPath, platformParam = null) {
const platform = platformParam || process.platform;
if (platform === "win32") {
return path.parse(externalSongPath).root;
} else {
return externalSongPath.split("/").slice(0, 3).join("/");
}
}
function isFromExternalDrive(songPath, platformParam = null) {
const platform = platformParam || process.platform;
return (
(platform === "win32" && !songPath.startsWith("C:\\")) ||
(platform === "darwin" && songPath.startsWith("/Volumes"))
);
}
module.exports = {
parse,
removeDriveRoot,
toSeratoString,
intToHexbin,
sanitizeFilename,
selectExternalRoot,
isFromExternalDrive,
};