5 Commits

9 changed files with 112 additions and 101 deletions

56
app.py
View File

@@ -28,7 +28,6 @@ import tjaf
from functools import wraps
from flask import Flask, g, jsonify, render_template, request, abort, redirect, session, flash, make_response, send_from_directory
import mimetypes
from flask_caching import Cache
from flask_session import Session
from flask_wtf.csrf import CSRFProtect, generate_csrf, CSRFError
@@ -66,30 +65,37 @@ def get_remote_address() -> str:
limiter = Limiter(
get_remote_address,
app=app,
strategy="fixed-window",
storage_uri=os.environ.get("REDIS_URI") or "memory://",
# default_limits=[],
# storage_uri="memory://",
# Redis
storage_uri=os.environ.get("REDIS_URI", "redis://127.0.0.1:6379/"),
# Redis cluster
# storage_uri="redis+cluster://localhost:7000,localhost:7001,localhost:70002",
# Memcached
# storage_uri="memcached://localhost:11211",
# Memcached Cluster
# storage_uri="memcached://localhost:11211,localhost:11212,localhost:11213",
# MongoDB
# storage_uri="mongodb://localhost:27017",
# Etcd
# storage_uri="etcd://localhost:2379",
strategy="fixed-window", # or "moving-window"
)
client = MongoClient(host=os.environ.get("TAIKO_WEB_MONGO_HOST") or take_config('MONGO', required=True)['host'])
basedir = take_config('BASEDIR') or '/'
app.secret_key = take_config('SECRET_KEY') or 'change-me'
app.config['SESSION_TYPE'] = 'redis'
redis_config = take_config('REDIS', required=True)
redis_config['CACHE_REDIS_HOST'] = os.environ.get("TAIKO_WEB_REDIS_HOST") or redis_config['CACHE_REDIS_HOST']
try:
_r = Redis(
app.config['SESSION_REDIS'] = Redis(
host=redis_config['CACHE_REDIS_HOST'],
port=redis_config['CACHE_REDIS_PORT'],
password=redis_config['CACHE_REDIS_PASSWORD'],
db=redis_config['CACHE_REDIS_DB']
)
_r.ping()
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = _r
app.cache = Cache(app, config=redis_config)
except Exception:
app.config['SESSION_TYPE'] = 'filesystem'
app.cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})
sess = Session()
sess.init_app(app)
#csrf = CSRFProtect(app)
@@ -800,33 +806,7 @@ def send_assets(ref):
@app.route(basedir + "songs/<path:ref>")
def send_songs(ref):
path = os.path.normpath(os.path.join("public/songs", ref))
if not os.path.isfile(path):
return abort(404)
rng = flask.request.headers.get("Range")
if rng and re.match(r"^bytes=\d+-\d*$", rng):
size = os.path.getsize(path)
m = re.match(r"bytes=(\d+)-(\d*)", rng)
start = int(m.group(1))
end = int(m.group(2) or size - 1)
start = max(0, start)
end = min(size - 1, end)
with open(path, "rb") as f:
f.seek(start)
data = f.read(end - start + 1)
mime = mimetypes.guess_type(path)[0] or "application/octet-stream"
resp = flask.Response(data, 206, mimetype=mime)
resp.headers["Content-Range"] = f"bytes {start}-{end}/{size}"
resp.headers["Accept-Ranges"] = "bytes"
resp.headers["Cache-Control"] = "public, max-age=604800, s-maxage=604800"
resp.headers["CDN-Cache-Control"] = "max-age=604800"
return resp
resp = flask.send_from_directory("public/songs", ref)
res = flask.make_response(resp)
res.headers["Accept-Ranges"] = "bytes"
res.headers["Cache-Control"] = "public, max-age=604800, s-maxage=604800"
res.headers["CDN-Cache-Control"] = "max-age=604800"
return res
return cache_wrap(flask.send_from_directory("public/songs", ref), 604800)
@app.route(basedir + "manifest.json")
def send_manifest():

Binary file not shown.

View File

