refactor: simplify file manager properties

This commit is contained in:
James Murdza 2024-10-25 06:57:28 -06:00
parent dc4be6392a
commit 28e6e2f889
2 changed files with 40 additions and 43 deletions

View File

@ -4,12 +4,6 @@ import RemoteFileStorage from "./RemoteFileStorage"
import { MAX_BODY_SIZE } from "./ratelimit" import { MAX_BODY_SIZE } from "./ratelimit"
import { TFile, TFileData, TFolder } from "./types" import { TFile, TFileData, TFolder } from "./types"
// Define the structure for sandbox files
export type SandboxFiles = {
files: (TFolder | TFile)[]
fileData: TFileData[]
}
// Convert list of paths to the hierchical file structure used by the editor // Convert list of paths to the hierchical file structure used by the editor
function generateFileStructure(paths: string[]): (TFolder | TFile)[] { function generateFileStructure(paths: string[]): (TFolder | TFile)[] {
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] } const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
@ -52,20 +46,22 @@ function generateFileStructure(paths: string[]): (TFolder | TFile)[] {
export class FileManager { export class FileManager {
private sandboxId: string private sandboxId: string
private sandbox: Sandbox private sandbox: Sandbox
public sandboxFiles: SandboxFiles public files: (TFolder | TFile)[]
public fileData: TFileData[]
private fileWatchers: WatchHandle[] = [] private fileWatchers: WatchHandle[] = []
private dirName = "/home/user/project" private dirName = "/home/user/project"
private refreshFileList: (files: SandboxFiles) => void private refreshFileList: (files: (TFolder | TFile)[]) => void
// Constructor to initialize the FileManager // Constructor to initialize the FileManager
constructor( constructor(
sandboxId: string, sandboxId: string,
sandbox: Sandbox, sandbox: Sandbox,
refreshFileList: (files: SandboxFiles) => void refreshFileList: (files: (TFolder | TFile)[]) => void
) { ) {
this.sandboxId = sandboxId this.sandboxId = sandboxId
this.sandbox = sandbox this.sandbox = sandbox
this.sandboxFiles = { files: [], fileData: [] } this.files = []
this.fileData = []
this.refreshFileList = refreshFileList this.refreshFileList = refreshFileList
} }
@ -110,16 +106,16 @@ export class FileManager {
private async updateFileData(): Promise<TFileData[]> { private async updateFileData(): Promise<TFileData[]> {
const remotePaths = await RemoteFileStorage.getSandboxPaths(this.sandboxId) const remotePaths = await RemoteFileStorage.getSandboxPaths(this.sandboxId)
const localPaths = this.getLocalFileIds(remotePaths) const localPaths = this.getLocalFileIds(remotePaths)
this.sandboxFiles.fileData = await this.generateFileData(localPaths) this.fileData = await this.generateFileData(localPaths)
return this.sandboxFiles.fileData return this.fileData
} }
// Update file structure // Update file structure
private async updateFileStructure(): Promise<(TFolder | TFile)[]> { private async updateFileStructure(): Promise<(TFolder | TFile)[]> {
const remotePaths = await RemoteFileStorage.getSandboxPaths(this.sandboxId) const remotePaths = await RemoteFileStorage.getSandboxPaths(this.sandboxId)
const localPaths = this.getLocalFileIds(remotePaths) const localPaths = this.getLocalFileIds(remotePaths)
this.sandboxFiles.files = generateFileStructure(localPaths) this.files = generateFileStructure(localPaths)
return this.sandboxFiles.files return this.files
} }
// Initialize the FileManager // Initialize the FileManager
@ -130,7 +126,7 @@ export class FileManager {
await this.updateFileData() await this.updateFileData()
// Copy all files from the project to the container // Copy all files from the project to the container
const promises = this.sandboxFiles.fileData.map(async (file) => { const promises = this.fileData.map(async (file) => {
try { try {
const filePath = path.join(this.dirName, file.id) const filePath = path.join(this.dirName, file.id)
const parentDirectory = path.dirname(filePath) const parentDirectory = path.dirname(filePath)
@ -209,7 +205,7 @@ export class FileManager {
// Handle file/directory creation event // Handle file/directory creation event
if (event.type === "create") { if (event.type === "create") {
const folder = findFolderById( const folder = findFolderById(
this.sandboxFiles.files, this.files,
sandboxDirectory sandboxDirectory
) as TFolder ) as TFolder
const isDir = await this.isDirectory(containerFilePath) const isDir = await this.isDirectory(containerFilePath)
@ -232,7 +228,7 @@ export class FileManager {
folder.children.push(newItem) folder.children.push(newItem)
} else { } else {
// If folder doesn't exist, add the new item to the root // If folder doesn't exist, add the new item to the root
this.sandboxFiles.files.push(newItem) this.files.push(newItem)
} }
if (!isDir) { if (!isDir) {
@ -241,7 +237,7 @@ export class FileManager {
) )
const fileContents = const fileContents =
typeof fileData === "string" ? fileData : "" typeof fileData === "string" ? fileData : ""
this.sandboxFiles.fileData.push({ this.fileData.push({
id: sandboxFilePath, id: sandboxFilePath,
data: fileContents, data: fileContents,
}) })
@ -253,7 +249,7 @@ export class FileManager {
// Handle file/directory removal or rename event // Handle file/directory removal or rename event
else if (event.type === "remove" || event.type == "rename") { else if (event.type === "remove" || event.type == "rename") {
const folder = findFolderById( const folder = findFolderById(
this.sandboxFiles.files, this.files,
sandboxDirectory sandboxDirectory
) as TFolder ) as TFolder
const isDir = await this.isDirectory(containerFilePath) const isDir = await this.isDirectory(containerFilePath)
@ -269,13 +265,13 @@ export class FileManager {
) )
} else { } else {
// Remove from the root if it's not inside a folder // Remove from the root if it's not inside a folder
this.sandboxFiles.files = this.sandboxFiles.files.filter( this.files = this.files.filter(
(file: TFolder | TFile) => !isFileMatch(file) (file: TFolder | TFile) => !isFileMatch(file)
) )
} }
// Also remove any corresponding file data // Also remove any corresponding file data
this.sandboxFiles.fileData = this.sandboxFiles.fileData.filter( this.fileData = this.fileData.filter(
(file: TFileData) => !isFileMatch(file) (file: TFileData) => !isFileMatch(file)
) )
@ -285,10 +281,10 @@ export class FileManager {
// Handle file write event // Handle file write event
else if (event.type === "write") { else if (event.type === "write") {
const folder = findFolderById( const folder = findFolderById(
this.sandboxFiles.files, this.files,
sandboxDirectory sandboxDirectory
) as TFolder ) as TFolder
const fileToWrite = this.sandboxFiles.fileData.find( const fileToWrite = this.fileData.find(
(file) => file.id === sandboxFilePath (file) => file.id === sandboxFilePath
) )
@ -308,7 +304,7 @@ export class FileManager {
) )
const fileContents = const fileContents =
typeof fileData === "string" ? fileData : "" typeof fileData === "string" ? fileData : ""
this.sandboxFiles.fileData.push({ this.fileData.push({
id: sandboxFilePath, id: sandboxFilePath,
data: fileContents, data: fileContents,
}) })
@ -318,7 +314,7 @@ export class FileManager {
} }
// Tell the client to reload the file list // Tell the client to reload the file list
this.refreshFileList(this.sandboxFiles) this.refreshFileList(this.files)
} catch (error) { } catch (error) {
console.error( console.error(
`Error handling ${event.type} event for ${event.name}:`, `Error handling ${event.type} event for ${event.name}:`,
@ -350,7 +346,7 @@ export class FileManager {
// Get file content // Get file content
async getFile(fileId: string): Promise<string | undefined> { async getFile(fileId: string): Promise<string | undefined> {
const file = this.sandboxFiles.fileData.find((f) => f.id === fileId) const file = this.fileData.find((f) => f.id === fileId)
return file?.data return file?.data
} }
@ -368,7 +364,7 @@ export class FileManager {
throw new Error("File size too large. Please reduce the file size.") throw new Error("File size too large. Please reduce the file size.")
} }
await RemoteFileStorage.saveFile(this.getRemoteFileId(fileId), body) await RemoteFileStorage.saveFile(this.getRemoteFileId(fileId), body)
const file = this.sandboxFiles.fileData.find((f) => f.id === fileId) const file = this.fileData.find((f) => f.id === fileId)
if (!file) return if (!file) return
file.data = body file.data = body
@ -381,9 +377,9 @@ export class FileManager {
fileId: string, fileId: string,
folderId: string folderId: string
): Promise<(TFolder | TFile)[]> { ): Promise<(TFolder | TFile)[]> {
const fileData = this.sandboxFiles.fileData.find((f) => f.id === fileId) const fileData = this.fileData.find((f) => f.id === fileId)
const file = this.sandboxFiles.files.find((f) => f.id === fileId) const file = this.files.find((f) => f.id === fileId)
if (!fileData || !file) return this.sandboxFiles.files if (!fileData || !file) return this.files
const parts = fileId.split("/") const parts = fileId.split("/")
const newFileId = folderId + "/" + parts.pop() const newFileId = folderId + "/" + parts.pop()
@ -427,13 +423,13 @@ export class FileManager {
await this.sandbox.files.write(path.posix.join(this.dirName, id), "") await this.sandbox.files.write(path.posix.join(this.dirName, id), "")
await this.fixPermissions() await this.fixPermissions()
this.sandboxFiles.files.push({ this.files.push({
id, id,
name, name,
type: "file", type: "file",
}) })
this.sandboxFiles.fileData.push({ this.fileData.push({
id, id,
data: "", data: "",
}) })
@ -451,8 +447,8 @@ export class FileManager {
// Rename a file // Rename a file
async renameFile(fileId: string, newName: string): Promise<void> { async renameFile(fileId: string, newName: string): Promise<void> {
const fileData = this.sandboxFiles.fileData.find((f) => f.id === fileId) const fileData = this.fileData.find((f) => f.id === fileId)
const file = this.sandboxFiles.files.find((f) => f.id === fileId) const file = this.files.find((f) => f.id === fileId)
if (!fileData || !file) return if (!fileData || !file) return
const parts = fileId.split("/") const parts = fileId.split("/")
@ -468,11 +464,11 @@ export class FileManager {
// Delete a file // Delete a file
async deleteFile(fileId: string): Promise<(TFolder | TFile)[]> { async deleteFile(fileId: string): Promise<(TFolder | TFile)[]> {
const file = this.sandboxFiles.fileData.find((f) => f.id === fileId) const file = this.fileData.find((f) => f.id === fileId)
if (!file) return this.sandboxFiles.files if (!file) return this.files
await this.sandbox.files.remove(path.posix.join(this.dirName, fileId)) await this.sandbox.files.remove(path.posix.join(this.dirName, fileId))
this.sandboxFiles.fileData = this.sandboxFiles.fileData.filter( this.fileData = this.fileData.filter(
(f) => f.id !== fileId (f) => f.id !== fileId
) )
@ -487,7 +483,7 @@ export class FileManager {
await Promise.all( await Promise.all(
files.map(async (file) => { files.map(async (file) => {
await this.sandbox.files.remove(path.posix.join(this.dirName, file)) await this.sandbox.files.remove(path.posix.join(this.dirName, file))
this.sandboxFiles.fileData = this.sandboxFiles.fileData.filter( this.fileData = this.fileData.filter(
(f) => f.id !== file (f) => f.id !== file
) )
await RemoteFileStorage.deleteFile(this.getRemoteFileId(file)) await RemoteFileStorage.deleteFile(this.getRemoteFileId(file))

View File

@ -3,7 +3,7 @@ import { Socket } from 'socket.io'
import { AIWorker } from "./AIWorker" import { AIWorker } from "./AIWorker"
import { CONTAINER_TIMEOUT } from "./constants" import { CONTAINER_TIMEOUT } from "./constants"
import { DokkuClient } from "./DokkuClient" import { DokkuClient } from "./DokkuClient"
import { FileManager, SandboxFiles } from "./FileManager" import { FileManager } from "./FileManager"
import { import {
createFileRL, createFileRL,
createFolderRL, createFolderRL,
@ -13,6 +13,7 @@ import {
} from "./ratelimit" } from "./ratelimit"
import { SecureGitClient } from "./SecureGitClient" import { SecureGitClient } from "./SecureGitClient"
import { TerminalManager } from "./TerminalManager" import { TerminalManager } from "./TerminalManager"
import { TFile, TFolder } from "./types"
import { LockManager } from "./utils" import { LockManager } from "./utils"
const lockManager = new LockManager() const lockManager = new LockManager()
@ -81,12 +82,12 @@ export class SandboxManager {
this.fileManager = new FileManager( this.fileManager = new FileManager(
this.sandboxId, this.sandboxId,
this.container, this.container,
(files: SandboxFiles) => { (files: (TFolder | TFile)[]) => {
this.socket.emit("loaded", files.files) this.socket.emit("loaded", files)
} }
) )
this.fileManager.initialize() this.fileManager.initialize()
this.socket.emit("loaded", this.fileManager.sandboxFiles.files) this.socket.emit("loaded", this.fileManager.files)
} }
} }
@ -133,7 +134,7 @@ export class SandboxManager {
const handleDeploy: SocketHandler = async (_: any) => { const handleDeploy: SocketHandler = async (_: any) => {
if (!this.gitClient) throw Error("No git client") if (!this.gitClient) throw Error("No git client")
if (!this.fileManager) throw Error("No file manager") if (!this.fileManager) throw Error("No file manager")
const fixedFilePaths = this.fileManager?.sandboxFiles.fileData.map((file) => ({ const fixedFilePaths = this.fileManager?.fileData.map((file) => ({
...file, ...file,
id: file.id.split("/").slice(2).join("/"), id: file.id.split("/").slice(2).join("/"),
})) }))