diff --git a/src/features/webhook/assembly/api/webhook.controller.ts b/src/features/webhook/assembly/api/webhook.controller.ts index 5619d55..09c0c19 100644 --- a/src/features/webhook/assembly/api/webhook.controller.ts +++ b/src/features/webhook/assembly/api/webhook.controller.ts @@ -19,6 +19,11 @@ export const handleWebhookEvent = async (req: NextRequest) => { const dropboxConnectionService = new DropboxConnectionsService(user) const connection = await dropboxConnectionService.getConnectionForWorkspace() + if (!connection.status) { + console.info(`Sync is not enabled for this workspace. Skipping webhook event`) + return NextResponse.json({}) + } + if (!connection.refreshToken) throw new APIError('No refresh token found', httpStatus.NOT_FOUND) if (!connection.accountId) throw new APIError('No accountId found', httpStatus.NOT_FOUND) diff --git a/src/lib/copilot/CopilotAPI.ts b/src/lib/copilot/CopilotAPI.ts index ac51919..24f1661 100644 --- a/src/lib/copilot/CopilotAPI.ts +++ b/src/lib/copilot/CopilotAPI.ts @@ -23,7 +23,7 @@ import { CopilotFileCreateSchema, type CopilotFileList, CopilotFileListSchema, - CopilotFileRetrieve, + type CopilotFileRetrieve, CopilotFileRetrieveSchema, type CopilotListArgs, type CopilotPrice, @@ -191,10 +191,13 @@ export class CopilotAPI { channelId: string, fileType: ObjectTypeValue, ): Promise { + const saniztedPath = sanitizeFileNameForAssembly(path) + console.info(`CopilotAPI#_createFile. Path: ${saniztedPath}`) + const createFileResponse = await this.copilot.createFile({ fileType, requestBody: { - path: sanitizeFileNameForAssembly(path), + path: saniztedPath, channelId, }, }) @@ -220,8 +223,16 @@ export class CopilotAPI { return await this.copilot.deleteFile({ id }) } - async _listFiles(channelId: string, nextToken?: string): Promise { - const list = await this.copilot.listFiles({ channelId, nextToken, limit: MAX_FILES_LIMIT }) + async _listFiles( + channelId: string, + nextToken?: string, + customLimit?: number, + ): Promise { + const list = await this.copilot.listFiles({ + channelId, + nextToken, + limit: customLimit || MAX_FILES_LIMIT, + }) return CopilotFileListSchema.parse(list) } diff --git a/src/utils/filePath.ts b/src/utils/filePath.ts index 211cc1b..95093f8 100644 --- a/src/utils/filePath.ts +++ b/src/utils/filePath.ts @@ -82,6 +82,15 @@ export function sanitizePath(path: string) { } export function sanitizeFileNameForAssembly(filename: string): string { + return unorm + .nfd(filename) // decompose accents + .replace(/[\u0300-\u036f]/g, '') // remove diacritics + .replace(/[^a-zA-Z0-9._/() -]/g, '_') // replace special chars with _ + .replace(/_+/g, '_') // collapse multiple _ + .replace(/^_+|_+$/g, '') // trim _ from ends +} + +export function getFaultyPath(filename: string): string { return unorm .nfd(filename) // decompose accents .replace(/[\u0300-\u036f]/g, '') // remove diacritics