enhance file download handling to track real users

This commit is contained in:
2025-09-01 17:51:06 +08:00
parent 44b876ba0e
commit b804741e27
2 changed files with 7 additions and 5 deletions

View File

@@ -205,7 +205,7 @@ func deleteFile(c fiber.Ctx) error {
func downloadFile(c fiber.Ctx) error {
cfToken := c.Query("cf_token")
s, filename, err := service.DownloadFile(c.Params("id"), cfToken)
s, filename, err := service.DownloadFile(c.Params("id"), cfToken, c.Locals("real_user") == true)
if err != nil {
return err
}

View File

@@ -389,7 +389,7 @@ func GetFile(fid string) (*model.FileView, error) {
}
// DownloadFile handles the file download request. Return a presigned URL or a direct file path.
func DownloadFile(fid, cfToken string) (string, string, error) {
func DownloadFile(fid, cfToken string, isRealUser bool) (string, string, error) {
file, err := dao.GetFile(fid)
if err != nil {
log.Error("failed to get file: ", err)
@@ -436,9 +436,11 @@ func DownloadFile(fid, cfToken string) (string, string, error) {
return "", "", model.NewInternalServerError("failed to download file from storage")
}
err = dao.AddResourceDownloadCount(file.ResourceID)
if err != nil {
log.Errorf("failed to add resource download count: %v", err)
if isRealUser {
err = dao.AddResourceDownloadCount(file.ResourceID)
if err != nil {
log.Errorf("failed to add resource download count: %v", err)
}
}
return path, file.Filename, nil