This commit is contained in:
Mrx
2026-05-29 21:13:46 +08:00
parent 4b1176b1e3
commit 5ee816d6c1
3 changed files with 61 additions and 26 deletions

View File

@@ -391,27 +391,56 @@ func (s *InvoiceApplicationServiceImpl) DownloadInvoiceFile(ctx context.Context,
return nil, fmt.Errorf("发票尚未通过审核") return nil, fmt.Errorf("发票尚未通过审核")
} }
// 4. 验证文件信息 fileContent, fileID, fileName, fileSize, fileURL, err := downloadInvoiceFileFromStorage(ctx, s.storageService, application)
if application.FileURL == nil || *application.FileURL == "" {
return nil, fmt.Errorf("发票文件不存在")
}
// 5. 从七牛云下载文件内容
fileContent, err := s.storageService.DownloadFile(ctx, *application.FileURL)
if err != nil { if err != nil {
return nil, fmt.Errorf("下载文件失败: %w", err) return nil, err
} }
// 6. 构建响应DTO
return &dto.FileDownloadResponse{ return &dto.FileDownloadResponse{
FileID: *application.FileID, FileID: fileID,
FileName: *application.FileName, FileName: fileName,
FileSize: *application.FileSize, FileSize: fileSize,
FileURL: *application.FileURL, FileURL: fileURL,
FileContent: fileContent, FileContent: fileContent,
}, nil }, nil
} }
// downloadInvoiceFileFromStorage 从七牛云下载发票文件(优先用 FileID 生成新 URL避免私有空间签名过期
func downloadInvoiceFileFromStorage(ctx context.Context, storageService *storage.QiNiuStorageService, application *entities.InvoiceApplication) ([]byte, string, string, int64, string, error) {
if application.FileID == nil || *application.FileID == "" {
if application.FileURL == nil || *application.FileURL == "" {
return nil, "", "", 0, "", fmt.Errorf("发票文件不存在")
}
fileContent, err := storageService.DownloadFile(ctx, *application.FileURL)
if err != nil {
return nil, "", "", 0, "", fmt.Errorf("下载文件失败: %w", err)
}
return fileContent, derefString(application.FileID), derefString(application.FileName), derefInt64(application.FileSize), *application.FileURL, nil
}
fileContent, err := storageService.DownloadFileByKey(ctx, *application.FileID)
if err != nil {
return nil, "", "", 0, "", fmt.Errorf("下载文件失败: %w", err)
}
fileURL := storageService.GetFileURL(ctx, *application.FileID)
return fileContent, *application.FileID, derefString(application.FileName), derefInt64(application.FileSize), fileURL, nil
}
func derefString(v *string) string {
if v == nil {
return ""
}
return *v
}
func derefInt64(v *int64) int64 {
if v == nil {
return 0
}
return *v
}
// GetAvailableAmount 获取可开票金额 // GetAvailableAmount 获取可开票金额
func (s *InvoiceApplicationServiceImpl) GetAvailableAmount(ctx context.Context, userID string) (*dto.AvailableAmountResponse, error) { func (s *InvoiceApplicationServiceImpl) GetAvailableAmount(ctx context.Context, userID string) (*dto.AvailableAmountResponse, error) {
// 1. 验证用户是否存在 // 1. 验证用户是否存在
@@ -764,23 +793,16 @@ func (s *AdminInvoiceApplicationServiceImpl) DownloadInvoiceFile(ctx context.Con
return nil, fmt.Errorf("发票尚未通过审核") return nil, fmt.Errorf("发票尚未通过审核")
} }
// 3. 验证文件信息 fileContent, fileID, fileName, fileSize, fileURL, err := downloadInvoiceFileFromStorage(ctx, s.storageService, application)
if application.FileURL == nil || *application.FileURL == "" {
return nil, fmt.Errorf("发票文件不存在")
}
// 4. 从七牛云下载文件内容
fileContent, err := s.storageService.DownloadFile(ctx, *application.FileURL)
if err != nil { if err != nil {
return nil, fmt.Errorf("下载文件失败: %w", err) return nil, err
} }
// 5. 构建响应DTO
return &dto.FileDownloadResponse{ return &dto.FileDownloadResponse{
FileID: *application.FileID, FileID: fileID,
FileName: *application.FileName, FileName: fileName,
FileSize: *application.FileSize, FileSize: fileSize,
FileURL: *application.FileURL, FileURL: fileURL,
FileContent: fileContent, FileContent: fileContent,
}, nil }, nil
} }

View File

@@ -281,6 +281,15 @@ func (s *QiNiuStorageService) UploadFromReader(ctx context.Context, reader io.Re
return s.UploadFile(ctx, fileBytes, fileName) return s.UploadFile(ctx, fileBytes, fileName)
} }
// DownloadFileByKey 通过存储 key 下载文件(每次生成最新访问 URL避免私有空间签名过期
func (s *QiNiuStorageService) DownloadFileByKey(ctx context.Context, key string) ([]byte, error) {
if key == "" {
return nil, fmt.Errorf("文件 key 不能为空")
}
fileURL := s.GetFileURL(ctx, key)
return s.DownloadFile(ctx, fileURL)
}
// DownloadFile 从七牛云下载文件 // DownloadFile 从七牛云下载文件
func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) { func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) {
s.logger.Info("开始从七牛云下载文件", zap.String("file_url", fileURL)) s.logger.Info("开始从七牛云下载文件", zap.String("file_url", fileURL))

View File

@@ -1070,6 +1070,10 @@ func (h *FinanceHandler) AdminDownloadInvoiceFile(c *gin.Context) {
result, err := h.adminInvoiceAppService.DownloadInvoiceFile(c.Request.Context(), applicationID) result, err := h.adminInvoiceAppService.DownloadInvoiceFile(c.Request.Context(), applicationID)
if err != nil { if err != nil {
h.logger.Error("下载发票文件失败",
zap.String("application_id", applicationID),
zap.Error(err),
)
h.responseBuilder.InternalError(c, "下载发票文件失败") h.responseBuilder.InternalError(c, "下载发票文件失败")
return return
} }