fix(im): make presigned URL flow diagnosable end-to-end

When IM image rendering breaks, operators previously had no log line to
correlate against the IM platform's fetch attempt. Add observability
hooks on both ends and unblock HEAD probes so common IM previews work
at all.

- log rewriteStorageURLs success/failure/no-op with the full signed URL
  (operators can copy it from logs and verify public reachability)
- log presigned handler 4xx with client_ip + UA + tenant_id + file_path
  so failures correlate against IM platform fetch logs; use the request
  context so trace IDs are preserved
- accept HEAD on /api/v1/files/presigned: IM platforms (Feishu, Slack
  etc.) probe with HEAD before GET when rendering image previews, and a
  401 there is enough to break the inline image even when the GET would
  have succeeded
- add Admin-only GET /api/v1/files/presigned-preview that returns the
  exact URL an IM channel would embed for the calling tenant, for
  self-service verification without sending a real IM message
- clarify APP_EXTERNAL_URL and MINIO_ENDPOINT public-reachability rules
  in .env.example; misconfigured endpoints are the most common cause of
  "image broken in IM" reports
This commit is contained in:
wizardchen
2026-05-27 20:50:20 +08:00
committed by lyingbug
parent f5820c13ac
commit ae4ec0cf06
5 changed files with 395 additions and 23 deletions
+18 -1
View File
@@ -81,15 +81,32 @@ var storageSchemeRe = regexp.MustCompile(`\b(local|minio|s3|cos|tos|oss)://[^\s)
// rewriteStorageURLs replaces all provider:// URLs in content with HTTP URLs
// obtained from fileService.GetFileURL. URLs that are already HTTP or cannot
// be resolved are left unchanged.
//
// Logging policy:
// - Successful rewrite logs at INFO with the full signed URL so operators
// can copy it out of logs and verify public reachability directly. The
// trade-off: anyone with log access can use a signed URL until it
// expires (WeKnora 2h, MinIO 24h). Acceptable for diagnosability.
// - Failure or no-op rewrite logs at WARN. The no-op case typically means
// APP_EXTERNAL_URL is not configured for the local backend, which is
// the most common cause of "image broken in IM" reports.
func rewriteStorageURLs(ctx context.Context, content string, fileSvc interfaces.FileService) string {
if fileSvc == nil {
return content
}
return storageSchemeRe.ReplaceAllStringFunc(content, func(match string) string {
httpURL, err := fileSvc.GetFileURL(ctx, match)
if err != nil || httpURL == match {
if err != nil {
logger.Warnf(ctx, "[IM] rewriteStorageURLs failed: src=%s err=%v", match, err)
return match
}
if httpURL == match {
logger.Warnf(ctx,
"[IM] rewriteStorageURLs no-op (URL unchanged; for local storage set APP_EXTERNAL_URL): src=%s",
match)
return match
}
logger.Infof(ctx, "[IM] rewriteStorageURLs: src=%s dst=%s", match, httpURL)
return httpURL
})
}