File size: 1,064 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** Convert current or legacy app-container paths into a portable Documents-relative path. */
export function toPortableGalleryPath(path: string, documentDirectoryPath: string): string | null {
  if (!path) return null
  const cleanPath = path.replace(/^file:\/\//, "")
  const docPath = documentDirectoryPath.replace(/\/$/, "")
  let candidate: string | null = null
  if (cleanPath.startsWith(`${docPath}/`)) candidate = cleanPath.substring(docPath.length + 1)
  else if (!cleanPath.startsWith("/")) candidate = cleanPath.replace(/^\/+/, "")

  if (!candidate) {
    const documentsMarker = "/Documents/"
    const documentsIndex = cleanPath.indexOf(documentsMarker)
    if (documentsIndex >= 0) candidate = cleanPath.substring(documentsIndex + documentsMarker.length)
  }

  if (!candidate || candidate.includes("\0")) return null
  const segments = candidate.split("/")
  if (segments[0] !== "MentraPhotos" || segments.some((segment) => !segment || segment === "." || segment === "..")) {
    return null
  }
  return segments.join("/")
}