/
resfile.go
57 lines (49 loc) · 1.02 KB
/
resfile.go
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
package convertapi
import (
"github.com/ConvertAPI/convertapi-go/lib"
"io"
"net/http"
"os"
"path/filepath"
)
type ResFile struct {
client *http.Client
resp *http.Response
FileName string
FileSize int
Url string
}
func (this *ResFile) download() (err error) {
if this.resp == nil {
this.resp, err = this.client.Get(this.Url)
}
return
}
func (this *ResFile) Read(p []byte) (n int, err error) {
err = this.download()
if err == nil {
n, err = this.resp.Body.Read(p)
if err != nil {
this.resp.Body.Close()
this.resp = nil
}
}
return
}
func (this *ResFile) ToFile(file *os.File) (err error) {
_, err = io.Copy(file, this)
return
}
func (this *ResFile) ToPath(path string) (file *os.File, err error) {
if info, e := os.Stat(path); e == nil && info.IsDir() {
path = filepath.Join(path, this.FileName)
}
if file, err = os.Create(path); err == nil {
defer file.Close()
err = this.ToFile(file)
}
return
}
func (this *ResFile) Delete() error {
return lib.RequestDelete(this.Url, this.client)
}