mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Improve statistic
This commit is contained in:
@@ -293,6 +293,10 @@ func init() {
|
||||
for id, stats := range cachedResourcesStats {
|
||||
var count int64
|
||||
if err := tx.Model(&model.Resource{}).Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Warnf("Resource with ID %d not found, skipping stats update", id)
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
@@ -322,15 +326,6 @@ func init() {
|
||||
}
|
||||
|
||||
func AddResourceViewCount(id uint) error {
|
||||
// 检查资源是否存在
|
||||
exists, err := ExistsResource(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
|
||||
cacheMutex.RLock()
|
||||
stats, exists := cachedResourcesStats[id]
|
||||
cacheMutex.RUnlock()
|
||||
@@ -350,15 +345,6 @@ func AddResourceViewCount(id uint) error {
|
||||
}
|
||||
|
||||
func AddResourceDownloadCount(id uint) error {
|
||||
// 检查资源是否存在
|
||||
exists, err := ExistsResource(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
|
||||
cacheMutex.RLock()
|
||||
stats, exists := cachedResourcesStats[id]
|
||||
cacheMutex.RUnlock()
|
||||
|
@@ -437,7 +437,10 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) {
|
||||
|
||||
if file.StorageID == nil {
|
||||
if file.RedirectUrl != "" {
|
||||
_ = dao.AddResourceDownloadCount(file.ResourceID)
|
||||
err := dao.AddResourceDownloadCount(file.ResourceID)
|
||||
if err != nil {
|
||||
log.Errorf("failed to add resource download count: %v", err)
|
||||
}
|
||||
return file.RedirectUrl, file.Filename, nil
|
||||
}
|
||||
return "", "", model.NewRequestError("file is not available")
|
||||
@@ -454,10 +457,17 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) {
|
||||
}
|
||||
|
||||
path, err := iStorage.Download(file.StorageKey, file.Filename)
|
||||
if err != nil {
|
||||
log.Error("failed to download file from storage: ", err)
|
||||
return "", "", model.NewInternalServerError("failed to download file from storage")
|
||||
}
|
||||
|
||||
_ = dao.AddResourceDownloadCount(file.ResourceID)
|
||||
err = dao.AddResourceDownloadCount(file.ResourceID)
|
||||
if err != nil {
|
||||
log.Errorf("failed to add resource download count: %v", err)
|
||||
}
|
||||
|
||||
return path, file.Filename, err
|
||||
return path, file.Filename, nil
|
||||
}
|
||||
|
||||
func testFileUrl(url string) (int64, error) {
|
||||
|
@@ -113,10 +113,13 @@ func parseResourceIfPresent(line string, host string) *model.ResourceView {
|
||||
|
||||
func GetResource(id uint, host string) (*model.ResourceDetailView, error) {
|
||||
r, err := dao.GetResourceByID(id)
|
||||
_ = dao.AddResourceViewCount(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = dao.AddResourceViewCount(id)
|
||||
if err != nil {
|
||||
log.Error("AddResourceViewCount error: ", err)
|
||||
}
|
||||
v := r.ToDetailView()
|
||||
if host != "" {
|
||||
related := findRelatedResources(r, host)
|
||||
|
Reference in New Issue
Block a user