@@ -2,6 +2,12 @@ package local
22
33import (
44 "context"
5+ "github.com/alist-org/alist/v3/internal/errs"
6+ "io"
7+ "io/ioutil"
8+ "os"
9+ "path/filepath"
10+ "strings"
511
612 "github.com/alist-org/alist/v3/internal/driver"
713 "github.com/alist-org/alist/v3/internal/model"
@@ -44,48 +50,110 @@ func (d *Driver) GetAddition() driver.Additional {
4450}
4551
4652func (d * Driver ) List (ctx context.Context , dir model.Obj ) ([]model.Obj , error ) {
47- //TODO implement me
48- panic ("implement me" )
53+ fullPath := dir .GetID ()
54+ rawFiles , err := ioutil .ReadDir (fullPath )
55+ if err != nil {
56+ return nil , errors .Wrapf (err , "error while read dir %s" , fullPath )
57+ }
58+ var files []model.Obj
59+ for _ , f := range rawFiles {
60+ if strings .HasPrefix (f .Name (), "." ) {
61+ continue
62+ }
63+ file := model.Object {
64+ Name : f .Name (),
65+ Modified : f .ModTime (),
66+ Size : f .Size (),
67+ IsFolder : f .IsDir (),
68+ }
69+ files = append (files , & file )
70+ }
71+ return files , nil
4972}
5073
5174func (d * Driver ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
52- //TODO implement me
53- panic ("implement me" )
75+ fullPath := file .GetID ()
76+ link := model.Link {
77+ FilePath : & fullPath ,
78+ }
79+ return & link , nil
5480}
5581
5682func (d * Driver ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) error {
57- //TODO implement me
58- panic ("implement me" )
83+ fullPath := filepath .Join (parentDir .GetID (), dirName )
84+ err := os .MkdirAll (fullPath , 0700 )
85+ if err != nil {
86+ return errors .Wrapf (err , "error while make dir %s" , fullPath )
87+ }
88+ return nil
5989}
6090
6191func (d * Driver ) Move (ctx context.Context , srcObj , dstDir model.Obj ) error {
62- //TODO implement me
63- panic ("implement me" )
92+ srcPath := srcObj .GetID ()
93+ dstPath := filepath .Join (dstDir .GetID (), srcObj .GetName ())
94+ err := os .Rename (srcPath , dstPath )
95+ if err != nil {
96+ return errors .Wrapf (err , "error while move %s to %s" , srcPath , dstPath )
97+ }
98+ return nil
6499}
65100
66101func (d * Driver ) Rename (ctx context.Context , srcObj model.Obj , newName string ) error {
67- //TODO implement me
68- panic ("implement me" )
102+ srcPath := srcObj .GetID ()
103+ dstPath := filepath .Join (filepath .Dir (srcPath ), newName )
104+ err := os .Rename (srcPath , dstPath )
105+ if err != nil {
106+ return errors .Wrapf (err , "error while rename %s to %s" , srcPath , dstPath )
107+ }
108+ return nil
69109}
70110
71111func (d * Driver ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) error {
72- //TODO implement me
73- panic ("implement me" )
112+ srcPath := srcObj .GetID ()
113+ dstPath := filepath .Join (dstDir .GetID (), srcObj .GetName ())
114+ var err error
115+ if srcObj .IsDir () {
116+ err = copyDir (srcPath , dstPath )
117+ } else {
118+ err = copyFile (srcPath , dstPath )
119+ }
120+ if err != nil {
121+ return errors .Wrapf (err , "error while copy %s to %s" , srcPath , dstPath )
122+ }
123+ return nil
74124}
75125
76126func (d * Driver ) Remove (ctx context.Context , obj model.Obj ) error {
77- //TODO implement me
78- panic ("implement me" )
127+ var err error
128+ if obj .IsDir () {
129+ err = os .RemoveAll (obj .GetID ())
130+ } else {
131+ err = os .Remove (obj .GetID ())
132+ }
133+ if err != nil {
134+ return errors .Wrapf (err , "error while remove %s" , obj .GetID ())
135+ }
136+ return nil
79137}
80138
81139func (d * Driver ) Put (ctx context.Context , dstDir model.Obj , stream model.FileStreamer , up driver.UpdateProgress ) error {
82- //TODO implement me
83- panic ("implement me" )
140+ fullPath := filepath .Join (dstDir .GetID (), stream .GetName ())
141+ out , err := os .Create (fullPath )
142+ if err != nil {
143+ return errors .Wrapf (err , "error while create file %s" , fullPath )
144+ }
145+ defer func () {
146+ _ = out .Close ()
147+ }()
148+ _ , err = io .Copy (out , stream )
149+ if err != nil {
150+ return errors .Wrapf (err , "error while copy file %s" , fullPath )
151+ }
152+ return nil
84153}
85154
86155func (d Driver ) Other (ctx context.Context , data interface {}) (interface {}, error ) {
87- //TODO implement me
88- panic ("implement me" )
156+ return nil , errs .NotSupport
89157}
90158
91159var _ driver.Driver = (* Driver )(nil )
0 commit comments