108 lines
4.7 KiB
Python
108 lines
4.7 KiB
Python
"""로고 그림을 서버에서 직접 넣는 운영 도구.
|
|
|
|
관리자 페이지의 '로고' 메뉴와 같은 저장 방식과 검사를 쓴다. 서버 밖에서 만든 그림을
|
|
후보 목록에 넣거나, 미리 만든 크기별 PNG로 로고를 바로 바꿀 때 쓴다. 작업은 관리 작업 기록에 남는다.
|
|
서비스 계정으로 실행한다(데이터 폴더 권한 때문):
|
|
|
|
sudo -u duty-calendar env DUTY_DATA=/var/lib/duty-calendar /opt/duty-calendar/venv/bin/python brand_import.py \\
|
|
add-candidate 그림.jpg --prompt-file 설명.txt --actor syskiller
|
|
sudo -u duty-calendar env DUTY_DATA=/var/lib/duty-calendar /opt/duty-calendar/venv/bin/python brand_import.py \\
|
|
apply PNG폴더 --candidate 후보ID --actor syskiller (폴더에 logo-512.png logo-192.png logo-180.png logo-64.png logo-maskable.png)
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import secrets
|
|
import sqlite3
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from brand_admin import FILES, SIZES, image_kind, png_size
|
|
|
|
DATA = Path(os.environ.get("DUTY_DATA", Path(__file__).parent / "data"))
|
|
|
|
|
|
def connect():
|
|
conn = sqlite3.connect(DATA / "calendar.sqlite3", timeout=10)
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
return conn
|
|
|
|
|
|
def actor_id(conn, username):
|
|
row = conn.execute("SELECT id FROM members WHERE username=? AND role='admin' AND active=1", (username,)).fetchone()
|
|
if row is None:
|
|
sys.exit("이용 중인 관리자 아이디가 아닙니다: " + username)
|
|
return row[0]
|
|
|
|
|
|
def log(conn, actor, action, detail):
|
|
conn.execute("INSERT INTO admin_log(actor,area,target,action,detail,created) VALUES(?,?,?,?,?,?)",
|
|
(actor, "site", "", action, detail[:500], int(time.time())))
|
|
|
|
|
|
def add_candidate(args):
|
|
raw = Path(args.file).read_bytes()
|
|
mime, ext = image_kind(raw)
|
|
if not mime or len(raw) > 20_000_000:
|
|
sys.exit("PNG·JPG·WebP 그림이 아니거나 너무 큽니다.")
|
|
prompt = Path(args.prompt_file).read_text(encoding="utf8").strip()[:1500] if args.prompt_file else "서버에서 가져온 그림"
|
|
folder = DATA / "brand" / "candidates"
|
|
folder.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
cid = secrets.token_hex(8)
|
|
(folder / (cid + "." + ext)).write_bytes(raw)
|
|
with connect() as conn:
|
|
who = actor_id(conn, args.actor)
|
|
conn.execute("INSERT INTO brand_candidates(id,created,prompt,model,ext,actor) VALUES(?,?,?,?,?,?)",
|
|
(cid, int(time.time()), prompt, args.model[:80], ext, who))
|
|
log(conn, who, "logo-import", "운영 도구 · " + Path(args.file).name)
|
|
print(cid)
|
|
|
|
|
|
def apply(args):
|
|
source = Path(args.folder)
|
|
data = {}
|
|
for name, size in SIZES.items():
|
|
raw = (source / FILES[name]).read_bytes()
|
|
if png_size(raw) != (size, size):
|
|
sys.exit(FILES[name] + " 이 " + str(size) + "px PNG가 아닙니다.")
|
|
data[name] = raw
|
|
brand = DATA / "brand"
|
|
with connect() as conn:
|
|
who = actor_id(conn, args.actor)
|
|
name = conn.execute("SELECT display_name FROM members WHERE id=?", (who,)).fetchone()[0]
|
|
if args.candidate and not conn.execute("SELECT 1 FROM brand_candidates WHERE id=?", (args.candidate,)).fetchone():
|
|
sys.exit("후보를 찾을 수 없습니다: " + args.candidate)
|
|
# 관리자 화면과 같은 순서: 표식을 먼저 지우고 파일을 바꿔 끼운 뒤 표식을 다시 쓴다.
|
|
(brand / "logo.json").unlink(missing_ok=True)
|
|
for key, raw in data.items():
|
|
temp = brand / (FILES[key] + ".tmp")
|
|
temp.write_bytes(raw)
|
|
temp.replace(brand / FILES[key])
|
|
meta = {"updated": int(time.time()), "by": name, "source": "candidate" if args.candidate else "upload", "candidate": args.candidate}
|
|
temp = brand / "logo.json.tmp"
|
|
temp.write_text(json.dumps(meta, ensure_ascii=False))
|
|
temp.replace(brand / "logo.json")
|
|
log(conn, who, "logo", "운영 도구 · " + ("후보 " + args.candidate if args.candidate else "미리 만든 그림"))
|
|
print("로고를 바꿨습니다.")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="로고 그림 넣기")
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
one = sub.add_parser("add-candidate")
|
|
one.add_argument("file")
|
|
one.add_argument("--prompt-file")
|
|
one.add_argument("--model", default="서버에서 가져옴")
|
|
one.add_argument("--actor", required=True)
|
|
two = sub.add_parser("apply")
|
|
two.add_argument("folder")
|
|
two.add_argument("--candidate")
|
|
two.add_argument("--actor", required=True)
|
|
args = parser.parse_args()
|
|
(add_candidate if args.command == "add-candidate" else apply)(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|