MCPcopy
hub / github.com/simstudioai/sim / resolveFileInputToUrl

Function resolveFileInputToUrl

apps/sim/lib/uploads/utils/file-utils.server.ts:62–142  ·  view source on GitHub ↗
(
  options: ResolveFileInputOptions
)

Source from the content-addressed store, hash-verified

60 * - Validating external URLs via validateUrlWithDNS
61 */
62export async function resolveFileInputToUrl(
63 options: ResolveFileInputOptions
64): Promise<FileResolutionResult> {
65 const { file, filePath, userId, requestId, logger } = options
66
67 if (file) {
68 let userFile: UserFile
69 try {
70 userFile = processSingleFileToUserFile(file, requestId, logger)
71 } catch (error) {
72 return {
73 error: {
74 status: 400,
75 message: getErrorMessage(error, 'Failed to process file'),
76 },
77 }
78 }
79
80 let fileUrl = userFile.url || ''
81
82 // Handle internal URLs
83 if (fileUrl && isInternalFileUrl(fileUrl)) {
84 const resolution = await resolveInternalFileUrl(fileUrl, userId, requestId, logger)
85 if (resolution.error) {
86 return { error: resolution.error }
87 }
88 fileUrl = resolution.fileUrl || ''
89 }
90
91 // Generate presigned URL if we have a key but no URL
92 if (!fileUrl && userFile.key) {
93 const context = (userFile.context as StorageContext) || inferContextFromKey(userFile.key)
94 const hasAccess = await verifyFileAccess(userFile.key, userId, undefined, context, false)
95
96 if (!hasAccess) {
97 logger.warn(`[${requestId}] Unauthorized presigned URL generation attempt`, {
98 userId,
99 key: userFile.key,
100 context,
101 })
102 return { error: { status: 404, message: 'File not found' } }
103 }
104
105 fileUrl = await StorageService.generatePresignedDownloadUrl(userFile.key, context, 5 * 60)
106 }
107
108 return { fileUrl }
109 }
110
111 if (filePath) {
112 let fileUrl = filePath
113
114 if (isInternalFileUrl(filePath)) {
115 const resolution = await resolveInternalFileUrl(filePath, userId, requestId, logger)
116 if (resolution.error) {
117 return { error: resolution.error }
118 }
119 fileUrl = resolution.fileUrl || fileUrl

Callers 3

route.tsFile · 0.90
route.tsFile · 0.90
route.tsFile · 0.90

Calls 8

getErrorMessageFunction · 0.90
isInternalFileUrlFunction · 0.90
inferContextFromKeyFunction · 0.90
verifyFileAccessFunction · 0.90
validateUrlWithDNSFunction · 0.90
resolveInternalFileUrlFunction · 0.70
warnMethod · 0.65

Tested by

no test coverage detected