Skip to content

Commit

Permalink
feat(cloudreve): support thumbnail (#5373 close #5348)
Browse files Browse the repository at this point in the history
* feat(cloudreve): support thumbnail

* chore: remove unnecessary code
  • Loading branch information
itsHenry35 authored Oct 14, 2023
1 parent b9e192b commit 7f73354
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion drivers/cloudreve/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (d *Cloudreve) List(ctx context.Context, dir model.Obj, args model.ListArgs
}

return utils.SliceConvert(r.Objects, func(src Object) (model.Obj, error) {
return objectToObj(src), nil
thumb, err := d.GetThumb(src)
if err != nil {
return nil, err
}
return objectToObj(src, thumb), nil
})
}

Expand Down
17 changes: 10 additions & 7 deletions drivers/cloudreve/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ type Object struct {
SourceEnabled bool `json:"source_enabled"`
}

func objectToObj(f Object) *model.Object {
return &model.Object{
ID: f.Id,
Name: f.Name,
Size: int64(f.Size),
Modified: f.Date,
IsFolder: f.Type == "dir",
func objectToObj(f Object, t model.Thumbnail) *model.ObjThumb {
return &model.ObjThumb{
Object: model.Object{
ID: f.Id,
Name: f.Name,
Size: int64(f.Size),
Modified: f.Date,
IsFolder: f.Type == "dir",
},
Thumbnail: t,
}
}

Expand Down
20 changes: 20 additions & 0 deletions drivers/cloudreve/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,23 @@ func convertSrc(obj model.Obj) map[string]interface{} {
m["items"] = items
return m
}

func (d *Cloudreve) GetThumb(file Object) (model.Thumbnail, error) {
ua := d.CustomUA
if ua == "" {
ua = base.UserAgent
}
req := base.NoRedirectClient.R()
req.SetHeaders(map[string]string{
"Cookie": "cloudreve-session=" + d.Cookie,
"Accept": "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
"User-Agent": ua,
})
resp, err := req.Execute(http.MethodGet, d.Address+"/api/v3/file/thumb/"+file.Id)
if err != nil {
return model.Thumbnail{}, err
}
return model.Thumbnail{
Thumbnail: resp.Header().Get("Location"),
}, nil
}

0 comments on commit 7f73354

Please sign in to comment.