|
| 1 | +package strm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + stdpath "path" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 12 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 13 | + "github.com/OpenListTeam/OpenList/v4/internal/stream" |
| 14 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | + "github.com/tchap/go-patricia/v2/patricia" |
| 17 | +) |
| 18 | + |
| 19 | +var strmTrie = patricia.NewTrie() |
| 20 | + |
| 21 | +func UpdateLocalStrm(ctx context.Context, path string, objs []model.Obj) { |
| 22 | + path = utils.FixAndCleanPath(path) |
| 23 | + updateLocal := func(driver *Strm, basePath string, objs []model.Obj) { |
| 24 | + relParent := strings.TrimPrefix(basePath, driver.MountPath) |
| 25 | + localParentPath := stdpath.Join(driver.SaveStrmLocalPath, relParent) |
| 26 | + for _, obj := range objs { |
| 27 | + localPath := stdpath.Join(localParentPath, obj.GetName()) |
| 28 | + generateStrm(ctx, driver, obj, localPath) |
| 29 | + } |
| 30 | + deleteExtraFiles(localParentPath, objs) |
| 31 | + } |
| 32 | + |
| 33 | + _ = strmTrie.VisitPrefixes(patricia.Prefix(path), func(needPathPrefix patricia.Prefix, item patricia.Item) error { |
| 34 | + strmDrivers := item.([]*Strm) |
| 35 | + needPath := string(needPathPrefix) |
| 36 | + restPath := strings.TrimPrefix(path, needPath) |
| 37 | + if len(restPath) > 0 && restPath[0] != '/' { |
| 38 | + return nil |
| 39 | + } |
| 40 | + for _, strmDriver := range strmDrivers { |
| 41 | + strmObjs, _ := utils.SliceConvert(objs, func(obj model.Obj) (model.Obj, error) { |
| 42 | + ret := strmDriver.convert2strmObj(ctx, path, obj) |
| 43 | + return &ret, nil |
| 44 | + }) |
| 45 | + updateLocal(strmDriver, stdpath.Join(stdpath.Base(needPath), restPath), strmObjs) |
| 46 | + } |
| 47 | + return nil |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func InsertStrm(dstPath string, d *Strm) error { |
| 52 | + prefix := patricia.Prefix(strings.TrimRight(dstPath, "/")) |
| 53 | + existing := strmTrie.Get(prefix) |
| 54 | + |
| 55 | + if existing == nil { |
| 56 | + if !strmTrie.Insert(prefix, []*Strm{d}) { |
| 57 | + return errors.New("failed to insert strm") |
| 58 | + } |
| 59 | + return nil |
| 60 | + } |
| 61 | + if lst, ok := existing.([]*Strm); ok { |
| 62 | + strmTrie.Set(prefix, append(lst, d)) |
| 63 | + } else { |
| 64 | + return errors.New("invalid trie item type") |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func RemoveStrm(dstPath string, d *Strm) { |
| 71 | + prefix := patricia.Prefix(strings.TrimRight(dstPath, "/")) |
| 72 | + existing := strmTrie.Get(prefix) |
| 73 | + if existing == nil { |
| 74 | + return |
| 75 | + } |
| 76 | + lst, ok := existing.([]*Strm) |
| 77 | + if !ok { |
| 78 | + return |
| 79 | + } |
| 80 | + if len(lst) == 1 && lst[0] == d { |
| 81 | + strmTrie.Delete(prefix) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + for i, di := range lst { |
| 86 | + if di == d { |
| 87 | + newList := append(lst[:i], lst[i+1:]...) |
| 88 | + strmTrie.Set(prefix, newList) |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func generateStrm(ctx context.Context, driver *Strm, obj model.Obj, localPath string) { |
| 95 | + link, err := driver.Link(ctx, obj, model.LinkArgs{}) |
| 96 | + if err != nil { |
| 97 | + log.Warnf("failed to generate strm of obj %s: failed to link: %v", localPath, err) |
| 98 | + return |
| 99 | + } |
| 100 | + seekableStream, err := stream.NewSeekableStream(&stream.FileStream{ |
| 101 | + Obj: obj, |
| 102 | + Ctx: ctx, |
| 103 | + }, link) |
| 104 | + if err != nil { |
| 105 | + _ = link.Close() |
| 106 | + log.Warnf("failed to generate strm of obj %s: failed to get seekable stream: %v", localPath, err) |
| 107 | + return |
| 108 | + } |
| 109 | + defer seekableStream.Close() |
| 110 | + file, err := utils.CreateNestedFile(localPath) |
| 111 | + if err != nil { |
| 112 | + log.Warnf("failed to generate strm of obj %s: failed to create local file: %v", localPath, err) |
| 113 | + return |
| 114 | + } |
| 115 | + defer file.Close() |
| 116 | + if _, err := io.Copy(file, seekableStream); err != nil { |
| 117 | + log.Warnf("failed to generate strm of obj %s: copy failed: %v", localPath, err) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func deleteExtraFiles(localPath string, objs []model.Obj) { |
| 122 | + localFiles, err := getLocalFiles(localPath) |
| 123 | + if err != nil { |
| 124 | + log.Errorf("Failed to read local files from %s: %v", localPath, err) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + objsSet := make(map[string]struct{}) |
| 129 | + for _, obj := range objs { |
| 130 | + if obj.IsDir() { |
| 131 | + continue |
| 132 | + } |
| 133 | + objsSet[stdpath.Join(localPath, obj.GetName())] = struct{}{} |
| 134 | + } |
| 135 | + |
| 136 | + for _, localFile := range localFiles { |
| 137 | + if _, exists := objsSet[localFile]; !exists { |
| 138 | + err := os.Remove(localFile) |
| 139 | + if err != nil { |
| 140 | + log.Errorf("Failed to delete file: %s, error: %v\n", localFile, err) |
| 141 | + } else { |
| 142 | + log.Infof("Deleted file %s", localFile) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func getLocalFiles(localPath string) ([]string, error) { |
| 149 | + var files []string |
| 150 | + entries, err := os.ReadDir(localPath) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + for _, entry := range entries { |
| 155 | + if !entry.IsDir() { |
| 156 | + files = append(files, stdpath.Join(localPath, entry.Name())) |
| 157 | + } |
| 158 | + } |
| 159 | + return files, nil |
| 160 | +} |
| 161 | + |
| 162 | +func init() { |
| 163 | + op.RegisterObjsUpdateHook(UpdateLocalStrm) |
| 164 | +} |
0 commit comments