Skip to content

Commit

Permalink
add vvlib manifest validation
Browse files Browse the repository at this point in the history
  • Loading branch information
y-chan committed Jun 16, 2023
1 parent 115e9a1 commit 959770b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ async def block_origin_middleware(request: Request, call_next):
engine_manifest_loader = EngineManifestLoader(
root_dir / "engine_manifest.json", root_dir
)
library_manager = LibraryManager(get_save_dir() / "installed_libraries")
library_manager = LibraryManager(
get_save_dir() / "installed_libraries", engine_manifest_loader.load_manifest()
)

metas_store = MetasStore(root_dir / "speaker_info")

Expand Down
36 changes: 34 additions & 2 deletions voicevox_engine/downloadable_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
from typing import Dict

from fastapi import HTTPException
from pydantic import ValidationError
from semver.version import Version

from voicevox_engine.model import DownloadableLibrary, InstalledLibrary
from voicevox_engine.engine_manifest import EngineManifest
from voicevox_engine.model import DownloadableLibrary, InstalledLibrary, VvlibManifest

__all__ = ["LibraryManager"]

INFO_FILE = "metas.json"


class LibraryManager:
def __init__(self, library_root_dir: Path):
def __init__(self, library_root_dir: Path, engine_manifest: EngineManifest):
self.library_root_dir = library_root_dir
self.library_root_dir.mkdir(exist_ok=True)
self.supported_vvlib_version = engine_manifest.supported_vvlib_manifest_version

def downloadable_libraries(self):
# == ダウンロード情報をネットワーク上から取得する場合
Expand Down Expand Up @@ -84,5 +88,33 @@ def install_library(self, library_id: str, file: BytesIO):
if zf.testzip() is not None:
raise HTTPException(status_code=422, detail="不正なZIPファイルです。")

# validate manifest version
vvlib_manifest = None
try:
vvlib_manifest = json.loads(
zf.read("vvlib_manifest.json").decode("utf-8")
)
except KeyError:
raise HTTPException(
status_code=422, detail="指定された音声ライブラリにvvlib_manifest.jsonが存在しません。"
)
except:
raise HTTPException(
status_code=422, detail="指定された音声ライブラリのvvlib_manifest.jsonは不正です。"
)

try:
VvlibManifest.validate(vvlib_manifest)
except ValidationError:
raise HTTPException(
status_code=422,
detail="指定された音声ライブラリのvvlib_manifest.jsonに不正なデータが含まれています。",
)

if Version.parse(vvlib_manifest["manifest_version"]) > Version.parse(
self.supported_vvlib_version
):
raise HTTPException(status_code=422, detail="指定された音声ライブラリは未対応です。")

zf.extractall(library_dir)
return library_dir
14 changes: 14 additions & 0 deletions voicevox_engine/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,17 @@ class SupportedFeaturesInfo(BaseModel):
support_adjusting_silence_scale: bool = Field(title="前後の無音時間が調節可能かどうか")
support_interrogative_upspeak: bool = Field(title="疑似疑問文に対応しているかどうか")
support_switching_device: bool = Field(title="CPU/GPUの切り替えが可能かどうか")


class VvlibManifest(BaseModel):
"""
vvlib(VOICEVOX Library)に関する情報
"""

manifest_version: str = Field(title="マニフェストバージョン")
name: str = Field(title="音声ライブラリ名")
version: str = Field(title="音声ライブラリバージョン")
uuid: str = Field(title="音声ライブラリのUUID")
brand_name: str = Field(title="エンジンのブランド名")
engine_name: str = Field(title="エンジン名")
engine_uuid: str = Field(title="エンジンのUUID")

0 comments on commit 959770b

Please sign in to comment.