File: download_file.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (107 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package utils

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
)

var _ = fmt.Print

type ReportFunc = func(done, total uint64) error

type write_counter struct {
	done, total uint64
	report      ReportFunc
}

func (self *write_counter) Write(p []byte) (int, error) {
	n := len(p)
	self.done += uint64(n)
	if self.report != nil {
		err := self.report(self.done, self.total)
		if err != nil {
			return 0, err
		}
	}
	return n, nil
}

func DownloadToWriter(url string, dest io.Writer, progress_callback ReportFunc) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("The server responded with the HTTP error: %s", resp.Status)
	}
	wc := write_counter{report: progress_callback}
	cl, err := strconv.Atoi(resp.Header.Get("Content-Length"))
	if err == nil {
		wc.total = uint64(cl)
	}
	_, err = io.Copy(dest, io.TeeReader(resp.Body, &wc))
	if err != nil {
		return err
	}
	return nil
}

func DownloadAsSlice(url string, progress_callback ReportFunc) (data []byte, err error) {
	b := bytes.Buffer{}
	b.Grow(4096)
	err = DownloadToWriter(url, &b, progress_callback)
	if err == nil {
		return b.Bytes(), nil
	}
	return nil, err
}

func DownloadToFile(destpath, url string, progress_callback ReportFunc, temp_file_path_callback func(string)) error {
	destpath, err := filepath.EvalSymlinks(destpath)
	if err != nil {
		return err
	}
	dest, err := os.CreateTemp(filepath.Dir(destpath), filepath.Base(destpath)+".partial-download.")
	if err != nil {
		return err
	}
	if temp_file_path_callback != nil {
		temp_file_path_callback(dest.Name())
	}
	dest_removed := false
	defer func() {
		dest.Close()
		if !dest_removed {
			os.Remove(dest.Name())
		}
	}()
	err = DownloadToWriter(url, dest, progress_callback)
	if err != nil {
		return err
	}
	dest.Close()
	fi, err := os.Stat(destpath)
	if err == nil {
		err = os.Chmod(dest.Name(), fi.Mode().Perm())
		if err != nil {
			return err
		}
	}
	if err != nil {
		return err
	}
	err = os.Rename(dest.Name(), destpath)
	if err != nil {
		return err
	}
	dest_removed = true
	return nil
}