github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/serializer/aria2.go (about)

     1  package serializer
     2  
     3  import (
     4  	"path"
     5  	"time"
     6  
     7  	model "github.com/cloudreve/Cloudreve/v3/models"
     8  	"github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc"
     9  )
    10  
    11  // DownloadListResponse 下载列表响应条目
    12  type DownloadListResponse struct {
    13  	UpdateTime     time.Time      `json:"update"`
    14  	UpdateInterval int            `json:"interval"`
    15  	Name           string         `json:"name"`
    16  	Status         int            `json:"status"`
    17  	Dst            string         `json:"dst"`
    18  	Total          uint64         `json:"total"`
    19  	Downloaded     uint64         `json:"downloaded"`
    20  	Speed          int            `json:"speed"`
    21  	Info           rpc.StatusInfo `json:"info"`
    22  	NodeName       string         `json:"node"`
    23  }
    24  
    25  // FinishedListResponse 已完成任务条目
    26  type FinishedListResponse struct {
    27  	Name       string         `json:"name"`
    28  	GID        string         `json:"gid"`
    29  	Status     int            `json:"status"`
    30  	Dst        string         `json:"dst"`
    31  	Error      string         `json:"error"`
    32  	Total      uint64         `json:"total"`
    33  	Files      []rpc.FileInfo `json:"files"`
    34  	TaskStatus int            `json:"task_status"`
    35  	TaskError  string         `json:"task_error"`
    36  	CreateTime time.Time      `json:"create"`
    37  	UpdateTime time.Time      `json:"update"`
    38  	NodeName   string         `json:"node"`
    39  }
    40  
    41  // BuildFinishedListResponse 构建已完成任务条目
    42  func BuildFinishedListResponse(tasks []model.Download) Response {
    43  	resp := make([]FinishedListResponse, 0, len(tasks))
    44  
    45  	for i := 0; i < len(tasks); i++ {
    46  		fileName := tasks[i].StatusInfo.BitTorrent.Info.Name
    47  		if len(tasks[i].StatusInfo.Files) == 1 {
    48  			fileName = path.Base(tasks[i].StatusInfo.Files[0].Path)
    49  		}
    50  
    51  		// 过滤敏感信息
    52  		for i2 := 0; i2 < len(tasks[i].StatusInfo.Files); i2++ {
    53  			tasks[i].StatusInfo.Files[i2].Path = path.Base(tasks[i].StatusInfo.Files[i2].Path)
    54  		}
    55  
    56  		download := FinishedListResponse{
    57  			Name:       fileName,
    58  			GID:        tasks[i].GID,
    59  			Status:     tasks[i].Status,
    60  			Error:      tasks[i].Error,
    61  			Dst:        tasks[i].Dst,
    62  			Total:      tasks[i].TotalSize,
    63  			Files:      tasks[i].StatusInfo.Files,
    64  			TaskStatus: -1,
    65  			UpdateTime: tasks[i].UpdatedAt,
    66  			CreateTime: tasks[i].CreatedAt,
    67  			NodeName:   tasks[i].NodeName,
    68  		}
    69  
    70  		if tasks[i].Task != nil {
    71  			download.TaskError = tasks[i].Task.Error
    72  			download.TaskStatus = tasks[i].Task.Status
    73  		}
    74  
    75  		resp = append(resp, download)
    76  	}
    77  
    78  	return Response{Data: resp}
    79  }
    80  
    81  // BuildDownloadingResponse 构建正在下载的列表响应
    82  func BuildDownloadingResponse(tasks []model.Download, intervals map[uint]int) Response {
    83  	resp := make([]DownloadListResponse, 0, len(tasks))
    84  
    85  	for i := 0; i < len(tasks); i++ {
    86  		fileName := ""
    87  		if len(tasks[i].StatusInfo.Files) > 0 {
    88  			fileName = path.Base(tasks[i].StatusInfo.Files[0].Path)
    89  		}
    90  
    91  		// 过滤敏感信息
    92  		tasks[i].StatusInfo.Dir = ""
    93  		for i2 := 0; i2 < len(tasks[i].StatusInfo.Files); i2++ {
    94  			tasks[i].StatusInfo.Files[i2].Path = path.Base(tasks[i].StatusInfo.Files[i2].Path)
    95  		}
    96  
    97  		interval := 10
    98  		if actualInterval, ok := intervals[tasks[i].ID]; ok {
    99  			interval = actualInterval
   100  		}
   101  
   102  		resp = append(resp, DownloadListResponse{
   103  			UpdateTime:     tasks[i].UpdatedAt,
   104  			UpdateInterval: interval,
   105  			Name:           fileName,
   106  			Status:         tasks[i].Status,
   107  			Dst:            tasks[i].Dst,
   108  			Total:          tasks[i].TotalSize,
   109  			Downloaded:     tasks[i].DownloadedSize,
   110  			Speed:          tasks[i].Speed,
   111  			Info:           tasks[i].StatusInfo,
   112  			NodeName:       tasks[i].NodeName,
   113  		})
   114  	}
   115  
   116  	return Response{Data: resp}
   117  }