-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go.chunked
107 lines (84 loc) · 2.09 KB
/
file.go.chunked
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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
const BUFSIZE = (4 * 1024) * 10
type File struct {
fs.NodeRef
location string
fuse *fs.Server
content []byte
count uint64
Size uint64
Body *io.ReadCloser
File *os.File
}
var _ fs.Node = (*File)(nil)
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = 2
a.Mode = 0444
a.Size = f.Size
return nil
}
var _ fs.NodeOpener = (*File)(nil)
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
// todo: check if exists in cache
// todo: stream from remote
dirPath := path.Dir(CACHE + f.location)
os.MkdirAll(dirPath, 0777)
// out, err := os.Create(CACHE + f.location + ".tmp")
// if err != nil {
// panic(err)
// }
// defer out.Close()
client := http.Client{}
for i := 0; i <= int(f.Size/BUFSIZE); i++ {
out, _ := os.Create(CACHE + f.location + "." + strconv.Itoa(i))
rq, err := http.NewRequest("GET", ROOT+f.location, nil)
if err != nil {
panic(err)
}
rq.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", BUFSIZE*i, BUFSIZE*(i+1)))
rz, err := client.Do(rq)
if err != nil {
panic(err)
}
_, err = io.Copy(out, rz.Body)
if err != nil {
panic(err)
}
rz.Body.Close()
out.Close()
}
// os.Rename(CACHE+f.location+".tmp", CACHE+f.location)
if !req.Flags.IsReadOnly() {
return nil, fuse.Errno(syscall.EACCES)
}
resp.Flags |= fuse.OpenKeepCache
return f, nil
}
var _ fs.Handle = (*File)(nil)
var _ fs.HandleReader = (*File)(nil)
func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
i := int(req.Offset / BUFSIZE)
fmt.Println(req.Offset, i)
buf, _ := ioutil.ReadFile(CACHE + f.location + "." + strconv.Itoa(i))
off := int(req.Offset % BUFSIZE)
if off+req.Size > BUFSIZE {
fmt.Println(i, "++")
buff, _ := ioutil.ReadFile(CACHE + f.location + "." + strconv.Itoa(i+1))
buf = append(buf, buff[0:(off+req.Size-BUFSIZE)]...)
}
resp.Data = buf[off:]
return nil
}