Bladeren bron

добавил путь /ipfs в url для http и /ipfs/ для webdav

kpmy 8 jaren geleden
bovenliggende
commit
68801d1f20
2 gewijzigde bestanden met toevoegingen van 16 en 10 verwijderingen
  1. 8 2
      dav_cmd/main.go
  2. 8 8
      wdfs/fs.go

+ 8 - 2
dav_cmd/main.go

@@ -35,6 +35,7 @@ func main() {
 	fs := wdfs.NewFS(nodeID, root)
 	ls := wdfs.NewLS(fs)
 	h := &webdav.Handler{
+		Prefix:     "/ipfs",
 		FileSystem: fs,
 		LockSystem: ls,
 		Logger: func(r *http.Request, err error) {
@@ -49,10 +50,15 @@ func main() {
 			default:
 				log.Println(r.Method, r.URL.Path, err)
 			}
-			log.Println(fs)
+			KV.Write("root", []byte(fs.String()))
 		},
 	}
-	http.Handle("/", h)
+	http.Handle("/ipfs/", h)
+	http.HandleFunc("/ipfs", func(resp http.ResponseWriter, req *http.Request) {
+		if r, err := KV.Read("root"); err == nil {
+			resp.Write(r)
+		}
+	})
 	const addr = "0.0.0.0:6001"
 	log.Println("webdav server started at", addr)
 	http.ListenAndServe(addr, nil)

+ 8 - 8
wdfs/fs.go

@@ -300,7 +300,7 @@ type filesystem struct {
 }
 
 func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
-	chain := newChain(f.root.mirror(), f.root.Hash+name)
+	chain := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(name, "/"))
 	if tail := chain.tail(); !tail.exists() {
 		for tail != nil {
 			if !tail.exists() {
@@ -319,7 +319,7 @@ func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
 }
 
 func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
-	chain := newChain(f.root.mirror(), f.root.Hash+name)
+	chain := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(name, "/"))
 	switch tail := chain.tail(); {
 	case tail.exists() && tail.IsDir():
 		ret = &loc{ch: tail}
@@ -335,7 +335,7 @@ func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webd
 }
 
 func (f *filesystem) RemoveAll(name string) (err error) {
-	chain := newChain(f.root.mirror(), f.root.Hash+name)
+	chain := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(name, "/"))
 	if tail := chain.tail(); tail.exists() {
 		tail = tail.up
 		tail.Hash, _ = ipfs_api.Shell().Patch(tail.Hash, "rm-link", tail.down.name)
@@ -350,8 +350,8 @@ func (f *filesystem) RemoveAll(name string) (err error) {
 }
 
 func (f *filesystem) Rename(oldName, newName string) (err error) {
-	on := newChain(f.root.mirror(), f.root.Hash+oldName)
-	nn := newChain(f.root.mirror(), f.root.Hash+newName)
+	on := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(oldName, "/"))
+	nn := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(newName, "/"))
 	if ot := on.tail(); ot.exists() {
 		if nt := nn.tail(); !nt.exists() {
 			Assert(ot.depth() == nt.depth(), 40)
@@ -374,7 +374,7 @@ func (f *filesystem) Rename(oldName, newName string) (err error) {
 }
 
 func (f *filesystem) Stat(name string) (fi os.FileInfo, err error) {
-	chain := newChain(f.root.mirror(), f.root.Hash+name)
+	chain := newChain(f.root.mirror(), f.root.Hash+"/"+strings.Trim(name, "/"))
 	tail := chain.tail()
 	if fi = tail; !tail.exists() {
 		err = os.ErrNotExist
@@ -386,7 +386,7 @@ func (f *filesystem) String() string {
 	return f.root.Hash
 }
 
-func NewFS(id *shell.IdOutput, root string) webdav.FileSystem {
+func NewFS(id *shell.IdOutput, root string) *filesystem {
 	ch := &chain{}
 	ch.Hash = root
 	ch.name = root
@@ -443,7 +443,7 @@ func (l *locksystem) Unlock(now time.Time, token string) (err error) {
 	return
 }
 
-func NewLS(fs webdav.FileSystem) webdav.LockSystem {
+func NewLS(fs webdav.FileSystem) *locksystem {
 	ret := &locksystem{}
 	ret.locks = make(map[string]string)
 	ret.tokens = make(map[string]webdav.LockDetails)