@@ -3,6 +3,7 @@ package crypt
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
78 "io"
89 stdpath "path"
@@ -109,43 +110,35 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
109110
110111 result := make ([]model.Obj , 0 , len (objs ))
111112 for _ , obj := range objs {
112- rawName := model .UnwrapObj (obj ).GetName ()
113- if obj .IsDir () {
114- name , err := d .cipher .DecryptDirName (rawName )
115- if err != nil {
116- // filter illegal files
117- continue
118- }
119- if ! d .ShowHidden && strings .HasPrefix (name , "." ) {
120- continue
113+ size := obj .GetSize ()
114+ mask := model .GetObjMask (obj )
115+ name := obj .GetName ()
116+ if mask & model .Virtual == 0 {
117+ if obj .IsDir () {
118+ name , err = d .cipher .DecryptDirName (model .UnwrapObjName (obj ).GetName ())
119+ if err != nil {
120+ // filter illegal files
121+ continue
122+ }
123+ } else {
124+ size , err = d .cipher .DecryptedSize (size )
125+ if err != nil {
126+ // filter illegal files
127+ continue
128+ }
129+ name , err = d .cipher .DecryptFileName (model .UnwrapObjName (obj ).GetName ())
130+ if err != nil {
131+ // filter illegal files
132+ continue
133+ }
121134 }
122- result = append (result , & model.Object {
123- Path : stdpath .Join (remoteFullPath , rawName ),
124- Name : name ,
125- Size : 0 ,
126- Modified : obj .ModTime (),
127- IsFolder : obj .IsDir (),
128- Ctime : obj .CreateTime (),
129- // discarding hash as it's encrypted
130- })
131- continue
132- }
133-
134- size , err := d .cipher .DecryptedSize (obj .GetSize ())
135- if err != nil {
136- // filter illegal files
137- continue
138- }
139- name , err := d .cipher .DecryptFileName (rawName )
140- if err != nil {
141- // filter illegal files
142- continue
143135 }
144136 if ! d .ShowHidden && strings .HasPrefix (name , "." ) {
145137 continue
146138 }
139+ mask &^= model .Temp
147140 objRes := & model.Object {
148- Path : stdpath .Join (remoteFullPath , rawName ),
141+ Path : stdpath .Join (remoteFullPath , obj . GetName () ),
149142 Name : name ,
150143 Size : size ,
151144 Modified : obj .ModTime (),
@@ -154,20 +147,20 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
154147 // discarding hash as it's encrypted
155148 }
156149 if ! d .Thumbnail || ! strings .HasPrefix (args .ReqPath , "/" ) {
157- result = append (result , objRes )
150+ result = append (result , model . ObjAddMask ( objRes , mask ) )
158151 continue
159152 }
160153 thumbPath := stdpath .Join (args .ReqPath , ".thumbnails" , name + ".webp" )
161154 thumb := fmt .Sprintf ("%s/d%s?sign=%s" ,
162155 common .GetApiUrl (ctx ),
163156 utils .EncodePath (thumbPath , true ),
164157 sign .Sign (thumbPath ))
165- result = append (result , & model.ObjThumb {
158+ result = append (result , model . ObjAddMask ( & model.ObjThumb {
166159 Object : * objRes ,
167160 Thumbnail : model.Thumbnail {
168161 Thumbnail : thumb ,
169162 },
170- })
163+ }, mask ) )
171164 }
172165
173166 return result , nil
@@ -182,7 +175,14 @@ func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) {
182175 remoteFullPath := stdpath .Join (d .RemotePath , d .encryptPath (path , firstTryIsFolder ))
183176 remoteObj , err := fs .Get (ctx , remoteFullPath , & fs.GetArgs {NoLog : true })
184177 if err != nil {
185- if secondTry && errs .IsObjectNotFound (err ) {
178+ if errors .Is (err , errs .StorageNotFound ) {
179+ remoteFullPath = stdpath .Join (d .RemotePath , path )
180+ remoteObj , err = fs .Get (ctx , remoteFullPath , & fs.GetArgs {NoLog : true })
181+ if err != nil {
182+ // 可能是 虚拟路径+开启文件夹加密:返回NotSupport让op.Get去尝试op.List查找
183+ return nil , errs .NotSupport
184+ }
185+ } else if secondTry && errs .IsObjectNotFound (err ) {
186186 // try the opposite
187187 remoteFullPath = stdpath .Join (d .RemotePath , d .encryptPath (path , ! firstTryIsFolder ))
188188 remoteObj , err = fs .Get (ctx , remoteFullPath , & fs.GetArgs {NoLog : true })
@@ -195,20 +195,30 @@ func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) {
195195 }
196196
197197 size := remoteObj .GetSize ()
198- name := model .UnwrapObj (remoteObj ).GetName ()
199- if ! remoteObj .IsDir () {
200- size , err = d .cipher .DecryptedSize (size )
201- if err != nil {
202- log .Warnf ("DecryptedSize failed for %s ,will use original size, err:%s" , path , err )
203- }
204- name , err = d .cipher .DecryptFileName (name )
205- if err != nil {
206- log .Warnf ("DecryptFileName failed for %s ,will use original name, err:%s" , path , err )
207- }
208- } else {
209- name , err = d .cipher .DecryptDirName (name )
210- if err != nil {
211- log .Warnf ("DecryptDirName failed for %s ,will use original name, err:%s" , path , err )
198+ name := remoteObj .GetName ()
199+ mask := model .GetObjMask (remoteObj )
200+ mask &^= model .Temp
201+ if mask & model .Virtual == 0 {
202+ if ! remoteObj .IsDir () {
203+ decryptedSize , err := d .cipher .DecryptedSize (size )
204+ if err != nil {
205+ log .Warnf ("DecryptedSize failed for %s ,will use original size, err:%s" , path , err )
206+ } else {
207+ size = decryptedSize
208+ }
209+ decryptedName , err := d .cipher .DecryptFileName (model .UnwrapObjName (remoteObj ).GetName ())
210+ if err != nil {
211+ log .Warnf ("DecryptFileName failed for %s ,will use original name, err:%s" , path , err )
212+ } else {
213+ name = decryptedName
214+ }
215+ } else {
216+ decryptedName , err := d .cipher .DecryptDirName (model .UnwrapObjName (remoteObj ).GetName ())
217+ if err != nil {
218+ log .Warnf ("DecryptDirName failed for %s ,will use original name, err:%s" , path , err )
219+ } else {
220+ name = decryptedName
221+ }
212222 }
213223 }
214224 obj := & model.Object {
@@ -217,8 +227,9 @@ func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) {
217227 Size : size ,
218228 Modified : remoteObj .ModTime (),
219229 IsFolder : remoteObj .IsDir (),
230+ Ctime : remoteObj .CreateTime (),
220231 }
221- return obj , nil
232+ return model . ObjAddMask ( obj , mask ) , nil
222233}
223234
224235// https://github.com/rclone/rclone/blob/v1.67.0/backend/crypt/cipher.go#L37
0 commit comments