Ver código fonte

связка работает

kpmy 8 anos atrás
pai
commit
6ddbcbf6a3
6 arquivos alterados com 233 adições e 0 exclusões
  1. 13 0
      LICENSE
  2. 40 0
      dav_cmd/main.go
  3. 121 0
      fs.go
  4. 10 0
      fs_test.go
  5. 28 0
      ipfs_api/api.go
  6. 21 0
      ipfs_api/ipfs_api_test.go

+ 13 - 0
LICENSE

@@ -0,0 +1,13 @@
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+                   Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.

+ 40 - 0
dav_cmd/main.go

@@ -0,0 +1,40 @@
+package main
+
+import (
+	"log"
+	"net/http"
+	"net/url"
+
+	"github.com/kpmy/mipfs"
+	"golang.org/x/net/webdav"
+)
+
+func init()  {
+	log.SetFlags(0);
+}
+
+func main() {
+	fs := mipfs.NewFS()
+	ls := mipfs.NewLS()
+	h := &webdav.Handler{
+		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.Printf("%-20s%-10s%-30s%-30so=%-2s%v", r.Method, r.URL.Path, dst, o, err)
+			default:
+				log.Printf("%-20s%-10s%-30s%v", r.Method, r.URL.Path, err)
+			}
+		},
+	}
+	http.Handle("/", h)
+	const addr = "0.0.0.0:6001"
+	log.Println("webdav server started at", addr)
+	http.ListenAndServe(addr, nil)
+}

+ 121 - 0
fs.go

@@ -0,0 +1,121 @@
+package mipfs
+
+import (
+	"os"
+	"time"
+
+	ipfs_api "github.com/ipfs/go-ipfs-api"
+
+	ipfs "github.com/kpmy/mipfs/ipfs_api"
+	. "github.com/kpmy/ypk/tc"
+	"golang.org/x/net/webdav"
+)
+
+type dir struct {
+	ipfs_api.UnixLsObject
+}
+
+func (d *dir) Name() string {
+	return d.UnixLsObject.Hash
+}
+
+func (d *dir) Size() int64 {
+	return 0
+}
+
+func (d *dir) Mode() os.FileMode { return os.ModeDir }
+
+func (d *dir) ModTime() time.Time {
+	return time.Now()
+}
+
+func (d *dir) IsDir() bool      { return true }
+func (d *dir) Sys() interface{} { return nil }
+
+func (d *dir) Close() error {
+	return nil
+}
+
+func (d *dir) Read(p []byte) (n int, err error) {
+	panic(100)
+}
+
+func (d *dir) Seek(offset int64, whence int) (int64, error) {
+	panic(100)
+}
+
+func (d *dir) Readdir(count int) ([]os.FileInfo, error) {
+	return []os.FileInfo{}, nil
+}
+
+func (d *dir) Stat() (os.FileInfo, error) {
+	ls, _ := ipfs.Shell().FileList(d.Hash)
+	return &dir{*ls}, nil
+}
+
+func (d *dir) Write(p []byte) (n int, err error) {
+	panic(100)
+}
+
+type filesystem struct {
+	node string
+	root string
+}
+
+func (f *filesystem) Mkdir(name string, perm os.FileMode) error {
+	Assert(name != "", 20)
+	panic(100)
+}
+
+func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (webdav.File, error) {
+	ls, _ := ipfs.Shell().FileList(f.root)
+	return &dir{*ls}, nil
+}
+
+func (f *filesystem) RemoveAll(name string) error {
+	panic(100)
+}
+
+func (f *filesystem) Rename(oldName, newName string) error {
+	panic(100)
+}
+
+func (f *filesystem) Stat(name string) (os.FileInfo, error) {
+	ls, _ := ipfs.Shell().FileList(f.root)
+	return &dir{*ls}, nil
+}
+
+var nodeID *ipfs_api.IdOutput;
+
+func init()  {
+	nodeID, _ = ipfs.Shell().ID()
+}
+
+func NewFS() webdav.FileSystem {
+	//root, _ := ipfs.Shell().Resolve(nodeID.ID)
+	root := "QmbuSdtGUUfL7DSvvA9DmiGSRqAzkHEjWtsxZDRPBWcawg"
+	return &filesystem{node: nodeID.ID, root: root}
+}
+
+type locksystem struct {
+}
+
+func (l *locksystem) Confirm(now time.Time, name0, name1 string, conditions ...webdav.Condition) (release func(), err error) {
+	panic(100)
+}
+
+func (l *locksystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
+	panic(100)
+}
+
+func (l *locksystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
+	panic(100)
+}
+
+func (l *locksystem) Unlock(now time.Time, token string) error {
+	panic(100)
+}
+
+func NewLS() webdav.LockSystem {
+	return &locksystem{}
+}

+ 10 - 0
fs_test.go

@@ -0,0 +1,10 @@
+package mipfs
+
+import (
+	"testing"
+)
+
+func TestFS(t *testing.T) {
+	NewFS()
+	NewLS()
+}

+ 28 - 0
ipfs_api/api.go

@@ -0,0 +1,28 @@
+package ipfs_api
+
+import (
+	"log"
+
+	ipfs "github.com/ipfs/go-ipfs-api"
+)
+
+var sh *ipfs.Shell
+
+func reset() {
+	if sh == nil || !sh.IsUp() {
+		sh = ipfs.NewShell("127.0.0.1:5001")
+		if id, err := sh.ID(); err == nil {
+			v0, _, _ := sh.Version()
+			log.Println("ipfs version", v0, "node", id.ID, "online")
+		}
+	}
+}
+
+func Shell() *ipfs.Shell {
+	reset()
+	return sh
+}
+
+func init() {
+	reset()
+}

+ 21 - 0
ipfs_api/ipfs_api_test.go

@@ -0,0 +1,21 @@
+package ipfs_api
+
+import (
+	"testing"
+
+	"net/http"
+
+	ipfs "github.com/ipfs/go-ipfs-api"
+)
+
+func TestShell(t *testing.T) {
+	sh := ipfs.NewShellWithClient("127.0.0.1:5001", http.DefaultClient)
+	id, _ := sh.ID()
+	t.Log(id)
+	root, _ := sh.Resolve("/ipns/" + id.ID)
+	ls, _ := sh.FileList(root)
+	t.Log(ls)
+	for _, x := range ls.Links {
+		t.Log(x)
+	}
+}