vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Render images and 3D models in the browser views
3b2bcc30534f24b4628b6365e69dcb9065115065 · Keenan Nemetz · 13d ago · parent 87bab8e1
Render images and 3D models in the browser views A blob whose name is an image is served as that image, so the <img> a README already emitted works and the source view can show it. An SVG keeps its source view and previews on request. The instance introduction resolves assets/, so a landing-page picture is no longer a broken link. A changed image or model shows both sides in the commit, compare, and change request diffs, where git only says "binary". The 3D viewer is a vendored three.js bundle, loaded only when a page has a model, with a download fallback for a reader without JavaScript or WebGL. closes: 01M7XQ9AKKKFNEX0F679JRRYQD
scripts/vendor-three.py +153
| @@ -0,0 +1,153 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | """Regenerate the vendored three.js bundle in server/web/static/models. | ||
| 3 | |||
| 4 | The 3D preview (static/models/model-viewer.js) needs three.js and four of its | ||
| 5 | example loaders, so the server embeds them as one minified ES module. app.js | ||
| 6 | imports model-viewer.js on demand, which is why a page without a model downloads | ||
| 7 | neither the viewer nor three.js. | ||
| 8 | |||
| 9 | server/web/static/models/three.bundle.js this script's output | ||
| 10 | server/web/static/models/three.LICENSE three.js's MIT license | ||
| 11 | server/web/static/models/model-viewer.js written by hand, imports the bundle | ||
| 12 | |||
| 13 | The bundle re-exports three's namespace and the loaders the viewer knows: | ||
| 14 | |||
| 15 | THREE, OrbitControls, STLLoader, OBJLoader, GLTFLoader, ThreeMFLoader | ||
| 16 | |||
| 17 | esbuild resolves the loaders' bare `three` imports and their own dependencies | ||
| 18 | (BufferGeometryUtils, SkeletonUtils, and fflate for 3MF) into the one file, so the | ||
| 19 | browser needs no import map and the CSP stays script-src 'self'. | ||
| 20 | |||
| 21 | scripts/vendor-three.py write the files | ||
| 22 | scripts/vendor-three.py --check compare, exit 1 if they differ | ||
| 23 | |||
| 24 | Bumping THREE_VERSION is the whole upgrade: rerun, then run the tests, which hold | ||
| 25 | the server's model extension table (server/web/media.go) to what the viewer can | ||
| 26 | load. node and npm are needed to regenerate the bundle; running Vole is not. | ||
| 27 | """ | ||
| 28 | |||
| 29 | import argparse | ||
| 30 | import os | ||
| 31 | import subprocess | ||
| 32 | import sys | ||
| 33 | import tarfile | ||
| 34 | import tempfile | ||
| 35 | import urllib.request | ||
| 36 | |||
| 37 | THREE_VERSION = "0.187.0" | ||
| 38 | ESBUILD_VERSION = "0.28.2" | ||
| 39 | |||
| 40 | PACKAGE_URL = "https://registry.npmjs.org/three/-/three-{version}.tgz" | ||
| 41 | |||
| 42 | STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, | ||
| 43 | "server", "web", "static", "models") | ||
| 44 | BUNDLE = "three.bundle.js" | ||
| 45 | LICENSE = "three.LICENSE" | ||
| 46 | |||
| 47 | # Re-exported from the bundle. The viewer imports exactly these names, so | ||
| 48 | # esbuild can drop the rest of three instead of shipping every export. Adding a | ||
| 49 | # loader or a three class is a change here and in model-viewer.js. | ||
| 50 | ENTRY = """export { | ||
| 51 | WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight, | ||
| 52 | Box3, Vector3, Group, Mesh, MeshStandardMaterial, SRGBColorSpace, | ||
| 53 | } from 'three'; | ||
| 54 | export { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; | ||
| 55 | export { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js'; | ||
| 56 | export { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js'; | ||
| 57 | export { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; | ||
| 58 | export { ThreeMFLoader } from 'three/examples/jsm/loaders/3MFLoader.js'; | ||
| 59 | """ | ||
| 60 | |||
| 61 | BANNER = ( | ||
| 62 | "/* three.js {version} for Vole's 3D preview: three.module.js with the\n" | ||
| 63 | " OrbitControls, STL, OBJ, glTF and 3MF loaders. Generated by\n" | ||
| 64 | " scripts/vendor-three.py; edit that, not this file. MIT license: three.LICENSE. */" | ||
| 65 | ) | ||
| 66 | |||
| 67 | |||
| 68 | def fetch_package(work): | ||
| 69 | url = PACKAGE_URL.format(version=THREE_VERSION) | ||
| 70 | archive = os.path.join(work, "three.tgz") | ||
| 71 | urllib.request.urlretrieve(url, archive) | ||
| 72 | with tarfile.open(archive, "r:gz") as tar: | ||
| 73 | try: | ||
| 74 | # Refuse links and absolute paths in the archive; the filter argument | ||
| 75 | # is newer than the oldest Python this repository may be built with. | ||
| 76 | tar.extractall(work, filter="data") | ||
| 77 | except TypeError: | ||
| 78 | tar.extractall(work) | ||
| 79 | package = os.path.join(work, "package") | ||
| 80 | if not os.path.isdir(package): | ||
| 81 | raise SystemExit("vendor-three: the tarball has no package directory") | ||
| 82 | return package | ||
| 83 | |||
| 84 | |||
| 85 | def build(work, package): | ||
| 86 | node_modules = os.path.join(work, "node_modules") | ||
| 87 | os.makedirs(node_modules, exist_ok=True) | ||
| 88 | os.symlink(package, os.path.join(node_modules, "three")) | ||
| 89 | entry = os.path.join(work, "entry.js") | ||
| 90 | with open(entry, "w", encoding="utf-8") as f: | ||
| 91 | f.write(ENTRY) | ||
| 92 | out = os.path.join(work, BUNDLE) | ||
| 93 | subprocess.run( | ||
| 94 | [ | ||
| 95 | "npx", "--yes", f"esbuild@{ESBUILD_VERSION}", entry, | ||
| 96 | "--bundle", "--minify", "--format=esm", "--target=es2020", | ||
| 97 | "--legal-comments=eof", | ||
| 98 | "--log-level=warning", | ||
| 99 | f"--banner:js={BANNER.format(version='r' + THREE_VERSION.split('.')[1])}", | ||
| 100 | f"--outfile={out}", | ||
| 101 | ], | ||
| 102 | check=True, | ||
| 103 | ) | ||
| 104 | with open(out, "rb") as f: | ||
| 105 | bundle = f.read() | ||
| 106 | with open(os.path.join(package, "LICENSE"), "rb") as f: | ||
| 107 | license_text = f.read() | ||
| 108 | return bundle, license_text | ||
| 109 | |||
| 110 | |||
| 111 | def main(): | ||
| 112 | parser = argparse.ArgumentParser(description=__doc__) | ||
| 113 | parser.add_argument("--check", action="store_true", | ||
| 114 | help="compare the committed files, do not write") | ||
| 115 | args = parser.parse_args() | ||
| 116 | |||
| 117 | if not os.path.isdir(STATIC_DIR): | ||
| 118 | os.makedirs(STATIC_DIR, exist_ok=True) | ||
| 119 | |||
| 120 | with tempfile.TemporaryDirectory() as work: | ||
| 121 | try: | ||
| 122 | package = fetch_package(work) | ||
| 123 | bundle, license_text = build(work, package) | ||
| 124 | except FileNotFoundError as err: | ||
| 125 | raise SystemExit(f"vendor-three: {err}: node and npx are needed to build the bundle") | ||
| 126 | except subprocess.CalledProcessError as err: | ||
| 127 | raise SystemExit(f"vendor-three: esbuild failed: {err}") | ||
| 128 | |||
| 129 | outputs = {BUNDLE: bundle, LICENSE: license_text} | ||
| 130 | failed = False | ||
| 131 | for name, want in outputs.items(): | ||
| 132 | path = os.path.join(STATIC_DIR, name) | ||
| 133 | current = None | ||
| 134 | if os.path.exists(path): | ||
| 135 | with open(path, "rb") as f: | ||
| 136 | current = f.read() | ||
| 137 | if args.check: | ||
| 138 | if current != want: | ||
| 139 | print(f"vendor-three: {name} is out of date", file=sys.stderr) | ||
| 140 | failed = True | ||
| 141 | else: | ||
| 142 | with open(path, "wb") as f: | ||
| 143 | f.write(want) | ||
| 144 | print(f"wrote {os.path.relpath(path)}") | ||
| 145 | if failed: | ||
| 146 | return 1 | ||
| 147 | if args.check: | ||
| 148 | print("vendor-three: up to date") | ||
| 149 | return 0 | ||
| 150 | |||
| 151 | |||
| 152 | if __name__ == "__main__": | ||
| 153 | sys.exit(main()) | ||