From 4da81d16dc160357dfe15657464fef83932d23d9 Mon Sep 17 00:00:00 2001 From: AnthonyDuan Date: Sat, 22 Nov 2025 22:29:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(upload):=20=E5=A4=84=E7=90=86=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=20ID=EF=BC=8C=E5=87=BA=E7=8E=B0=20DuplicateKey=20?= =?UTF-8?q?=E6=97=B6=E6=9B=BF=E6=8D=A2=E7=8E=B0=E6=9C=89=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=B9=B6=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 46825f1..d8ae298 100644 --- a/app.py +++ b/app.py @@ -33,6 +33,7 @@ from flask_session import Session from flask_wtf.csrf import CSRFProtect, generate_csrf, CSRFError from ffmpy import FFmpeg from pymongo import MongoClient +from pymongo.errors import DuplicateKeyError from redis import Redis def take_config(name, required=False): @@ -852,8 +853,12 @@ def upload_file(): db_entry['enabled'] = True pprint.pprint(db_entry) - # mongoDBにデータをぶち込む - client['taiko']["songs"].insert_one(db_entry) + # mongoDBにデータをぶち込む(重複IDは上書き) + coll = client['taiko']["songs"] + try: + coll.insert_one(db_entry) + except DuplicateKeyError: + coll.replace_one({"id": db_entry["id"]}, db_entry, upsert=True) # キャッシュ削除(/api/songs) try: app.cache.delete_memoized(route_api_songs)