File service improvements

This commit is contained in:
Hakan Shehu
2024-11-22 21:02:50 +01:00
parent d025058f16
commit 5d5aa6f4b9
4 changed files with 49 additions and 29 deletions

View File

@@ -85,7 +85,7 @@ const createDownloadsTable: Migration = {
await db.schema
.createTable('downloads')
.addColumn('node_id', 'text', (col) =>
col.notNull().primaryKey().references('nodes.id')
col.notNull().primaryKey().references('nodes.id').onDelete('cascade')
)
.addColumn('upload_id', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
@@ -105,7 +105,7 @@ const createUploadsTable: Migration = {
await db.schema
.createTable('uploads')
.addColumn('node_id', 'text', (col) =>
col.notNull().primaryKey().references('nodes.id')
col.notNull().primaryKey().references('nodes.id').onDelete('cascade')
)
.addColumn('upload_id', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())

View File

@@ -35,6 +35,15 @@ class FileService {
this.syncWorkspaceDownloads(event.userId);
} else if (event.type === 'upload_created') {
this.syncWorkspaceUploads(event.userId);
} else if (event.type === 'node_created' && event.node.type === 'file') {
this.syncWorkspaceDownloads(event.userId);
this.syncWorkspaceUploads(event.userId);
} else if (event.type === 'node_updated' && event.node.type === 'file') {
this.syncWorkspaceDownloads(event.userId);
this.syncWorkspaceUploads(event.userId);
} else if (event.type === 'node_deleted' && event.node.type === 'file') {
this.syncWorkspaceDownloads(event.userId);
this.syncWorkspaceUploads(event.userId);
}
});
}
@@ -138,7 +147,9 @@ class FileService {
.execute();
for (const workspace of workspaces) {
this.uploadWorkspaceFiles(workspace.user_id);
await this.syncWorkspaceUploads(workspace.user_id);
await this.syncWorkspaceDownloads(workspace.user_id);
await this.checkDeletedFiles(workspace.user_id);
}
}
@@ -211,26 +222,6 @@ class FileService {
private async uploadWorkspaceFiles(userId: string): Promise<void> {
this.logger.debug(`Uploading files for user ${userId}`);
if (!this.fileStates.has(userId)) {
this.fileStates.set(userId, {
isUploading: false,
isDownloading: false,
isUploadScheduled: false,
isDownloadScheduled: false,
});
}
const fileState = this.fileStates.get(userId)!;
if (fileState.isUploading) {
fileState.isUploadScheduled = true;
this.logger.debug(
`Uploading files for user ${userId} is in progress, scheduling upload`
);
return;
}
fileState.isUploading = true;
const workspaceDatabase =
await databaseService.getWorkspaceDatabase(userId);
@@ -515,6 +506,40 @@ class FileService {
}
}
}
private async checkDeletedFiles(userId: string): Promise<void> {
this.logger.debug(`Checking deleted files for user ${userId}`);
const workspaceDatabase =
await databaseService.getWorkspaceDatabase(userId);
const files = fs.readdirSync(getWorkspaceFilesDirectoryPath(userId));
while (files.length > 0) {
const batch = files.splice(0, 100);
const fileIdMap: Record<string, string> = {};
for (const file of batch) {
fileIdMap[file.split('_')[0]!] = file;
}
const fileIds = Object.keys(fileIdMap);
const downloads = await workspaceDatabase
.selectFrom('downloads')
.select(['node_id'])
.where('node_id', 'in', fileIds)
.execute();
for (const fileId of fileIds) {
if (!downloads.some((d) => d.node_id === fileId)) {
const filePath = path.join(
getWorkspaceFilesDirectoryPath(userId),
fileIdMap[fileId]!
);
fs.rmSync(filePath, { force: true });
}
}
}
}
}
export const fileService = new FileService();

View File

@@ -4,6 +4,7 @@ import pino, { Level } from 'pino';
const logConfig: Record<string, Level> = {
main: 'info',
'server-service': 'debug',
'file-service': 'debug',
};
class LogService {

View File

@@ -377,13 +377,7 @@ class NodeService {
};
await workspaceDatabase.transaction().execute(async (trx) => {
await trx
.deleteFrom('user_nodes')
.where('node_id', '=', nodeId)
.execute();
await trx.deleteFrom('nodes').where('id', '=', nodeId).execute();
await trx.deleteFrom('uploads').where('node_id', '=', nodeId).execute();
await trx.deleteFrom('downloads').where('node_id', '=', nodeId).execute();
const createdChange = await trx
.insertInto('changes')