Spaces:
Running
Running
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) | |
# データベース接続設定 | |
db_config = { | |
'host': 'sql309.infinityfree.com', | |
'user': 'if0_36911347', | |
'password': 'j9RRoK4mhTstU', | |
'database': 'if0_36911347_i_scratch_data', | |
'cursorclass': pymysql.cursors.DictCursor | |
} | |
# ファイルを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): | |
try: | |
connection = pymysql.connect(**db_config) | |
with connection.cursor() as cursor: | |
# データベースにデータを挿入 | |
sql = """INSERT INTO projects (thumbnail, group, name, explanation, file_data, like, view, upload_date, update_date) | |
VALUES (%s, %s, %s, %s, %s, 0, 0, NOW(), NOW())""" | |
cursor.execute(sql, (thumbnail, group, name, explanation, file_data)) | |
connection.commit() | |
except Exception as e: | |
print(f"Error: {e}") | |
finally: | |
connection.close() | |
# プロジェクトのアップロードフォーム | |
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') | |
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 }, "https://soiz1-penguinmod-editor.hf.space"); | |
window.close(); | |
} | |
} | |
window.close(); | |
</script>""" | |
def close2(): | |
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 }, "https://soiz1-penguinmod-editor-2.hf.space"); | |
window.close(); | |
} | |
} | |
window.close(); | |
</script>""" | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=7860) | |