|
| 1 | +package autoindex |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/OpenListTeam/OpenList/v4/drivers/base" |
| 9 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 10 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 11 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 12 | + "github.com/antchfx/htmlquery" |
| 13 | + "github.com/antchfx/xpath" |
| 14 | + "github.com/pkg/errors" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | +) |
| 17 | + |
| 18 | +type AutoIndex struct { |
| 19 | + model.Storage |
| 20 | + Addition |
| 21 | + itemXPath *xpath.Expr |
| 22 | + nameXPath *xpath.Expr |
| 23 | + modifiedXPath *xpath.Expr |
| 24 | + sizeXPath *xpath.Expr |
| 25 | + ignores map[string]any |
| 26 | +} |
| 27 | + |
| 28 | +func (d *AutoIndex) Config() driver.Config { |
| 29 | + return config |
| 30 | +} |
| 31 | + |
| 32 | +func (d *AutoIndex) GetAddition() driver.Additional { |
| 33 | + return &d.Addition |
| 34 | +} |
| 35 | + |
| 36 | +func (d *AutoIndex) Init(ctx context.Context) error { |
| 37 | + var err error |
| 38 | + d.itemXPath, err = xpath.Compile(d.ItemXPath) |
| 39 | + if err != nil { |
| 40 | + return errors.WithMessage(err, "failed to compile Item XPath") |
| 41 | + } |
| 42 | + d.nameXPath, err = xpath.Compile(d.NameXPath) |
| 43 | + if err != nil { |
| 44 | + return errors.WithMessage(err, "failed to compile Name XPath") |
| 45 | + } |
| 46 | + if len(d.ModifiedXPath) > 0 { |
| 47 | + d.modifiedXPath, err = xpath.Compile(d.ModifiedXPath) |
| 48 | + if err != nil { |
| 49 | + return errors.WithMessage(err, "failed to compile Modified XPath") |
| 50 | + } |
| 51 | + } |
| 52 | + if len(d.SizeXPath) > 0 { |
| 53 | + d.sizeXPath, err = xpath.Compile(d.SizeXPath) |
| 54 | + if err != nil { |
| 55 | + return errors.WithMessage(err, "failed to compile Size XPath") |
| 56 | + } |
| 57 | + } |
| 58 | + ignores := strings.Split(d.IgnoreFileNames, "\n") |
| 59 | + d.ignores = make(map[string]any, len(ignores)) |
| 60 | + for _, i := range ignores { |
| 61 | + i = strings.TrimSpace(i) |
| 62 | + if len(i) == 0 { |
| 63 | + continue |
| 64 | + } |
| 65 | + d.ignores[i] = struct{}{} |
| 66 | + } |
| 67 | + hasScheme := strings.Contains(d.URL, "://") |
| 68 | + hasSuffix := strings.HasSuffix(d.URL, "/") |
| 69 | + if !hasScheme || !hasSuffix { |
| 70 | + if !hasSuffix { |
| 71 | + d.URL = d.URL + "/" |
| 72 | + } |
| 73 | + if !hasScheme { |
| 74 | + d.URL = "https://" + d.URL |
| 75 | + } |
| 76 | + op.MustSaveDriverStorage(d) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (d *AutoIndex) Drop(ctx context.Context) error { |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (d *AutoIndex) GetRoot(ctx context.Context) (model.Obj, error) { |
| 86 | + return &model.Object{ |
| 87 | + Name: op.RootName, |
| 88 | + Path: d.URL, |
| 89 | + Modified: d.Modified, |
| 90 | + Mask: model.Locked, |
| 91 | + IsFolder: true, |
| 92 | + }, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (d *AutoIndex) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { |
| 96 | + res, err := base.RestyClient.R(). |
| 97 | + SetContext(ctx). |
| 98 | + SetDoNotParseResponse(true). |
| 99 | + Get(dir.GetPath()) |
| 100 | + if err != nil { |
| 101 | + return nil, errors.WithMessagef(err, "failed to get url [%s]", dir.GetPath()) |
| 102 | + } |
| 103 | + defer res.RawResponse.Body.Close() |
| 104 | + doc, err := htmlquery.Parse(res.RawBody()) |
| 105 | + if err != nil { |
| 106 | + return nil, errors.WithMessagef(err, "failed to parse [%s]", dir.GetPath()) |
| 107 | + } |
| 108 | + itemsIter := d.itemXPath.Select(htmlquery.CreateXPathNavigator(doc)) |
| 109 | + var objs []model.Obj |
| 110 | + for itemsIter.MoveNext() { |
| 111 | + nameFull, err := parseString(d.nameXPath.Evaluate(itemsIter.Current().Copy())) |
| 112 | + if err != nil { |
| 113 | + log.Warnf("skip invalid name evaluating result: %v", err) |
| 114 | + continue |
| 115 | + } |
| 116 | + nameFull = strings.TrimSpace(nameFull) |
| 117 | + name, isDir := strings.CutSuffix(nameFull, "/") |
| 118 | + if _, ok := d.ignores[name]; ok { |
| 119 | + continue |
| 120 | + } |
| 121 | + var size int64 = 0 |
| 122 | + exact := false |
| 123 | + modified := time.Now() |
| 124 | + if d.sizeXPath != nil { |
| 125 | + size, exact, err = parseSize(d.sizeXPath.Evaluate(itemsIter.Current().Copy())) |
| 126 | + if err != nil { |
| 127 | + log.Errorf("failed to parse size of %s: %v", name, err) |
| 128 | + } |
| 129 | + } |
| 130 | + if d.modifiedXPath != nil { |
| 131 | + modified, err = parseTime(d.modifiedXPath.Evaluate(itemsIter.Current().Copy()), d.ModifiedTimeFormat) |
| 132 | + if err != nil { |
| 133 | + log.Errorf("failed to parse modified time of %s: %v", name, err) |
| 134 | + } |
| 135 | + } |
| 136 | + var o model.Obj = &model.Object{ |
| 137 | + Name: name, |
| 138 | + IsFolder: isDir, |
| 139 | + Path: dir.GetPath() + nameFull, |
| 140 | + Modified: modified, |
| 141 | + Size: size, |
| 142 | + } |
| 143 | + if exact { |
| 144 | + o = &exactSizeObj{Obj: o} |
| 145 | + } |
| 146 | + objs = append(objs, o) |
| 147 | + } |
| 148 | + return objs, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (d *AutoIndex) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { |
| 152 | + if _, ok := file.(*exactSizeObj); ok || args.Redirect { |
| 153 | + return &model.Link{URL: file.GetPath()}, nil |
| 154 | + } |
| 155 | + res, err := base.RestyClient.R(). |
| 156 | + SetContext(ctx). |
| 157 | + SetDoNotParseResponse(true). |
| 158 | + Head(file.GetPath()) |
| 159 | + if err != nil { |
| 160 | + return nil, errors.WithMessagef(err, "failed to head [%s]", file.GetPath()) |
| 161 | + } |
| 162 | + _ = res.RawResponse.Body.Close() |
| 163 | + return &model.Link{ |
| 164 | + URL: file.GetPath(), |
| 165 | + ContentLength: res.RawResponse.ContentLength, |
| 166 | + }, nil |
| 167 | +} |
| 168 | + |
| 169 | +var _ driver.Driver = (*AutoIndex)(nil) |
0 commit comments