diff --git a/server/dao/file.go b/server/dao/file.go index 10b82fe..646c278 100644 --- a/server/dao/file.go +++ b/server/dao/file.go @@ -74,7 +74,7 @@ func GetUploadingFilesOlderThan(time time.Time) ([]model.UploadingFile, error) { return files, nil } -func CreateFile(filename string, description string, resourceID uint, storageID *uint, storageKey string, redirectUrl string, size int64) (*model.File, error) { +func CreateFile(filename string, description string, resourceID uint, storageID *uint, storageKey string, redirectUrl string, size int64, userID uint) (*model.File, error) { if storageID == nil && redirectUrl == "" { return nil, errors.New("storageID and redirectUrl cannot be both empty") } @@ -87,6 +87,7 @@ func CreateFile(filename string, description string, resourceID uint, storageID RedirectUrl: redirectUrl, StorageKey: storageKey, Size: size, + UserID: userID, } if err := db.Create(f).Error; err != nil { return nil, err diff --git a/server/dao/statistic.go b/server/dao/statistic.go index 6005348..d2dbad3 100644 --- a/server/dao/statistic.go +++ b/server/dao/statistic.go @@ -15,7 +15,9 @@ func SetStatistic(key string, value int64) error { func GetStatistic(key string) int64 { statistic := &model.Statistic{} - if err := db.Where("key = ?", key).First(statistic).Error; err != nil { + if err := db.Where(&model.Statistic{ + Key: key, + }).First(statistic).Error; err != nil { return 0 } return statistic.Value diff --git a/server/service/file.go b/server/service/file.go index 14fe144..9d7d030 100644 --- a/server/service/file.go +++ b/server/service/file.go @@ -232,7 +232,7 @@ func FinishUploadingFile(uid uint, fid uint) (*model.FileView, error) { return nil, model.NewInternalServerError("failed to finish uploading file. please re-upload") } - dbFile, err := dao.CreateFile(uploadingFile.Filename, uploadingFile.Description, uploadingFile.TargetResourceID, &uploadingFile.TargetStorageID, "", "", uploadingFile.TotalSize) + dbFile, err := dao.CreateFile(uploadingFile.Filename, uploadingFile.Description, uploadingFile.TargetResourceID, &uploadingFile.TargetStorageID, "", "", uploadingFile.TotalSize, uid) if err != nil { log.Error("failed to create file in db: ", err) _ = os.Remove(resultFilePath) @@ -305,7 +305,7 @@ func CreateRedirectFile(uid uint, filename string, description string, resourceI return nil, model.NewUnAuthorizedError("user cannot upload file") } - file, err := dao.CreateFile(filename, description, resourceID, nil, "", redirectUrl, 0) + file, err := dao.CreateFile(filename, description, resourceID, nil, "", redirectUrl, 0, uid) if err != nil { log.Error("failed to create file in db: ", err) return nil, model.NewInternalServerError("failed to create file in db") @@ -397,6 +397,7 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) { log.Info("cf token verification failed") return "", "", model.NewRequestError("cf token verification failed") } + log.Info("File download request from: " + ip) downloads, _ := ipDownloads.Load(ip) if downloads == nil { ipDownloads.Store(ip, 1)