MCPcopy Index your code
hub / github.com/simstudioai/sim / uploadToBlob

Function uploadToBlob

apps/sim/lib/uploads/providers/blob/client.ts:104–164  ·  view source on GitHub ↗
(
  file: Buffer,
  fileName: string,
  contentType: string,
  configOrSize?: BlobConfig | number,
  size?: number,
  preserveKey?: boolean,
  metadata?: Record<string, string>
)

Source from the content-addressed store, hash-verified

102 * @returns Object with file information
103 */
104export async function uploadToBlob(
105 file: Buffer,
106 fileName: string,
107 contentType: string,
108 configOrSize?: BlobConfig | number,
109 size?: number,
110 preserveKey?: boolean,
111 metadata?: Record<string, string>
112): Promise<FileInfo> {
113 let config: BlobConfig
114 let fileSize: number
115 let shouldPreserveKey: boolean
116
117 if (typeof configOrSize === 'object') {
118 config = configOrSize
119 fileSize = size ?? file.length
120 shouldPreserveKey = preserveKey ?? false
121 } else {
122 config = {
123 containerName: BLOB_CONFIG.containerName,
124 accountName: BLOB_CONFIG.accountName,
125 accountKey: BLOB_CONFIG.accountKey,
126 connectionString: BLOB_CONFIG.connectionString,
127 }
128 fileSize = configOrSize ?? file.length
129 shouldPreserveKey = preserveKey ?? false
130 }
131
132 const safeFileName = sanitizeFileName(fileName)
133 const uniqueKey = shouldPreserveKey ? fileName : `${Date.now()}-${safeFileName}`
134
135 const blobServiceClient = await getBlobServiceClient()
136 const containerClient = blobServiceClient.getContainerClient(config.containerName)
137 const blockBlobClient = containerClient.getBlockBlobClient(uniqueKey)
138
139 const blobMetadata: Record<string, string> = {
140 originalName: encodeURIComponent(fileName), // Encode filename to prevent invalid characters in HTTP headers
141 uploadedAt: new Date().toISOString(),
142 }
143
144 if (metadata) {
145 Object.assign(blobMetadata, sanitizeStorageMetadata(metadata, 8000))
146 }
147
148 await blockBlobClient.upload(file, fileSize, {
149 blobHTTPHeaders: {
150 blobContentType: contentType,
151 },
152 metadata: blobMetadata,
153 })
154
155 const servePath = `/api/files/serve/${encodeURIComponent(uniqueKey)}`
156
157 return {
158 path: servePath,
159 key: uniqueKey,
160 name: fileName,
161 size: fileSize,

Callers 2

client.test.tsFile · 0.90
uploadFileFunction · 0.85

Calls 3

sanitizeFileNameFunction · 0.90
sanitizeStorageMetadataFunction · 0.90
getBlobServiceClientFunction · 0.85

Tested by

no test coverage detected