func (self *PiDownloader) parseCommandFile(filePath string) ([]string, error) { f, err := os.Open(filePath) if err != nil { return nil, err } defer f.Close() reader := bufio.NewReader(f) commands := make([]string, 0) var lastCommand string for { line, e := readln(reader) if e != nil { if e == io.EOF { if lastCommand != "" && len(lastCommand) > 0 { commands = append(commands, lastCommand) } break } else { return nil, e } } l4g.Debug("Read line from %s: %s", filePath, line) trimLine := strings.TrimSpace(line) if trimLine == "" || len(trimLine) == 0 { if lastCommand != "" && len(lastCommand) > 0 { commands = append(commands, lastCommand) } lastCommand = "" continue } if utils.IsHttpUrl(trimLine) { lastCommand = "add " + trimLine } else { paramNameValue := strings.SplitN(trimLine, "=", 2) lastCommand = lastCommand + fmt.Sprintf(" %s=%s", paramNameValue[0], strings.Replace(paramNameValue[1], " ", "", -1)) } } return commands, nil }
func (self *PiDownloader) addUri(args []string) (string, error) { if args == nil || len(args) == 0 { return "", errors.New("missing args!") } uris := make([]string, 0) params := make(map[string]interface{}) for _, arg := range args { if utils.IsHttpUrl(arg) { uris = append(uris, arg) } else { argNameValue := strings.SplitN(arg, "=", 2) if len(argNameValue) != 2 { return "", errors.New("invalid args!") } values := strings.Split(strings.TrimSpace(argNameValue[1]), ";") if len(values) == 1 { params[argNameValue[0]] = values[0] } else { params[argNameValue[0]] = values } } } gids := make([]string, 0) for _, uri := range uris { l4g.Debug("Dowanload uri: %v", uri) l4g.Debug("Dowanload params: %v", params) gid, err := aria2rpc.AddUri(uri, params) if err != nil { return "", err } gids = append(gids, gid) } return fmt.Sprintf("Add successful, gids:%v", gids), nil }