Home

dotnix @d9c3b5c59cd01f3923f8ba090a3c17087b099456 - refs - log -
-
https://git.jolheiser.com/dotnix.git
My nix dotfiles
dotnix / apps / nogui / nushell / miniserve.nu
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export def serve [
  path: path         # The path to serve 
  --dir (-d): string # The base dir in miniserve for the path
  --public (-p)      # Send to http://pubserve (via http://privserve) rather than http://files
  --verbose (-v)     # Print uploads
] {
  let endpoint = if $public { "http://privserve" } else { "http://files" }
  let url = if $public { "http://pubserve.serval-vibes.ts.net" } else { "http://files" }
  let upload = $"($endpoint)/upload"
  let baseName = $path | path basename
  let cleanDir = if $dir != null { $"($dir | str trim --char '/')" } else { "" }
  # When uploading, "" is invalid. It must either be "/" or relative "foo/bar/"
  let uploadDir = $"($cleanDir)/"
  let serveDir = if $cleanDir != "" { $uploadDir } else { "" }
  if ($path | path type) == "dir" {
    let prefix = $path | path expand
    let baseDir = [$cleanDir, ($prefix | path basename | str trim --char '/')] | path join
    glob -F $"($prefix)/**" | each {|g| 
      let b = $g | str replace $prefix "" | str trim --left --char '/'
      let d = [$baseDir, $b] | path join
      if $verbose { print $"Creating dir ($d)" }
      ^curl -F $"mkdir=($d)" $"($upload)?path=/" | complete
    }
    glob -D $"($prefix)/**" | each {|g| 
      let f = $g | str replace $prefix "" | str trim --left --char '/'
      let t = [$baseDir, $f] | path join
      let p = $t | path dirname
      if $verbose { print $"Uploading ($g) to ($t)" }
      ^curl -F $"path=@($g)" $"($upload)?path=($p)" | complete
    }
  } else {
    if $verbose { print $"Creating dir ($uploadDir)" }
    ^curl -F $"mkdir=($uploadDir)" $"($upload)?path=/" | complete
    if $verbose { print $"Uploading ($path | path expand) to ($serveDir)($baseName)" }
    ^curl -F $"path=@($path)" $"($upload)?path=($uploadDir)" | complete
  }
  $"($url)/($serveDir)($baseName)"
}