mirror of https://github.com/cloudreve/Cloudreve
				
				
				
			Feat: delete aria2 record in client side (#335)
							parent
							
								
									c6110e9e75
								
							
						
					
					
						commit
						f0a68236a8
					
				
							
								
								
									
										2
									
								
								assets
								
								
								
								
							
							
								
								
								
								
								
								
							
						
						
									
										2
									
								
								assets
								
								
								
								
							| 
						 | 
				
			
			@ -1 +1 @@
 | 
			
		|||
Subproject commit 253bf0c5a064345af2ba4e5c3df68978de49755b
 | 
			
		||||
Subproject commit 1a2f8ce8ca7346b4b1822a7f4dd84bef70db71de
 | 
			
		||||
| 
						 | 
				
			
			@ -109,3 +109,8 @@ func (task *Download) GetOwner() *User {
 | 
			
		|||
	}
 | 
			
		||||
	return task.User
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Delete 删除离线下载记录
 | 
			
		||||
func (download *Download) Delete() error {
 | 
			
		||||
	return DB.Model(download).Delete(download).Error
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -161,3 +161,19 @@ func TestGetDownloadsByStatusAndUser(t *testing.T) {
 | 
			
		|||
		asserts.Len(res, 2)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestDownload_Delete(t *testing.T) {
 | 
			
		||||
	asserts := assert.New(t)
 | 
			
		||||
	share := Download{}
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
		mock.ExpectBegin()
 | 
			
		||||
		mock.ExpectExec("UPDATE(.+)").
 | 
			
		||||
			WillReturnResult(sqlmock.NewResult(1, 1))
 | 
			
		||||
		mock.ExpectCommit()
 | 
			
		||||
		err := share.Delete()
 | 
			
		||||
		asserts.NoError(mock.ExpectationsWereMet())
 | 
			
		||||
		asserts.NoError(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,6 +23,7 @@ type DownloadListResponse struct {
 | 
			
		|||
// FinishedListResponse 已完成任务条目
 | 
			
		||||
type FinishedListResponse struct {
 | 
			
		||||
	Name       string         `json:"name"`
 | 
			
		||||
	GID        string         `json:"gid"`
 | 
			
		||||
	Status     int            `json:"status"`
 | 
			
		||||
	Dst        string         `json:"dst"`
 | 
			
		||||
	Error      string         `json:"error"`
 | 
			
		||||
| 
						 | 
				
			
			@ -51,6 +52,7 @@ func BuildFinishedListResponse(tasks []model.Download) Response {
 | 
			
		|||
 | 
			
		||||
		download := FinishedListResponse{
 | 
			
		||||
			Name:       fileName,
 | 
			
		||||
			GID:        tasks[i].GID,
 | 
			
		||||
			Status:     tasks[i].Status,
 | 
			
		||||
			Error:      tasks[i].Error,
 | 
			
		||||
			Dst:        tasks[i].Dst,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -63,7 +63,7 @@ func AddAria2Torrent(c *gin.Context) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CancelAria2Download 取消aria2离线下载任务
 | 
			
		||||
// CancelAria2Download 取消或删除aria2离线下载任务
 | 
			
		||||
func CancelAria2Download(c *gin.Context) {
 | 
			
		||||
	var selectService aria2.DownloadTaskService
 | 
			
		||||
	if err := c.ShouldBindUri(&selectService); err == nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -485,7 +485,7 @@ func InitMasterRouter() *gin.Engine {
 | 
			
		|||
				aria2.POST("torrent/:id", middleware.HashID(hashid.FileID), controllers.AddAria2Torrent)
 | 
			
		||||
				// 重新选择要下载的文件
 | 
			
		||||
				aria2.PUT("select/:gid", controllers.SelectAria2File)
 | 
			
		||||
				// 取消下载任务
 | 
			
		||||
				// 取消或删除下载任务
 | 
			
		||||
				aria2.DELETE("task/:gid", controllers.CancelAria2Download)
 | 
			
		||||
				// 获取正在下载中的任务
 | 
			
		||||
				aria2.GET("downloading", controllers.ListDownloading)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,7 +36,7 @@ func (service *DownloadListService) Downloading(c *gin.Context, user *model.User
 | 
			
		|||
	return serializer.BuildDownloadingResponse(downloads)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Delete 取消下载任务
 | 
			
		||||
// Delete 取消或删除下载任务
 | 
			
		||||
func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response {
 | 
			
		||||
	userCtx, _ := c.Get("user")
 | 
			
		||||
	user := userCtx.(*model.User)
 | 
			
		||||
| 
						 | 
				
			
			@ -47,8 +47,12 @@ func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response {
 | 
			
		|||
		return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if download.Status != aria2.Downloading && download.Status != aria2.Paused {
 | 
			
		||||
		return serializer.Err(serializer.CodeNoPermissionErr, "此下载任务无法取消", err)
 | 
			
		||||
	if download.Status >= aria2.Error {
 | 
			
		||||
		// 如果任务已完成,则删除任务记录
 | 
			
		||||
		if err := download.Delete(); err != nil {
 | 
			
		||||
			return serializer.Err(serializer.CodeDBError, "任务记录删除失败", err)
 | 
			
		||||
		}
 | 
			
		||||
		return serializer.Response{}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 取消任务
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue