Module: Supabase::Storage::Utils

Defined in:
lib/supabase/storage/utils.rb

Class Method Summary collapse

Class Method Details

.encode_segments(parts) ⇒ Object

URL-encode each path segment so user-supplied filenames don’t break the URL.



34
35
36
# File 'lib/supabase/storage/utils.rb', line 34

def encode_segments(parts)
  parts.map { |p| rfc3986_encode_segment(p) }
end

.join_url(base_url, segments, query = nil) ⇒ Object

Join the (already-trailing-slashed) base URL with the given path segments and an optional query Hash. Used so ‘_request` never has to concat strings by hand.



40
41
42
43
44
45
46
# File 'lib/supabase/storage/utils.rb', line 40

def join_url(base_url, segments, query = nil)
  path = encode_segments(segments).join("/")
  url = "#{base_url.chomp('/')}/#{path}"
  return url if query.nil? || query.empty?

  "#{url}?#{URI.encode_www_form(query)}"
end

.relative_path_to_parts(path) ⇒ Object

Splits a relative storage path into its path segments, dropping a leading ‘/` if the caller supplied one. Mirrors storage3.utils.relative_path_to_parts.

relative_path_to_parts("folder/avatar.png") # => ["folder", "avatar.png"]
relative_path_to_parts("/folder/x.png")     # => ["folder", "x.png"]


21
22
23
# File 'lib/supabase/storage/utils.rb', line 21

def relative_path_to_parts(path)
  path.to_s.split("/").reject(&:empty?)
end

.rfc3986_encode_segment(segment) ⇒ Object

Percent-encode a single path segment per RFC 3986 (unreserved set only). Notably: space → “%20” (not “+”), “+” → “%2B”, “/” → “%2F”. ‘URI.encode_www_form_component` cannot be used here — it follows application/x-www-form-urlencoded, which mis-encodes spaces and “+”.



29
30
31
# File 'lib/supabase/storage/utils.rb', line 29

def rfc3986_encode_segment(segment)
  segment.to_s.b.gsub(RFC3986_UNRESERVED) { |b| format("%%%02X", b.unpack1("C")) }
end