|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "github.com/docopt/docopt-go" |
6 |
| - "github.com/eventials/go-tus" |
7 |
| - "github.com/eventials/go-tus/leveldbstore" |
8 |
| - "github.com/tus/tusd/cmd/tusd/cli" |
9 |
| - "net/http" |
10 |
| - "os" |
11 |
| - "strconv" |
| 4 | + "github.com/jackhftang/tusc/internal" |
| 5 | + "os" |
12 | 6 | )
|
13 | 7 |
|
14 |
| -const Version = "0.1.0" |
15 |
| - |
16 | 8 | const usage = `Usage:
|
17 | 9 | tusc (server|s) [options]
|
18 | 10 | tusc (client|c) <file> [<url>] [options]
|
19 |
| - tusc --help |
20 |
| - tusc --version` |
21 |
| - |
22 |
| -const serverUsage = `tusc server |
23 |
| -
|
24 |
| -Usage: |
25 |
| - tusc (server|s) [options] |
26 |
| - tusc (server|s) --help |
27 |
| -
|
28 |
| -Options: |
29 |
| - -h --host HOST Host to bind HTTP server to [default: 0.0.0.0] |
30 |
| - -p --port PORT Port to bind HTTP server to [default: 1080] |
31 |
| - -d --dir PATH Directory to store uploads in [default: ./data] |
32 |
| - -b --base-path PATH Basepath of the HTTP server [default: /files/] |
33 |
| - --unix-sock PATH If set will listen to a UNIX socket at this location instead of a TCP socket |
34 |
| - --max-size SIZE Maximum size of a single upload in bytes [default: 0] |
35 |
| - --store-size BYTE Size of space allowed for storage [default: 0] |
36 |
| - --timeout TIMEOUT Read timeout for connections in milliseconds. A zero value means that reads will not timeout [default: 30*1000] |
37 |
| - --s3-bucket BUCKET Use AWS S3 with this bucket as storage backend requires the AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables to be set |
38 |
| - --s3-object-prefix Prefix for S3 object names |
39 |
| - --s3-endpoint PATH Endpoint to use S3 compatible implementations like minio requires s3-bucket to be pass |
40 |
| - --gcs-bucket BUCKET Use Google Cloud Storage with this bucket as storage backend requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set |
41 |
| - --gcs-object-prefix PREFIX Prefix for GCS object names can't contain underscore character |
42 |
| - --hooks-dir PATH Directory to search for available hooks scripts |
43 |
| - --hooks-http PATH An HTTP endpoint to which hook events will be sent to |
44 |
| - --hooks-http-retry NUM Number of times to retry on a 500 or network timeout [default: 3] |
45 |
| - --hooks-http-backoff SECOND Number of seconds to wait before retrying each retry [default: 1] |
46 |
| - --hooks-stop-code NUM Return code from post-receive hook which causes tusd to stop and delete the current upload. A zero value means that no uploads will be stopped [default: 0] |
47 |
| - --expose-metrics Expose metrics about tusd usage [default: true] |
48 |
| - --metrics-path PATH Path under which the metrics endpoint will be accessible [default: /metrics] |
49 |
| - --behind-proxy Respect X-Forwarded-* and similar headers which may be set by proxies [default: false] |
50 |
| -` |
51 |
| - |
52 |
| -const clientUsage = `tusc client |
53 |
| -
|
54 |
| -Usage: |
55 |
| - tusc (client|c) <url> <file> [options] |
56 |
| - tusc (client|c) --help |
57 |
| -
|
58 |
| -Options: |
59 |
| - -r --resumable Save meta data for resumable uploads |
60 |
| - --store PATH Path to save meta data for resume [default: ./.tusc] |
61 |
| - --chuck-size BYTE Size of chucks of file [default: 2097152] |
62 |
| - --override-patch-method Sending a POST request instead of PATCH [default: false] |
63 |
| -` |
64 |
| - |
65 |
| -func exitWithMessage(msg ...interface{}) { |
66 |
| - for _, m := range msg { |
67 |
| - fmt.Fprintln(os.Stderr, m) |
68 |
| - } |
69 |
| - //fmt.Fprintln(os.Stderr, msg...) |
70 |
| - os.Exit(1) |
71 |
| -} |
72 |
| - |
73 |
| -func getInt64(arg docopt.Opts, key string) int64 { |
74 |
| - s, _ := arg.String(key) |
75 |
| - i, _ := strconv.ParseInt(s, 10, 64) |
76 |
| - return i |
77 |
| -} |
78 |
| - |
79 |
| -func getBool(arg docopt.Opts, key string) bool { |
80 |
| - v, _ := arg[key] |
81 |
| - if b, ok := v.(bool); ok { |
82 |
| - return b |
83 |
| - } |
84 |
| - return false |
85 |
| -} |
86 |
| - |
87 |
| -func getString(arg docopt.Opts, key string) string { |
88 |
| - v, _ := arg[key] |
89 |
| - if s, ok := v.(string); ok { |
90 |
| - return s |
91 |
| - } |
92 |
| - return "" |
93 |
| -} |
94 |
| - |
95 |
| -func server() { |
96 |
| - arguments, _ := docopt.ParseArgs(serverUsage, nil, Version) |
97 |
| - cli.Flags.HttpHost, _ = arguments.String("--host") |
98 |
| - cli.Flags.HttpPort, _ = arguments.String("--port") |
99 |
| - cli.Flags.HttpSock, _ = arguments.String("--unix-sock") |
100 |
| - cli.Flags.MaxSize = getInt64(arguments, "--max-size") |
101 |
| - cli.Flags.UploadDir, _ = arguments.String("--dir") |
102 |
| - cli.Flags.StoreSize = getInt64(arguments, "--store-size") |
103 |
| - cli.Flags.Basepath, _ = arguments.String("--base-path") |
104 |
| - cli.Flags.Timeout = getInt64(arguments, "--timeout") |
105 |
| - cli.Flags.S3Bucket, _ = arguments.String("--s3-bucket") |
106 |
| - cli.Flags.S3ObjectPrefix, _ = arguments.String("--s3-object-prefix") |
107 |
| - cli.Flags.S3Endpoint, _ = arguments.String("--s3-endpoint") |
108 |
| - cli.Flags.GCSBucket, _ = arguments.String("--gcs-bucket") |
109 |
| - cli.Flags.GCSObjectPrefix, _ = arguments.String("--gcs-object-prefix") |
110 |
| - cli.Flags.FileHooksDir, _ = arguments.String("--hooks-dir") |
111 |
| - cli.Flags.HttpHooksEndpoint, _ = arguments.String("--hooks-http") |
112 |
| - cli.Flags.HttpHooksRetry, _ = arguments.Int("--hooks-http-retry") |
113 |
| - cli.Flags.HttpHooksBackoff, _ = arguments.Int("--hooks-http-backoff") |
114 |
| - cli.Flags.HooksStopUploadCode, _ = arguments.Int("--hooks-stop-code") |
115 |
| - cli.Flags.PluginHookPath, _ = arguments.String("--hooks-plugin") |
116 |
| - cli.Flags.ShowVersion, _ = arguments.Bool("--version") |
117 |
| - cli.Flags.ExposeMetrics, _ = arguments.Bool("--expose-metrics") |
118 |
| - cli.Flags.MetricsPath, _ = arguments.String("--metrics-path") |
119 |
| - cli.Flags.BehindProxy, _ = arguments.Bool("--behind-proxy") |
120 |
| - cli.CreateComposer() |
121 |
| - cli.Serve() |
122 |
| -} |
123 |
| - |
124 |
| -func client() { |
125 |
| - arguments, _ := docopt.ParseArgs(clientUsage, nil, Version) |
126 |
| - |
127 |
| - file, _ := arguments.String("<file>") |
128 |
| - url, _ := arguments.String("<url>") |
129 |
| - resume := getBool(arguments, "--resumable") |
130 |
| - |
131 |
| - f, err := os.Open(file) |
132 |
| - if err != nil { |
133 |
| - exitWithMessage("Cannot open file: " + file) |
134 |
| - } |
135 |
| - defer f.Close() |
136 |
| - |
137 |
| - // create the tus client |
138 |
| - var store tus.Store |
139 |
| - if resume { |
140 |
| - path := getString(arguments, "--store") |
141 |
| - store, err = leveldbstore.NewLeveldbStore(path) |
142 |
| - if err != nil { |
143 |
| - exitWithMessage("Cannot Open "+path, clientUsage) |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| - client, _ := tus.NewClient(url, &tus.Config{ |
148 |
| - ChunkSize: getInt64(arguments, "--chuck-size"), |
149 |
| - OverridePatchMethod: getBool(arguments, "--override-patch-method"), |
150 |
| - Resume: resume, |
151 |
| - Store: store, |
152 |
| - Header: make(http.Header), |
153 |
| - HttpClient: nil, |
154 |
| - }) |
155 |
| - |
156 |
| - // create an upload from a file. |
157 |
| - upload, _ := tus.NewUploadFromFile(f) |
158 |
| - |
159 |
| - // create the uploader. |
160 |
| - var uploader *tus.Uploader |
161 |
| - if resume { |
162 |
| - uploader, err = client.CreateOrResumeUpload(upload) |
163 |
| - } else { |
164 |
| - uploader, err = client.CreateUpload(upload) |
165 |
| - } |
166 |
| - |
167 |
| - fmt.Printf(uploader.Url()) |
168 |
| - |
169 |
| - // start the uploading process. |
170 |
| - if err = uploader.Upload(); err != nil { |
171 |
| - exitWithMessage("Upload incomplete") |
172 |
| - } |
173 |
| -} |
| 11 | + tusc --help` |
174 | 12 |
|
175 | 13 | func main() {
|
176 |
| - if len(os.Args) < 2 { |
177 |
| - exitWithMessage("No command", usage) |
178 |
| - } |
179 |
| - switch cmd := os.Args[1]; cmd { |
180 |
| - case "server", "s": |
181 |
| - server() |
182 |
| - case "client", "c": |
183 |
| - client() |
184 |
| - default: |
185 |
| - exitWithMessage("Unknown command: "+cmd, usage) |
186 |
| - } |
| 14 | + if len(os.Args) < 2 { |
| 15 | + internal.ExitWithMessages("No command", usage) |
| 16 | + } |
| 17 | + switch cmd := os.Args[1]; cmd { |
| 18 | + case "server", "s": |
| 19 | + //internal.Server() |
| 20 | + case "client", "c": |
| 21 | + //internal.Client() |
| 22 | + default: |
| 23 | + internal.ExitWithMessages("Unknown command: "+cmd, usage) |
| 24 | + } |
187 | 25 | }
|
0 commit comments