@@ -4,13 +4,13 @@ import (
44 "context"
55 "fmt"
66 "net/url"
7- stdpath "path"
87 "path/filepath"
98 "strings"
109
10+ shell "github.com/ipfs/go-ipfs-api"
11+
1112 "github.com/alist-org/alist/v3/internal/driver"
1213 "github.com/alist-org/alist/v3/internal/model"
13- shell "github.com/ipfs/go-ipfs-api"
1414)
1515
1616type IPFS struct {
@@ -44,27 +44,32 @@ func (d *IPFS) Drop(ctx context.Context) error {
4444
4545func (d * IPFS ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
4646 path := dir .GetPath ()
47- if path [len (path ):] != "/" {
48- path += "/"
47+ switch d .Mode {
48+ case "ipfs" :
49+ path , _ = url .JoinPath ("/ipfs" , path )
50+ case "ipns" :
51+ path , _ = url .JoinPath ("/ipns" , path )
52+ case "mfs" :
53+ fileStat , err := d .sh .FilesStat (ctx , path )
54+ if err != nil {
55+ return nil , err
56+ }
57+ path , _ = url .JoinPath ("/ipfs" , fileStat .Hash )
58+ default :
59+ return nil , fmt .Errorf ("mode error" )
4960 }
5061
51- path_cid , err := d .sh .FilesStat (ctx , path )
52- if err != nil {
53- return nil , err
54- }
55-
56- dirs , err := d .sh .List (path_cid .Hash )
62+ dirs , err := d .sh .List (path )
5763 if err != nil {
5864 return nil , err
5965 }
6066
6167 objlist := []model.Obj {}
6268 for _ , file := range dirs {
63- gateurl := * d .gateURL
64- gateurl .Path = "ipfs/" + file .Hash
69+ gateurl := * d .gateURL .JoinPath ("/ipfs/" + file .Hash )
6570 gateurl .RawQuery = "filename=" + url .PathEscape (file .Name )
6671 objlist = append (objlist , & model.ObjectURL {
67- Object : model.Object {ID : file .Hash , Name : file .Name , Size : int64 (file .Size ), IsFolder : file .Type == 1 },
72+ Object : model.Object {ID : "/ipfs/" + file .Hash , Name : file .Name , Size : int64 (file .Size ), IsFolder : file .Type == 1 },
6873 Url : model.Url {Url : gateurl .String ()},
6974 })
7075 }
@@ -73,11 +78,15 @@ func (d *IPFS) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]
7378}
7479
7580func (d * IPFS ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
76- link := d .Gateway + "/ipfs/" + file .GetID () + "/?filename=" + url .PathEscape (file .GetName ())
77- return & model.Link {URL : link }, nil
81+ gateurl := d .gateURL .JoinPath (file .GetID ())
82+ gateurl .RawQuery = "filename=" + url .PathEscape (file .GetName ())
83+ return & model.Link {URL : gateurl .String ()}, nil
7884}
7985
8086func (d * IPFS ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) error {
87+ if d .Mode != "mfs" {
88+ return fmt .Errorf ("only write in mfs mode" )
89+ }
8190 path := parentDir .GetPath ()
8291 if path [len (path ):] != "/" {
8392 path += "/"
@@ -86,42 +95,48 @@ func (d *IPFS) MakeDir(ctx context.Context, parentDir model.Obj, dirName string)
8695}
8796
8897func (d * IPFS ) Move (ctx context.Context , srcObj , dstDir model.Obj ) error {
98+ if d .Mode != "mfs" {
99+ return fmt .Errorf ("only write in mfs mode" )
100+ }
89101 return d .sh .FilesMv (ctx , srcObj .GetPath (), dstDir .GetPath ())
90102}
91103
92104func (d * IPFS ) Rename (ctx context.Context , srcObj model.Obj , newName string ) error {
105+ if d .Mode != "mfs" {
106+ return fmt .Errorf ("only write in mfs mode" )
107+ }
93108 newFileName := filepath .Dir (srcObj .GetPath ()) + "/" + newName
94109 return d .sh .FilesMv (ctx , srcObj .GetPath (), strings .ReplaceAll (newFileName , "\\ " , "/" ))
95110}
96111
97112func (d * IPFS ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) error {
98- // TODO copy obj, optional
99- fmt .Println ( srcObj . GetPath () )
100- fmt . Println ( dstDir . GetPath ())
113+ if d . Mode != "mfs" {
114+ return fmt .Errorf ( "only write in mfs mode" )
115+ }
101116 newFileName := dstDir .GetPath () + "/" + filepath .Base (srcObj .GetPath ())
102- fmt .Println (newFileName )
103117 return d .sh .FilesCp (ctx , srcObj .GetPath (), strings .ReplaceAll (newFileName , "\\ " , "/" ))
104118}
105119
106120func (d * IPFS ) Remove (ctx context.Context , obj model.Obj ) error {
107- // TODO remove obj, optional
121+ if d .Mode != "mfs" {
122+ return fmt .Errorf ("only write in mfs mode" )
123+ }
108124 return d .sh .FilesRm (ctx , obj .GetPath (), true )
109125}
110126
111127func (d * IPFS ) Put (ctx context.Context , dstDir model.Obj , s model.FileStreamer , up driver.UpdateProgress ) error {
112- // TODO upload file, optional
113- _ , err := d .sh .Add (driver .NewLimitedUploadStream (ctx , & driver.ReaderUpdatingProgress {
128+ if d .Mode != "mfs" {
129+ return fmt .Errorf ("only write in mfs mode" )
130+ }
131+ outHash , err := d .sh .Add (driver .NewLimitedUploadStream (ctx , & driver.ReaderUpdatingProgress {
114132 Reader : s ,
115133 UpdateProgress : up ,
116- }), ToFiles (stdpath .Join (dstDir .GetPath (), s .GetName ())))
117- return err
118- }
119-
120- func ToFiles (dstDir string ) shell.AddOpts {
121- return func (rb * shell.RequestBuilder ) error {
122- rb .Option ("to-files" , dstDir )
123- return nil
134+ }))
135+ if err != nil {
136+ return err
124137 }
138+ err = d .sh .FilesCp (ctx , "/ipfs/" + outHash , dstDir .GetPath ()+ "/" + strings .ReplaceAll (s .GetName (), "\\ " , "/" ))
139+ return err
125140}
126141
127142//func (d *Template) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
0 commit comments