File size: 1,547 Bytes
7475ce4
524d49f
 
 
 
 
 
834ef3b
 
524d49f
 
 
 
7475ce4
 
 
524d49f
7475ce4
524d49f
 
7475ce4
0bf3050
7475ce4
 
 
 
 
0bf3050
 
7475ce4
 
f692912
 
524d49f
7475ce4
f692912
7475ce4
524d49f
7475ce4
 
524d49f
 
21317c6
d9e7de9
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
from flask import Flask, request, jsonify, send_from_directory
import base64
import os

app = Flask(__name__)

@app.route('/')
def index():
    return send_from_directory(".", "index.html")

@app.route('/upload_audio', methods=['POST'])
def upload_audio():
    try:
        data = request.get_json()
        if not data:
            return jsonify({"error": "JSONが送信されていません"}), 400

        audio_data = data.get('audio_data')
        if not audio_data:
            return jsonify({"error": "音声データが送信されていません"}), 400

        # Base64デコード
        try:
            audio_binary = base64.b64decode(audio_data)
        except Exception as decode_err:
            return jsonify({"error": "Base64デコードに失敗しました", "details": str(decode_err)}), 400

        # 書き込み用ディレクトリとして /tmp/data を使用(/tmp は書き込み可能)
        persist_dir = "/tmp/data"
        os.makedirs(persist_dir, exist_ok=True)

        filepath = os.path.join(persist_dir, "recorded_audio.wav")
        with open(filepath, 'wb') as f:
            f.write(audio_binary)

        return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200

    except Exception as e:
        app.logger.error("エラー: %s", str(e))
        return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 7860))
    app.run(debug=True, host="0.0.0.0", port=port)