| 19 | } |
| 20 | |
| 21 | func pcreate(args []string) (err error) { |
| 22 | n := lib.Node{} |
| 23 | var filename string |
| 24 | if ne(conf.Flags["full"]) { |
| 25 | filename = (*conf.Flags["full"]) |
| 26 | } else { |
| 27 | helpf("pcreate requires file path: -full=<u>") |
| 28 | } |
| 29 | |
| 30 | var filesize int64 |
| 31 | fh, err := os.Open(filename) |
| 32 | if err != nil { |
| 33 | handleString(fmt.Sprintf("Error open file: %s\n", err.Error())) |
| 34 | } |
| 35 | if fi, _ := fh.Stat(); err == nil { |
| 36 | filesize = fi.Size() |
| 37 | } |
| 38 | |
| 39 | chunks := int(filesize / (conf.CHUNK_SIZE)) |
| 40 | if filesize%conf.CHUNK_SIZE != 0 { |
| 41 | chunks += 1 |
| 42 | } |
| 43 | |
| 44 | if chunks == 1 { |
| 45 | opts := lib.Opts{} |
| 46 | opts["upload_type"] = "full" |
| 47 | opts["full"] = filename |
| 48 | if err := n.Create(opts); err != nil { |
| 49 | handleString(fmt.Sprintf("Error creating node: %s\n", err.Error())) |
| 50 | } else { |
| 51 | n.PP() |
| 52 | } |
| 53 | } else { |
| 54 | threads, _ := strconv.Atoi(*conf.Flags["threads"]) |
| 55 | if threads == 0 { |
| 56 | threads = 1 |
| 57 | } |
| 58 | |
| 59 | //create node |
| 60 | opts := lib.Opts{} |
| 61 | opts["upload_type"] = "parts" |
| 62 | opts["parts"] = strconv.Itoa(chunks) |
| 63 | if err := n.Create(opts); err != nil { |
| 64 | handleString(fmt.Sprintf("Error creating node: %s\n", err.Error())) |
| 65 | } |
| 66 | |
| 67 | workers := pool.New(threads) |
| 68 | workers.Run() |
| 69 | for i := 0; i < chunks; i++ { |
| 70 | size := int64(conf.CHUNK_SIZE) |
| 71 | if size*(int64(i)+1) > filesize { |
| 72 | size = filesize - size*(int64(i)) |
| 73 | } |
| 74 | workers.Add(uploader, n, (i + 1), fh, size) |
| 75 | } |
| 76 | workers.Wait() |
| 77 | maxRetries := 10 |
| 78 | for i := 1; i <= maxRetries; i++ { |