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

     1  package controllers
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
     7  	"github.com/cloudreve/Cloudreve/v3/service/aria2"
     8  	"github.com/cloudreve/Cloudreve/v3/service/explorer"
     9  	"github.com/gin-gonic/gin"
    10  )
    11  
    12  // AddAria2URL 添加离线下载URL
    13  func AddAria2URL(c *gin.Context) {
    14  	var addService aria2.BatchAddURLService
    15  	if err := c.ShouldBindJSON(&addService); err == nil {
    16  		res := addService.Add(c, common.URLTask)
    17  		c.JSON(200, res)
    18  	} else {
    19  		c.JSON(200, ErrorResponse(err))
    20  	}
    21  }
    22  
    23  // SelectAria2File 选择多文件离线下载中要下载的文件
    24  func SelectAria2File(c *gin.Context) {
    25  	var selectService aria2.SelectFileService
    26  	if err := c.ShouldBindJSON(&selectService); err == nil {
    27  		res := selectService.Select(c)
    28  		c.JSON(200, res)
    29  	} else {
    30  		c.JSON(200, ErrorResponse(err))
    31  	}
    32  }
    33  
    34  // AddAria2Torrent 添加离线下载种子
    35  func AddAria2Torrent(c *gin.Context) {
    36  	// 创建上下文
    37  	ctx, cancel := context.WithCancel(context.Background())
    38  	defer cancel()
    39  
    40  	var service explorer.FileIDService
    41  	if err := c.ShouldBindUri(&service); err == nil {
    42  		// 获取种子内容的下载地址
    43  		res := service.CreateDownloadSession(ctx, c)
    44  		if res.Code != 0 {
    45  			c.JSON(200, res)
    46  			return
    47  		}
    48  
    49  		// 创建下载任务
    50  		var addService aria2.AddURLService
    51  		addService.URL = res.Data.(string)
    52  
    53  		if err := c.ShouldBindJSON(&addService); err == nil {
    54  			addService.URL = res.Data.(string)
    55  			res := addService.Add(c, nil, common.URLTask)
    56  			c.JSON(200, res)
    57  		} else {
    58  			c.JSON(200, ErrorResponse(err))
    59  		}
    60  
    61  	} else {
    62  		c.JSON(200, ErrorResponse(err))
    63  	}
    64  }
    65  
    66  // CancelAria2Download 取消或删除aria2离线下载任务
    67  func CancelAria2Download(c *gin.Context) {
    68  	var selectService aria2.DownloadTaskService
    69  	if err := c.ShouldBindUri(&selectService); err == nil {
    70  		res := selectService.Delete(c)
    71  		c.JSON(200, res)
    72  	} else {
    73  		c.JSON(200, ErrorResponse(err))
    74  	}
    75  }
    76  
    77  // ListDownloading 获取正在下载中的任务
    78  func ListDownloading(c *gin.Context) {
    79  	var service aria2.DownloadListService
    80  	if err := c.ShouldBindQuery(&service); err == nil {
    81  		res := service.Downloading(c, CurrentUser(c))
    82  		c.JSON(200, res)
    83  	} else {
    84  		c.JSON(200, ErrorResponse(err))
    85  	}
    86  }
    87  
    88  // ListFinished 获取已完成的任务
    89  func ListFinished(c *gin.Context) {
    90  	var service aria2.DownloadListService
    91  	if err := c.ShouldBindQuery(&service); err == nil {
    92  		res := service.Finished(c, CurrentUser(c))
    93  		c.JSON(200, res)
    94  	} else {
    95  		c.JSON(200, ErrorResponse(err))
    96  	}
    97  }