|
| 1 | +package client |
| 2 | + |
| 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/jackhftang/tusc/internal/util" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | +) |
| 12 | + |
| 13 | +const clientUsage = `tusc client |
| 14 | +
|
| 15 | +Usage: |
| 16 | + tusc (client|c) <url> <file> [options] |
| 17 | + tusc (client|c) --help |
| 18 | +
|
| 19 | +Options: |
| 20 | + -r --resumable Save meta data for resumable uploads |
| 21 | + --store PATH Path to save meta data for resume [default: ./.tusc] |
| 22 | + --chuck-size BYTE Size of chucks of file [default: 2097152] |
| 23 | + --override-patch-method Sending a POST request instead of PATCH [default: false] |
| 24 | +` |
| 25 | + |
| 26 | +type ClientConf struct { |
| 27 | + file string |
| 28 | + url string |
| 29 | + resume bool |
| 30 | +} |
| 31 | + |
| 32 | +func Client() { |
| 33 | + var err error |
| 34 | + |
| 35 | + var conf ClientConf |
| 36 | + arguments, _ := docopt.ParseDoc(clientUsage) |
| 37 | + conf.file, _ = arguments.String("<file>") |
| 38 | + conf.url, _ = arguments.String("<url>") |
| 39 | + conf.resume = util.GetBool(arguments, "--resumable") |
| 40 | + |
| 41 | + // open file |
| 42 | + f, err := os.Open(conf.file) |
| 43 | + if err != nil { |
| 44 | + util.ExitWithMessages("Cannot open file: " + conf.file) |
| 45 | + } |
| 46 | + defer f.Close() |
| 47 | + |
| 48 | + // create the tus client |
| 49 | + var store tus.Store |
| 50 | + if conf.resume { |
| 51 | + path := util.GetString(arguments, "--store") |
| 52 | + store, err = leveldbstore.NewLeveldbStore(path) |
| 53 | + if err != nil { |
| 54 | + util.ExitWithMessages("Cannot Open "+path, clientUsage) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + client, _ := tus.NewClient(conf.url, &tus.Config{ |
| 59 | + ChunkSize: util.GetInt64(arguments, "--chuck-size"), |
| 60 | + OverridePatchMethod: util.GetBool(arguments, "--override-patch-method"), |
| 61 | + Resume: conf.resume, |
| 62 | + Store: store, |
| 63 | + Header: make(http.Header), |
| 64 | + HttpClient: nil, |
| 65 | + }) |
| 66 | + |
| 67 | + // create an upload from a file. |
| 68 | + var upload *tus.Upload |
| 69 | + if upload, err = tus.NewUploadFromFile(f); err != nil { |
| 70 | + util.ExitWithMessages("Cannot create upload from file: " + f.Name()) |
| 71 | + } |
| 72 | + |
| 73 | + // create the uploader. |
| 74 | + var uploader *tus.Uploader |
| 75 | + if conf.resume { |
| 76 | + uploader, err = client.CreateOrResumeUpload(upload) |
| 77 | + } else { |
| 78 | + uploader, err = client.CreateUpload(upload) |
| 79 | + } |
| 80 | + if err != nil { |
| 81 | + util.ExitWithMessages("Failed to upload", err.Error()) |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Println(uploader.Url()) |
| 85 | + |
| 86 | + // start the uploading process. |
| 87 | + if err = uploader.Upload(); err != nil { |
| 88 | + util.ExitWithMessages("Upload incomplete", err.Error()) |
| 89 | + } |
| 90 | +} |
0 commit comments