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

192 lines
4.8 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";
import { Color } from "../../ViewModel/colors";
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)
);
};
getFileContainerPath = (): string[] => {
return [...this.getBasePath(), subDirectories.data];
};
getModelContainerPath = (
modelName: keyof typeof subDirectories
2024-07-22 12:39:26 +02:00
): string[] => {
return [...this.getBasePath(), subDirectories.model, modelName];
2024-07-22 12:39:26 +02:00
};
2024-07-22 03:01:04 +02:00
getFilePath = (fileId: string): string[] => {
return [...this.getFileContainerPath(), fileId];
2024-07-22 03:01:04 +02:00
};
getFileVersionPath = (fileId: string, fileVersionId: string): string[] => {
const filePath: string[] = this.getFilePath(fileId);
return [...filePath, fileVersionId];
2024-07-22 03:01:04 +02:00
};
// handler
handleStringifiedFileVersion = (stringifiedFileVersion: string): void => {
const fileVersion: FileVersion<string> | null = parseValidObject(
stringifiedFileVersion,
FileVersionReference
);
if (fileVersion == null) return;
2024-07-22 00:37:36 +02:00
this.handleFileVersion(fileVersion);
};
handleFileVersion = (fileVersion: FileVersion<string>): void => {
const didStore: boolean = this.storeFileVersion(fileVersion);
if (didStore == false) return;
2024-07-22 14:36:03 +02:00
this.taskModel.handleFileVersion(fileVersion);
2024-07-21 23:32:20 +02:00
};
2024-07-22 11:49:05 +02:00
// methods
addFileVersion = (fileVersion: FileVersion<string>): void => {
this.handleFileVersion(fileVersion);
this.chatModel.sendMessage("", fileVersion);
2024-07-22 11:49:05 +02:00
};
// storage
storeFileVersion = (fileVersion: FileVersion<string>): boolean => {
const fileVersionPath: string[] = this.getFileVersionPath(
fileVersion.fileId,
fileVersion.fileVersionId
2024-07-21 23:32:20 +02:00
);
2024-07-22 00:37:36 +02:00
// check if fileVersion already exists
const existingFileVersion: string | null =
this.storageModel.read(fileVersionPath);
if (existingFileVersion != null) return false;
2024-07-22 00:37:36 +02:00
const stringifiedContent: string = stringify(fileVersion);
this.storageModel.write(fileVersionPath, stringifiedContent);
return true;
2024-07-21 23:32:20 +02:00
};
listFileIds = (): string[] => {
2024-07-22 03:01:04 +02:00
return this.storageModel.list(this.getBasePath());
2024-07-21 23:32:20 +02:00
};
listFileVersionIds = (fileId: string): string[] => {
2024-07-22 13:09:33 +02:00
const filePath: string[] = this.getFilePath(fileId);
2024-07-22 11:57:47 +02:00
return this.storageModel.list(filePath);
};
selectLatestFileVersionId = (
fileVersionIds: string[]
): string | undefined => {
return fileVersionIds[fileVersionIds.length - 1];
2024-07-21 23:32:20 +02:00
};
getFileVersion = <T extends FileVersion<string>>(
fileId: string,
fileVersionName: string,
reference: T
): T | null => {
const filePath: string[] = this.getFileVersionPath(fileId, fileVersionName);
const fileVersionOrNull: T | null = this.storageModel.readStringifiable(
filePath,
reference
);
return fileVersionOrNull;
};
getLatestFileVersion = <T extends FileVersion<string>>(
fileId: string,
reference: T
): T | null => {
const fileVersionsIds: string[] = this.listFileVersionIds(fileId);
const latestFileVersionId: string | undefined =
this.selectLatestFileVersionId(fileVersionsIds);
if (latestFileVersionId == undefined) return null;
const fileVersion: T | null = this.getFileVersion(
fileId,
latestFileVersionId,
reference
);
return fileVersion;
2024-07-21 23:32:20 +02:00
};
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
2024-07-22 13:09:33 +02:00
this.taskModel = new TaskModel(this.storageModel, 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
static generateFileVersionId = (creationDate): string => {
return creationDate + v4();
2024-07-21 23:32:20 +02:00
};
2024-07-21 20:54:18 +02:00
static createFileVersion = <T extends string>(
fileId: string,
type: T
): FileVersion<T> => {
const creationDate: string = createTimestamp();
const fileVersionId: string = FileModel.generateFileVersionId(creationDate);
2024-07-22 11:49:05 +02:00
return {
dataVersion: DATA_VERSION,
fileId,
fileVersionId,
creationDate,
2024-07-22 11:49:05 +02:00
type,
2024-07-22 01:22:59 +02:00
};
};
2024-07-21 20:54:18 +02:00
}
2024-07-22 12:39:26 +02:00
// paths
export const subDirectories = {
data: "data",
model: "model",
2024-07-22 12:39:26 +02:00
taskModel: "tasks",
};
2024-07-21 23:32:20 +02:00
// types
export interface FileVersion<T extends string> extends ValidObject {
fileId: string;
fileVersionId: string;
2024-07-22 01:05:19 +02:00
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
export const FileVersionReference: FileVersion<string> = {
2024-07-22 01:22:59 +02:00
dataVersion: DATA_VERSION,
fileId: "",
fileVersionId: "",
2024-07-22 01:22:59 +02:00
creationDate: "",
type: "",
2024-07-22 01:38:08 +02:00
};