feat: migrate to asset uploader

This commit is contained in:
TrèsAbhi committed 2025-10-23 21:22:21 -05:00
1 parent 797307fb81
commit a3bb023309
29 files changed
+764 -1003

No files matched your search

@@ -67,7 +67,9 @@ export async function extractModel(data: string, path: string) {
.setMimeType("image/webp")
.setImage(
await readBaseColor(
`${data}/3d/${dirname(path)}/${textures.baseColorMap ?? textures.albedo}`,
`${data}/3d/${dirname(path)}/${
textures.baseColorMap ?? textures.albedo
}`,
textures.miscMap
? `${data}/3d/${dirname(path)}/${textures.miscMap}`
: undefined
@@ -99,7 +99,7 @@ export class AssetUploader {
}
if (diff.status === DiffStatus.Unchanged) {
console.log(`🔵 (${diff.change.toLocaleString()}B) ${change.path}`);
console.log(`🔵 ${change.path}`);
} else {
console.log(
`${diff.status === DiffStatus.New ? "🟢" : "🟡"} (${
@@ -1,27 +0,0 @@
import { assertSecret } from "@blitzkit/core";
import { chunk } from "lodash-es";
import { commitMultipleFiles, FileChange } from "./commitMultipleFiles";
export async function commitAssets(message: string, changes: FileChange[]) {
console.log(`Committing ${message}...`);
if (changes.length === 0) return;
const chunks = chunk(changes, 64);
let chunkIndex = 0;
for (const chunk of chunks) {
console.log(`Committing chunk ${chunkIndex + 1}/${chunks.length}`);
await commitMultipleFiles(
assertSecret(import.meta.env.PUBLIC_ASSET_REPO),
assertSecret(import.meta.env.PUBLIC_ASSET_BRANCH),
`${message} - ${new Date().toDateString()} (${chunkIndex + 1}/${
chunks.length
})`,
chunk
);
chunkIndex++;
}
}
@@ -1,135 +0,0 @@
import { asset } from "@blitzkit/core";
import ProgressBar from "progress";
import { compareUint8Arrays } from "./compareUint8Arrays";
import { type GithubChangeBlob, createBlob } from "./createBlob";
import { octokit } from "./octokit";
export interface FileChange {
path: string;
content: Uint8Array;
}
enum DiffStatus {
New,
Changed,
Unchanged,
}
const heuristicFormats = ["glb", "webp", "jpg", "png"];
export async function commitMultipleFiles(
repoRaw: string,
branch: string,
message: string,
changesRaw: FileChange[]
) {
const changes: FileChange[] = [];
await Promise.all(
changesRaw.map(async (change) => {
const response = await fetch(asset(change.path), {
headers: { "Accept-Encoding": "identity" },
});
let diff: { change: number; status: DiffStatus };
if (response.status === 404) {
diff = { change: change.content.length, status: DiffStatus.New };
} else if (
response.headers.has("Content-Length") &&
Number(response.headers.get("Content-Length")) !== change.content.length
) {
diff = {
change:
change.content.length -
Number(response.headers.get("Content-Length")),
status: DiffStatus.Changed,
};
} else if (response.status === 200) {
if (
heuristicFormats.some((format) => change.path.endsWith(`.${format}`))
) {
/**
* Heuristic: if it's a large blob like a .glb, a diff = 0 might end
* up being a false positive with isDifferent = true. So, it's okay
* to assume it's unchanged.
*/
diff = { change: 0, status: DiffStatus.Unchanged };
} else {
const buffer = new Uint8Array(await response.arrayBuffer());
if (compareUint8Arrays(buffer, change.content)) {
diff = { change: 0, status: DiffStatus.Unchanged };
} else {
diff = {
change: change.content.length - buffer.length,
status: DiffStatus.Changed,
};
}
}
} else {
throw new Error(
`Unexpected status code ${response.status} for ${change.path}`
);
}
if (diff.status === DiffStatus.Unchanged) {
// just don't bother
// console.log(`🔵 (${diff.change.toLocaleString()}B) ${change.path}`);
} else {
console.log(
`${diff.status === DiffStatus.New ? "🟢" : "🟡"} (${
diff.change > 0 ? "+" : ""
}${diff.change.toLocaleString()}B) ${change.path}`
);
changes.push(change);
}
})
);
if (changes.length === 0) return;
const [owner, repo] = repoRaw.split("/");
const latestCommitSha = (
await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
})
).data.object.sha;
const treeSha = (
await octokit.git.getCommit({
owner,
repo,
commit_sha: latestCommitSha,
})
).data.tree.sha;
const blobs: GithubChangeBlob[] = [];
const bar = new ProgressBar("Blobs :bar", changes.length);
for (const change of changes) {
const createdBlob = await createBlob(owner, repo, change);
blobs.push(createdBlob!);
bar.tick();
}
const { data: treeData } = await octokit.git.createTree({
owner,
repo,
base_tree: treeSha,
tree: blobs,
});
const { data: newCommitData } = await octokit.git.createCommit({
owner,
repo,
message,
tree: treeData.sha,
parents: [latestCommitSha],
});
await octokit.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitData.sha,
force: true,
});
}
@@ -1,5 +1,5 @@
import { throttle } from "lodash-es";
import type { FileChange } from "./commitMultipleFiles";
import { FileChange } from "./assetUploader";
import { octokit } from "./octokit";
export interface GithubChangeBlob {