feat: extension support (#78)

This commit is contained in:
Levi authored and GitHub committed 2023-10-16 09:47:31 +08:00
1 parent 82d316af83
commit c073fc0a67
118 files changed
+6971 -780

No files matched your search

+48 -2
View File
@@ -1,6 +1,7 @@
package base
import (
"fmt"
"github.com/GopeedLab/gopeed/pkg/util"
)
@@ -12,20 +13,46 @@ type Request struct {
// Resource download resource
type Resource struct {
// if name is not empty, the resource is a folder and the name is the folder name
Name string `json:"name"`
Size int64 `json:"size"`
// is support range download
Range bool `json:"range"`
RootDir string `json:"rootDir"`
Range bool `json:"range"`
// file list
Files []*FileInfo `json:"files"`
Hash string `json:"hash"`
}
func (r *Resource) Validate() error {
if r.Name == "" {
return fmt.Errorf("invalid resource name")
}
if r.Files == nil || len(r.Files) == 0 {
return fmt.Errorf("invalid resource files")
}
for _, file := range r.Files {
if file.Name == "" {
return fmt.Errorf("invalid resource file name")
}
}
return nil
}
func (r *Resource) CalcSize() {
var size int64
for _, file := range r.Files {
size += file.Size
}
r.Size = size
}
type FileInfo struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
Req *Request `json:"req"`
}
// Options for download
@@ -40,6 +67,25 @@ type Options struct {
Extra any `json:"extra"`
}
func (o *Options) InitSelectFiles(fileSize int) {
// if selectFiles is empty, select all files
if len(o.SelectFiles) == 0 {
o.SelectFiles = make([]int, fileSize)
for i := 0; i < fileSize; i++ {
o.SelectFiles[i] = i
}
}
}
func (o *Options) Clone() *Options {
return &Options{
Name: o.Name,
Path: o.Path,
SelectFiles: o.SelectFiles,
Extra: o.Extra,
}
}
func ParseReqExtra[E any](req *Request) error {
if req.Extra == nil {
return nil