Skip to content

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

Fallback to legacy upload for older server versions (#168)

Hakan Shehu85ad4e755142
diff --git a/packages/client/src/jobs/file-download.ts b/packages/client/src/jobs/file-download.ts
index 1640b180..088cc107 100644
--- a/packages/client/src/jobs/file-download.ts
+++ b/packages/client/src/jobs/file-download.ts
@@ -107,9 +107,9 @@ export class FileDownloadJobHandler implements JobHandler<FileDownloadInput> {
`v1/workspaces/${workspace.id}/files/${file.id}`,
{
onDownloadProgress: async (progress, _chunk) => {
- const percent = Math.round((progress.percent || 0) * 100);
+ const percentage = Math.round((progress.percent || 0) * 100);
await this.updateDownload(workspace, download.id, {
- progress: percent,
+ progress: percentage,
});
},
}
diff --git a/packages/client/src/jobs/file-upload.ts b/packages/client/src/jobs/file-upload.ts
index 11ff064a..b110d26a 100644
--- a/packages/client/src/jobs/file-upload.ts
+++ b/packages/client/src/jobs/file-upload.ts
@@ -35,6 +35,7 @@ declare module '@colanode/client/jobs' {
}
const UPLOAD_RETRIES_LIMIT = 10;
+const LEGACY_UPLOAD_SIZE_LIMIT = 1024 * 1024 * 50; // 50MB
export class FileUploadJobHandler implements JobHandler<FileUploadInput> {
private readonly app: AppService;
@@ -98,10 +99,41 @@ export class FileUploadJobHandler implements JobHandler<FileUploadInput> {
};
}
- return this.performUpload(account, workspace, upload, file, localFile);
+ if (account.server.isFeatureSupported('file.upload.tus')) {
+ return this.performUploadWithTus(
+ account,
+ workspace,
+ upload,
+ file,
+ localFile
+ );
+ }
+
+ if (file.attributes.size > LEGACY_UPLOAD_SIZE_LIMIT) {
+ await this.updateUpload(workspace, upload.file_id, {
+ status: UploadStatus.Failed,
+ completed_at: new Date().toISOString(),
+ progress: 0,
+ error_code: 'file_upload_failed',
+ error_message:
+ 'Please upgrade your server to the latest version to upload files larger than 50MB',
+ });
+
+ return {
+ type: 'cancel',
+ };
+ }
+
+ return this.performUploadWithLegacy(
+ account,
+ workspace,
+ upload,
+ file,
+ localFile
+ );
}
- private async performUpload(
+ private async performUploadWithTus(
account: AccountService,
workspace: WorkspaceService,
upload: SelectUpload,
@@ -223,6 +255,87 @@ export class FileUploadJobHandler implements JobHandler<FileUploadInput> {
}
}
+ private async performUploadWithLegacy(
+ account: AccountService,
+ workspace: WorkspaceService,
+ upload: SelectUpload,
+ file: LocalFileNode,
+ localFile: SelectLocalFile
+ ): Promise<JobOutput> {
+ try {
+ await this.updateUpload(workspace, upload.file_id, {
+ status: UploadStatus.Uploading,
+ started_at: new Date().toISOString(),
+ });
+
+ const updateUpload = async (values: UpdateUpload) => {
+ await this.updateUpload(workspace, upload.file_id, {
+ ...values,
+ });
+ };
+
+ const fileStream = await this.app.fs.readStream(localFile.path);
+ await account.client.put(
+ `v1/workspaces/${workspace.id}/files/${file.id}`,
+ {
+ body: fileStream,
+ headers: {
+ 'Content-Type': file.attributes.mimeType,
+ 'Content-Length': file.attributes.size.toString(),
+ },
+ onUploadProgress(progress, _chunk) {
+ const percentage = Math.round((progress.percent || 0) * 100);
+ updateUpload({
+ progress: percentage,
+ });
+ },
+ }
+ );
+
+ await this.updateUpload(workspace, upload.file_id, {
+ status: UploadStatus.Completed,
+ progress: 100,
+ completed_at: new Date().toISOString(),
+ error_code: null,
+ error_message: null,
+ });
+
+ return {
+ type: 'success',
+ };
+ } catch {
+ const newRetries = upload.retries + 1;
+
+ if (newRetries >= UPLOAD_RETRIES_LIMIT) {
+ await this.updateUpload(workspace, upload.file_id, {
+ status: UploadStatus.Failed,
+ completed_at: new Date().toISOString(),
+ progress: 0,
+ error_code: 'file_upload_failed',
+ error_message:
+ 'Failed to upload file after ' + newRetries + ' retries',
+ });
+
+ return {
+ type: 'cancel',
+ };
+ }
+
+ await this.updateUpload(workspace, upload.file_id, {
+ status: UploadStatus.Pending,
+ retries: newRetries,
+ started_at: new Date().toISOString(),
+ error_code: null,
+ error_message: null,
+ });
+
+ return {
+ type: 'retry',
+ delay: ms('1 minute'),
+ };
+ }
+ }
+
private async fetchUpload(
workspace: WorkspaceService,
fileId: string
diff --git a/packages/ui/src/lib/features.ts b/packages/client/src/lib/features.ts
similarity index 94%
rename from packages/ui/src/lib/features.ts
rename to packages/client/src/lib/features.ts
index 2491b392..165e0849 100644
--- a/packages/ui/src/lib/features.ts
+++ b/packages/client/src/lib/features.ts
@@ -2,6 +2,7 @@ import semver from 'semver';
export const FeatureVersions = {
'workspace.storage.management': '0.3.0',
+ 'file.upload.tus': '0.3.0',
} as const;
export type FeatureKey = keyof typeof FeatureVersions;
diff --git a/packages/client/src/lib/index.ts b/packages/client/src/lib/index.ts
index 7d60f184..cdd6e102 100644
--- a/packages/client/src/lib/index.ts
+++ b/packages/client/src/lib/index.ts
@@ -8,3 +8,4 @@ export * from './mentions';
export * from './servers';
export * from './types';
export * from './utils';
+export * from './features';
diff --git a/packages/client/src/services/server-service.ts b/packages/client/src/services/server-service.ts
index 3fe435a4..1485307e 100644
--- a/packages/client/src/services/server-service.ts
+++ b/packages/client/src/services/server-service.ts
@@ -1,6 +1,7 @@
import ky from 'ky';
import ms from 'ms';
+import { FeatureKey, isFeatureSupported } from '@colanode/client/lib';
import { eventBus } from '@colanode/client/lib/event-bus';
import { mapServer } from '@colanode/client/lib/mappers';
import { isServerOutdated } from '@colanode/client/lib/servers';
@@ -46,6 +47,10 @@ export class ServerService {
return this.server.version;
}
+ public isFeatureSupported(feature: FeatureKey) {
+ return isFeatureSupported(feature, this.version);
+ }
+
public async init(): Promise<void> {
const scheduleId = `server.sync.${this.domain}`;
await this.app.jobs.upsertJobSchedule(
diff --git a/packages/ui/src/components/accounts/login-form.tsx b/packages/ui/src/components/accounts/login-form.tsx
index 018e3d6d..4f35d2f2 100644
--- a/packages/ui/src/components/accounts/login-form.tsx
+++ b/packages/ui/src/components/accounts/login-form.tsx
@@ -2,6 +2,7 @@ import { HouseIcon } from 'lucide-react';
import { useState, Fragment, useEffect } from 'react';
import { match } from 'ts-pattern';
+import { isFeatureSupported } from '@colanode/client/lib';
import { Account, ServerDetails } from '@colanode/client/types';
import { EmailLogin } from '@colanode/ui/components/accounts/email-login';
import { EmailPasswordResetComplete } from '@colanode/ui/components/accounts/email-password-reset-complete';
@@ -13,7 +14,6 @@ import { Button } from '@colanode/ui/components/ui/button';
import { Separator } from '@colanode/ui/components/ui/separator';
import { useApp } from '@colanode/ui/contexts/app';
import { ServerContext } from '@colanode/ui/contexts/server';
-import { isFeatureSupported } from '@colanode/ui/lib/features';
interface LoginFormProps {
accounts: Account[];
diff --git a/packages/ui/src/components/servers/server-provider.tsx b/packages/ui/src/components/servers/server-provider.tsx
index 10e2dc9d..e194e5cc 100644
--- a/packages/ui/src/components/servers/server-provider.tsx
+++ b/packages/ui/src/components/servers/server-provider.tsx
@@ -1,7 +1,7 @@
+import { isFeatureSupported } from '@colanode/client/lib';
import { ServerNotFound } from '@colanode/ui/components/servers/server-not-found';
import { ServerContext } from '@colanode/ui/contexts/server';
import { useLiveQuery } from '@colanode/ui/hooks/use-live-query';
-import { isFeatureSupported } from '@colanode/ui/lib/features';
interface ServerProviderProps {
domain: string;
diff --git a/packages/ui/src/contexts/server.ts b/packages/ui/src/contexts/server.ts
index 54cb3a2a..0905dbd6 100644
--- a/packages/ui/src/contexts/server.ts
+++ b/packages/ui/src/contexts/server.ts
@@ -1,7 +1,7 @@
import { createContext, useContext } from 'react';
+import { FeatureKey } from '@colanode/client/lib';
import { ServerDetails } from '@colanode/client/types';
-import { FeatureKey } from '@colanode/ui/lib/features';
interface ServerContext extends ServerDetails {
supports(feature: FeatureKey): boolean;