1
0
Эх сурвалжийг харах

фикс пути webdav и докерфайл

kpmy 8 жил өмнө
parent
commit
d138f97d7b
5 өөрчлөгдсөн 70 нэмэгдсэн , 28 устгасан
  1. 10 0
      Dockerfile
  2. 10 0
      README.md
  3. 37 22
      dav_cmd/main.go
  4. 3 1
      ipfs_api/api.go
  5. 10 5
      wdfs/fs.go

+ 10 - 0
Dockerfile

@@ -0,0 +1,10 @@
+FROM golang:1.7
+RUN mkdir -p /go/src/github.com/kpmy/mipfs
+COPY . /go/src/github.com/kpmy/mipfs
+ENV GOPATH /go
+RUN go get -v github.com/kpmy/mipfs/dav_cmd
+RUN go install github.com/kpmy/mipfs/dav_cmd
+RUN mkdir -p /go/.diskv
+RUN printf "ipfs:5001" > /go/.diskv/ipfs
+EXPOSE 6001
+CMD dav_cmd

+ 10 - 0
README.md

@@ -1,2 +1,12 @@
 # mipfs
 ipfs2webdav
+
+установка ipfs
+`docker run -d --name ipfs_host -p 8080:8080 -p 4001:4001 -p 5001:5001 ipfs/go-ipfs:latest`
+`docker run -it --volumes-from ipfs_host ipfs/go-ipfs:latest`
+
+установка ipfs2webdav
+`git clone https://github.com/kpmy/mipfs.git`
+`docker build -t kpmy/mipfs:0.1 .`
+`docker create --restart always --name ipfs_webdav --link ipfs_host:ipfs -p 0.0.0.0:6001:6001 kpmy/mipfs:0.1`
+`docker start ipfs_webdav`

+ 37 - 22
dav_cmd/main.go

@@ -5,10 +5,13 @@ import (
 	"net/http"
 	"net/url"
 
+	"fmt"
 	"github.com/kpmy/mipfs/ipfs_api"
 	"github.com/kpmy/mipfs/wdfs"
+	"github.com/kpmy/ypk/fn"
 	"github.com/peterbourgon/diskv"
 	"golang.org/x/net/webdav"
+	"os"
 )
 
 var KV *diskv.Diskv
@@ -25,36 +28,48 @@ func init() {
 }
 
 func main() {
+	log.Println(os.Getwd())
 	root := "QmbuSdtGUUfL7DSvvA9DmiGSRqAzkHEjWtsxZDRPBWcawg"
 	if r, err := KV.Read("root"); err == nil {
 		root = string(r)
 	} else {
 		KV.Write("root", []byte(root))
 	}
-	nodeID, _ := ipfs_api.Shell().ID()
-	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) {
-			switch r.Method {
-			case "COPY", "MOVE":
-				dst := ""
-				if u, err := url.Parse(r.Header.Get("Destination")); err == nil {
-					dst = u.Path
+	if r, err := KV.Read("ipfs"); err == nil {
+		ipfs_api.Addr = string(r)
+	}
+	var fs webdav.FileSystem
+	var ls webdav.LockSystem
+	if nodeID, err := ipfs_api.Shell().ID(); err == nil {
+		fs = wdfs.NewFS(nodeID, root)
+		ls = wdfs.NewLS(fs)
+	} else {
+		log.Fatal(err)
+	}
+	if !fn.IsNil(fs) {
+		h := &webdav.Handler{
+			Prefix:     "/ipfs",
+			FileSystem: fs,
+			LockSystem: ls,
+			Logger: func(r *http.Request, err error) {
+				switch r.Method {
+				case "COPY", "MOVE":
+					dst := ""
+					if u, err := url.Parse(r.Header.Get("Destination")); err == nil {
+						dst = u.Path
+					}
+					o := r.Header.Get("Overwrite")
+					log.Println(r.Method, r.URL.Path, dst, o, err)
+				default:
+					log.Println(r.Method, r.URL.Path, err)
 				}
-				o := r.Header.Get("Overwrite")
-				log.Println(r.Method, r.URL.Path, dst, o, err)
-			default:
-				log.Println(r.Method, r.URL.Path, err)
-			}
-			KV.Write("root", []byte(fs.String()))
-		},
+				KV.Write("root", []byte(fmt.Sprint(fs)))
+			},
+		}
+		http.Handle("/ipfs/", h)
+		http.Handle("/ipfs", h)
 	}
-	http.Handle("/ipfs/", h)
-	http.HandleFunc("/ipfs", func(resp http.ResponseWriter, req *http.Request) {
+	http.HandleFunc("/hash", func(resp http.ResponseWriter, req *http.Request) {
 		if r, err := KV.Read("root"); err == nil {
 			resp.Write(r)
 		}

+ 3 - 1
ipfs_api/api.go

@@ -8,9 +8,11 @@ import (
 
 var sh *shell.Shell
 
+var Addr = "127.0.0.1:5001"
+
 func reset() {
 	if sh == nil || !sh.IsUp() {
-		sh = shell.NewShell("127.0.0.1:5001")
+		sh = shell.NewShell(Addr)
 		if id, err := sh.ID(); err == nil {
 			v0, _, _ := sh.Version()
 			log.Println("ipfs version", v0, "node", id.ID, "online")

+ 10 - 5
wdfs/fs.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/ipfs/go-ipfs-api"
 	"github.com/kpmy/mipfs/ipfs_api"
+	"github.com/kpmy/ypk/fn"
 	. "github.com/kpmy/ypk/tc"
 	"github.com/mattetti/filebuffer"
 	"golang.org/x/net/webdav"
@@ -108,11 +109,15 @@ func (f *file) Read(p []byte) (n int, err error) {
 		f.links, _ = ipfs_api.Shell().List(f.ch.Hash)
 	}
 	if len(f.links) == 0 {
-		buf := filebuffer.New(nil)
-		rd, _ := ipfs_api.Shell().Cat(f.ch.Hash)
-		io.Copy(buf, rd)
-		buf.Seek(f.pos, io.SeekStart)
-		return buf.Read(p)
+		if fn.IsNil(f.buf) {
+			f.buf = filebuffer.New(nil)
+			rd, _ := ipfs_api.Shell().Cat(f.ch.Hash)
+			io.Copy(f.buf, rd)
+		}
+		f.buf.Seek(f.pos, io.SeekStart)
+		n, err = f.buf.Read(p)
+		f.pos = f.pos + int64(n)
+		return n, err
 	} else {
 		var end int64 = 0
 		for _, l := range f.links {