@pradip1995/segment-primitives 0.4.4 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-primitives",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -4,7 +4,12 @@ export function resolveMediaUrl(
4
4
  ): string | null {
5
5
  if (!url || typeof url !== "string") return null
6
6
 
7
- const trimmed = url.trim()
7
+ let trimmed = url.trim()
8
+ if (!trimmed) return null
9
+
10
+ // Uploads stored while AWS_FILE_URL / backend_url was unset:
11
+ // "undefined/uploadsFoo.webp" or "undefined/uploads/Foo.webp"
12
+ trimmed = rewriteBrokenUndefinedUploadUrl(trimmed)
8
13
  if (!trimmed) return null
9
14
 
10
15
  if (trimmed.startsWith("/")) {
@@ -15,13 +20,47 @@ export function resolveMediaUrl(
15
20
  return rewriteStoredBackendUrl(trimmed)
16
21
  }
17
22
 
18
- const base =
23
+ const base = mediaBackendBase()
24
+ if (!base) return null
25
+
26
+ return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
27
+ }
28
+
29
+ function mediaBackendBase(): string | null {
30
+ const raw =
19
31
  process.env.FILE_LOCAL_BACKEND_URL ||
20
32
  process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
21
33
  process.env.MEDUSA_BACKEND_URL ||
22
34
  "http://localhost:9000"
23
35
 
24
- return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
36
+ if (!raw || raw === "undefined") return null
37
+ return raw
38
+ }
39
+
40
+ /** Fix DB values like `undefined/uploadsAnalyze_….webp` from misconfigured file provider. */
41
+ function rewriteBrokenUndefinedUploadUrl(url: string): string | null {
42
+ if (!url.startsWith("undefined/") && !url.startsWith("undefined")) {
43
+ return url
44
+ }
45
+
46
+ const withoutUndefined = url.replace(/^undefined\/?/, "")
47
+ // "uploadsFilename" → "uploads/Filename"; keep "uploads/..." as-is
48
+ const withSlash = withoutUndefined.replace(
49
+ /^uploads(?!\/)(.+)$/i,
50
+ "uploads/$1"
51
+ )
52
+
53
+ const base = mediaBackendBase()
54
+ if (!base) return null
55
+
56
+ // Local file provider serves from /static; S3 keys are usually uploads/...
57
+ const path = withSlash.startsWith("static/")
58
+ ? withSlash
59
+ : withSlash.startsWith("uploads/")
60
+ ? `static/${withSlash}`
61
+ : withSlash
62
+
63
+ return `${base.replace(/\/$/, "")}/${path.replace(/^\//, "")}`
25
64
  }
26
65
 
27
66
  function rewriteStoredBackendUrl(url: string): string {