File size: 1,710 Bytes
3324de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import server
import folder_paths

web = server.web


@server.PromptServer.instance.routes.get("/tangoflux/playaudio")
async def play_audio(request):
    query = request.rel_url.query

    filename = query.get("filename", None)

    if filename is None:
        return web.Response(status=404)
    
    if filename[0] == "/" or ".." in filename:
        return web.Response(status=403)

    filename, output_dir = folder_paths.annotated_filepath(filename)

    if not output_dir:
        file_type = query.get("type", "output")
        output_dir = folder_paths.get_directory_by_type(file_type)

    if output_dir is None:
        return web.Response(status=400)

    subfolder = query.get("subfolder", None)
    if subfolder:
        full_output_dir = os.path.join(output_dir, subfolder)
        if os.path.commonpath((os.path.abspath(full_output_dir), output_dir)) != output_dir:
            return web.Response(status=403)
        output_dir = full_output_dir

    filename = os.path.basename(filename)
    file_path = os.path.join(output_dir, filename)

    if not os.path.isfile(file_path):
        return web.Response(status=404)

    _, ext = os.path.splitext(filename)
    ext = ext.lower()

    content_types = {
        ".wav": "audio/wav",
        ".mp3": "audio/mpeg",
        ".flac": "audio/flac",
        ".aac": "audio/aac",
        ".wma": "audio/x-ms-wma",
    }

    content_type = content_types.get(ext, None)

    if content_type is None:
        return web.Response(status=400)

    try:
        with open(file_path, "rb") as file:
            data = file.read()
    except:
        return web.Response(status=500)

    return web.Response(body=data, content_type=content_type)