forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
121 lines (99 loc) · 2.15 KB
/
http.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The dbm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http VFS support
package dbm
import (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"time"
)
// HttpDir returns an object implementing http.FileSystem using the DB file
// system restricted to a specific directory tree.
//
// 'root' must be an absolute path beginning with '/'.
func (db *DB) HttpDir(root string) http.FileSystem {
dir := filepath.Clean(root)
if dir == "." {
dir = "/"
}
if dir[0] != '/' {
return &httpFileSystem{err: fmt.Errorf("HttpDir: invalid root %q", dir)}
}
return &httpFileSystem{db: db, root: dir}
}
type httpFileSystem struct {
db *DB
err error
root string
}
// Implements http.FileSystem
func (fs *httpFileSystem) Open(name string) (r http.File, err error) {
if err = fs.err; err != nil {
return
}
var f File
if f, err = fs.db.File(filepath.Join(fs.root, path.Clean("/"+name))); err != nil {
return
}
return &httpFile{f: f}, nil
}
type httpFile struct {
closed bool
f File
fp int64
}
// Implements http.File
func (f *httpFile) Close() error {
f.closed = true
return nil
}
// Implements http.File
func (f *httpFile) Stat() (os.FileInfo, error) {
return f, nil
}
// Implements http.File
func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
panic("TODO")
}
// Implements http.File
func (f *httpFile) Read(b []byte) (n int, err error) {
n, err = f.f.ReadAt(b, f.fp)
f.fp += int64(n)
return
}
// Implements http.File
func (f *httpFile) Seek(offset int64, whence int) (int64, error) {
panic("TODO")
}
// Implements os.FileInfo
func (f *httpFile) Name() string {
return f.f.Name()
}
// Implements os.FileInfo
func (f *httpFile) Size() int64 {
sz, err := f.f.Size()
if err != nil {
return 0
}
return sz
}
// Implements os.FileInfo
func (f *httpFile) Mode() os.FileMode {
panic("TODO")
}
// Implements os.FileInfo
func (f *httpFile) ModTime() time.Time {
return time.Now()
}
// Implements os.FileInfo
func (f *httpFile) IsDir() bool {
return false
}
// Implements os.FileInfo
func (f *httpFile) Sys() interface{} {
panic("TODO")
}