Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

File service improvements

Hakan Shehu5d5aa6f4b901
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.ts
index c2daa55c..53f9e1c8 100644
--- a/apps/desktop/src/main/data/workspace/migrations.ts
+++ b/apps/desktop/src/main/data/workspace/migrations.ts
@@ -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())
diff --git a/apps/desktop/src/main/services/file-service.ts b/apps/desktop/src/main/services/file-service.ts
index cff8fc3e..d554a178 100644
--- a/apps/desktop/src/main/services/file-service.ts
+++ b/apps/desktop/src/main/services/file-service.ts
@@ -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();
diff --git a/apps/desktop/src/main/services/log-service.ts b/apps/desktop/src/main/services/log-service.ts
index 58d27015..32f94b4e 100644
--- a/apps/desktop/src/main/services/log-service.ts
+++ b/apps/desktop/src/main/services/log-service.ts
@@ -4,6 +4,7 @@ import pino, { Level } from 'pino';
const logConfig: Record<string, Level> = {
main: 'info',
'server-service': 'debug',
+ 'file-service': 'debug',
};
class LogService {
diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.ts
index 8853cb2b..bb04432d 100644
--- a/apps/desktop/src/main/services/node-service.ts
+++ b/apps/desktop/src/main/services/node-service.ts
@@ -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')