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

213 lines
5.9 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-26 03:14:55 +02:00
import StorageModel, {
2026-04-08 14:02:12 +00:00
StorageModelSubPaths,
2026-04-01 08:29:39 +00:00
filePaths,
2024-07-26 03:14:55 +02:00
} from "../Global/storageModel";
2024-07-22 11:49:05 +02:00
import {
2026-04-01 08:29:39 +00:00
StringEntryObject,
createTimestamp,
parseValidObject,
stringify,
2024-07-22 11:49:05 +02:00
} from "../Utility/utility";
import BoardsAndTasksModel from "./boardsAndTasksModel";
2024-07-22 02:05:27 +02:00
import ChatModel from "../Chat/chatModel";
2024-07-26 03:14:55 +02:00
import SettingsModel from "../Global/settingsModel";
2024-07-22 00:37:36 +02:00
import { v4 } from "uuid";
2024-07-21 21:45:18 +02:00
export default class FileModel {
2026-04-13 10:58:21 +00:00
readonly storageModel: StorageModel;
readonly settingsModel: SettingsModel;
2026-04-01 08:29:39 +00:00
2026-04-13 10:58:21 +00:00
readonly chatModel: ChatModel;
readonly boardsAndTasksModel: BoardsAndTasksModel;
2026-04-01 08:29:39 +00:00
// paths
getBasePath = (): string[] => {
return StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.files(this.chatModel.id),
);
};
getFileContainerPath = (): string[] => {
return [...this.getBasePath(), FileModelSubPath.Data];
};
getModelContainerPath = (modelName: FileModelSubPath): string[] => {
return [...this.getBasePath(), FileModelSubPath.Model, modelName];
};
getFilePath = (fileId: string): string[] => {
return [...this.getFileContainerPath(), fileId];
};
getFileContentPath = (fileId: string, fileContentId: string): string[] => {
const filePath: string[] = this.getFilePath(fileId);
return [...filePath, fileContentId];
};
// handlers
handleStringifiedFileContent = (stringifiedFileContent: string): void => {
const fileContent: FileContent<string> | null = parseValidObject(
stringifiedFileContent,
FileContentReference,
);
if (fileContent == null) return;
this.handleFileContent(fileContent);
};
handleFileContent = (fileContent: FileContent<string>): void => {
const didStore: boolean = this.storeFileContent(fileContent);
if (didStore == false) return;
this.boardsAndTasksModel.handleFileContent(fileContent);
2026-04-06 20:47:49 +00:00
this.chatModel.handleReaction(fileContent);
2026-04-01 08:29:39 +00:00
};
// methods
addFileContentAndSend = (fileContent: FileContent<string>): void => {
this.handleFileContent(fileContent);
this.chatModel.sendMessage("", undefined, fileContent);
2026-04-01 08:29:39 +00:00
};
// storage
storeFileContent = (fileContent: FileContent<string>): boolean => {
const fileContentPath: string[] = this.getFileContentPath(
fileContent.fileId,
fileContent.fileContentId,
);
// check if fileContent already exists
const existingFileContent: string | null =
this.storageModel.read(fileContentPath);
if (existingFileContent != null) return false;
const stringifiedContent: string = stringify(fileContent);
this.storageModel.write(fileContentPath, stringifiedContent);
return true;
};
listFileIds = (): string[] => {
return this.storageModel.list(this.getBasePath());
};
listFileContentIds = (fileId: string): string[] => {
const filePath: string[] = this.getFilePath(fileId);
return this.storageModel.list(filePath);
};
selectLatestFileContentId = (
fileContentIds: string[],
): string | undefined => {
return fileContentIds[fileContentIds.length - 1];
};
getFileContent = <T extends FileContent<string>>(
fileId: string,
fileContentName: string,
reference: T,
): T | null => {
const filePath: string[] = this.getFileContentPath(
fileId,
fileContentName,
);
const fileContentOrNull: T | null = this.storageModel.readStringifiable(
filePath,
reference,
);
return fileContentOrNull;
};
getLatestFileContent = <T extends FileContent<string>>(
fileId: string,
reference: T,
): T | null => {
const fileContentsIds: string[] = this.listFileContentIds(fileId);
const latestFileContentId: string | undefined =
this.selectLatestFileContentId(fileContentsIds);
if (latestFileContentId == undefined) return null;
const fileContent: T | null = this.getFileContent(
fileId,
latestFileContentId,
reference,
);
return fileContent;
};
// init
constructor(
storageModel: StorageModel,
settingsModel: SettingsModel,
chatModel: ChatModel,
) {
this.chatModel = chatModel;
this.settingsModel = settingsModel;
this.storageModel = storageModel;
this.boardsAndTasksModel = new BoardsAndTasksModel(
this.storageModel,
this.settingsModel,
chatModel,
this,
);
}
// utility
static generateFileContentId = (creationDate: string): string => {
return creationDate + v4();
};
static createFileContent = <T extends string>(
fileId: string,
type: T,
): FileContent<T> => {
const creationDate: string = createTimestamp();
const fileContentId: string =
FileModel.generateFileContentId(creationDate);
return {
dataVersion: DATA_VERSION,
fileId,
fileContentId,
creationDate,
type,
};
};
2024-07-21 20:54:18 +02:00
}
2024-07-22 12:39:26 +02:00
// paths
2024-07-23 18:23:26 +02:00
export enum FileModelSubPath {
2026-04-01 08:29:39 +00:00
Data = "data",
Model = "model",
ModelView = "view",
ModelTask = "tasks",
ModelCalendar = "calendar",
2024-07-26 03:14:55 +02:00
}
2024-07-22 12:39:26 +02:00
2024-07-21 23:32:20 +02:00
// types
2024-07-26 03:14:55 +02:00
export interface FileContent<T extends string>
2026-04-01 08:29:39 +00:00
extends ValidObject, StringEntryObject {
2026-04-13 10:58:21 +00:00
readonly fileId: string;
readonly fileContentId: string;
readonly creationDate: string;
2024-07-21 23:32:20 +02:00
2026-04-13 10:58:21 +00:00
readonly type: T;
2024-07-21 20:54:18 +02:00
}
2024-07-22 01:22:59 +02:00
// references
2024-07-22 18:11:18 +02:00
export const FileContentReference: FileContent<string> = {
2026-04-01 08:29:39 +00:00
dataVersion: DATA_VERSION,
2024-07-22 01:22:59 +02:00
2026-04-01 08:29:39 +00:00
fileId: "",
fileContentId: "",
creationDate: "",
2024-07-22 01:22:59 +02:00
2026-04-01 08:29:39 +00:00
type: "",
2024-07-22 01:38:08 +02:00
};