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-21 22:42:32 +02:00
|
|
|
|
2024-07-22 02:05:27 +02:00
|
|
|
import ChatModel from "../Chat/chatModel";
|
2024-07-22 17:23:48 +02:00
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
getFileContainerPath = (): string[] => {
|
|
|
|
|
return [...this.getBasePath(), subDirectories.data];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getModelContainerPath = (
|
|
|
|
|
modelName: keyof typeof subDirectories
|
2024-07-22 12:39:26 +02:00
|
|
|
): string[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
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[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
return [...this.getFileContainerPath(), fileId];
|
2024-07-22 03:01:04 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
getFileContentPath = (fileId: string, fileContentId: string): string[] => {
|
2024-07-22 17:23:48 +02:00
|
|
|
const filePath: string[] = this.getFilePath(fileId);
|
2024-07-22 18:11:18 +02:00
|
|
|
return [...filePath, fileContentId];
|
2024-07-22 03:01:04 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-21 22:42:32 +02:00
|
|
|
// handler
|
2024-07-22 18:11:18 +02:00
|
|
|
handleStringifiedFileContent = (stringifiedFileContent: string): void => {
|
|
|
|
|
const fileContent: FileContent<string> | null = parseValidObject(
|
|
|
|
|
stringifiedFileContent,
|
|
|
|
|
FileContentReference
|
2024-07-22 17:23:48 +02:00
|
|
|
);
|
2024-07-22 18:11:18 +02:00
|
|
|
if (fileContent == null) return;
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
this.handleFileContent(fileContent);
|
2024-07-21 22:42:32 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
handleFileContent = (fileContent: FileContent<string>): void => {
|
|
|
|
|
const didStore: boolean = this.storeFileContent(fileContent);
|
2024-07-22 17:23:48 +02:00
|
|
|
if (didStore == false) return;
|
2024-07-22 14:36:03 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
this.taskModel.handleFileContent(fileContent);
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 11:49:05 +02:00
|
|
|
// methods
|
2024-07-22 18:11:18 +02:00
|
|
|
addFileContent = (fileContent: FileContent<string>): void => {
|
|
|
|
|
this.handleFileContent(fileContent);
|
|
|
|
|
this.chatModel.sendMessage("", fileContent);
|
2024-07-22 11:49:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// storage
|
2024-07-22 18:11:18 +02:00
|
|
|
storeFileContent = (fileContent: FileContent<string>): boolean => {
|
|
|
|
|
const fileContentPath: string[] = this.getFileContentPath(
|
|
|
|
|
fileContent.fileId,
|
|
|
|
|
fileContent.fileContentId
|
2024-07-21 23:32:20 +02:00
|
|
|
);
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
// check if fileContent already exists
|
|
|
|
|
const existingFileContent: string | null =
|
|
|
|
|
this.storageModel.read(fileContentPath);
|
|
|
|
|
if (existingFileContent != null) return false;
|
2024-07-22 00:37:36 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
const stringifiedContent: string = stringify(fileContent);
|
|
|
|
|
this.storageModel.write(fileContentPath, stringifiedContent);
|
2024-07-22 17:23:48 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
listFileContentIds = (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);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
selectLatestFileContentId = (
|
|
|
|
|
fileContentIds: string[]
|
2024-07-22 17:23:48 +02:00
|
|
|
): string | undefined => {
|
2024-07-22 18:11:18 +02:00
|
|
|
return fileContentIds[fileContentIds.length - 1];
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
getFileContent = <T extends FileContent<string>>(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string,
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentName: string,
|
2024-07-22 17:23:48 +02:00
|
|
|
reference: T
|
|
|
|
|
): T | null => {
|
2024-07-22 18:11:18 +02:00
|
|
|
const filePath: string[] = this.getFileContentPath(fileId, fileContentName);
|
|
|
|
|
const fileContentOrNull: T | null = this.storageModel.readStringifiable(
|
2024-07-22 17:23:48 +02:00
|
|
|
filePath,
|
|
|
|
|
reference
|
|
|
|
|
);
|
2024-07-22 18:11:18 +02:00
|
|
|
return fileContentOrNull;
|
2024-07-22 17:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
getLatestFileContent = <T extends FileContent<string>>(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string,
|
|
|
|
|
reference: T
|
|
|
|
|
): T | null => {
|
2024-07-22 18:11:18 +02:00
|
|
|
const fileContentsIds: string[] = this.listFileContentIds(fileId);
|
|
|
|
|
const latestFileContentId: string | undefined =
|
|
|
|
|
this.selectLatestFileContentId(fileContentsIds);
|
|
|
|
|
if (latestFileContentId == undefined) return null;
|
2024-07-22 17:23:48 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
const fileContent: T | null = this.getFileContent(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId,
|
2024-07-22 18:11:18 +02:00
|
|
|
latestFileContentId,
|
2024-07-22 17:23:48 +02:00
|
|
|
reference
|
|
|
|
|
);
|
2024-07-22 18:11:18 +02:00
|
|
|
return fileContent;
|
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
|
2024-07-22 18:11:18 +02:00
|
|
|
static generateFileContentId = (creationDate): string => {
|
2024-07-22 17:23:48 +02:00
|
|
|
return creationDate + v4();
|
2024-07-21 23:32:20 +02:00
|
|
|
};
|
2024-07-21 20:54:18 +02:00
|
|
|
|
2024-07-22 18:11:18 +02:00
|
|
|
static createFileContent = <T extends string>(
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string,
|
|
|
|
|
type: T
|
2024-07-22 18:11:18 +02:00
|
|
|
): FileContent<T> => {
|
2024-07-22 17:23:48 +02:00
|
|
|
const creationDate: string = createTimestamp();
|
2024-07-22 18:11:18 +02:00
|
|
|
const fileContentId: string = FileModel.generateFileContentId(creationDate);
|
2024-07-22 11:49:05 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId,
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentId,
|
2024-07-22 17:23:48 +02:00
|
|
|
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
|
2024-07-22 17:23:48 +02:00
|
|
|
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
|
2024-07-22 18:11:18 +02:00
|
|
|
export interface FileContent<T extends string> extends ValidObject {
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: string;
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentId: 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
|
2024-07-22 18:11:18 +02:00
|
|
|
export const FileContentReference: FileContent<string> = {
|
2024-07-22 01:22:59 +02:00
|
|
|
dataVersion: DATA_VERSION,
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
fileId: "",
|
2024-07-22 18:11:18 +02:00
|
|
|
fileContentId: "",
|
2024-07-22 01:22:59 +02:00
|
|
|
creationDate: "",
|
|
|
|
|
|
2024-07-22 17:23:48 +02:00
|
|
|
type: "",
|
2024-07-22 01:38:08 +02:00
|
|
|
};
|