Files
waf-platform/EdgeAdmin/internal/web/actions/default/settings/ip-library/download.go

74 lines
1.9 KiB
Go

// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
)
type DownloadAction struct {
actionutils.ParentAction
}
func (this *DownloadAction) Init() {
this.Nav("", "", "")
}
func (this *DownloadAction) RunGet(params struct {
ArtifactId int64
}) {
artifactResp, err := this.RPC().IPLibraryArtifactRPC().FindIPLibraryArtifact(this.AdminContext(), &pb.FindIPLibraryArtifactRequest{
IpLibraryArtifactId: params.ArtifactId,
})
if err != nil {
this.ErrorPage(err)
return
}
var artifact = artifactResp.IpLibraryArtifact
if artifact == nil {
this.NotFound("IPLibraryArtifact", params.ArtifactId)
return
}
var fileId = artifact.FileId
if fileId <= 0 {
this.WriteString("ip artifact file not generated")
return
}
fileResp, err := this.RPC().FileRPC().FindEnabledFile(this.AdminContext(), &pb.FindEnabledFileRequest{FileId: fileId})
if err != nil {
this.ErrorPage(err)
return
}
var file = fileResp.File
if file == nil {
this.WriteString("file not found")
return
}
chunkIdsResp, err := this.RPC().FileChunkRPC().FindAllFileChunkIds(this.AdminContext(), &pb.FindAllFileChunkIdsRequest{FileId: file.Id})
if err != nil {
this.ErrorPage(err)
return
}
this.AddHeader("Content-Disposition", "attachment; filename=\"ip-"+artifact.Code+".db\";")
this.AddHeader("Content-Length", types.String(file.Size))
for _, chunkId := range chunkIdsResp.FileChunkIds {
chunkResp, err := this.RPC().FileChunkRPC().DownloadFileChunk(this.AdminContext(), &pb.DownloadFileChunkRequest{FileChunkId: chunkId})
if err != nil {
this.ErrorPage(err)
return
}
var chunk = chunkResp.FileChunk
if chunk == nil {
break
}
_, _ = this.Write(chunkResp.FileChunk.Data)
}
}