Files
CT/src/Model/Files/fileModel.ts
T

171 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-07-21 21:41:19 +02:00
// this file is responsible for managing files within chats.
2024-07-22 02:05:27 +02:00
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
2024-07-22 03:01:04 +02:00
import StorageModel, { filePaths } from "../Global/storageModel";
2024-07-22 11:49:05 +02:00
import {
createTimestamp,
parseValidObject,
stringify,
} from "../Utility/utility";
2024-07-22 02:05:27 +02:00
import ChatModel from "../Chat/chatModel";
2024-07-22 02:16:58 +02:00
import TaskModel from "./taskModel";
2024-07-22 00:37:36 +02:00
import { v4 } from "uuid";
2024-07-21 21:45:18 +02:00
export default class FileModel {
chatModel: ChatModel;
2024-07-21 23:32:20 +02:00
storageModel: StorageModel;
2024-07-21 21:45:18 +02:00
2024-07-22 02:16:58 +02:00
taskModel: TaskModel;
2024-07-22 03:01:04 +02:00
// paths
getBasePath = (): string[] => {
return StorageModel.getPath(
"chat",
filePaths.chat.files(this.chatModel.id)
);
};
getFilePath = (fileId: string): string[] => {
return [...this.getBasePath(), fileId];
};
getFileContentPath = (file: File, fileContentName: string): string[] => {
const filePath: string[] = this.getFilePath(file.id);
return [...filePath, fileContentName];
};
// handler
handleStringifiedFile = (stringifiedFile: string): void => {
const file: File | null = parseValidObject(stringifiedFile, FileReference);
if (file == null) return;
2024-07-22 00:37:36 +02:00
2024-07-22 11:49:05 +02:00
this.handleFileAndClearContents(file);
};
2024-07-22 11:49:05 +02:00
handleFileAndClearContents = (file: File): void => {
2024-07-21 23:32:20 +02:00
for (const fileContent of file.contentVersions) {
this.storeFileContent(file, fileContent);
2024-07-22 11:49:05 +02:00
this.taskModel.handleTaskFileContent(file, fileContent);
file.contentVersions.delete(fileContent);
2024-07-21 23:32:20 +02:00
}
};
2024-07-22 11:49:05 +02:00
// methods
addOrUpdateFile = (file: File): void => {
this.chatModel.sendMessage("", file);
this.handleFileAndClearContents(file);
};
// storage
storeFileContent = (file: File, fileContent: FileContent<string>): void => {
2024-07-21 23:32:20 +02:00
const fileContentName: string = FileModel.getFileContentName(fileContent);
2024-07-22 03:01:04 +02:00
const fileContentPath: string[] = this.getFileContentPath(
2024-07-21 23:32:20 +02:00
file,
fileContentName
);
2024-07-22 00:37:36 +02:00
// check if fileContent already exists
2024-07-22 01:22:59 +02:00
const existingFileContent: string | null =
this.storageModel.read(fileContentPath);
2024-07-22 00:37:36 +02:00
if (existingFileContent != null) return;
2024-07-21 23:32:20 +02:00
const stringifiedContent: string = stringify(fileContent);
this.storageModel.write(fileContentPath, stringifiedContent);
};
listFileIds = (): string[] => {
2024-07-22 03:01:04 +02:00
return this.storageModel.list(this.getBasePath());
2024-07-21 23:32:20 +02:00
};
getFile = (fileId: string): File | null => {
2024-07-22 03:01:04 +02:00
const filePath = this.getFilePath(fileId);
2024-07-22 01:38:08 +02:00
const fileOrNull: File | null = this.storageModel.readStringifiable(
filePath,
FileReference
);
2024-07-21 23:32:20 +02:00
return fileOrNull;
};
2024-07-22 11:57:47 +02:00
listFileContents = (file: File): string[] => {
const filePath: string[] = this.getFilePath(file.id);
return this.storageModel.list(filePath);
};
2024-07-21 23:32:20 +02:00
getLatestFileContentName = (fileContentNames: string[]): string | null => {
return fileContentNames[fileContentNames.length - 1];
};
getFileContent = (
file: File,
fileContentName: string
2024-07-22 11:49:05 +02:00
): FileContent<string> | null => {
2024-07-22 03:01:04 +02:00
const filePath = this.getFileContentPath(file, fileContentName);
2024-07-22 11:49:05 +02:00
const fileContentOrNull: FileContent<string> | null =
2024-07-22 01:38:08 +02:00
this.storageModel.readStringifiable(filePath, FileContentReference);
2024-07-21 23:32:20 +02:00
return fileContentOrNull;
};
2024-07-21 21:45:18 +02:00
// init
2024-07-21 23:32:20 +02:00
constructor(chatModel: ChatModel, storageModel: StorageModel) {
2024-07-21 21:45:18 +02:00
this.chatModel = chatModel;
2024-07-21 23:32:20 +02:00
this.storageModel = storageModel;
2024-07-22 02:16:58 +02:00
this.taskModel = new TaskModel(chatModel, this);
2024-07-21 21:45:18 +02:00
}
2024-07-21 21:41:19 +02:00
2024-07-21 23:32:20 +02:00
// utility
2024-07-22 11:49:05 +02:00
static getFileContentName = (fileContent: FileContent<string>): string => {
2024-07-22 01:05:19 +02:00
return fileContent.creationDate + fileContent.id;
2024-07-21 23:32:20 +02:00
};
2024-07-21 20:54:18 +02:00
2024-07-22 00:37:36 +02:00
static createFile = (): File => {
return {
dataVersion: DATA_VERSION,
id: v4(),
2024-07-22 11:49:05 +02:00
contentVersions: new Set(),
};
};
static createFileContent = <T extends string>(type: T): FileContent<T> => {
return {
dataVersion: DATA_VERSION,
id: v4(),
creationDate: createTimestamp(),
type,
2024-07-22 01:22:59 +02:00
};
};
2024-07-21 20:54:18 +02:00
}
2024-07-21 23:32:20 +02:00
// types
export interface File extends ValidObject {
2024-07-22 01:05:19 +02:00
id: string;
2024-07-22 11:49:05 +02:00
contentVersions: Set<FileContent<string>>;
2024-07-21 23:32:20 +02:00
}
2024-07-21 20:54:18 +02:00
2024-07-22 11:49:05 +02:00
export interface FileContent<T extends string> extends ValidObject {
2024-07-22 01:05:19 +02:00
id: string;
creationDate: string;
2024-07-21 23:32:20 +02:00
2024-07-22 11:49:05 +02:00
type: T;
2024-07-21 20:54:18 +02:00
}
2024-07-22 01:22:59 +02:00
// references
2024-07-22 11:49:05 +02:00
export const FileContentReference: FileContent<string> = {
2024-07-22 01:22:59 +02:00
dataVersion: DATA_VERSION,
id: "",
creationDate: "",
type: "",
};
export const FileReference: File = {
dataVersion: DATA_VERSION,
id: "",
2024-07-22 11:49:05 +02:00
contentVersions: new Set([FileContentReference]),
2024-07-22 01:38:08 +02:00
};