File size: 5,608 Bytes
a3c64dc
 
8199055
 
a3c64dc
8199055
 
 
a3c64dc
 
 
 
8199055
a3c64dc
 
 
 
 
8199055
 
872b75e
8199055
 
 
 
 
 
a3c64dc
8199055
 
a3c64dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8199055
 
 
 
 
 
 
0485f08
086d1b5
029ef30
e4b9efa
 
 
 
010d6b2
e4b9efa
 
 
 
 
8199055
354cd44
970d141
9949bb2
354cd44
 
 
970d141
 
354cd44
 
970d141
354cd44
 
970d141
 
 
f6e95ca
778df5f
44dd7bd
f6e95ca
 
adb3811
f6e95ca
adb3811
 
f6e95ca
adb3811
f6e95ca
 
adb3811
f6e95ca
 
 
 
44dd7bd
 
f6e95ca
 
adb3811
44dd7bd
adb3811
f6e95ca
44dd7bd
adb3811
f6e95ca
adb3811
44dd7bd
f6e95ca
 
 
adb3811
 
f6e95ca
 
 
 
 
 
adb3811
 
f6e95ca
 
 
 
 
f75f7c7
 
 
 
 
 
 
 
 
 
291e34d
f75f7c7
 
 
 
 
 
 
44dd7bd
8199055
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import base64
from flask import Flask, render_template, request, redirect, url_for
import pymysql
import magic

app = Flask(__name__)

# アップロードされたファイルを保存するためのディレクトリ
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)


# ファイルをbase64エンコードする関数
def file_to_base64(file_path):
    with open(file_path, "rb") as f:
        return base64.b64encode(f.read()).decode('utf-8')

# 新しいプロジェクトをデータベースにアップロードする関数
def upload_project(thumbnail, group, name, explanation, file_data):
    pass

# プロジェクトのアップロードフォーム
@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # フォームからのデータを取得
        thumbnail_file = request.files['thumbnail']
        name = request.form['name']
        explanation = request.form['explanation']
        file_data_file = request.files['file_data']
        
        # 画像とファイルのBase64エンコード
        if thumbnail_file:
            thumbnail_path = os.path.join(UPLOAD_FOLDER, thumbnail_file.filename)
            thumbnail_file.save(thumbnail_path)
            thumbnail = file_to_base64(thumbnail_path)
        else:
            thumbnail = None  # サムネイルがなければNoneに設定
        
        if file_data_file:
            file_data_path = os.path.join(UPLOAD_FOLDER, file_data_file.filename)
            file_data_file.save(file_data_path)
            file_data = file_to_base64(file_data_path)
        else:
            file_data = None  # txtファイルがない場合の処理

        group = ""  # 所属グループは空白
        
        # プロジェクトをデータベースにアップロード
        upload_project(thumbnail, group, name, explanation, file_data)
        
        return redirect(url_for('index'))
    
    return render_template('index.html')
    
@app.route('/close', methods=['GET', 'POST'])
def close():
    return """<script>if (window.opener) {
  const hashParams = new URLSearchParams(window.location.hash.substring(1));
  const token = hashParams.get("access_token");
  if (token) {
    window.opener.postMessage({ token }, "*");
    window.close();
  }
}
window.close();
</script>"""


@app.route('/close2', methods=['GET', 'POST'])
def close2():
    target_origin = request.args.get('target_origin', '*')  # デフォルトはワイルドカード
    return f"""<script>
if (window.opener) {{
  const hashParams = new URLSearchParams(window.location.hash.substring(1));
  const token = hashParams.get("access_token");
  if (token) {{
    window.opener.postMessage({{ token }}, "{target_origin}");
    window.close();
  }}
}}
window.close();
</script>"""

@app.route('/close3')
def close3():
    target_origin = request.args.get('target_origin', '*')
    return f'''
    <!DOCTYPE html>
    <html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>認証処理中...</title>
        <script>
            window.addEventListener('load', function () {{
                const hashParams = new URLSearchParams(window.location.hash.substring(1));
                const queryParams = new URLSearchParams(window.location.search);

                const tokenData = {{
                    access_token: hashParams.get('access_token'),
                    expires_in: hashParams.get('expires_in'),
                    error: hashParams.get('error') || queryParams.get('error'),
                    state: queryParams.get('state'),
                    close_window: true  // ✅ ここで一緒に送る
                }};

                console.log("Token Data:", tokenData);

                if (window.opener && typeof window.opener.postMessage === 'function') {{
                    try {{
                        window.opener.postMessage(tokenData, '{target_origin}');
                        console.log('✅ メッセージ送信成功');
                    }} catch (e) {{
                        console.error('❌ postMessage 送信エラー:', e);
                        window.opener.postMessage({{ error: 'postmessage_failed' }}, '{target_origin}');
                    }}
                    setTimeout(() => window.close(), 500);
                }} else {{
                    console.warn("❌ window.opener が存在しません。ポップアップがブロックされた可能性があります。");
                    document.body.innerHTML += '<p style="color:red;">ポップアップブロックが原因で処理が中断されました。<br>このウィンドウを閉じてやり直してください。</p>';
                }}
            }});
        </script>
    </head>
    <body>
        <div style="padding: 20px; text-align: center;">
            <h3>認証処理中...</h3>
            <p>このウィンドウは自動的に閉じられます</p>
            <button onclick="window.close()">手動で閉じる</button>
        </div>
    </body>
    </html>
    '''
    
@app.route('/close4')
def close4():
    query = request.query_string.decode()
    return f'''
    <html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>ログイン中...</title>
        <script>
            window.location.href = "https://scratch-school.ct.ws/login.php?{query}";
        </script>
    </head>
    <body>
        <p>ログイン処理中です。しばらくお待ちください...</p>
    </body>
    </html>
    '''

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)