File size: 2,343 Bytes
11fcc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de4b571
11fcc5a
de4b571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// characters that are disallowed on windows:
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
const characterMap = {
    '<':  '<',
    '>':  '>',
    ':':  ':',
    '"':  '"',
    '/':  '/',
    '\\': '\',
    '|': '|',
    '?': '?',
    '*': '*'
};

export const sanitizeString = (string) => {
    // remove any potential control characters the string might contain
    string = string.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");

    for (const [ char, replacement ] of Object.entries(characterMap)) {
        string = string.replaceAll(char, replacement);
    }

    return string;
}

export default (f, style, isAudioOnly, isAudioMuted) => {
    let filename = '';

    let infoBase = [f.service, f.id];
    let classicTags = [...infoBase];
    let basicTags = [];

    let title = sanitizeString(f.title);

    if (f.author) {
        title += ` - ${sanitizeString(f.author)}`;
    }

    if (f.resolution) {
        classicTags.push(f.resolution);
    }

    if (f.qualityLabel) {
        basicTags.push(f.qualityLabel);
    }

    if (f.youtubeFormat) {
        classicTags.push(f.youtubeFormat);
        basicTags.push(f.youtubeFormat);
    }

    if (isAudioMuted) {
        classicTags.push("mute");
        basicTags.push("mute");
    } else if (f.youtubeDubName) {
        classicTags.push(f.youtubeDubName);
        basicTags.push(f.youtubeDubName);
    }

    switch (style) {
        default:
        case "classic":
            if (isAudioOnly) {
                if (f.youtubeDubName) {
                    infoBase.push(f.youtubeDubName);
                }
                return `${infoBase.join("_")}_audio`;
            }
            filename = classicTags.join("_");
            break;
        case "basic":
            if (isAudioOnly) return title;
            filename = `${title} (${basicTags.join(", ")})`;
            break;
        case "pretty":
            if (isAudioOnly) return `${title} (${infoBase[0]})`;
            filename = `${title} (${[...basicTags, infoBase[0]].join(", ")})`;
            break;
        case "nerdy":
            if (isAudioOnly) return `${title} (${infoBase.join(", ")})`;
            filename = `${title} (${basicTags.concat(infoBase).join(", ")})`;
            break;
    }
    return `${filename}.${f.extension}`;
}