Chore: optimized imports, use logger pkg instead of `util.Log()`

pull/1457/head
AH-dark 3 years ago
parent 1601a88fe1
commit 9475cc19fe

@ -1 +1 @@
Subproject commit a1028e7e0ae96be4bb67d8c117cf39e07c207473 Subproject commit e428b86964988cadf329014349a1e3bf5f44b12a

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/hashicorp/go-version" "github.com/hashicorp/go-version"
) )
@ -37,13 +37,13 @@ func CheckUpdate() {
client := request.NewClient() client := request.NewClient()
res, err := client.Request("GET", "https://api.github.com/repos/cloudreve/cloudreve/releases", nil).GetResponse() res, err := client.Request("GET", "https://api.github.com/repos/cloudreve/cloudreve/releases", nil).GetResponse()
if err != nil { if err != nil {
util.Log().Warning("更新检查失败, %s", err) logger.Warning("更新检查失败, %s", err)
return return
} }
var list []GitHubRelease var list []GitHubRelease
if err := json.Unmarshal([]byte(res), &list); err != nil { if err := json.Unmarshal([]byte(res), &list); err != nil {
util.Log().Warning("更新检查失败, %s", err) logger.Warning("更新检查失败, %s", err)
return return
} }
@ -51,7 +51,7 @@ func CheckUpdate() {
present, err1 := version.NewVersion(conf.BackendVersion) present, err1 := version.NewVersion(conf.BackendVersion)
latest, err2 := version.NewVersion(list[0].Tag) latest, err2 := version.NewVersion(list[0].Tag)
if err1 == nil && err2 == nil && latest.GreaterThan(present) { if err1 == nil && err2 == nil && latest.GreaterThan(present) {
util.Log().Info("有新的版本 [%s] 可用,下载:%s", list[0].Name, list[0].URL) logger.Info("有新的版本 [%s] 可用,下载:%s", list[0].Name, list[0].URL)
} }
} }

