-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
161 lines (144 loc) · 4.52 KB
/
example_test.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package joint_test
import (
"fmt"
"io"
"log"
"net/http"
"os"
jnt "github.com/schwarzlichtbezirk/joint"
)
// JoinPath can be used for high performance path concatenation.
func ExampleJoinPath() {
fmt.Println(jnt.JoinPath("any/path", "fox.txt"))
fmt.Println(jnt.JoinPath("any/path/", "fox.txt"))
fmt.Println(jnt.JoinPath("any", "/path/fox.txt"))
fmt.Println(jnt.JoinPath("any/path/", "/fox.txt"))
fmt.Println(jnt.JoinPath("/any/path", "fox.txt"))
// Output:
// any/path/fox.txt
// any/path/fox.txt
// any/path/fox.txt
// any/path/fox.txt
// /any/path/fox.txt
}
func ExampleSplitUrl() {
var list = []string{
"ftp://music:[email protected]:21/Music/DJ.m3u",
"sftp://music:[email protected]:22/Video",
"https://github.com/schwarzlichtbezirk/joint",
"https://pkg.go.dev/github.com/schwarzlichtbezirk/joint",
"C:\\Windows\\System",
}
for _, s := range list {
var addr, fpath, url = jnt.SplitUrl(s)
fmt.Printf("addr: %s, path: %s, url: %t\n", addr, fpath, url)
}
// Output:
// addr: ftp://music:[email protected]:21, path: Music/DJ.m3u, url: true
// addr: sftp://music:[email protected]:22, path: Video, url: true
// addr: https://github.com, path: schwarzlichtbezirk/joint, url: true
// addr: https://pkg.go.dev, path: github.com/schwarzlichtbezirk/joint, url: true
// addr: C:, path: Windows\System, url: false
}
func ExampleSplitKey() {
var list = []string{
"some/path/fox.txt",
"testdata/external.iso",
"testdata/external.iso/fox.txt",
"testdata/external.iso/disk/internal.iso/fox.txt",
"ftp://music:[email protected]:21/Music",
"ftp://music:[email protected]:21/testdata/external.iso/disk/internal.iso/docs/doc1.txt",
"https://music:[email protected]/webdav/Global%20Underground/Nubreed/",
}
jnt.SetDavRoot("https://music:[email protected]", "/webdav/")
for _, s := range list {
var key, fpath, _ = jnt.SplitKey(s)
fmt.Printf("key: '%s', path: '%s'\n", key, fpath)
}
// Output:
// key: '', path: 'some/path/fox.txt'
// key: 'testdata/external.iso', path: ''
// key: 'testdata/external.iso', path: 'fox.txt'
// key: 'testdata/external.iso/disk/internal.iso', path: 'fox.txt'
// key: 'ftp://music:[email protected]:21', path: 'Music'
// key: 'ftp://music:[email protected]:21/testdata/external.iso/disk/internal.iso', path: 'docs/doc1.txt'
// key: 'https://music:[email protected]/webdav/', path: 'Global%20Underground/Nubreed/'
}
func ExampleFtpEscapeBrackets() {
fmt.Println(jnt.FtpEscapeBrackets("Music/Denney [2018]"))
// Output: Music/Denney [[]2018[]]
}
func ExampleJointCache_Open() {
var jc = jnt.NewJointCache("testdata/external.iso")
defer jc.Close()
var f, err = jc.Open("fox.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(os.Stdout, f)
// Output: The quick brown fox jumps over the lazy dog.
}
// Opens file on joints pool. Path to file can starts with
// WebDAV/SFTP/FTP service address or at local filesystem, and
// include ISO9660 disks as chunks of path.
func ExampleJointPool_Open() {
var jp = jnt.NewJointPool() // can be global declaration
defer jp.Close()
var f, err = jp.Open("testdata/external.iso/fox.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(os.Stdout, f)
// Output: The quick brown fox jumps over the lazy dog.
}
func ExampleJointPool_Stat() {
var jp = jnt.NewJointPool() // can be global declaration
defer jp.Close()
var fi, err = jp.Stat("testdata/external.iso/fox.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("name: %s, size: %d\n", fi.Name(), fi.Size())
// Output:
// name: fox.txt, size: 44
}
func ExampleJointPool_ReadDir() {
var jp = jnt.NewJointPool() // can be global declaration
defer jp.Close()
var files, err = jp.ReadDir("testdata/external.iso/data")
if err != nil {
log.Fatal(err)
}
for _, de := range files {
if de.IsDir() {
fmt.Printf("dir: %s\n", de.Name())
} else {
var fi, _ = de.Info()
fmt.Printf("file: %s, %d bytes\n", de.Name(), fi.Size())
}
}
// Output:
// dir: docs
// dir: empty
// file: lorem1.txt, 2747 bytes
// file: lorem2.txt, 2629 bytes
// file: lorem3.txt, 2714 bytes
// dir: доки
// file: рыба.txt, 1789 bytes
}
// Open http://localhost:8080/ in browser
// to get a list of files in ISO-image.
func ExampleJointPool_Sub() {
// create map with caches for all currently unused joints
var jp = jnt.NewJointPool()
// file system, that shares content of "testdata" folder
// and all embedded into ISO-disks files
var sp, err = jp.Sub("testdata")
if err != nil {
log.Fatal(err)
}
http.Handle("/", http.FileServer(http.FS(sp)))
log.Fatal(http.ListenAndServe(":8080", nil))
}