Example #1
0
func fetchContent(server string, fileId string) (filename string, content []byte, e error) {
	fileUrl, lookupError := operation.LookupFileId(server, fileId, "", true)
	if lookupError != nil {
		return "", nil, lookupError
	}
	var rc io.ReadCloser
	if filename, rc, e = util.DownloadUrl(fileUrl); e != nil {
		return "", nil, e
	}
	content, e = ioutil.ReadAll(rc)
	rc.Close()
	return
}
Example #2
0
func downloadToFile(server, fileId, saveDir string) error {
	fileUrl, lookupError := operation.LookupFileId(server, fileId, "", true)
	if lookupError != nil {
		return lookupError
	}
	filename, rc, err := util.DownloadUrl(fileUrl)
	if err != nil {
		return err
	}
	defer rc.Close()
	if filename == "" {
		filename = fileId
	}
	isFileList := false
	if strings.HasSuffix(filename, "-list") {
		// old command compatible
		isFileList = true
		filename = filename[0 : len(filename)-len("-list")]
	}
	f, err := os.OpenFile(path.Join(saveDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
	if err != nil {
		return err
	}
	defer f.Close()
	if isFileList {
		content, err := ioutil.ReadAll(rc)
		if err != nil {
			return err
		}
		fids := strings.Split(string(content), "\n")
		for _, partId := range fids {
			var n int
			_, part, err := fetchContent(*d.server, partId)
			if err == nil {
				n, err = f.Write(part)
			}
			if err == nil && n < len(part) {
				err = io.ErrShortWrite
			}
			if err != nil {
				return err
			}
		}
	} else {
		if _, err = io.Copy(f, rc); err != nil {
			return err
		}

	}
	return nil
}