[优化] 代码优化

This commit is contained in:
ryan
2026-05-31 20:31:57 +08:00
parent 7ab11154e3
commit 2514e7edc4
5 changed files with 46 additions and 71 deletions
+12 -8
View File
@@ -2,6 +2,7 @@ package model
import (
"errors"
"openflare/utils"
"regexp"
"strings"
"time"
@@ -56,14 +57,17 @@ type ExternalAccountView struct {
}
func (source *AuthSource) Normalize() {
source.Name = strings.TrimSpace(source.Name)
source.Type = strings.TrimSpace(strings.ToLower(source.Type))
source.DisplayName = strings.TrimSpace(source.DisplayName)
source.ClientID = strings.TrimSpace(source.ClientID)
source.ClientSecret = strings.TrimSpace(source.ClientSecret)
source.OpenIDDiscoveryURL = strings.TrimSpace(source.OpenIDDiscoveryURL)
source.Scopes = strings.TrimSpace(source.Scopes)
source.IconURL = strings.TrimSpace(source.IconURL)
source.Type = strings.ToLower(source.Type)
utils.TrimStringFields(
&source.Name,
&source.Type,
&source.DisplayName,
&source.ClientID,
&source.ClientSecret,
&source.OpenIDDiscoveryURL,
&source.Scopes,
&source.IconURL,
)
if source.DisplayName == "" {
source.DisplayName = source.Name
}
-50
View File
@@ -1,50 +0,0 @@
package model
import (
"gorm.io/gorm"
"openflare/common"
"os"
"path"
)
type File struct {
Id int `json:"id"`
Filename string `json:"filename" gorm:"index"`
Description string `json:"description"`
Uploader string `json:"uploader" gorm:"index"`
UploaderId int `json:"uploader_id" gorm:"index"`
Link string `json:"link" gorm:"unique;index"`
UploadTime string `json:"upload_time"`
DownloadCounter int `json:"download_counter"`
}
func GetAllFiles(startIdx int, num int) ([]*File, error) {
var files []*File
var err error
err = DB.Order("id desc").Limit(num).Offset(startIdx).Find(&files).Error
return files, err
}
func SearchFiles(keyword string) (files []*File, err error) {
err = DB.Select([]string{"id", "filename", "description", "uploader", "uploader_id", "link", "upload_time", "download_counter"}).Where(
"filename LIKE ? or uploader LIKE ? or uploader_id = ?", keyword+"%", keyword+"%", keyword).Find(&files).Error
return files, err
}
func (file *File) Insert() error {
var err error
err = DB.Create(file).Error
return err
}
// Delete Make sure link is valid! Because we will use os.Remove to delete it!
func (file *File) Delete() error {
var err error
err = DB.Delete(file).Error
err = os.Remove(path.Join(common.UploadPath, file.Link))
return err
}
func UpdateDownloadCounter(link string) {
DB.Model(&File{}).Where("link = ?", link).UpdateColumn("download_counter", gorm.Expr("download_counter + 1"))
}
+5 -5
View File
@@ -2,10 +2,6 @@ package model
import (
"fmt"
"github.com/glebarez/sqlite"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"log/slog"
"openflare/common"
"openflare/utils/security"
@@ -13,6 +9,11 @@ import (
"reflect"
"strings"
"sync"
"github.com/glebarez/sqlite"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
var DB *gorm.DB
@@ -25,7 +26,6 @@ type dbModel struct {
func registeredModels() []any {
return []any{
&File{},
&User{},
&AuthSource{},
&ExternalAccount{},