@@ -51,55 +51,6 @@ class RemoteFile{
request.responseType = "arraybuffer"
})
}
arrayBufferFast(threads){
var t = threads || 4
var head = new XMLHttpRequest()
head.open("HEAD", this.url)
var headPromise = pageEvents.load(head).then(() => {
if(head.status !== 200){
return Promise.reject()
}
var len = parseInt(head.getResponseHeader("Content-Length"))
if(!len || len < 262144){
return this.arrayBuffer()
}
var chunk = Math.ceil(len / t)
var ranges = []
for(var i = 0; i < t; i++){
var start = i * chunk
var end = Math.min(len - 1, (i + 1) * chunk - 1)
if(start > end){ break }
ranges.push([start, end])
}
var promises = ranges.map(r => {
var req = new XMLHttpRequest()
req.open("GET", this.url)
req.responseType = "arraybuffer"
req.setRequestHeader("Range", "bytes=" + r[0] + "-" + r[1])
return pageEvents.load(req).then(() => {
if(req.status !== 206 && req.status !== 200){
return Promise.reject()
}
return req.response
})
})
return Promise.all(promises).then(parts => {
var total = 0
for(var i = 0; i < parts.length; i++){
total += parts[i].byteLength
}
var out = new Uint8Array(total)
var offset = 0
for(var i = 0; i < parts.length; i++){
out.set(new Uint8Array(parts[i]), offset)
offset += parts[i].byteLength
}
return out.buffer
})
})
head.send()
return headPromise.catch(() => this.arrayBuffer())
}
read(encoding){
if(encoding){
return this.blob().then(blob => readFile(blob, false, encoding))

View File

@@ -1,4 +1,4 @@
class ImportSongs{
class ImportSongs{
constructor(...args){
this.init(...args)
}
@@ -322,6 +322,10 @@
songTitle = songTitle.slice(0, uraPos)
}
}
if(id === "cn" && !meta["titlecn"] && meta.titlezh){
titleLang.cn = meta.titlezh
titleLangAdded = true
}
if(meta["title" + id]){
titleLang[id] = meta["title" + id]
titleLangAdded = true
@@ -329,6 +333,10 @@
titleLang[id] = this.songTitle[songTitle][id] + ura
titleLangAdded = true
}
if(id === "cn" && !meta["subtitlecn"] && meta.subtitlezh){
subtitleLang.cn = meta.subtitlezh
subtitleLangAdded = true
}
if(meta["subtitle" + id]){
subtitleLang[id] = meta["subtitle" + id]
subtitleLangAdded = true

View File

@@ -2965,6 +2965,12 @@ class SongSelect{
var categoryName = song.category
var originalCategory = song.category
}
if(!categoryName){
if(song.song_type){
categoryName = song.song_type
originalCategory = song.song_type
}
}
var addedSong = {
title: title,
originalTitle: song.title,
@@ -3107,6 +3113,15 @@ class SongSelect{
getLocalTitle(title, titleLang){
if(titleLang){
if(strings.id === "cn"){
if(titleLang.cn){
return titleLang.cn
}
if(titleLang.ja){
return titleLang.ja
}
return title
}
for(var id in titleLang){
if(id === "en" && strings.preferEn && !(strings.id in titleLang) && titleLang.en || id === strings.id && titleLang[id]){
return titleLang[id]

View File

@@ -1,4 +1,4 @@
class SoundBuffer{
class SoundBuffer{
constructor(...args){
this.init(...args)
}
@@ -12,8 +12,7 @@ class SoundBuffer{
}
load(file, gain){
var decoder = file.name.endsWith(".ogg") ? this.oggDecoder : this.audioDecoder
var promise = typeof file.arrayBufferFast === "function" ? file.arrayBufferFast(4) : file.arrayBuffer()
return promise.then(response => {
return file.arrayBuffer().then(response => {
return new Promise((resolve, reject) => {
return decoder(response, resolve, reject)
}).catch(error => Promise.reject([error, file.url]))

View File

@@ -29,9 +29,9 @@
<div id="screen" class="pattern-bg"></div>
<div data-nosnippet id="version">
{% if version.version and version.commit_short and version.commit %}
<a href="{{version.url}}commit/{{version.commit}}" target="_blank" rel="noopener" id="version-link" class="stroke-sub" alt="taiko-web ver.{{version.version}} ({{version.commit_short}})">taiko-web ver.{{version.version}} ({{version.commit_short}})</a>
<a href="{{version.url}}commit/{{version.commit}}" target="_blank" rel="noopener" id="version-link" class="stroke-sub" alt="vLightNova 1.0.0">vLightNova 1.0.0</a>
{% else %}
<a target="_blank" rel="noopener" id="version-link" class="stroke-sub" alt="taiko-web vRAINBOW-BETA4">taiko-web vRAINBOW-BETA4</a>
<a target="_blank" rel="noopener" id="version-link" class="stroke-sub" alt="vLightNova 1.0.0">vLightNova 1.0.0</a>
{% endif %}
</div>
<script src="src/js/browsersupport.js?{{version.commit_short}}"></script>

22
tjaf.py
View File

@@ -7,6 +7,8 @@ class Tja:
self.text = text
self.title: Optional[str] = None
self.subtitle: Optional[str] = None
self.title_ja: Optional[str] = None
self.subtitle_ja: Optional[str] = None
self.wave: Optional[str] = None
self.offset: Optional[float] = None
self.courses: Dict[str, Dict[str, Optional[int]]] = {}
@@ -25,8 +27,12 @@ class Tja:
val = v.strip()
if key == "TITLE":
self.title = val or None
elif key == "TITLEJA":
self.title_ja = val or None
elif key == "SUBTITLE":
self.subtitle = val or None
elif key == "SUBTITLEJA":
self.subtitle_ja = val or None
elif key == "WAVE":
self.wave = val or None
elif key == "OFFSET":
@@ -73,8 +79,20 @@ class Tja:
"type": "tja",
"title": self.title,
"subtitle": self.subtitle,
"title_lang": {"ja": self.title, "en": None, "cn": None, "tw": None, "ko": None},
"subtitle_lang": {"ja": self.subtitle, "en": None, "cn": None, "tw": None, "ko": None},
"title_lang": {
"ja": self.title_ja or self.title,
"en": None,
"cn": self.title_ja or None,
"tw": None,
"ko": None,
},
"subtitle_lang": {
"ja": self.subtitle_ja or self.subtitle,
"en": None,
"cn": self.subtitle_ja or None,
"tw": None,
"ko": None,
},
"courses": courses_out,
"enabled": False,
"category_id": None,

40
update.sh Normal file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -Eeuo pipefail
if [ "${EUID}" -ne 0 ]; then echo "需要 root 权限"; exit 1; fi
SRC_DIR=$(cd "$(dirname "$0")" && pwd)
DEST_DIR=/srv/taiko-web
SONGS_DIR="$DEST_DIR/public/songs"
BACKUP_DIR="$DEST_DIR/.backup_songs_$(date +%Y%m%d_%H%M%S)"
systemctl stop taiko-web || true
if [ -d "$SONGS_DIR" ]; then
mkdir -p "$BACKUP_DIR"
rsync -a "$SONGS_DIR/" "$BACKUP_DIR/" || cp -a "$SONGS_DIR/." "$BACKUP_DIR/"
fi
mkdir -p "$DEST_DIR"
rsync -a --delete \
--exclude '.git' \
--exclude '.venv' \
--exclude 'public/songs' \
"$SRC_DIR/" "$DEST_DIR/"
if [ -x "$DEST_DIR/.venv/bin/pip" ]; then
"$DEST_DIR/.venv/bin/pip" install -U pip
"$DEST_DIR/.venv/bin/pip" install -r "$DEST_DIR/requirements.txt"
fi
chown -R www-data:www-data "$DEST_DIR"
if [ -d "$BACKUP_DIR" ]; then
mkdir -p "$SONGS_DIR"
rsync -a "$BACKUP_DIR/" "$SONGS_DIR/" || cp -a "$BACKUP_DIR/." "$SONGS_DIR/"
fi
systemctl daemon-reload || true
systemctl restart taiko-web || systemctl start taiko-web || true
systemctl is-active --quiet taiko-web