| 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) |
|
|
|
|
| |
| 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'] |
| |
| |
| 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 |
| |
| 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 |
|
|
| 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) |
|
|