colly icon indicating copy to clipboard operation
colly copied to clipboard

How to save binary file?

Open meshchaninov opened this issue 4 years ago • 0 comments

I'm trying to save binary file, but after saving, the file structure changes. The file that was supposed to be 19kb became 29kb.

func createCollector(login string, password string) (*colly.Collector, error) {
	c := colly.NewCollector()

	err := c.Post(loginURL, map[string]string{"login_username": login, "login_password": password, "login": "%C2%F5%EE%E4"})
	if err != nil {
		return nil, err
	}

	return c, nil
}

func downloadTorrent(login string, password string, id string, dir string) (string, error) {
	c, err := createCollector(login, password)
	filename := id + ".torrent"
	torrentPath := filepath.Join(dir, filename)
	if err != nil {
		return "", err
	}

	f, err := os.Create(torrentPath)
	if err != nil {
		return "", nil
	}

	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
		return "", err
	}

	c.OnResponse(func(r *colly.Response) {
		_, err = f.Write(r.Body)
	})

	log.Printf("Trying download: %s", getTorrentURLFromID(id))
	c.Visit(getTorrentURLFromID(id))

	if err != nil {
		return "", err
	}

	return torrentPath, nil
}

what am I doing wrong?

meshchaninov avatar Jun 14 '20 08:06 meshchaninov