mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
improvement(code-quality): centralize regex checks, normalization (#2554)
* improvement(code-quality): centralize regex checks, normalization * simplify resolution * fix(copilot): don't allow duplicate name blocks * centralize uuid check
This commit is contained in:
committed by
GitHub
parent
b23299dae4
commit
bf8fbebe22
@@ -1,3 +1,4 @@
|
||||
import { isUuid, sanitizeFileName } from '@/executor/constants'
|
||||
import type { UserFile } from '@/executor/types'
|
||||
|
||||
/**
|
||||
@@ -15,7 +16,7 @@ export interface ExecutionContext {
|
||||
*/
|
||||
export function generateExecutionFileKey(context: ExecutionContext, fileName: string): string {
|
||||
const { workspaceId, workflowId, executionId } = context
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
return `execution/${workspaceId}/${workflowId}/${executionId}/${safeFileName}`
|
||||
}
|
||||
|
||||
@@ -26,17 +27,7 @@ export function generateFileId(): string {
|
||||
return `file_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID pattern for validating execution context IDs
|
||||
*/
|
||||
const UUID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
||||
|
||||
/**
|
||||
* Check if a string matches UUID pattern
|
||||
*/
|
||||
export function isUuid(str: string): boolean {
|
||||
return UUID_PATTERN.test(str)
|
||||
}
|
||||
export { isUuid }
|
||||
|
||||
/**
|
||||
* Check if a key matches execution file pattern
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
uploadFile,
|
||||
} from '@/lib/uploads/core/storage-service'
|
||||
import { getFileMetadataByKey, insertFileMetadata } from '@/lib/uploads/server/metadata'
|
||||
import { isUuid, sanitizeFileName } from '@/executor/constants'
|
||||
import type { UserFile } from '@/executor/types'
|
||||
|
||||
const logger = createLogger('WorkspaceFileStorage')
|
||||
@@ -36,11 +37,6 @@ export interface WorkspaceFileRecord {
|
||||
uploadedAt: Date
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID pattern for validating workspace IDs
|
||||
*/
|
||||
const UUID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
||||
|
||||
/**
|
||||
* Workspace file key pattern: workspace/{workspaceId}/{timestamp}-{random}-{filename}
|
||||
*/
|
||||
@@ -73,7 +69,7 @@ export function parseWorkspaceFileKey(key: string): string | null {
|
||||
}
|
||||
|
||||
const workspaceId = match[1]
|
||||
return UUID_PATTERN.test(workspaceId) ? workspaceId : null
|
||||
return isUuid(workspaceId) ? workspaceId : null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +79,7 @@ export function parseWorkspaceFileKey(key: string): string | null {
|
||||
export function generateWorkspaceFileKey(workspaceId: string, fileName: string): string {
|
||||
const timestamp = Date.now()
|
||||
const random = Math.random().toString(36).substring(2, 9)
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
return `workspace/${workspaceId}/${timestamp}-${random}-${safeFileName}`
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
} from '@/lib/uploads/providers/blob/types'
|
||||
import type { FileInfo } from '@/lib/uploads/shared/types'
|
||||
import { sanitizeStorageMetadata } from '@/lib/uploads/utils/file-utils'
|
||||
import { sanitizeFileName } from '@/executor/constants'
|
||||
|
||||
type BlobServiceClientInstance = Awaited<
|
||||
ReturnType<typeof import('@azure/storage-blob').BlobServiceClient.fromConnectionString>
|
||||
@@ -79,7 +80,7 @@ export async function uploadToBlob(
|
||||
shouldPreserveKey = preserveKey ?? false
|
||||
}
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-') // Replace spaces with hyphens
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
const uniqueKey = shouldPreserveKey ? fileName : `${Date.now()}-${safeFileName}`
|
||||
|
||||
const blobServiceClient = await getBlobServiceClient()
|
||||
@@ -357,7 +358,7 @@ export async function initiateMultipartUpload(
|
||||
containerName = BLOB_CONFIG.containerName
|
||||
}
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
const { v4: uuidv4 } = await import('uuid')
|
||||
const uniqueKey = `kb/${uuidv4()}-${safeFileName}`
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
sanitizeFilenameForMetadata,
|
||||
sanitizeStorageMetadata,
|
||||
} from '@/lib/uploads/utils/file-utils'
|
||||
import { sanitizeFileName } from '@/executor/constants'
|
||||
|
||||
let _s3Client: S3Client | null = null
|
||||
|
||||
@@ -84,7 +85,7 @@ export async function uploadToS3(
|
||||
shouldSkipTimestamp = skipTimestampPrefix ?? false
|
||||
}
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-') // Replace spaces with hyphens
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
const uniqueKey = shouldSkipTimestamp ? fileName : `${Date.now()}-${safeFileName}`
|
||||
|
||||
const s3Client = getS3Client()
|
||||
@@ -223,7 +224,7 @@ export async function initiateS3MultipartUpload(
|
||||
const config = customConfig || { bucket: S3_KB_CONFIG.bucket, region: S3_KB_CONFIG.region }
|
||||
const s3Client = getS3Client()
|
||||
|
||||
const safeFileName = fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const safeFileName = sanitizeFileName(fileName)
|
||||
const { v4: uuidv4 } = await import('uuid')
|
||||
const uniqueKey = `kb/${uuidv4()}-${safeFileName}`
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Logger } from '@/lib/logs/console/logger'
|
||||
import type { StorageContext } from '@/lib/uploads'
|
||||
import { ACCEPTED_FILE_TYPES, SUPPORTED_DOCUMENT_EXTENSIONS } from '@/lib/uploads/utils/validation'
|
||||
import { isUuid } from '@/executor/constants'
|
||||
import type { UserFile } from '@/executor/types'
|
||||
|
||||
export interface FileAttachment {
|
||||
@@ -625,11 +626,9 @@ export function extractCleanFilename(urlOrPath: string): string {
|
||||
export function extractWorkspaceIdFromExecutionKey(key: string): string | null {
|
||||
const segments = key.split('/')
|
||||
|
||||
const UUID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
||||
|
||||
if (segments[0] === 'execution' && segments.length >= 5) {
|
||||
const workspaceId = segments[1]
|
||||
if (workspaceId && UUID_PATTERN.test(workspaceId)) {
|
||||
if (workspaceId && isUuid(workspaceId)) {
|
||||
return workspaceId
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user