@ -2,17 +2,18 @@ package bootstrap
import ( import (
"context" "context"
"github.com/cloudreve/Cloudreve/v3/models/scripts/invoker" "github.com/cloudreve/Cloudreve/v3/models/scripts/invoker"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
func RunScript(name string) { func RunScript(name string) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := invoker.RunDBScript(name, ctx); err != nil { if err := invoker.RunDBScript(name, ctx); err != nil {
util.Log().Error("数据库脚本执行失败: %s", err) logger.Error("数据库脚本执行失败: %s", err)
return return
} }
util.Log().Info("数据库脚本 [%s] 执行完毕", name) logger.Info("数据库脚本 [%s] 执行完毕", name)
} }

@ -11,6 +11,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
@ -46,13 +47,13 @@ func (b *GinFS) Exists(prefix string, filepath string) bool {
// InitStatic 初始化静态资源文件 // InitStatic 初始化静态资源文件
func InitStatic(statics fs.FS) { func InitStatic(statics fs.FS) {
if util.Exists(util.RelativePath(StaticFolder)) { if util.Exists(util.RelativePath(StaticFolder)) {
util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") logger.Info("检测到 statics 目录存在,将使用此目录下的静态资源文件")
StaticFS = static.LocalFile(util.RelativePath("statics"), false) StaticFS = static.LocalFile(util.RelativePath("statics"), false)
} else { } else {
// 初始化静态资源 // 初始化静态资源
embedFS, err := fs.Sub(statics, "assets/build") embedFS, err := fs.Sub(statics, "assets/build")
if err != nil { if err != nil {
util.Log().Panic("无法初始化静态资源, %s", err) logger.Panic("无法初始化静态资源, %s", err)
} }
StaticFS = &GinFS{ StaticFS = &GinFS{
@ -62,19 +63,19 @@ func InitStatic(statics fs.FS) {
// 检查静态资源的版本 // 检查静态资源的版本
f, err := StaticFS.Open("version.json") f, err := StaticFS.Open("version.json")
if err != nil { if err != nil {
util.Log().Warning("静态资源版本标识文件不存在,请重新构建或删除 statics 目录") logger.Warning("静态资源版本标识文件不存在,请重新构建或删除 statics 目录")
return return
} }
b, err := io.ReadAll(f) b, err := io.ReadAll(f)
if err != nil { if err != nil {
util.Log().Warning("无法读取静态资源文件版本,请重新构建或删除 statics 目录") logger.Warning("无法读取静态资源文件版本,请重新构建或删除 statics 目录")
return return
} }
var v staticVersion var v staticVersion
if err := json.Unmarshal(b, &v); err != nil { if err := json.Unmarshal(b, &v); err != nil {
util.Log().Warning("无法解析静态资源文件版本, %s", err) logger.Warning("无法解析静态资源文件版本, %s", err)
return return
} }
@ -84,12 +85,12 @@ func InitStatic(statics fs.FS) {
} }
if v.Name != staticName { if v.Name != staticName {
util.Log().Warning("静态资源版本不匹配,请重新构建或删除 statics 目录") logger.Warning("静态资源版本不匹配,请重新构建或删除 statics 目录")
return return
} }
if v.Version != conf.RequiredStaticVersion { if v.Version != conf.RequiredStaticVersion {
util.Log().Warning("静态资源版本不匹配 [当前 %s, 需要: %s],请重新构建或删除 statics 目录", v.Version, conf.RequiredStaticVersion) logger.Warning("静态资源版本不匹配 [当前 %s, 需要: %s],请重新构建或删除 statics 目录", v.Version, conf.RequiredStaticVersion)
return return
} }
} }
@ -99,7 +100,7 @@ func Eject(statics fs.FS) {
// 初始化静态资源 // 初始化静态资源
embedFS, err := fs.Sub(statics, "assets/build") embedFS, err := fs.Sub(statics, "assets/build")
if err != nil { if err != nil {
util.Log().Panic("无法初始化静态资源, %s", err) logger.Panic("无法初始化静态资源, %s", err)
} }
var walk func(relPath string, d fs.DirEntry, err error) error var walk func(relPath string, d fs.DirEntry, err error) error
@ -117,7 +118,7 @@ func Eject(statics fs.FS) {
return errors.Errorf("无法创建文件[%s], %s, 跳过...", relPath, err) return errors.Errorf("无法创建文件[%s], %s, 跳过...", relPath, err)
} }
util.Log().Info("导出 [%s]...", relPath) logger.Info("导出 [%s]...", relPath)
obj, _ := embedFS.Open(relPath) obj, _ := embedFS.Open(relPath)
if _, err := io.Copy(out, bufio.NewReader(obj)); err != nil { if _, err := io.Copy(out, bufio.NewReader(obj)); err != nil {
return errors.Errorf("无法写入文件[%s], %s, 跳过...", relPath, err) return errors.Errorf("无法写入文件[%s], %s, 跳过...", relPath, err)
@ -126,11 +127,11 @@ func Eject(statics fs.FS) {
return nil return nil
} }
// util.Log().Info("开始导出内置静态资源...") // logger.Info("开始导出内置静态资源...")
err = fs.WalkDir(embedFS, ".", walk) err = fs.WalkDir(embedFS, ".", walk)
if err != nil { if err != nil {
util.Log().Error("导出内置静态资源遇到错误:%s", err) logger.Error("导出内置静态资源遇到错误:%s", err)
return return
} }
util.Log().Info("内置静态资源导出完成") logger.Info("内置静态资源导出完成")
} }

@ -30,6 +30,7 @@ require (
github.com/qiniu/go-sdk/v7 v7.11.1 github.com/qiniu/go-sdk/v7 v7.11.1
github.com/rafaeljusto/redigomock v0.0.0-20191117212112-00b2509252a1 github.com/rafaeljusto/redigomock v0.0.0-20191117212112-00b2509252a1
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.0
github.com/speps/go-hashids v2.0.0+incompatible github.com/speps/go-hashids v2.0.0+incompatible
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/captcha v1.0.393 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/captcha v1.0.393
@ -99,6 +100,7 @@ require (
github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.12 // indirect github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/mattn/go-sqlite3 v1.14.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@ -116,7 +118,6 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/cobra v1.1.3 // indirect github.com/spf13/cobra v1.1.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect

@ -786,7 +786,6 @@ github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
@ -1159,7 +1158,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 h1:LOlKVhfDyahgmqa97awczplwkjzNaELFg3zRIJ13RYo=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=

@ -17,6 +17,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/bootstrap" "github.com/cloudreve/Cloudreve/v3/bootstrap"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/routers" "github.com/cloudreve/Cloudreve/v3/routers"
@ -71,7 +72,7 @@ func main() {
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
go func() { go func() {
sig := <-sigChan sig := <-sigChan
util.Log().Info("收到信号 %s开始关闭 server", sig) logger.Info("收到信号 %s开始关闭 server", sig)
ctx := context.Background() ctx := context.Background()
if conf.SystemConfig.GracePeriod != 0 { if conf.SystemConfig.GracePeriod != 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
@ -81,16 +82,16 @@ func main() {
err := server.Shutdown(ctx) err := server.Shutdown(ctx)
if err != nil { if err != nil {
util.Log().Error("关闭 server 错误, %s", err) logger.Error("关闭 server 错误, %s", err)
} }
}() }()
// 如果启用了SSL // 如果启用了SSL
if conf.SSLConfig.CertPath != "" { if conf.SSLConfig.CertPath != "" {
util.Log().Info("开始监听 %s", conf.SSLConfig.Listen) logger.Info("开始监听 %s", conf.SSLConfig.Listen)
server.Addr = conf.SSLConfig.Listen server.Addr = conf.SSLConfig.Listen
if err := server.ListenAndServeTLS(conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil { if err := server.ListenAndServeTLS(conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil {
util.Log().Error("无法监听[%s]%s", conf.SSLConfig.Listen, err) logger.Error("无法监听[%s]%s", conf.SSLConfig.Listen, err)
return return
} }
} }
@ -100,23 +101,23 @@ func main() {
// delete socket file before listening // delete socket file before listening
if _, err := os.Stat(conf.UnixConfig.Listen); err == nil { if _, err := os.Stat(conf.UnixConfig.Listen); err == nil {
if err = os.Remove(conf.UnixConfig.Listen); err != nil { if err = os.Remove(conf.UnixConfig.Listen); err != nil {
util.Log().Error("删除 socket 文件错误, %s", err) logger.Error("删除 socket 文件错误, %s", err)
return return
} }
} }
api.TrustedPlatform = conf.UnixConfig.ProxyHeader api.TrustedPlatform = conf.UnixConfig.ProxyHeader
util.Log().Info("开始监听 %s", conf.UnixConfig.Listen) logger.Info("开始监听 %s", conf.UnixConfig.Listen)
if err := RunUnix(server); err != nil { if err := RunUnix(server); err != nil {
util.Log().Error("无法监听[%s]%s", conf.UnixConfig.Listen, err) logger.Error("无法监听[%s]%s", conf.UnixConfig.Listen, err)
} }
return return
} }
util.Log().Info("开始监听 %s", conf.SystemConfig.Listen) logger.Info("开始监听 %s", conf.SystemConfig.Listen)
server.Addr = conf.SystemConfig.Listen server.Addr = conf.SystemConfig.Listen
if err := server.ListenAndServe(); err != nil { if err := server.ListenAndServe(); err != nil {
util.Log().Error("无法监听[%s]%s", conf.SystemConfig.Listen, err) logger.Error("无法监听[%s]%s", conf.SystemConfig.Listen, err)
} }
} }

@ -5,21 +5,21 @@ import (
"context" "context"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/oss"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/upyun"
"github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/oss"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/upyun"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/qiniu/go-sdk/v7/auth/qbox"
) )
const ( const (
@ -194,7 +194,7 @@ func QiniuCallbackAuth() gin.HandlerFunc {
mac := qbox.NewMac(session.Policy.AccessKey, session.Policy.SecretKey) mac := qbox.NewMac(session.Policy.AccessKey, session.Policy.SecretKey)
ok, err := mac.VerifyCallback(c.Request) ok, err := mac.VerifyCallback(c.Request)
if err != nil { if err != nil {
util.Log().Debug("无法验证回调请求,%s", err) logger.Debug("无法验证回调请求,%s", err)
c.JSON(401, serializer.GeneralUploadCallbackFailed{Error: "无法验证回调请求"}) c.JSON(401, serializer.GeneralUploadCallbackFailed{Error: "无法验证回调请求"})
c.Abort() c.Abort()
return return
@ -215,7 +215,7 @@ func OSSCallbackAuth() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
err := oss.VerifyCallbackSignature(c.Request) err := oss.VerifyCallbackSignature(c.Request)
if err != nil { if err != nil {
util.Log().Debug("回调签名验证失败,%s", err) logger.Debug("回调签名验证失败,%s", err)
c.JSON(401, serializer.GeneralUploadCallbackFailed{Error: "回调签名验证失败"}) c.JSON(401, serializer.GeneralUploadCallbackFailed{Error: "回调签名验证失败"})
c.Abort() c.Abort()
return return

@ -3,7 +3,13 @@ package middleware
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io"
"io/ioutil"
"strconv"
"time"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/recaptcha" "github.com/cloudreve/Cloudreve/v3/pkg/recaptcha"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
@ -12,10 +18,6 @@ import (
captcha "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/captcha/v20190722" captcha "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/captcha/v20190722"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"io"
"io/ioutil"
"strconv"
"time"
) )
type req struct { type req struct {
@ -76,14 +78,14 @@ func CaptchaRequired(configName string) gin.HandlerFunc {
case "recaptcha": case "recaptcha":
reCAPTCHA, err := recaptcha.NewReCAPTCHA(options["captcha_ReCaptchaSecret"], recaptcha.V2, 10*time.Second) reCAPTCHA, err := recaptcha.NewReCAPTCHA(options["captcha_ReCaptchaSecret"], recaptcha.V2, 10*time.Second)
if err != nil { if err != nil {
util.Log().Warning("reCAPTCHA verification failed, %s", err) logger.Warning("reCAPTCHA verification failed, %s", err)
c.Abort() c.Abort()
break break
} }
err = reCAPTCHA.Verify(service.CaptchaCode) err = reCAPTCHA.Verify(service.CaptchaCode)
if err != nil { if err != nil {
util.Log().Warning("reCAPTCHA verification failed, %s", err) logger.Warning("reCAPTCHA verification failed, %s", err)
c.JSON(200, serializer.Err(serializer.CodeCaptchaRefreshNeeded, captchaRefresh, nil)) c.JSON(200, serializer.Err(serializer.CodeCaptchaRefreshNeeded, captchaRefresh, nil))
c.Abort() c.Abort()
return return
@ -108,7 +110,7 @@ func CaptchaRequired(configName string) gin.HandlerFunc {
request.UserIp = common.StringPtr(c.ClientIP()) request.UserIp = common.StringPtr(c.ClientIP())
response, err := client.DescribeCaptchaResult(request) response, err := client.DescribeCaptchaResult(request)
if err != nil { if err != nil {
util.Log().Warning("TCaptcha verification failed, %s", err) logger.Warning("TCaptcha verification failed, %s", err)
c.Abort() c.Abort()
break break
} }

@ -1,13 +1,15 @@
package middleware package middleware
import ( import (
"io/ioutil"
"net/http"
"strings"
"github.com/cloudreve/Cloudreve/v3/bootstrap" "github.com/cloudreve/Cloudreve/v3/bootstrap"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"strings"
) )
// FrontendFileHandler 前端静态文件处理 // FrontendFileHandler 前端静态文件处理
@ -23,13 +25,13 @@ func FrontendFileHandler() gin.HandlerFunc {
// 读取index.html // 读取index.html
file, err := bootstrap.StaticFS.Open("/index.html") file, err := bootstrap.StaticFS.Open("/index.html")
if err != nil { if err != nil {
util.Log().Warning("静态文件[index.html]不存在,可能会影响首页展示") logger.Warning("静态文件[index.html]不存在,可能会影响首页展示")
return ignoreFunc return ignoreFunc
} }
fileContentBytes, err := ioutil.ReadAll(file) fileContentBytes, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
util.Log().Warning("静态文件[index.html]读取失败,可能会影响首页展示") logger.Warning("静态文件[index.html]读取失败,可能会影响首页展示")
return ignoreFunc return ignoreFunc
} }
fileContent := string(fileContentBytes) fileContent := string(fileContentBytes)

@ -2,6 +2,7 @@ package middleware
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
@ -20,10 +21,10 @@ func Session(secret string) gin.HandlerFunc {
var err error var err error
Store, err = redis.NewStoreWithDB(10, conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret)) Store, err = redis.NewStoreWithDB(10, conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret))
if err != nil { if err != nil {
util.Log().Panic("无法连接到 Redis%s", err) logger.Panic("无法连接到 Redis%s", err)
} }
util.Log().Info("已连接到 Redis 服务器:%s", conf.RedisConfig.Server) logger.Info("已连接到 Redis 服务器:%s", conf.RedisConfig.Server)
} else { } else {
Store = memstore.NewStore([]byte(secret)) Store = memstore.NewStore([]byte(secret))
} }

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -60,7 +60,7 @@ func (task *Download) BeforeSave() (err error) {
// Create 创建离线下载记录 // Create 创建离线下载记录
func (task *Download) Create() (uint, error) { func (task *Download) Create() (uint, error) {
if err := DB.Create(task).Error; err != nil { if err := DB.Create(task).Error; err != nil {
util.Log().Warning("无法插入离线下载记录, %s", err) logger.Warning("无法插入离线下载记录, %s", err)
return 0, err return 0, err
} }
return task.ID, nil return task.ID, nil
@ -69,7 +69,7 @@ func (task *Download) Create() (uint, error) {
// Save 更新 // Save 更新
func (task *Download) Save() error { func (task *Download) Save() error {
if err := DB.Save(task).Error; err != nil { if err := DB.Save(task).Error; err != nil {
util.Log().Warning("无法更新离线下载记录, %s", err) logger.Warning("无法更新离线下载记录, %s", err)
return err return err
} }
return nil return nil

@ -7,7 +7,7 @@ import (
"path" "path"
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -43,7 +43,7 @@ func (file *File) Create() error {
tx := DB.Begin() tx := DB.Begin()
if err := tx.Create(file).Error; err != nil { if err := tx.Create(file).Error; err != nil {
util.Log().Warning("无法插入文件记录, %s", err) logger.Warning("无法插入文件记录, %s", err)
tx.Rollback() tx.Rollback()
return err return err
} }

@ -5,6 +5,7 @@ import (
"path" "path"
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -161,7 +162,7 @@ func (folder *Folder) MoveOrCopyFileTo(files []uint, dstFolder *Folder, isCopy b
// 复制文件记录 // 复制文件记录
for _, oldFile := range originFiles { for _, oldFile := range originFiles {
if !oldFile.CanCopy() { if !oldFile.CanCopy() {
util.Log().Warning("无法复制正在上传中的文件 [%s] 跳过...", oldFile.Name) logger.Warning("无法复制正在上传中的文件 [%s] 跳过...", oldFile.Name)
continue continue
} }
@ -224,7 +225,7 @@ func (folder *Folder) CopyFolderTo(folderID uint, dstFolder *Folder) (size uint6
} else if IDCache, ok := newIDCache[*folder.ParentID]; ok { } else if IDCache, ok := newIDCache[*folder.ParentID]; ok {
newID = IDCache newID = IDCache
} else { } else {
util.Log().Warning("无法取得新的父目录:%d", folder.ParentID) logger.Warning("无法取得新的父目录:%d", folder.ParentID)
return size, errors.New("无法取得新的父目录") return size, errors.New("无法取得新的父目录")
} }
@ -254,7 +255,7 @@ func (folder *Folder) CopyFolderTo(folderID uint, dstFolder *Folder) (size uint6
// 复制文件记录 // 复制文件记录
for _, oldFile := range originFiles { for _, oldFile := range originFiles {
if !oldFile.CanCopy() { if !oldFile.CanCopy() {
util.Log().Warning("无法复制正在上传中的文件 [%s] 跳过...", oldFile.Name) logger.Warning("无法复制正在上传中的文件 [%s] 跳过...", oldFile.Name)
continue continue
} }

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -20,7 +21,7 @@ var DB *gorm.DB
// Init 初始化 MySQL 链接 // Init 初始化 MySQL 链接
func Init() { func Init() {
util.Log().Info("初始化数据库连接") logger.Info("初始化数据库连接")
var ( var (
db *gorm.DB db *gorm.DB
@ -51,13 +52,13 @@ func Init() {
conf.DatabaseConfig.Name, conf.DatabaseConfig.Name,
conf.DatabaseConfig.Charset)) conf.DatabaseConfig.Charset))
default: default:
util.Log().Panic("不支持数据库类型: %s", conf.DatabaseConfig.Type) logger.Panic("不支持数据库类型: %s", conf.DatabaseConfig.Type)
} }
} }
//db.SetLogger(util.Log()) //db.SetLogger(logger)
if err != nil { if err != nil {
util.Log().Panic("连接数据库不成功, %s", err) logger.Panic("连接数据库不成功, %s", err)
} }
// 处理表前缀 // 处理表前缀

@ -2,15 +2,17 @@ package model
import ( import (
"context" "context"
"sort"
"strings"
"github.com/cloudreve/Cloudreve/v3/models/scripts/invoker" "github.com/cloudreve/Cloudreve/v3/models/scripts/invoker"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/hashicorp/go-version" "github.com/hashicorp/go-version"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"sort"
"strings"
) )
// 是否需要迁移 // 是否需要迁移
@ -23,12 +25,12 @@ func needMigration() bool {
func migration() { func migration() {
// 确认是否需要执行迁移 // 确认是否需要执行迁移
if !needMigration() { if !needMigration() {
util.Log().Info("数据库版本匹配,跳过数据库迁移") logger.Info("数据库版本匹配,跳过数据库迁移")
return return
} }
util.Log().Info("开始进行数据库初始化...") logger.Info("开始进行数据库初始化...")
// 清除所有缓存 // 清除所有缓存
if instance, ok := cache.Store.(*cache.RedisStore); ok { if instance, ok := cache.Store.(*cache.RedisStore); ok {
@ -61,7 +63,7 @@ func migration() {
// 执行数据库升级脚本 // 执行数据库升级脚本
execUpgradeScripts() execUpgradeScripts()
util.Log().Info("数据库初始化结束") logger.Info("数据库初始化结束")
} }
@ -82,7 +84,7 @@ func addDefaultPolicy() {
}, },
} }
if err := DB.Create(&defaultPolicy).Error; err != nil { if err := DB.Create(&defaultPolicy).Error; err != nil {
util.Log().Panic("无法创建初始存储策略, %s", err) logger.Panic("无法创建初始存储策略, %s", err)
} }
} }
} }
@ -113,7 +115,7 @@ func addDefaultGroups() {
}, },
} }
if err := DB.Create(&defaultAdminGroup).Error; err != nil { if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建管理用户组, %s", err) logger.Panic("无法创建管理用户组, %s", err)
} }
} }
@ -134,7 +136,7 @@ func addDefaultGroups() {
}, },
} }
if err := DB.Create(&defaultAdminGroup).Error; err != nil { if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建初始注册会员用户组, %s", err) logger.Panic("无法创建初始注册会员用户组, %s", err)
} }
} }
@ -151,7 +153,7 @@ func addDefaultGroups() {
}, },
} }
if err := DB.Create(&defaultAdminGroup).Error; err != nil { if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建初始游客用户组, %s", err) logger.Panic("无法创建初始游客用户组, %s", err)
} }
} }
} }
@ -169,15 +171,15 @@ func addDefaultUser() {
defaultUser.GroupID = 1 defaultUser.GroupID = 1
err := defaultUser.SetPassword(password) err := defaultUser.SetPassword(password)
if err != nil { if err != nil {
util.Log().Panic("无法创建密码, %s", err) logger.Panic("无法创建密码, %s", err)
} }
if err := DB.Create(&defaultUser).Error; err != nil { if err := DB.Create(&defaultUser).Error; err != nil {
util.Log().Panic("无法创建初始用户, %s", err) logger.Panic("无法创建初始用户, %s", err)
} }
c := color.New(color.FgWhite).Add(color.BgBlack).Add(color.Bold) c := color.New(color.FgWhite).Add(color.BgBlack).Add(color.Bold)
util.Log().Info("初始管理员账号:" + c.Sprint("admin@cloudreve.org")) logger.Info("初始管理员账号:" + c.Sprint("admin@cloudreve.org"))
util.Log().Info("初始管理员密码:" + c.Sprint(password)) logger.Info("初始管理员密码:" + c.Sprint(password))
} }
} }
@ -195,7 +197,7 @@ func addDefaultNode() {
}, },
} }
if err := DB.Create(&defaultAdminGroup).Error; err != nil { if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建初始节点记录, %s", err) logger.Panic("无法创建初始节点记录, %s", err)
} }
} }
} }

