completed game importing; partial work on version importing

This commit is contained in:
DecDuck
2024-10-11 00:37:08 +11:00
parent 718f5ba514
commit a7c33e7d43
42 changed files with 1499 additions and 281 deletions

View File

@ -45,10 +45,10 @@ export class FsObjectBackend extends ObjectBackend {
return false;
}
async create(
id: string,
source: Source,
metadata: ObjectMetadata
): Promise<ObjectReference | undefined> {
const id = uuidv4();
const objectPath = path.join(this.baseObjectPath, sanitize(id));
const metadataPath = path.join(
this.baseMetadataPath,

View File

@ -17,6 +17,7 @@
import { parse as getMimeTypeBuffer } from "file-type-mime";
import { Readable } from "stream";
import { getMimeType as getMimeTypeStream } from "stream-mime-type";
import { v4 as uuidv4 } from "uuid";
export type ObjectReference = string;
export type ObjectMetadata = {
@ -46,6 +47,7 @@ export abstract class ObjectBackend {
abstract fetch(id: ObjectReference): Promise<Source | undefined>;
abstract write(id: ObjectReference, source: Source): Promise<boolean>;
abstract create(
id: string,
source: Source,
metadata: ObjectMetadata
): Promise<ObjectReference | undefined>;
@ -59,6 +61,7 @@ export abstract class ObjectBackend {
): Promise<boolean>;
async createFromSource(
id: string,
sourceFetcher: () => Promise<Source>,
metadata: { [key: string]: string },
permissions: Array<string>
@ -83,13 +86,11 @@ export abstract class ObjectBackend {
if (!mime)
throw new Error("Unable to calculate MIME type - is the source empty?");
const objectId = this.create(source, {
await this.create(id, source, {
permissions,
userMetadata: metadata,
mime,
});
return objectId;
}
async fetchWithPermissions(id: ObjectReference, userId?: string) {

View File

@ -2,7 +2,9 @@
The purpose of this class is to hold references to remote objects (like images) until they're actually needed
This is used as a utility in metadata handling, so we only fetch the objects if we're actually creating a database record.
*/
import { v4 as uuidv4 } from 'uuid';
import { Readable } from "stream";
import { v4 as uuidv4 } from "uuid";
import { GlobalObjectHandler } from "~/server/plugins/objects";
type TransactionTable = { [key: string]: string }; // ID to URL
type GlobalTransactionRecord = { [key: string]: TransactionTable }; // Transaction ID to table
@ -12,27 +14,38 @@ type Pull = () => Promise<void>;
type Dump = () => void;
export class ObjectTransactionalHandler {
private record: GlobalTransactionRecord = {};
private record: GlobalTransactionRecord = {};
new(): [Register, Pull, Dump] {
const transactionId = uuidv4();
new(
metadata: { [key: string]: string },
permissions: Array<string>
): [Register, Pull, Dump] {
const transactionId = uuidv4();
const register = (url: string) => {
const objectId = uuidv4();
this.record[transactionId][objectId] = url;
this.record[transactionId] ??= {};
return objectId;
}
const register = (url: string) => {
const objectId = uuidv4();
this.record[transactionId][objectId] = url;
const pull = async () => {
// Dummy function
dump();
}
return objectId;
};
const dump = () => {
delete this.record[transactionId];
}
const pull = async () => {
for (const [id, url] of Object.entries(this.record[transactionId])) {
await GlobalObjectHandler.createFromSource(
id,
() => $fetch<Readable>(url, { responseType: "stream" }),
metadata,
permissions
);
}
};
return [register, pull, dump];
}
}
const dump = () => {
delete this.record[transactionId];
};
return [register, pull, dump];
}
}