Add video download failure reporting feature
- Introduced a new API endpoint `POST /api/v1/video/remove_watermark/report_failure` for reporting download failures. - Added a new database table `video_download_failures` to store details about failed downloads, including domain, URL, error message, and reporting metadata. - Updated the video handler to process failure reports and save them to the database. - Enhanced documentation to include details about the new reporting feature and its usage.
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
||||
rmmodel "wx_service/internal/remove_watermark/model"
|
||||
)
|
||||
|
||||
const removeWatermarkEndpoint = "https://api.23bt.cn/api/d1w/index"
|
||||
const removeWatermarkEndpoint = "https://api.23bt.cn/api/dsp/index"
|
||||
|
||||
var (
|
||||
// 从用户输入的文本里“抓取 URL”的简单正则(找到第一个 http/https 链接)
|
||||
@@ -56,6 +56,15 @@ func (e *ThirdPartyError) Error() string {
|
||||
return fmt.Sprintf("third-party api error: status=%d message=%s", e.StatusCode, e.Message)
|
||||
}
|
||||
|
||||
type DownloadFailureReport struct {
|
||||
Domain string
|
||||
FailedURL string
|
||||
ErrorMessage string
|
||||
ReportedAt time.Time
|
||||
UserAgent string
|
||||
ClientIP string
|
||||
}
|
||||
|
||||
func NewVideoService(db *gorm.DB, cfg config.ShortVideoConfig) (*VideoService, error) {
|
||||
timeout := cfg.RequestTimeout
|
||||
if timeout <= 0 {
|
||||
@@ -253,3 +262,33 @@ func truncateString(input string, max int) string {
|
||||
}
|
||||
return input[:max]
|
||||
}
|
||||
|
||||
func (s *VideoService) ReportDownloadFailure(ctx context.Context, report DownloadFailureReport) error {
|
||||
if report.ReportedAt.IsZero() {
|
||||
report.ReportedAt = time.Now()
|
||||
}
|
||||
|
||||
if report.Domain == "" && report.FailedURL != "" {
|
||||
if parsed, err := url.Parse(report.FailedURL); err == nil {
|
||||
report.Domain = parsed.Host
|
||||
}
|
||||
}
|
||||
|
||||
entry := rmmodel.VideoDownloadFailure{
|
||||
Domain: report.Domain,
|
||||
FailedURL: report.FailedURL,
|
||||
ErrorMessage: truncateString(report.ErrorMessage, 2000),
|
||||
ReportedAt: report.ReportedAt,
|
||||
UserAgent: truncateString(report.UserAgent, 255),
|
||||
ClientIP: truncateString(report.ClientIP, 64),
|
||||
}
|
||||
|
||||
if entry.FailedURL == "" {
|
||||
return errors.New("failed_url is required")
|
||||
}
|
||||
|
||||
if err := s.db.WithContext(ctx).Create(&entry).Error; err != nil {
|
||||
return fmt.Errorf("save download failure report: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user