@ -3,7 +3,6 @@ package model
import ( import (
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"github.com/gofrs/uuid"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -12,6 +11,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )

@ -3,8 +3,9 @@ package invoker
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"strings" "strings"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
type DBScript interface { type DBScript interface {
@ -15,7 +16,7 @@ var availableScripts = make(map[string]DBScript)
func RunDBScript(name string, ctx context.Context) error { func RunDBScript(name string, ctx context.Context) error {
if script, ok := availableScripts[name]; ok { if script, ok := availableScripts[name]; ok {
util.Log().Info("开始执行数据库脚本 [%s]", name) logger.Info("开始执行数据库脚本 [%s]", name)
script.Run(ctx) script.Run(ctx)
return nil return nil
} }

@ -2,7 +2,9 @@ package scripts
import ( import (
"context" "context"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/fatih/color" "github.com/fatih/color"
) )
@ -14,7 +16,7 @@ func (script ResetAdminPassword) Run(ctx context.Context) {
// 查找用户 // 查找用户
user, err := model.GetUserByID(1) user, err := model.GetUserByID(1)
if err != nil { if err != nil {
util.Log().Panic("初始管理员用户不存在, %s", err) logger.Panic("初始管理员用户不存在, %s", err)
} }
// 生成密码 // 生成密码
@ -23,9 +25,9 @@ func (script ResetAdminPassword) Run(ctx context.Context) {
// 更改为新密码 // 更改为新密码
user.SetPassword(password) user.SetPassword(password)
if err := user.Update(map[string]interface{}{"password": user.Password}); err != nil { if err := user.Update(map[string]interface{}{"password": user.Password}); err != nil {
util.Log().Panic("密码更改失败, %s", err) logger.Panic("密码更改失败, %s", err)
} }
c := color.New(color.FgWhite).Add(color.BgBlack).Add(color.Bold) c := color.New(color.FgWhite).Add(color.BgBlack).Add(color.Bold)
util.Log().Info("初始管理员密码已更改为:" + c.Sprint(password)) logger.Info("初始管理员密码已更改为:" + c.Sprint(password))
} }

@ -2,8 +2,9 @@ package scripts
import ( import (
"context" "context"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
type UserStorageCalibration int type UserStorageCalibration int
@ -25,7 +26,7 @@ func (script UserStorageCalibration) Run(ctx context.Context) {
model.DB.Model(&model.File{}).Where("user_id = ?", user.ID).Select("sum(size) as total").Scan(&total) model.DB.Model(&model.File{}).Where("user_id = ?", user.ID).Select("sum(size) as total").Scan(&total)
// 更新用户的容量 // 更新用户的容量
if user.Storage != total.Total { if user.Storage != total.Total {
util.Log().Info("将用户 [%s] 的容量由 %d 校准为 %d", user.Email, logger.Info("将用户 [%s] 的容量由 %d 校准为 %d", user.Email,
user.Storage, total.Total) user.Storage, total.Total)
} }
model.DB.Model(&user).Update("storage", total.Total) model.DB.Model(&user).Update("storage", total.Total)

@ -2,9 +2,10 @@ package scripts
import ( import (
"context" "context"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"strconv" "strconv"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
type UpgradeTo340 int type UpgradeTo340 int
@ -20,7 +21,7 @@ func (script UpgradeTo340) Run(ctx context.Context) {
// 写入到新版本的节点设定 // 写入到新版本的节点设定
n, err := model.GetNodeByID(1) n, err := model.GetNodeByID(1)
if err != nil { if err != nil {
util.Log().Error("找不到主机节点, %s", err) logger.Error("找不到主机节点, %s", err)
} }
n.Aria2Enabled = old["aria2_rpcurl"] != "" n.Aria2Enabled = old["aria2_rpcurl"] != ""
@ -35,9 +36,9 @@ func (script UpgradeTo340) Run(ctx context.Context) {
n.Aria2OptionsSerialized.TempPath = old["aria2_temp_path"] n.Aria2OptionsSerialized.TempPath = old["aria2_temp_path"]
n.Aria2OptionsSerialized.Token = old["aria2_token"] n.Aria2OptionsSerialized.Token = old["aria2_token"]
if err := model.DB.Save(&n).Error; err != nil { if err := model.DB.Save(&n).Error; err != nil {
util.Log().Error("无法保存主机节点 Aria2 配置信息, %s", err) logger.Error("无法保存主机节点 Aria2 配置信息, %s", err)
} else { } else {
model.DB.Where("type = ?", "aria2").Delete(model.Setting{}) model.DB.Where("type = ?", "aria2").Delete(model.Setting{})
util.Log().Info("Aria2 配置信息已成功迁移至 3.4.0+ 版本的模式") logger.Info("Aria2 配置信息已成功迁移至 3.4.0+ 版本的模式")
} }
} }

@ -8,6 +8,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid" "github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -36,7 +37,7 @@ type Share struct {
// Create 创建分享 // Create 创建分享
func (share *Share) Create() (uint, error) { func (share *Share) Create() (uint, error) {
if err := DB.Create(share).Error; err != nil { if err := DB.Create(share).Error; err != nil {
util.Log().Warning("无法插入数据库记录, %s", err) logger.Warning("无法插入数据库记录, %s", err)
return 0, err return 0, err
} }
return share.ID, nil return share.ID, nil

@ -1,7 +1,7 @@
package model package model
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -26,7 +26,7 @@ const (
// Create 创建标签记录 // Create 创建标签记录
func (tag *Tag) Create() (uint, error) { func (tag *Tag) Create() (uint, error) {
if err := DB.Create(tag).Error; err != nil { if err := DB.Create(tag).Error; err != nil {
util.Log().Warning("无法插入离线下载记录, %s", err) logger.Warning("无法插入离线下载记录, %s", err)
return 0, err return 0, err
} }
return tag.ID, nil return tag.ID, nil

@ -1,7 +1,7 @@
package model package model
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
@ -19,7 +19,7 @@ type Task struct {
// Create 创建任务记录 // Create 创建任务记录
func (task *Task) Create() (uint, error) { func (task *Task) Create() (uint, error) {
if err := DB.Create(task).Error; err != nil { if err := DB.Create(task).Error; err != nil {
util.Log().Warning("无法插入任务记录, %s", err) logger.Warning("无法插入任务记录, %s", err)
return 0, err return 0, err
} }
return task.ID, nil return task.ID, nil

@ -14,9 +14,9 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq" "github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/task" "github.com/cloudreve/Cloudreve/v3/pkg/task"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
// Monitor 离线下载状态监控 // Monitor 离线下载状态监控
@ -77,11 +77,11 @@ func (monitor *Monitor) Update() bool {
if err != nil { if err != nil {
monitor.retried++ monitor.retried++
util.Log().Warning("无法获取下载任务[%s]的状态,%s", monitor.Task.GID, err) logger.Warning("无法获取下载任务[%s]的状态,%s", monitor.Task.GID, err)
// 十次重试后认定为任务失败 // 十次重试后认定为任务失败
if monitor.retried > MAX_RETRY { if monitor.retried > MAX_RETRY {
util.Log().Warning("无法获取下载任务[%s]的状态,超过最大重试次数限制,%s", monitor.Task.GID, err) logger.Warning("无法获取下载任务[%s]的状态,超过最大重试次数限制,%s", monitor.Task.GID, err)
monitor.setErrorStatus(err) monitor.setErrorStatus(err)
monitor.RemoveTempFolder() monitor.RemoveTempFolder()
return true return true
@ -93,7 +93,7 @@ func (monitor *Monitor) Update() bool {
// 磁力链下载需要跟随 // 磁力链下载需要跟随
if len(status.FollowedBy) > 0 { if len(status.FollowedBy) > 0 {
util.Log().Debug("离线下载[%s]重定向至[%s]", monitor.Task.GID, status.FollowedBy[0]) logger.Debug("离线下载[%s]重定向至[%s]", monitor.Task.GID, status.FollowedBy[0])
monitor.Task.GID = status.FollowedBy[0] monitor.Task.GID = status.FollowedBy[0]
monitor.Task.Save() monitor.Task.Save()
return false return false
@ -101,13 +101,13 @@ func (monitor *Monitor) Update() bool {
// 更新任务信息 // 更新任务信息
if err := monitor.UpdateTaskInfo(status); err != nil { if err := monitor.UpdateTaskInfo(status); err != nil {
util.Log().Warning("无法更新下载任务[%s]的任务信息[%s]", monitor.Task.GID, err) logger.Warning("无法更新下载任务[%s]的任务信息[%s]", monitor.Task.GID, err)
monitor.setErrorStatus(err) monitor.setErrorStatus(err)
monitor.RemoveTempFolder() monitor.RemoveTempFolder()
return true return true
} }
util.Log().Debug("离线下载[%s]更新状态[%s]", status.Gid, status.Status) logger.Debug("离线下载[%s]更新状态[%s]", status.Gid, status.Status)
switch status.Status { switch status.Status {
case "complete": case "complete":
@ -122,7 +122,7 @@ func (monitor *Monitor) Update() bool {
monitor.RemoveTempFolder() monitor.RemoveTempFolder()
return true return true
default: default:
util.Log().Warning("下载任务[%s]返回未知状态信息[%s]", monitor.Task.GID, status.Status) logger.Warning("下载任务[%s]返回未知状态信息[%s]", monitor.Task.GID, status.Status)
return true return true
} }
} }

@ -268,6 +268,7 @@ func (c *client) TellStatus(gid string, keys ...string) (info StatusInfo, err er
// `aria2.getUris([secret, ]gid)` // `aria2.getUris([secret, ]gid)`
// This method returns the URIs used in the download denoted by gid (string). // This method returns the URIs used in the download denoted by gid (string).
// The response is an array of structs and it contains following keys. Values are string. // The response is an array of structs and it contains following keys. Values are string.
//
// uri URI // uri URI
// status 'used' if the URI is in use. 'waiting' if the URI is still waiting in the queue. // status 'used' if the URI is in use. 'waiting' if the URI is still waiting in the queue.
func (c *client) GetURIs(gid string) (infos []URIInfo, err error) { func (c *client) GetURIs(gid string) (infos []URIInfo, err error) {
@ -456,12 +457,14 @@ func (c *client) GetOption(gid string) (m Option, err error) {
// `aria2.changeOption([secret, ]gid, options)` // `aria2.changeOption([secret, ]gid, options)`
// This method changes options of the download denoted by gid (string) dynamically. options is a struct. // This method changes options of the download denoted by gid (string) dynamically. options is a struct.
// The following options are available for active downloads: // The following options are available for active downloads:
//
// bt-max-peers // bt-max-peers
// bt-request-peer-speed-limit // bt-request-peer-speed-limit
// bt-remove-unselected-file // bt-remove-unselected-file
// force-save // force-save
// max-download-limit // max-download-limit
// max-upload-limit // max-upload-limit
//
// For waiting or paused downloads, in addition to the above options, options listed in Input File subsection are available, except for following options: dry-run, metalink-base-uri, parameterized-uri, pause, piece-length and rpc-save-upload-metadata option. // For waiting or paused downloads, in addition to the above options, options listed in Input File subsection are available, except for following options: dry-run, metalink-base-uri, parameterized-uri, pause, piece-length and rpc-save-upload-metadata option.
// This method returns OK for success. // This method returns OK for success.
func (c *client) ChangeOption(gid string, option Option) (ok string, err error) { func (c *client) ChangeOption(gid string, option Option) (ok string, err error) {
@ -496,6 +499,7 @@ func (c *client) GetGlobalOption() (m Option, err error) {
// This method changes global options dynamically. // This method changes global options dynamically.
// options is a struct. // options is a struct.
// The following options are available: // The following options are available:
//
// bt-max-open-files // bt-max-open-files
// download-result // download-result
// log // log
@ -507,6 +511,7 @@ func (c *client) GetGlobalOption() (m Option, err error) {
// save-cookies // save-cookies
// save-session // save-session
// server-stat-of // server-stat-of
//
// In addition, options listed in the Input File subsection are available, except for following options: checksum, index-out, out, pause and select-file. // In addition, options listed in the Input File subsection are available, except for following options: checksum, index-out, out, pause and select-file.
// With the log option, you can dynamically start logging or change log file. // With the log option, you can dynamically start logging or change log file.
// To stop logging, specify an empty string("") as the parameter value. // To stop logging, specify an empty string("") as the parameter value.
@ -525,6 +530,7 @@ func (c *client) ChangeGlobalOption(options Option) (ok string, err error) {
// `aria2.getGlobalStat([secret])` // `aria2.getGlobalStat([secret])`
// This method returns global statistics such as the overall download and upload speeds. // This method returns global statistics such as the overall download and upload speeds.
// The response is a struct and contains the following keys. Values are strings. // The response is a struct and contains the following keys. Values are strings.
//
// downloadSpeed Overall download speed (byte/sec). // downloadSpeed Overall download speed (byte/sec).
// uploadSpeed Overall upload speed(byte/sec). // uploadSpeed Overall upload speed(byte/sec).
// numActive The number of active downloads. // numActive The number of active downloads.
@ -569,6 +575,7 @@ func (c *client) RemoveDownloadResult(gid string) (ok string, err error) {
// `aria2.getVersion([secret])` // `aria2.getVersion([secret])`
// This method returns the version of aria2 and the list of enabled features. // This method returns the version of aria2 and the list of enabled features.
// The response is a struct and contains following keys. // The response is a struct and contains following keys.
//
// version Version number of aria2 as a string. // version Version number of aria2 as a string.
// enabledFeatures List of enabled features. Each feature is given as a string. // enabledFeatures List of enabled features. Each feature is given as a string.
func (c *client) GetVersion() (info VersionInfo, err error) { func (c *client) GetVersion() (info VersionInfo, err error) {
@ -583,6 +590,7 @@ func (c *client) GetVersion() (info VersionInfo, err error) {
// `aria2.getSessionInfo([secret])` // `aria2.getSessionInfo([secret])`
// This method returns session information. // This method returns session information.
// The response is a struct and contains following key. // The response is a struct and contains following key.
//
// sessionId Session ID, which is generated each time when aria2 is invoked. // sessionId Session ID, which is generated each time when aria2 is invoked.
func (c *client) GetSessionInfo() (info SessionInfo, err error) { func (c *client) GetSessionInfo() (info SessionInfo, err error) {
params := []string{} params := []string{}

@ -12,8 +12,8 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
var ( var (
@ -136,7 +136,7 @@ func Init() {
} else { } else {
secretKey = conf.SlaveConfig.Secret secretKey = conf.SlaveConfig.Secret
if secretKey == "" { if secretKey == "" {
util.Log().Panic("未指定 SlaveSecret请前往配置文件中指定") logger.Panic("未指定 SlaveSecret请前往配置文件中指定")
} }
} }
General = HMACAuth{ General = HMACAuth{

@ -2,7 +2,7 @@ package cache
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -24,7 +24,7 @@ func Init(isSlave bool) {
if isSlave { if isSlave {
err := Store.Sets(conf.OptionOverwrite, "setting_") err := Store.Sets(conf.OptionOverwrite, "setting_")
if err != nil { if err != nil {
util.Log().Warning("无法覆盖数据库设置: %s", err) logger.Warning("无法覆盖数据库设置: %s", err)
} }
} }
} }

5
pkg/cache/memo.go vendored

@ -1,10 +1,9 @@
package cache package cache
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"sync" "sync"
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
// MemoStore 内存存储驱动 // MemoStore 内存存储驱动
@ -53,7 +52,7 @@ func (store *MemoStore) GarbageCollect() {
store.Store.Range(func(key, value interface{}) bool { store.Store.Range(func(key, value interface{}) bool {
if item, ok := value.(itemWithTTL); ok { if item, ok := value.(itemWithTTL); ok {
if item.expires > 0 && item.expires < time.Now().Unix() { if item.expires > 0 && item.expires < time.Now().Unix() {
util.Log().Debug("回收垃圾[%s]", key.(string)) logger.Debug("回收垃圾[%s]", key.(string))
store.Store.Delete(key) store.Store.Delete(key)
} }
} }

@ -6,7 +6,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
) )
@ -66,7 +66,7 @@ func NewRedisStore(size int, network, address, password, database string) *Redis
redis.DialPassword(password), redis.DialPassword(password),
) )
if err != nil { if err != nil {
util.Log().Warning("无法创建Redis连接%s", err) logger.Warning("无法创建Redis连接%s", err)
return nil, err return nil, err
} }
return c, nil return c, nil

@ -7,9 +7,9 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq" "github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"net/url" "net/url"
"os" "os"
@ -161,7 +161,7 @@ func (r *rpcService) Init() error {
// 解析RPC服务地址 // 解析RPC服务地址
server, err := url.Parse(r.parent.Model.Aria2OptionsSerialized.Server) server, err := url.Parse(r.parent.Model.Aria2OptionsSerialized.Server)
if err != nil { if err != nil {
util.Log().Warning("无法解析主机 Aria2 RPC 服务地址,%s", err) logger.Warning("无法解析主机 Aria2 RPC 服务地址,%s", err)
return err return err
} }
server.Path = "/jsonrpc" server.Path = "/jsonrpc"
@ -171,7 +171,7 @@ func (r *rpcService) Init() error {
if r.parent.Model.Aria2OptionsSerialized.Options != "" { if r.parent.Model.Aria2OptionsSerialized.Options != "" {
err = json.Unmarshal([]byte(r.parent.Model.Aria2OptionsSerialized.Options), &globalOptions) err = json.Unmarshal([]byte(r.parent.Model.Aria2OptionsSerialized.Options), &globalOptions)
if err != nil { if err != nil {
util.Log().Warning("无法解析主机 Aria2 配置,%s", err) logger.Warning("无法解析主机 Aria2 配置,%s", err)
return err return err
} }
} }
@ -221,7 +221,7 @@ func (r *rpcService) Status(task *model.Download) (rpc.StatusInfo, error) {
res, err := r.Caller.TellStatus(task.GID) res, err := r.Caller.TellStatus(task.GID)
if err != nil { if err != nil {
// 失败后重试 // 失败后重试
util.Log().Debug("无法获取离线下载状态,%s稍后重试", err) logger.Debug("无法获取离线下载状态,%s稍后重试", err)
time.Sleep(r.retryDuration) time.Sleep(r.retryDuration)
res, err = r.Caller.TellStatus(task.GID) res, err = r.Caller.TellStatus(task.GID)
} }
@ -233,7 +233,7 @@ func (r *rpcService) Cancel(task *model.Download) error {
// 取消下载任务 // 取消下载任务
_, err := r.Caller.Remove(task.GID) _, err := r.Caller.Remove(task.GID)
if err != nil { if err != nil {
util.Log().Warning("无法取消离线下载任务[%s], %s", task.GID, err) logger.Warning("无法取消离线下载任务[%s], %s", task.GID, err)
} }
return err return err
@ -264,7 +264,7 @@ func (s *rpcService) DeleteTempFile(task *model.Download) error {
time.Sleep(d) time.Sleep(d)
err := os.RemoveAll(src) err := os.RemoveAll(src)
if err != nil { if err != nil {
util.Log().Warning("无法删除离线下载临时目录[%s], %s", src, err) logger.Warning("无法删除离线下载临时目录[%s], %s", src, err)
} }
}(s.deletePaddingDuration, task.Parent) }(s.deletePaddingDuration, task.Parent)

@ -1,10 +1,11 @@
package cluster package cluster
import ( import (
"sync"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/balancer" "github.com/cloudreve/Cloudreve/v3/pkg/balancer"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"sync"
) )
var Default *NodePool var Default *NodePool
@ -42,7 +43,7 @@ func Init() {
Default = &NodePool{} Default = &NodePool{}
Default.Init() Default.Init()
if err := Default.initFromDB(); err != nil { if err := Default.initFromDB(); err != nil {
util.Log().Warning("节点池初始化失败, %s", err) logger.Warning("节点池初始化失败, %s", err)
} }
} }
@ -83,7 +84,7 @@ func (pool *NodePool) GetNodeByID(id uint) Node {
} }
func (pool *NodePool) nodeStatusChange(isActive bool, id uint) { func (pool *NodePool) nodeStatusChange(isActive bool, id uint) {
util.Log().Debug("从机节点 [ID=%d] 状态变更 [Active=%t]", id, isActive) logger.Debug("从机节点 [ID=%d] 状态变更 [Active=%t]", id, isActive)
var node Node var node Node
pool.lock.Lock() pool.lock.Lock()
if n, ok := pool.inactive[id]; ok { if n, ok := pool.inactive[id]; ok {

@ -5,19 +5,20 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/url"
"strings"
"sync"
"time"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"io"
"net/url"
"strings"
"sync"
"time"
) )
type SlaveNode struct { type SlaveNode struct {
@ -172,7 +173,7 @@ func (node *SlaveNode) StartPingLoop() {
recoverDuration := time.Duration(model.GetIntSetting("slave_recover_interval", 600)) * time.Second recoverDuration := time.Duration(model.GetIntSetting("slave_recover_interval", 600)) * time.Second
pingTicker := time.Duration(0) pingTicker := time.Duration(0)
util.Log().Debug("从机节点 [%s] 启动心跳循环", node.Model.Name) logger.Debug("从机节点 [%s] 启动心跳循环", node.Model.Name)
retry := 0 retry := 0
recoverMode := false recoverMode := false
isFirstLoop := true isFirstLoop := true
@ -185,39 +186,39 @@ loop:
pingTicker = tickDuration pingTicker = tickDuration
} }
util.Log().Debug("从机节点 [%s] 发送Ping", node.Model.Name) logger.Debug("从机节点 [%s] 发送Ping", node.Model.Name)
res, err := node.Ping(node.getHeartbeatContent(isFirstLoop)) res, err := node.Ping(node.getHeartbeatContent(isFirstLoop))
isFirstLoop = false isFirstLoop = false
if err != nil { if err != nil {
util.Log().Debug("Ping从机节点 [%s] 时发生错误: %s", node.Model.Name, err) logger.Debug("Ping从机节点 [%s] 时发生错误: %s", node.Model.Name, err)
retry++ retry++
if retry >= model.GetIntSetting("slave_node_retry", 3) { if retry >= model.GetIntSetting("slave_node_retry", 3) {
util.Log().Debug("从机节点 [%s] Ping 重试已达到最大限制,将从机节点标记为不可用", node.Model.Name) logger.Debug("从机节点 [%s] Ping 重试已达到最大限制,将从机节点标记为不可用", node.Model.Name)
node.changeStatus(false) node.changeStatus(false)
if !recoverMode { if !recoverMode {
// 启动恢复监控循环 // 启动恢复监控循环
util.Log().Debug("从机节点 [%s] 进入恢复模式", node.Model.Name) logger.Debug("从机节点 [%s] 进入恢复模式", node.Model.Name)
pingTicker = recoverDuration pingTicker = recoverDuration
recoverMode = true recoverMode = true
} }
} }
} else { } else {
if recoverMode { if recoverMode {
util.Log().Debug("从机节点 [%s] 复活", node.Model.Name) logger.Debug("从机节点 [%s] 复活", node.Model.Name)
pingTicker = tickDuration pingTicker = tickDuration
recoverMode = false recoverMode = false
isFirstLoop = true isFirstLoop = true
} }
util.Log().Debug("从机节点 [%s] 状态: %s", node.Model.Name, res) logger.Debug("从机节点 [%s] 状态: %s", node.Model.Name, res)
node.changeStatus(true) node.changeStatus(true)
retry = 0 retry = 0
} }
case <-node.close: case <-node.close:
util.Log().Debug("从机节点 [%s] 收到关闭信号", node.Model.Name) logger.Debug("从机节点 [%s] 收到关闭信号", node.Model.Name)
break loop break loop
} }
} }

@ -1,9 +1,11 @@
package conf package conf
import ( import (
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/sirupsen/logrus"
) )
// database 数据库 // database 数据库
@ -86,13 +88,13 @@ func Init(path string) {
}, defaultConf) }, defaultConf)
f, err := util.CreatNestedFile(path) f, err := util.CreatNestedFile(path)
if err != nil { if err != nil {
util.Log().Panic("无法创建配置文件, %s", err) logger.Panic("无法创建配置文件, %s", err)
} }
// 写入配置文件 // 写入配置文件
_, err = f.WriteString(confContent) _, err = f.WriteString(confContent)
if err != nil { if err != nil {
util.Log().Panic("无法写入配置文件, %s", err) logger.Panic("无法写入配置文件, %s", err)
} }
f.Close() f.Close()
@ -100,7 +102,7 @@ func Init(path string) {
cfg, err = ini.Load(path) cfg, err = ini.Load(path)
if err != nil { if err != nil {
util.Log().Panic("无法解析配置文件 '%s': %s", path, err) logger.Panic("无法解析配置文件 '%s': %s", path, err)
} }
sections := map[string]interface{}{ sections := map[string]interface{}{
@ -115,7 +117,7 @@ func Init(path string) {
for sectionName, sectionStruct := range sections { for sectionName, sectionStruct := range sections {
err = mapSection(sectionName, sectionStruct) err = mapSection(sectionName, sectionStruct)
if err != nil { if err != nil {
util.Log().Panic("配置文件 %s 分区解析失败: %s", sectionName, err) logger.Panic("配置文件 %s 分区解析失败: %s", sectionName, err)
} }
} }
@ -126,9 +128,7 @@ func Init(path string) {
// 重设log等级 // 重设log等级
if !SystemConfig.Debug { if !SystemConfig.Debug {
util.Level = util.LevelInformational logger.SetLevel(logrus.InfoLevel)
util.GloablLogger = nil
util.Log()
} }
} }

@ -10,6 +10,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
@ -22,7 +23,7 @@ func garbageCollect() {
collectCache(store) collectCache(store)
} }
util.Log().Info("定时任务 [cron_garbage_collect] 执行完毕") logger.Info("定时任务 [cron_garbage_collect] 执行完毕")
} }
func collectArchiveFile() { func collectArchiveFile() {
@ -36,23 +37,23 @@ func collectArchiveFile() {
if err == nil && !info.IsDir() && if err == nil && !info.IsDir() &&
strings.HasPrefix(filepath.Base(path), "archive_") && strings.HasPrefix(filepath.Base(path), "archive_") &&
time.Now().Sub(info.ModTime()).Seconds() > float64(expires) { time.Now().Sub(info.ModTime()).Seconds() > float64(expires) {
util.Log().Debug("删除过期打包下载临时文件 [%s]", path) logger.Debug("删除过期打包下载临时文件 [%s]", path)
// 删除符合条件的文件 // 删除符合条件的文件
if err := os.Remove(path); err != nil { if err := os.Remove(path); err != nil {
util.Log().Debug("临时文件 [%s] 删除失败 , %s", path, err) logger.Debug("临时文件 [%s] 删除失败 , %s", path, err)
} }
} }
return nil return nil
}) })
if err != nil { if err != nil {
util.Log().Debug("[定时任务] 无法列取临时打包目录") logger.Debug("[定时任务] 无法列取临时打包目录")
} }
} }
func collectCache(store *cache.MemoStore) { func collectCache(store *cache.MemoStore) {
util.Log().Debug("清理内存缓存") logger.Debug("清理内存缓存")
store.GarbageCollect() store.GarbageCollect()
} }
@ -78,22 +79,22 @@ func uploadSessionCollect() {
for uid, filesIDs := range userToFiles { for uid, filesIDs := range userToFiles {
user, err := model.GetUserByID(uid) user, err := model.GetUserByID(uid)
if err != nil { if err != nil {
util.Log().Warning("上传会话所属用户不存在, %s", err) logger.Warning("上传会话所属用户不存在, %s", err)
continue continue
} }
fs, err := filesystem.NewFileSystem(&user) fs, err := filesystem.NewFileSystem(&user)
if err != nil { if err != nil {
util.Log().Warning("无法初始化文件系统, %s", err) logger.Warning("无法初始化文件系统, %s", err)
continue continue
} }
if err = fs.Delete(context.Background(), []uint{}, filesIDs, false); err != nil { if err = fs.Delete(context.Background(), []uint{}, filesIDs, false); err != nil {
util.Log().Warning("无法删除上传会话, %s", err) logger.Warning("无法删除上传会话, %s", err)
} }
fs.Recycle() fs.Recycle()
} }
util.Log().Info("定时任务 [cron_recycle_upload_session] 执行完毕") logger.Info("定时任务 [cron_recycle_upload_session] 执行完毕")
} }

@ -2,7 +2,7 @@ package crontab
import ( import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
) )
@ -19,7 +19,7 @@ func Reload() {
// Init 初始化定时任务 // Init 初始化定时任务
func Init() { func Init() {
util.Log().Info("初始化定时任务...") logger.Info("初始化定时任务...")
// 读取cron日程设置 // 读取cron日程设置
options := model.GetSettingByNames( options := model.GetSettingByNames(
"cron_garbage_collect", "cron_garbage_collect",
@ -34,12 +34,12 @@ func Init() {
case "cron_recycle_upload_session": case "cron_recycle_upload_session":
handler = uploadSessionCollect handler = uploadSessionCollect
default: default:
util.Log().Warning("未知定时任务类型 [%s],跳过", k) logger.Warning("未知定时任务类型 [%s],跳过", k)
continue continue
} }
if _, err := Cron.AddFunc(v, handler); err != nil { if _, err := Cron.AddFunc(v, handler); err != nil {
util.Log().Warning("无法启动定时任务 [%s] , %s", k, err) logger.Warning("无法启动定时任务 [%s] , %s", k, err)
} }
} }

@ -4,7 +4,7 @@ import (
"sync" "sync"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
// Client 默认的邮件发送客户端 // Client 默认的邮件发送客户端
@ -15,7 +15,7 @@ var Lock sync.RWMutex
// Init 初始化 // Init 初始化
func Init() { func Init() {
util.Log().Debug("邮件队列初始化") logger.Debug("邮件队列初始化")
Lock.Lock() Lock.Lock()
defer Lock.Unlock() defer Lock.Unlock()

@ -3,7 +3,7 @@ package email
import ( import (
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/go-mail/mail" "github.com/go-mail/mail"
) )
@ -68,7 +68,7 @@ func (client *SMTP) Init() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
client.chOpen = false client.chOpen = false
util.Log().Error("邮件发送队列出现异常, %s ,10 秒后重置", err) logger.Error("邮件发送队列出现异常, %s ,10 秒后重置", err)
time.Sleep(time.Duration(10) * time.Second) time.Sleep(time.Duration(10) * time.Second)
client.Init() client.Init()
} }
@ -91,7 +91,7 @@ func (client *SMTP) Init() {
select { select {
case m, ok := <-client.ch: case m, ok := <-client.ch:
if !ok { if !ok {
util.Log().Debug("邮件队列关闭") logger.Debug("邮件队列关闭")
client.chOpen = false client.chOpen = false
return return
} }
@ -102,15 +102,15 @@ func (client *SMTP) Init() {
open = true open = true
} }
if err := mail.Send(s, m); err != nil { if err := mail.Send(s, m); err != nil {
util.Log().Warning("邮件发送失败, %s", err) logger.Warning("邮件发送失败, %s", err)
} else { } else {
util.Log().Debug("邮件已发送") logger.Debug("邮件已发送")
} }
// 长时间没有新邮件则关闭SMTP连接 // 长时间没有新邮件则关闭SMTP连接
case <-time.After(time.Duration(client.Config.Keepalive) * time.Second): case <-time.After(time.Duration(client.Config.Keepalive) * time.Second):
if open { if open {
if err := s.Close(); err != nil { if err := s.Close(); err != nil {
util.Log().Warning("无法关闭 SMTP 连接 %s", err) logger.Warning("无法关闭 SMTP 连接 %s", err)
} }
open = false open = false
} }

@ -14,6 +14,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/mholt/archiver/v4" "github.com/mholt/archiver/v4"
@ -107,7 +108,7 @@ func (fs *FileSystem) doCompress(ctx context.Context, file *model.File, folder *
fs.Policy = file.GetPolicy() fs.Policy = file.GetPolicy()
err := fs.DispatchHandler() err := fs.DispatchHandler()
if err != nil { if err != nil {
util.Log().Warning("无法压缩文件%s%s", file.Name, err) logger.Warning("无法压缩文件%s%s", file.Name, err)
return return
} }
@ -117,7 +118,7 @@ func (fs *FileSystem) doCompress(ctx context.Context, file *model.File, folder *
file.SourceName, file.SourceName,
) )
if err != nil { if err != nil {
util.Log().Debug("Open%s%s", file.Name, err) logger.Debug("Open%s%s", file.Name, err)
return return
} }
if closer, ok := fileToZip.(io.Closer); ok { if closer, ok := fileToZip.(io.Closer); ok {
@ -176,7 +177,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
// 结束时删除临时压缩文件 // 结束时删除临时压缩文件
if tempZipFilePath != "" { if tempZipFilePath != "" {
if err := os.Remove(tempZipFilePath); err != nil { if err := os.Remove(tempZipFilePath); err != nil {
util.Log().Warning("无法删除临时压缩文件 %s , %s", tempZipFilePath, err) logger.Warning("无法删除临时压缩文件 %s , %s", tempZipFilePath, err)
} }
} }
}() }()
@ -197,7 +198,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
zipFile, err := util.CreatNestedFile(tempZipFilePath) zipFile, err := util.CreatNestedFile(tempZipFilePath)
if err != nil { if err != nil {
util.Log().Warning("无法创建临时压缩文件 %s , %s", tempZipFilePath, err) logger.Warning("无法创建临时压缩文件 %s , %s", tempZipFilePath, err)
tempZipFilePath = "" tempZipFilePath = ""
return err return err
} }
@ -206,7 +207,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
// 下载前先判断是否是可解压的格式 // 下载前先判断是否是可解压的格式
format, readStream, err := archiver.Identify(fs.FileTarget[0].SourceName, fileStream) format, readStream, err := archiver.Identify(fs.FileTarget[0].SourceName, fileStream)
if err != nil { if err != nil {
util.Log().Warning("无法识别文件格式 %s , %s", fs.FileTarget[0].SourceName, err) logger.Warning("无法识别文件格式 %s , %s", fs.FileTarget[0].SourceName, err)
return err return err
} }
@ -228,7 +229,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
if isZip { if isZip {
_, err = io.Copy(zipFile, readStream) _, err = io.Copy(zipFile, readStream)
if err != nil { if err != nil {
util.Log().Warning("无法写入临时压缩文件 %s , %s", tempZipFilePath, err) logger.Warning("无法写入临时压缩文件 %s , %s", tempZipFilePath, err)
return err return err
} }
@ -261,7 +262,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
wg.Done() wg.Done()
} }
if err := recover(); err != nil { if err := recover(); err != nil {
util.Log().Warning("上传压缩包内文件时出错") logger.Warning("上传压缩包内文件时出错")
fmt.Println(err) fmt.Println(err)
} }
}() }()
@ -274,7 +275,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
}, true) }, true)
fileStream.Close() fileStream.Close()
if err != nil { if err != nil {
util.Log().Debug("无法上传压缩包内的文件%s , %s , 跳过", rawPath, err) logger.Debug("无法上传压缩包内的文件%s , %s , 跳过", rawPath, err)
} }
} }
@ -284,7 +285,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
savePath := path.Join(dst, rawPath) savePath := path.Join(dst, rawPath)
// 路径是否合法 // 路径是否合法
if !strings.HasPrefix(savePath, util.FillSlash(path.Clean(dst))) { if !strings.HasPrefix(savePath, util.FillSlash(path.Clean(dst))) {
util.Log().Warning("%s: illegal file path", f.NameInArchive) logger.Warning("%s: illegal file path", f.NameInArchive)
return nil return nil
} }
@ -297,7 +298,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst, encoding string)
// 上传文件 // 上传文件
fileStream, err := f.Open() fileStream, err := f.Open()
if err != nil { if err != nil {
util.Log().Warning("无法打开压缩包内文件%s , %s , 跳过", rawPath, err) logger.Warning("无法打开压缩包内文件%s , %s , 跳过", rawPath, err)
return nil return nil
} }

@ -3,11 +3,12 @@ package chunk
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"io" "io"
"os" "os"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
const bufferTempPattern = "cdChunk.*.tmp" const bufferTempPattern = "cdChunk.*.tmp"
@ -89,7 +90,7 @@ func (c *ChunkGroup) Process(processor ChunkProcessFunc) error {
return fmt.Errorf("failed to seek temp file back to chunk start: %w", err) return fmt.Errorf("failed to seek temp file back to chunk start: %w", err)
} }
util.Log().Debug("Chunk %d will be read from temp file %q.", c.Index(), c.bufferTemp.Name()) logger.Debug("Chunk %d will be read from temp file %q.", c.Index(), c.bufferTemp.Name())
reader = c.bufferTemp reader = c.bufferTemp
} }
} }
@ -103,14 +104,14 @@ func (c *ChunkGroup) Process(processor ChunkProcessFunc) error {
} }
} }
util.Log().Debug("Retrying chunk %d, last error: %s", c.currentIndex, err) logger.Debug("Retrying chunk %d, last error: %s", c.currentIndex, err)
return c.Process(processor) return c.Process(processor)
} }
return err return err
} }
util.Log().Debug("Chunk %d processed", c.currentIndex) logger.Debug("Chunk %d processed", c.currentIndex)
return nil return nil
} }

@ -14,6 +14,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
@ -43,7 +44,7 @@ func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]
} }
if err != nil { if err != nil {
util.Log().Warning("无法遍历目录 %s, %s", path, err) logger.Warning("无法遍历目录 %s, %s", path, err)
return filepath.SkipDir return filepath.SkipDir
} }
@ -78,7 +79,7 @@ func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser,
// 打开文件 // 打开文件
file, err := os.Open(util.RelativePath(path)) file, err := os.Open(util.RelativePath(path))
if err != nil { if err != nil {
util.Log().Debug("无法打开文件:%s", err) logger.Debug("无法打开文件:%s", err)
return nil, err return nil, err
} }
@ -94,7 +95,7 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
// 如果非 Overwrite则检查是否有重名冲突 // 如果非 Overwrite则检查是否有重名冲突
if fileInfo.Mode&fsctx.Overwrite != fsctx.Overwrite { if fileInfo.Mode&fsctx.Overwrite != fsctx.Overwrite {
if util.Exists(dst) { if util.Exists(dst) {
util.Log().Warning("物理同名文件已存在或不可用: %s", dst) logger.Warning("物理同名文件已存在或不可用: %s", dst)
return errors.New("物理同名文件已存在或不可用") return errors.New("物理同名文件已存在或不可用")
} }
} }
@ -104,7 +105,7 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
if !util.Exists(basePath) { if !util.Exists(basePath) {
err := os.MkdirAll(basePath, Perm) err := os.MkdirAll(basePath, Perm)
if err != nil { if err != nil {
util.Log().Warning("无法创建目录,%s", err) logger.Warning("无法创建目录,%s", err)
return err return err
} }
} }
@ -123,7 +124,7 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
out, err = os.OpenFile(dst, openMode, Perm) out, err = os.OpenFile(dst, openMode, Perm)
if err != nil { if err != nil {
util.Log().Warning("无法打开或创建文件,%s", err) logger.Warning("无法打开或创建文件,%s", err)
return err return err
} }
defer out.Close() defer out.Close()
@ -131,7 +132,7 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
if fileInfo.Mode&fsctx.Append == fsctx.Append { if fileInfo.Mode&fsctx.Append == fsctx.Append {
stat, err := out.Stat() stat, err := out.Stat()
if err != nil { if err != nil {
util.Log().Warning("无法读取文件信息,%s", err) logger.Warning("无法读取文件信息,%s", err)
return err return err
} }
@ -146,7 +147,7 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
out, err = os.OpenFile(dst, openMode, Perm) out, err = os.OpenFile(dst, openMode, Perm)
defer out.Close() defer out.Close()
if err != nil { if err != nil {
util.Log().Warning("无法打开或创建文件,%s", err) logger.Warning("无法打开或创建文件,%s", err)
return err return err
} }
} }
@ -158,10 +159,10 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
} }
func (handler Driver) Truncate(ctx context.Context, src string, size uint64) error { func (handler Driver) Truncate(ctx context.Context, src string, size uint64) error {
util.Log().Warning("截断文件 [%s] 至 [%d]", src, size) logger.Warning("截断文件 [%s] 至 [%d]", src, size)
out, err := os.OpenFile(src, os.O_WRONLY, Perm) out, err := os.OpenFile(src, os.O_WRONLY, Perm)
if err != nil { if err != nil {
util.Log().Warning("无法打开文件,%s", err) logger.Warning("无法打开文件,%s", err)
return err return err
} }
@ -180,7 +181,7 @@ func (handler Driver) Delete(ctx context.Context, files []string) ([]string, err
if util.Exists(filePath) { if util.Exists(filePath) {
err := os.Remove(filePath) err := os.Remove(filePath)
if err != nil { if err != nil {
util.Log().Warning("无法删除文件,%s", err) logger.Warning("无法删除文件,%s", err)
retErr = err retErr = err
deleteFailed = append(deleteFailed, value) deleteFailed = append(deleteFailed, value)
} }

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -17,12 +16,13 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq" "github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
const ( const (
@ -95,7 +95,7 @@ func (client *Client) ListChildren(ctx context.Context, path string) ([]FileInfo
} }
if retried < ListRetry { if retried < ListRetry {
retried++ retried++
util.Log().Debug("路径[%s]列取请求失败[%s]5秒钟后重试", path, err) logger.Debug("路径[%s]列取请求失败[%s]5秒钟后重试", path, err)
time.Sleep(time.Duration(5) * time.Second) time.Sleep(time.Duration(5) * time.Second)
return client.ListChildren(context.WithValue(ctx, fsctx.RetryCtx, retried), path) return client.ListChildren(context.WithValue(ctx, fsctx.RetryCtx, retried), path)
} }
@ -460,39 +460,39 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui
for { for {
select { select {
case <-callbackChan: case <-callbackChan:
util.Log().Debug("客户端完成回调") logger.Debug("客户端完成回调")
return return
case <-time.After(time.Duration(ttl) * time.Second): case <-time.After(time.Duration(ttl) * time.Second):
// 上传会话到期,仍未完成上传,创建占位符 // 上传会话到期,仍未完成上传,创建占位符
client.DeleteUploadSession(context.Background(), uploadURL) client.DeleteUploadSession(context.Background(), uploadURL)
_, err := client.SimpleUpload(context.Background(), path, strings.NewReader(""), 0, WithConflictBehavior("replace")) _, err := client.SimpleUpload(context.Background(), path, strings.NewReader(""), 0, WithConflictBehavior("replace"))
if err != nil { if err != nil {
util.Log().Debug("无法创建占位文件,%s", err) logger.Debug("无法创建占位文件,%s", err)
} }
return return
case <-time.After(time.Duration(timeout) * time.Second): case <-time.After(time.Duration(timeout) * time.Second):
util.Log().Debug("检查上传情况") logger.Debug("检查上传情况")
status, err := client.GetUploadSessionStatus(context.Background(), uploadURL) status, err := client.GetUploadSessionStatus(context.Background(), uploadURL)
if err != nil { if err != nil {
if resErr, ok := err.(*RespError); ok { if resErr, ok := err.(*RespError); ok {
if resErr.APIError.Code == "itemNotFound" { if resErr.APIError.Code == "itemNotFound" {
util.Log().Debug("上传会话已完成,稍后检查回调") logger.Debug("上传会话已完成,稍后检查回调")
select { select {
case <-time.After(time.Duration(interval) * time.Second): case <-time.After(time.Duration(interval) * time.Second):
util.Log().Warning("未发送回调,删除文件") logger.Warning("未发送回调,删除文件")
cache.Deletes([]string{callbackKey}, "callback_") cache.Deletes([]string{callbackKey}, "callback_")
_, err = client.Delete(context.Background(), []string{path}) _, err = client.Delete(context.Background(), []string{path})
if err != nil { if err != nil {
util.Log().Warning("无法删除未回调的文件,%s", err) logger.Warning("无法删除未回调的文件,%s", err)
} }
case <-callbackChan: case <-callbackChan:
util.Log().Debug("客户端完成回调") logger.Debug("客户端完成回调")
} }
return return
} }
} }
util.Log().Debug("无法获取上传会话状态,继续下一轮,%s", err.Error()) logger.Debug("无法获取上传会话状态,继续下一轮,%s", err.Error())
continue continue
} }
@ -509,13 +509,13 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui
} }
uploadFullSize, _ := strconv.ParseUint(sizeRange[1], 10, 64) uploadFullSize, _ := strconv.ParseUint(sizeRange[1], 10, 64)
if (sizeRange[0] == "0" && sizeRange[1] == "") || uploadFullSize+1 != size { if (sizeRange[0] == "0" && sizeRange[1] == "") || uploadFullSize+1 != size {
util.Log().Debug("未开始上传或文件大小不一致,取消上传会话") logger.Debug("未开始上传或文件大小不一致,取消上传会话")
// 取消上传会话实测OneDrive取消上传会话后客户端还是可以上传 // 取消上传会话实测OneDrive取消上传会话后客户端还是可以上传
// 所以上传一个空文件占位,阻止客户端上传 // 所以上传一个空文件占位,阻止客户端上传
client.DeleteUploadSession(context.Background(), uploadURL) client.DeleteUploadSession(context.Background(), uploadURL)
_, err := client.SimpleUpload(context.Background(), path, strings.NewReader(""), 0, WithConflictBehavior("replace")) _, err := client.SimpleUpload(context.Background(), path, strings.NewReader(""), 0, WithConflictBehavior("replace"))
if err != nil { if err != nil {
util.Log().Debug("无法创建占位文件,%s", err) logger.Debug("无法创建占位文件,%s", err)
} }
return return
} }
@ -577,7 +577,7 @@ func (client *Client) request(ctx context.Context, method string, url string, bo
if res.Response.StatusCode < 200 || res.Response.StatusCode >= 300 { if res.Response.StatusCode < 200 || res.Response.StatusCode >= 300 {
decodeErr = json.Unmarshal([]byte(respBody), &errResp) decodeErr = json.Unmarshal([]byte(respBody), &errResp)
if decodeErr != nil { if decodeErr != nil {
util.Log().Debug("Onedrive返回未知响应[%s]", respBody) logger.Debug("Onedrive返回未知响应[%s]", respBody)
return "", sysError(decodeErr) return "", sysError(decodeErr)
} }
return "", &errResp return "", &errResp

@ -10,8 +10,8 @@ import (
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
// Error 实现error接口 // Error 实现error接口
@ -152,7 +152,7 @@ func (client *Client) UpdateCredential(ctx context.Context, isSlave bool) error
// 获取新的凭证 // 获取新的凭证
if client.Credential == nil || client.Credential.RefreshToken == "" { if client.Credential == nil || client.Credential.RefreshToken == "" {
// 无有效的RefreshToken // 无有效的RefreshToken
util.Log().Error("上传策略[%s]凭证刷新失败请重新授权OneDrive账号", client.Policy.Name) logger.Error("上传策略[%s]凭证刷新失败请重新授权OneDrive账号", client.Policy.Name)
return ErrInvalidRefreshToken return ErrInvalidRefreshToken
} }

@ -4,21 +4,22 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
) )
const ( const (
@ -101,7 +102,7 @@ func (c *remoteClient) Upload(ctx context.Context, file fsctx.FileHeader) error
for chunks.Next() { for chunks.Next() {
if err := chunks.Process(uploadFunc); err != nil { if err := chunks.Process(uploadFunc); err != nil {
if err := c.DeleteUploadSession(ctx, session.Key); err != nil { if err := c.DeleteUploadSession(ctx, session.Key); err != nil {
util.Log().Warning("failed to delete upload session: %s", err) logger.Warning("failed to delete upload session: %s", err)
} }
return fmt.Errorf("failed to upload chunk #%d: %w", chunks.Index(), err) return fmt.Errorf("failed to upload chunk #%d: %w", chunks.Index(), err)

@ -10,8 +10,8 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/juju/ratelimit" "github.com/juju/ratelimit"
) )
@ -72,7 +72,7 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder, file fs
if err != nil { if err != nil {
if err := fs.Trigger(ctx, "AfterValidateFailed", file); err != nil { if err := fs.Trigger(ctx, "AfterValidateFailed", file); err != nil {
util.Log().Debug("AfterValidateFailed 钩子执行失败,%s", err) logger.Debug("AfterValidateFailed 钩子执行失败,%s", err)
} }
return nil, ErrFileExisted.WithError(err) return nil, ErrFileExisted.WithError(err)
} }
@ -97,6 +97,7 @@ func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (
} }
// Preview 预览文件 // Preview 预览文件
//
// path - 文件虚拟路径 // path - 文件虚拟路径
// isText - 是否为文本文件,文本文件会忽略重定向,直接由 // isText - 是否为文本文件,文本文件会忽略重定向,直接由
// 服务端拉取中转给用户,故会对文件大小进行限制 // 服务端拉取中转给用户,故会对文件大小进行限制
@ -203,7 +204,7 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m
// 取消上传会话 // 取消上传会话
for _, upSession := range uploadSessions { for _, upSession := range uploadSessions {
if err := fs.Handler.CancelToken(ctx, upSession); err != nil { if err := fs.Handler.CancelToken(ctx, upSession); err != nil {
util.Log().Warning("无法取消 [%s] 的上传会话: %s", upSession.Name, err) logger.Warning("无法取消 [%s] 的上传会话: %s", upSession.Name, err)
} }
cache.Deletes([]string{upSession.Key}, UploadSessionCachePrefix) cache.Deletes([]string{upSession.Key}, UploadSessionCachePrefix)

@ -2,15 +2,16 @@ package filesystem
import ( import (
"context" "context"
"io/ioutil"
"strings"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"io/ioutil"
"strings"
) )
// Hook 钩子函数 // Hook 钩子函数
@ -44,7 +45,7 @@ func (fs *FileSystem) Trigger(ctx context.Context, name string, file fsctx.FileH
for _, hook := range hooks { for _, hook := range hooks {
err := hook(ctx, fs, file) err := hook(ctx, fs, file)
if err != nil { if err != nil {
util.Log().Warning("钩子执行失败:%s", err) logger.Warning("钩子执行失败:%s", err)
return err return err
} }
} }
@ -112,7 +113,7 @@ func HookDeleteTempFile(ctx context.Context, fs *FileSystem, file fsctx.FileHead
// 删除临时文件 // 删除临时文件
_, err := fs.Handler.Delete(ctx, []string{file.Info().SavePath}) _, err := fs.Handler.Delete(ctx, []string{file.Info().SavePath})
if err != nil { if err != nil {
util.Log().Warning("无法清理上传临时文件,%s", err) logger.Warning("无法清理上传临时文件,%s", err)
} }
return nil return nil

@ -11,6 +11,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/thumb" "github.com/cloudreve/Cloudreve/v3/pkg/thumb"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
@ -71,16 +72,16 @@ func getThumbWorker() *Pool {
thumbPool = &Pool{ thumbPool = &Pool{
worker: make(chan int, maxWorker), worker: make(chan int, maxWorker),
} }
util.Log().Debug("初始化Thumb任务队列WorkerNum = %d", maxWorker) logger.Debug("初始化Thumb任务队列WorkerNum = %d", maxWorker)
}) })
return thumbPool return thumbPool
} }
func (pool *Pool) addWorker() { func (pool *Pool) addWorker() {
pool.worker <- 1 pool.worker <- 1
util.Log().Debug("Thumb任务队列addWorker") logger.Debug("Thumb任务队列addWorker")
} }
func (pool *Pool) releaseWorker() { func (pool *Pool) releaseWorker() {
util.Log().Debug("Thumb任务队列releaseWorker") logger.Debug("Thumb任务队列releaseWorker")
<-pool.worker <-pool.worker
} }
@ -107,7 +108,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
image, err := thumb.NewThumbFromFile(source, file.Name) image, err := thumb.NewThumbFromFile(source, file.Name)
if err != nil { if err != nil {
util.Log().Warning("生成缩略图时无法解析 [%s] 图像数据:%s", file.SourceName, err) logger.Warning("生成缩略图时无法解析 [%s] 图像数据:%s", file.SourceName, err)
return return
} }
@ -120,12 +121,12 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
err = image.Save(util.RelativePath(file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"))) err = image.Save(util.RelativePath(file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb")))
image = nil image = nil
if model.IsTrueVal(model.GetSettingByName("thumb_gc_after_gen")) { if model.IsTrueVal(model.GetSettingByName("thumb_gc_after_gen")) {
util.Log().Debug("GenerateThumbnail runtime.GC") logger.Debug("GenerateThumbnail runtime.GC")
runtime.GC() runtime.GC()
} }
if err != nil { if err != nil {
util.Log().Warning("无法保存缩略图:%s", err) logger.Warning("无法保存缩略图:%s", err)
return return
} }

@ -9,6 +9,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/request" "github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
@ -69,7 +70,7 @@ func (fs *FileSystem) Upload(ctx context.Context, file *fsctx.FileStream) (err e
followUpErr := fs.Trigger(ctx, "AfterValidateFailed", file) followUpErr := fs.Trigger(ctx, "AfterValidateFailed", file)
// 失败后再失败... // 失败后再失败...
if followUpErr != nil { if followUpErr != nil {
util.Log().Debug("AfterValidateFailed 钩子执行失败,%s", followUpErr) logger.Debug("AfterValidateFailed 钩子执行失败,%s", followUpErr)
} }
return err return err
@ -113,13 +114,13 @@ func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file fsctx.
// 客户端正常关闭,不执行操作 // 客户端正常关闭,不执行操作
default: default:
// 客户端取消上传,删除临时文件 // 客户端取消上传,删除临时文件
util.Log().Debug("客户端取消上传") logger.Debug("客户端取消上传")
if fs.Hooks["AfterUploadCanceled"] == nil { if fs.Hooks["AfterUploadCanceled"] == nil {
return return
} }
err := fs.Trigger(ctx, "AfterUploadCanceled", file) err := fs.Trigger(ctx, "AfterUploadCanceled", file)
if err != nil { if err != nil {
util.Log().Debug("执行 AfterUploadCanceled 钩子出错,%s", err) logger.Debug("执行 AfterUploadCanceled 钩子出错,%s", err)
} }
} }

@ -67,6 +67,7 @@ type ReCAPTCHA struct {
} }
// NewReCAPTCHA new ReCAPTCHA instance if version is set to V2 uses recatpcha v2 API, get your secret from https://www.google.com/recaptcha/admin // NewReCAPTCHA new ReCAPTCHA instance if version is set to V2 uses recatpcha v2 API, get your secret from https://www.google.com/recaptcha/admin
//
// if version is set to V2 uses recatpcha v2 API, get your secret from https://g.co/recaptcha/v3 // if version is set to V2 uses recatpcha v2 API, get your secret from https://g.co/recaptcha/v3
func NewReCAPTCHA(ReCAPTCHASecret string, version VERSION, timeout time.Duration) (ReCAPTCHA, error) { func NewReCAPTCHA(ReCAPTCHASecret string, version VERSION, timeout time.Duration) (ReCAPTCHA, error) {
if ReCAPTCHASecret == "" { if ReCAPTCHASecret == "" {

@ -14,8 +14,8 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
// GeneralClient 通用 HTTP Client // GeneralClient 通用 HTTP Client
@ -179,7 +179,7 @@ func (resp *Response) DecodeResponse() (*serializer.Response, error) {
var res serializer.Response var res serializer.Response
err = json.Unmarshal([]byte(respString), &res) err = json.Unmarshal([]byte(respString), &res)
if err != nil { if err != nil {
util.Log().Debug("无法解析回调服务端响应:%s", string(respString)) logger.Debug("无法解析回调服务端响应:%s", string(respString))
return nil, err return nil, err
} }
return &res, nil return &res, nil

@ -10,6 +10,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
@ -69,7 +70,7 @@ func (job *CompressTask) SetError(err *JobError) {
func (job *CompressTask) removeZipFile() { func (job *CompressTask) removeZipFile() {
if job.zipPath != "" { if job.zipPath != "" {
if err := os.Remove(job.zipPath); err != nil { if err := os.Remove(job.zipPath); err != nil {
util.Log().Warning("无法删除临时压缩文件 %s , %s", job.zipPath, err) logger.Warning("无法删除临时压缩文件 %s , %s", job.zipPath, err)
} }
} }
} }
@ -93,7 +94,7 @@ func (job *CompressTask) Do() {
return return
} }
util.Log().Debug("开始压缩文件") logger.Debug("开始压缩文件")
job.TaskModel.SetProgress(CompressingProgress) job.TaskModel.SetProgress(CompressingProgress)
// 创建临时压缩文件 // 创建临时压缩文件
@ -105,7 +106,7 @@ func (job *CompressTask) Do() {
) )
zipFile, err := util.CreatNestedFile(zipFilePath) zipFile, err := util.CreatNestedFile(zipFilePath)
if err != nil { if err != nil {
util.Log().Warning("%s", err) logger.Warning("%s", err)
job.SetErrorMsg(err.Error()) job.SetErrorMsg(err.Error())
return return
} }
@ -122,7 +123,7 @@ func (job *CompressTask) Do() {
job.zipPath = zipFilePath job.zipPath = zipFilePath
zipFile.Close() zipFile.Close()
util.Log().Debug("压缩文件存放至%s开始上传", zipFilePath) logger.Debug("压缩文件存放至%s开始上传", zipFilePath)
job.TaskModel.SetProgress(TransferringProgress) job.TaskModel.SetProgress(TransferringProgress)
// 上传文件 // 上传文件

@ -8,7 +8,7 @@ import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
// ImportTask 导入务 // ImportTask 导入务
@ -126,7 +126,7 @@ func (job *ImportTask) Do() {
virtualPath := path.Join(job.TaskProps.Dst, object.RelativePath) virtualPath := path.Join(job.TaskProps.Dst, object.RelativePath)
folder, err := fs.CreateDirectory(coxIgnoreConflict, virtualPath) folder, err := fs.CreateDirectory(coxIgnoreConflict, virtualPath)
if err != nil { if err != nil {
util.Log().Warning("导入任务无法创建用户目录[%s], %s", virtualPath, err) logger.Warning("导入任务无法创建用户目录[%s], %s", virtualPath, err)
} else if folder.ID > 0 { } else if folder.ID > 0 {
pathCache[virtualPath] = folder pathCache[virtualPath] = folder
} }
@ -152,7 +152,7 @@ func (job *ImportTask) Do() {
} else { } else {
folder, err := fs.CreateDirectory(context.Background(), virtualPath) folder, err := fs.CreateDirectory(context.Background(), virtualPath)
if err != nil { if err != nil {
util.Log().Warning("导入任务无法创建用户目录[%s], %s", logger.Warning("导入任务无法创建用户目录[%s], %s",
virtualPath, err) virtualPath, err)
continue continue
} }
@ -163,7 +163,7 @@ func (job *ImportTask) Do() {
// 插入文件记录 // 插入文件记录
_, err := fs.AddFile(context.Background(), parentFolder, &fileHeader) _, err := fs.AddFile(context.Background(), parentFolder, &fileHeader)
if err != nil { if err != nil {
util.Log().Warning("导入任务无法创插入文件[%s], %s", logger.Warning("导入任务无法创插入文件[%s], %s",
object.RelativePath, err) object.RelativePath, err)
if err == filesystem.ErrInsufficientCapacity { if err == filesystem.ErrInsufficientCapacity {
job.SetErrorMsg("容量不足", err) job.SetErrorMsg("容量不足", err)

@ -2,7 +2,7 @@ package task
import ( import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
// 任务类型 // 任务类型
@ -87,12 +87,12 @@ func Resume(p Pool) {
if len(tasks) == 0 { if len(tasks) == 0 {
return return
} }
util.Log().Info("从数据库中恢复 %d 个未完成任务", len(tasks)) logger.Info("从数据库中恢复 %d 个未完成任务", len(tasks))
for i := 0; i < len(tasks); i++ { for i := 0; i < len(tasks); i++ {
job, err := GetJobFromModel(&tasks[i]) job, err := GetJobFromModel(&tasks[i])
if err != nil { if err != nil {
util.Log().Warning("无法恢复任务,%s", err) logger.Warning("无法恢复任务,%s", err)
continue continue
} }

@ -3,7 +3,7 @@ package task
import ( import (
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
// TaskPoll 要使用的任务池 // TaskPoll 要使用的任务池
@ -44,11 +44,11 @@ func (pool *AsyncPool) freeWorker() {
// Submit 开始提交任务 // Submit 开始提交任务
func (pool *AsyncPool) Submit(job Job) { func (pool *AsyncPool) Submit(job Job) {
go func() { go func() {
util.Log().Debug("等待获取Worker") logger.Debug("等待获取Worker")
worker := pool.obtainWorker() worker := pool.obtainWorker()
util.Log().Debug("获取到Worker") logger.Debug("获取到Worker")
worker.Do(job) worker.Do(job)
util.Log().Debug("释放Worker") logger.Debug("释放Worker")
pool.freeWorker() pool.freeWorker()
}() }()
} }
@ -60,7 +60,7 @@ func Init() {
idleWorker: make(chan int, maxWorker), idleWorker: make(chan int, maxWorker),
} }
TaskPoll.Add(maxWorker) TaskPoll.Add(maxWorker)
util.Log().Info("初始化任务队列WorkerNum = %d", maxWorker) logger.Info("初始化任务队列WorkerNum = %d", maxWorker)
if conf.SystemConfig.Mode == "master" { if conf.SystemConfig.Mode == "master" {
Resume(TaskPoll) Resume(TaskPoll)

@ -2,15 +2,17 @@ package slavetask
import ( import (
"context" "context"
"os"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq" "github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/task" "github.com/cloudreve/Cloudreve/v3/pkg/task"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
"os"
) )
// TransferTask 文件中转任务 // TransferTask 文件中转任务
@ -68,7 +70,7 @@ func (job *TransferTask) SetErrorMsg(msg string, err error) {
} }
if err := cluster.DefaultController.SendNotification(job.MasterID, job.Req.Hash(job.MasterID), notifyMsg); err != nil { if err := cluster.DefaultController.SendNotification(job.MasterID, job.Req.Hash(job.MasterID), notifyMsg); err != nil {
util.Log().Warning("无法发送转存失败通知到从机, %s", err) logger.Warning("无法发送转存失败通知到从机, %s", err)
} }
} }
@ -134,7 +136,7 @@ func (job *TransferTask) Do() {
} }
if err := cluster.DefaultController.SendNotification(job.MasterID, job.Req.Hash(job.MasterID), msg); err != nil { if err := cluster.DefaultController.SendNotification(job.MasterID, job.Req.Hash(job.MasterID), msg); err != nil {
util.Log().Warning("无法发送转存成功通知到从机, %s", err) logger.Warning("无法发送转存成功通知到从机, %s", err)
} }
} }
@ -142,6 +144,6 @@ func (job *TransferTask) Do() {
func (job *TransferTask) Recycle() { func (job *TransferTask) Recycle() {
err := os.Remove(job.Req.Src) err := os.Remove(job.Req.Src)
if err != nil { if err != nil {
util.Log().Warning("无法删除中转临时文件[%s], %s", job.Req.Src, err) logger.Warning("无法删除中转临时文件[%s], %s", job.Req.Src, err)
} }
} }

@ -12,6 +12,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
) )
@ -144,7 +145,7 @@ func (job *TransferTask) Recycle() {
if job.TaskProps.NodeID == 1 { if job.TaskProps.NodeID == 1 {
err := os.RemoveAll(job.TaskProps.Parent) err := os.RemoveAll(job.TaskProps.Parent)
if err != nil { if err != nil {
util.Log().Warning("无法删除中转临时目录[%s], %s", job.TaskProps.Parent, err) logger.Warning("无法删除中转临时目录[%s], %s", job.TaskProps.Parent, err)
} }
} }
} }

@ -2,7 +2,8 @@ package task
import ( import (
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
) )
// Worker 处理任务的对象 // Worker 处理任务的对象
@ -16,13 +17,13 @@ type GeneralWorker struct {
// Do 执行任务 // Do 执行任务
func (worker *GeneralWorker) Do(job Job) { func (worker *GeneralWorker) Do(job Job) {
util.Log().Debug("开始执行任务") logger.Debug("开始执行任务")
job.SetStatus(Processing) job.SetStatus(Processing)
defer func() { defer func() {
// 致命错误捕获 // 致命错误捕获
if err := recover(); err != nil { if err := recover(); err != nil {
util.Log().Debug("任务执行出错,%s", err) logger.Debug("任务执行出错,%s", err)
job.SetError(&JobError{Msg: "致命错误", Error: fmt.Sprintf("%s", err)}) job.SetError(&JobError{Msg: "致命错误", Error: fmt.Sprintf("%s", err)})
job.SetStatus(Error) job.SetStatus(Error)
} }
@ -33,12 +34,12 @@ func (worker *GeneralWorker) Do(job Job) {
// 任务执行失败 // 任务执行失败
if err := job.GetError(); err != nil { if err := job.GetError(); err != nil {
util.Log().Debug("任务执行出错") logger.Debug("任务执行出错")
job.SetStatus(Error) job.SetStatus(Error)
return return
} }
util.Log().Debug("任务执行完成") logger.Debug("任务执行完成")
// 执行完成 // 执行完成
job.SetStatus(Complete) job.SetStatus(Complete)
} }

@ -1,3 +1,4 @@
//go:build !race
// +build !race // +build !race
package util package util

@ -56,4 +56,3 @@ func RelativePath(name string) string {
e, _ := os.Executable() e, _ := os.Executable()
return filepath.Join(filepath.Dir(e), name) return filepath.Join(filepath.Dir(e), name)
} }

@ -35,57 +35,57 @@ import (
// In the rules, the tag of a field refers to the value associated with the // In the rules, the tag of a field refers to the value associated with the
// key 'xml' in the struct field's tag (see the example above). // key 'xml' in the struct field's tag (see the example above).
// //
// * If the struct has a field of type []byte or string with tag // - If the struct has a field of type []byte or string with tag
// ",innerxml", Unmarshal accumulates the raw XML nested inside the // ",innerxml", Unmarshal accumulates the raw XML nested inside the
// element in that field. The rest of the rules still apply. // element in that field. The rest of the rules still apply.
// //
// * If the struct has a field named XMLName of type xml.Name, // - If the struct has a field named XMLName of type xml.Name,
// Unmarshal records the element name in that field. // Unmarshal records the element name in that field.
// //
// * If the XMLName field has an associated tag of the form // - If the XMLName field has an associated tag of the form
// "name" or "namespace-URL name", the XML element must have // "name" or "namespace-URL name", the XML element must have
// the given name (and, optionally, name space) or else Unmarshal // the given name (and, optionally, name space) or else Unmarshal
// returns an error. // returns an error.
// //
// * If the XML element has an attribute whose name matches a // - If the XML element has an attribute whose name matches a
// struct field name with an associated tag containing ",attr" or // struct field name with an associated tag containing ",attr" or
// the explicit name in a struct field tag of the form "name,attr", // the explicit name in a struct field tag of the form "name,attr",
// Unmarshal records the attribute value in that field. // Unmarshal records the attribute value in that field.
// //
// * If the XML element contains character data, that data is // - If the XML element contains character data, that data is
// accumulated in the first struct field that has tag ",chardata". // accumulated in the first struct field that has tag ",chardata".
// The struct field may have type []byte or string. // The struct field may have type []byte or string.
// If there is no such field, the character data is discarded. // If there is no such field, the character data is discarded.
// //
// * If the XML element contains comments, they are accumulated in // - If the XML element contains comments, they are accumulated in
// the first struct field that has tag ",comment". The struct // the first struct field that has tag ",comment". The struct
// field may have type []byte or string. If there is no such // field may have type []byte or string. If there is no such
// field, the comments are discarded. // field, the comments are discarded.
// //
// * If the XML element contains a sub-element whose name matches // - If the XML element contains a sub-element whose name matches
// the prefix of a tag formatted as "a" or "a>b>c", unmarshal // the prefix of a tag formatted as "a" or "a>b>c", unmarshal
// will descend into the XML structure looking for elements with the // will descend into the XML structure looking for elements with the
// given names, and will map the innermost elements to that struct // given names, and will map the innermost elements to that struct
// field. A tag starting with ">" is equivalent to one starting // field. A tag starting with ">" is equivalent to one starting
// with the field name followed by ">". // with the field name followed by ">".
// //
// * If the XML element contains a sub-element whose name matches // - If the XML element contains a sub-element whose name matches
// a struct field's XMLName tag and the struct field has no // a struct field's XMLName tag and the struct field has no
// explicit name tag as per the previous rule, unmarshal maps // explicit name tag as per the previous rule, unmarshal maps
// the sub-element to that struct field. // the sub-element to that struct field.
// //
// * If the XML element contains a sub-element whose name matches a // - If the XML element contains a sub-element whose name matches a
// field without any mode flags (",attr", ",chardata", etc), Unmarshal // field without any mode flags (",attr", ",chardata", etc), Unmarshal
// maps the sub-element to that struct field. // maps the sub-element to that struct field.
// //
// * If the XML element contains a sub-element that hasn't matched any // - If the XML element contains a sub-element that hasn't matched any
// of the above rules and the struct has a field with tag ",any", // of the above rules and the struct has a field with tag ",any",
// unmarshal maps the sub-element to that struct field. // unmarshal maps the sub-element to that struct field.
// //
// * An anonymous struct field is handled as if the fields of its // - An anonymous struct field is handled as if the fields of its
// value were part of the outer struct. // value were part of the outer struct.
// //
// * A struct field with tag "-" is never unmarshalled into. // - A struct field with tag "-" is never unmarshalled into.
// //
// Unmarshal maps an XML element to a string or []byte by saving the // Unmarshal maps an XML element to a string or []byte by saving the
// concatenation of that element's character data in the string or // concatenation of that element's character data in the string or
@ -110,7 +110,6 @@ import (
// //
// Unmarshal maps an XML element to a pointer by setting the pointer // Unmarshal maps an XML element to a pointer by setting the pointer
// to a freshly allocated value and then mapping the element to that value. // to a freshly allocated value and then mapping the element to that value.
//
func Unmarshal(data []byte, v interface{}) error { func Unmarshal(data []byte, v interface{}) error {
return NewDecoder(bytes.NewReader(data)).Decode(v) return NewDecoder(bytes.NewReader(data)).Decode(v)
} }

@ -787,6 +787,7 @@ const (
// - COPY accepts only "0" or "infinity", as per section 9.8.3. // - COPY accepts only "0" or "infinity", as per section 9.8.3.
// - MOVE accepts only "infinity", as per section 9.9.2. // - MOVE accepts only "infinity", as per section 9.9.2.
// - LOCK accepts only "0" or "infinity", as per section 9.10.3. // - LOCK accepts only "0" or "infinity", as per section 9.10.3.
//
// These constraints are enforced by the handleXxx methods. // These constraints are enforced by the handleXxx methods.
func parseDepth(s string) int { func parseDepth(s string) int {
switch s { switch s {

@ -1,12 +1,12 @@
package controllers package controllers
import ( import (
model "github.com/cloudreve/Cloudreve/v3/models"
"path" "path"
"strconv" "strconv"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/service/callback" "github.com/cloudreve/Cloudreve/v3/service/callback"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -56,7 +56,7 @@ func UpyunCallback(c *gin.Context) {
var callbackBody callback.UpyunCallbackService var callbackBody callback.UpyunCallbackService
if err := c.ShouldBind(&callbackBody); err == nil { if err := c.ShouldBind(&callbackBody); err == nil {
if callbackBody.Code != 200 { if callbackBody.Code != 200 {
util.Log().Debug( logger.Debug(
"Upload callback returned error code:%d, message: %s", "Upload callback returned error code:%d, message: %s",
callbackBody.Code, callbackBody.Code,
callbackBody.Message, callbackBody.Message,

@ -1,13 +1,14 @@
package controllers package controllers
import ( import (
"sync"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/webdav" "github.com/cloudreve/Cloudreve/v3/pkg/webdav"
"github.com/cloudreve/Cloudreve/v3/service/setting" "github.com/cloudreve/Cloudreve/v3/service/setting"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"sync"
) )
var handler *webdav.Handler var handler *webdav.Handler
@ -24,7 +25,7 @@ func init() {
func ServeWebDAV(c *gin.Context) { func ServeWebDAV(c *gin.Context) {
fs, err := filesystem.NewFileSystemFromContext(c) fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil { if err != nil {
util.Log().Warning("无法为WebDAV初始化文件系统%s", err) logger.Warning("无法为WebDAV初始化文件系统%s", err)
return return
} }

@ -6,7 +6,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid" "github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/routers/controllers" "github.com/cloudreve/Cloudreve/v3/routers/controllers"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
@ -16,10 +16,10 @@ import (
// InitRouter 初始化路由 // InitRouter 初始化路由
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
if conf.SystemConfig.Mode == "master" { if conf.SystemConfig.Mode == "master" {
util.Log().Info("当前运行模式Master") logger.Info("当前运行模式Master")
return InitMasterRouter() return InitMasterRouter()
} }
util.Log().Info("当前运行模式Slave") logger.Info("当前运行模式Slave")
return InitSlaveRouter() return InitSlaveRouter()
} }
@ -108,7 +108,7 @@ func InitCORS(router *gin.Engine) {
// slave模式下未启动跨域的警告 // slave模式下未启动跨域的警告
if conf.SystemConfig.Mode == "slave" { if conf.SystemConfig.Mode == "slave" {
util.Log().Warning("当前作为存储端Slave运行但未启用跨域配置可能会导致 Master 端无法正常上传文件") logger.Warning("当前作为存储端Slave运行但未启用跨域配置可能会导致 Master 端无法正常上传文件")
} }
} }

@ -7,9 +7,9 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/monitor" "github.com/cloudreve/Cloudreve/v3/pkg/aria2/monitor"
"github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/mq" "github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -143,7 +143,7 @@ func Add(c *gin.Context, service *serializer.SlaveAria2Call) serializer.Response
siteID, _ := c.Get("MasterSiteID") siteID, _ := c.Get("MasterSiteID")
mq.GlobalMQ.SubscribeCallback(gid, func(message mq.Message) { mq.GlobalMQ.SubscribeCallback(gid, func(message mq.Message) {
if err := cluster.DefaultController.SendNotification(siteID.(string), message.TriggeredBy, message); err != nil { if err := cluster.DefaultController.SendNotification(siteID.(string), message.TriggeredBy, message); err != nil {
util.Log().Warning("Failed to send remote download task status change notifications: %s", err) logger.Warning("Failed to send remote download task status change notifications: %s", err)
} }
}) })

@ -3,6 +3,11 @@ package explorer
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"strconv"
"strings"
"time"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/cache" "github.com/cloudreve/Cloudreve/v3/pkg/cache"
@ -10,13 +15,9 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid" "github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/logger"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil"
"strconv"
"strings"
"time"
) )
// CreateUploadSessionService 获取上传凭证服务 // CreateUploadSessionService 获取上传凭证服务
@ -118,7 +119,7 @@ func (service *UploadService) LocalUpload(ctx context.Context, c *gin.Context) s
} }
if expectedSizeStart > actualSizeStart { if expectedSizeStart > actualSizeStart {
util.Log().Info("Trying to overwrite chunk[%d] Start=%d", service.Index, actualSizeStart) logger.Info("Trying to overwrite chunk[%d] Start=%d", service.Index, actualSizeStart)
} }
return processChunkUpload(ctx, c, fs, &uploadSession, service.Index, file, fsctx.Append) return processChunkUpload(ctx, c, fs, &uploadSession, service.Index, file, fsctx.Append)

Loading…
Cancel
Save