Compare commits

..
13 Commits
31 changed files with 449 additions and 359 deletions
+1 -2
View File
@@ -2,7 +2,7 @@ FROM golang:1.17-alpine as cloudreve_builder
# install dependencies and build tools
RUN apk update && apk add --no-cache wget curl git yarn build-base gcc abuild binutils binutils-doc gcc-doc zip
RUN apk update && apk add --no-cache wget curl git yarn build-base gcc abuild binutils binutils-doc gcc-doc
WORKDIR /cloudreve_builder
RUN git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git
@@ -14,7 +14,6 @@ RUN yarn run build && rm -rf build/*.map
# build backend
WORKDIR /cloudreve_builder/Cloudreve
RUN zip -r assets.zip assets
RUN tag_name=$(git describe --tags) \
&& export COMMIT_SHA=$(git rev-parse --short HEAD) \
&& go build -a -o cloudreve -ldflags " -X 'github.com/HFO4/cloudreve/pkg/conf.BackendVersion=$tag_name' -X 'github.com/HFO4/cloudreve/pkg/conf.LastCommit=$COMMIT_SHA'"
+1 -1
Submodule assets updated: 335880b776...1daecbfe7e
+2 -2
View File
@@ -1,6 +1,7 @@
package bootstrap
import (
"embed"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/models/scripts"
"github.com/cloudreve/Cloudreve/v3/pkg/aria2"
@@ -13,11 +14,10 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/mq"
"github.com/cloudreve/Cloudreve/v3/pkg/task"
"github.com/gin-gonic/gin"
"io/fs"
)
// Init 初始化启动
func Init(path string, statics fs.FS) {
func Init(path string, statics embed.FS) {
InitApplication()
conf.Init(path)
// Debug 关闭时,切换为生产模式
+3 -2
View File
@@ -2,6 +2,7 @@ package bootstrap
import (
"bufio"
"embed"
"encoding/json"
"io"
"io/fs"
@@ -44,7 +45,7 @@ func (b *GinFS) Exists(prefix string, filepath string) bool {
}
// InitStatic 初始化静态资源文件
func InitStatic(statics fs.FS) {
func InitStatic(statics embed.FS) {
if util.Exists(util.RelativePath(StaticFolder)) {
util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件")
StaticFS = static.LocalFile(util.RelativePath("statics"), false)
@@ -95,7 +96,7 @@ func InitStatic(statics fs.FS) {
}
// Eject 抽离内置静态资源
func Eject(statics fs.FS) {
func Eject(statics embed.FS) {
// 初始化静态资源
embedFS, err := fs.Sub(statics, "assets/build")
if err != nil {
-2
View File
@@ -31,8 +31,6 @@ buildAssets() {
yarn run build
cd build
rm -rf *.map
cd $REPO
zip -r assets.zip assets
}
buildBinary() {
-10
View File
@@ -22,7 +22,6 @@ require (
github.com/hashicorp/go-version v1.3.0
github.com/jinzhu/gorm v1.9.11
github.com/juju/ratelimit v1.0.1
github.com/mholt/archiver/v4 v4.0.0-alpha.6
github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.2.0
@@ -42,7 +41,6 @@ require (
require (
cloud.google.com/go v0.37.4 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
@@ -50,7 +48,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
@@ -58,7 +55,6 @@ require (
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/certificate-transparency-go v1.0.21 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
@@ -67,8 +63,6 @@ require (
github.com/jmespath/go-jmespath v0.3.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/katzenpost/core v0.0.7 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
@@ -78,16 +72,12 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mozillazg/go-httpheader v0.2.1 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/pierrec/lz4/v4 v4.1.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quasoft/memstore v0.0.0-20180925164028-84a050167438 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
-25
View File
@@ -14,8 +14,6 @@ github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7I
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/aws/aws-sdk-go v1.31.5 h1:DFA7BzTydO4etqsTja+x7UfkOKQUv1xzEluLvNk81L0=
github.com/aws/aws-sdk-go v1.31.5/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
@@ -40,9 +38,6 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 h1:tkum0XDgf
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/duo-labs/webauthn v0.0.0-20191119193225-4bf9a0f776d4 h1:rXVUYM3uQcdXgSvQ5Bo+JZFMnLi0H3jib+2mz7B6M4U=
github.com/duo-labs/webauthn v0.0.0-20191119193225-4bf9a0f776d4/go.mod h1:KR2KScxcZAWdZGOUnsPGjD3ow0cvNfv3WHXC/Xz+d9g=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
@@ -108,8 +103,6 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
@@ -163,12 +156,6 @@ github.com/katzenpost/core v0.0.7 h1:ZI4oYACe/3n2iS2XsCccGbTFdhhRJunjFW05Utwna+g
github.com/katzenpost/core v0.0.7/go.mod h1:UXMLmMXlBHrhMXhWTy4DvCXqwTRLOh4DP/mR1Cm1sR8=
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b/go.mod h1:g2nVr8KZVXJSS97Jo8pJ0jgq29P6H7dG0oplUA86MQw=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.15.1 h1:y9FcTHGyrebwfP0ZZqFiaxTaiDnUrGkJkI+f583BL1A=
github.com/klauspost/compress v1.15.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -197,8 +184,6 @@ github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc=
github.com/mholt/archiver/v4 v4.0.0-alpha.6 h1:3wvos9Kn1GpKNBz+MpozinGREPslLo1ds1W16vTkErQ=
github.com/mholt/archiver/v4 v4.0.0-alpha.6/go.mod h1:9PTygYq90FQBWPspdwAng6dNjYiBuTYKqmA6c15KuCo=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -212,16 +197,11 @@ github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2/go.mod h1:wAQ
github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSrwhe88TQQ=
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 h1:e3mzJFJs4k83GXBEiTaQ5HgSc/kOK8q0rDaRO0MPaOk=
github.com/nwaples/rardecode/v2 v2.0.0-beta.2/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -274,16 +254,11 @@ github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible h1:dqpmYaez7VB
github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20200120023323-87ff3bc489ac h1:PSBhZblOjdwH7SIVgcue+7OlnLHkM45KuScLZ+PiVbQ=
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20200120023323-87ff3bc489ac/go.mod h1:wQBO5HdAkLjj2q6XQiIfDSP8DXDNrppDRw2Kp/1BODA=
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/upyun/go-sdk v2.1.0+incompatible h1:OdjXghQ/TVetWV16Pz3C1/SUpjhGBVPr+cLiqZLLyq0=
github.com/upyun/go-sdk v2.1.0+incompatible/go.mod h1:eu3F5Uz4b9ZE5bE5QsCL6mgSNWRwfj0zpJ9J626HEqs=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
+5 -17
View File
@@ -1,18 +1,13 @@
package main
import (
_ "embed"
"embed"
"flag"
"io"
"io/fs"
"strings"
"github.com/cloudreve/Cloudreve/v3/bootstrap"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/cloudreve/Cloudreve/v3/routers"
"github.com/mholt/archiver/v4"
)
var (
@@ -21,28 +16,21 @@ var (
scriptName string
)
//go:embed assets.zip
var staticZip string
var staticFS fs.FS
//go:embed assets/build
var staticEmbed embed.FS
func init() {
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
flag.BoolVar(&isEject, "eject", false, "导出内置静态资源")
flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本")
flag.Parse()
staticFS = archiver.ArchiveFS{
Stream: io.NewSectionReader(strings.NewReader(staticZip), 0, int64(len(staticZip))),
Format: archiver.Zip{},
}
bootstrap.Init(confPath, staticFS)
bootstrap.Init(confPath, staticEmbed)
}
func main() {
if isEject {
// 开始导出内置静态资源文件
bootstrap.Eject(staticFS)
bootstrap.Eject(staticEmbed)
return
}
+4
View File
@@ -62,6 +62,10 @@ func FrontendFileHandler() gin.HandlerFunc {
return
}
if path == "/service-worker.js" {
c.Header("Cache-Control", "public, no-cache")
}
// 存在的静态文件
fileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
+6 -2
View File
@@ -102,12 +102,16 @@ func (folder *Folder) GetChildFiles() ([]File, error) {
// GetFilesByIDs 根据文件ID批量获取文件,
// UID为0表示忽略用户,只根据文件ID检索
func GetFilesByIDs(ids []uint, uid uint) ([]File, error) {
return GetFilesByIDsFromTX(DB, ids, uid)
}
func GetFilesByIDsFromTX(tx *gorm.DB, ids []uint, uid uint) ([]File, error) {
var files []File
var result *gorm.DB
if uid == 0 {
result = DB.Where("id in (?)", ids).Find(&files)
result = tx.Where("id in (?)", ids).Find(&files)
} else {
result = DB.Where("id in (?) AND user_id = ?", ids, uid).Find(&files)
result = tx.Where("id in (?) AND user_id = ?", ids, uid).Find(&files)
}
return files, result.Error
}
+6 -2
View File
@@ -23,11 +23,15 @@ type Folder struct {
// Create 创建目录
func (folder *Folder) Create() (uint, error) {
if err := DB.Create(folder).Error; err != nil {
tx := DB.Begin()
if err := tx.FirstOrCreate(folder, *folder).Error; err != nil {
tx.Rollback()
util.Log().Warning("无法插入目录记录, %s", err)
return 0, err
}
return folder.ID, nil
err := tx.Commit().Error
return folder.ID, err
}
// GetChild 返回folder下名为name的子目录,不存在则返回错误
+12 -1
View File
@@ -17,8 +17,9 @@ func TestFolder_Create(t *testing.T) {
Name: "new folder",
}
// 插入成功
// 不存在,插入成功
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
fid, err := folder.Create()
@@ -28,12 +29,22 @@ func TestFolder_Create(t *testing.T) {
// 插入失败
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
fid, err = folder.Create()
asserts.Error(err)
asserts.Equal(uint(0), fid)
asserts.NoError(mock.ExpectationsWereMet())
// 存在,直接返回
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(5))
mock.ExpectCommit()
fid, err = folder.Create()
asserts.NoError(err)
asserts.Equal(uint(5), fid)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestFolder_GetChild(t *testing.T) {
+6 -3
View File
@@ -73,10 +73,13 @@ func Init() {
}
//设置连接池
//空闲
db.DB().SetMaxIdleConns(50)
//打开
db.DB().SetMaxOpenConns(100)
if conf.DatabaseConfig.Type == "sqlite" || conf.DatabaseConfig.Type == "sqlite3" || conf.DatabaseConfig.Type == "UNSET" {
db.DB().SetMaxOpenConns(1)
} else {
db.DB().SetMaxOpenConns(100)
}
//超时
db.DB().SetConnMaxLifetime(time.Second * 30)
+15 -5
View File
@@ -23,6 +23,11 @@ func IsTrueVal(val string) bool {
// GetSettingByName 用 Name 获取设置值
func GetSettingByName(name string) string {
return GetSettingByNameFromTx(DB, name)
}
// GetSettingByNameFromTx 用 Name 获取设置值,使用事务
func GetSettingByNameFromTx(tx *gorm.DB, name string) string {
var setting Setting
// 优先从缓存中查找
@@ -32,14 +37,19 @@ func GetSettingByName(name string) string {
}
// 尝试数据库中查找
if DB != nil {
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
_ = cache.Set(cacheKey, setting.Value, -1)
return setting.Value
if tx == nil {
tx = DB
if tx == nil {
return ""
}
}
result := tx.Where("name = ?", name).First(&setting)
if result.Error == nil {
_ = cache.Set(cacheKey, setting.Value, -1)
return setting.Value
}
return ""
}
+3 -3
View File
@@ -1,13 +1,13 @@
package conf
// BackendVersion 当前后端版本号
var BackendVersion = "3.5.0-beta2"
var BackendVersion = "3.5.1"
// RequiredDBVersion 与当前版本匹配的数据库版本
var RequiredDBVersion = "3.5.0-beta1"
var RequiredDBVersion = "3.5.0"
// RequiredStaticVersion 与当前版本匹配的静态资源版本
var RequiredStaticVersion = "3.5.0-beta2"
var RequiredStaticVersion = "3.5.1"
// IsPro 是否为Pro版本
var IsPro = "false"
+1
View File
@@ -461,6 +461,7 @@ func (handler *Driver) Token(ctx context.Context, ttl int64, uploadSession *seri
// 签名完成分片上传的URL
completeURL, err := handler.bucket.SignURL(fileInfo.SavePath, oss.HTTPPost, ttl,
oss.ContentType("application/octet-stream"),
oss.UploadID(imur.UploadID),
oss.Expires(time.Now().Add(time.Duration(ttl)*time.Second)),
oss.CompleteAll("yes"),
+7 -7
View File
@@ -30,7 +30,7 @@ const (
// Client to operate uploading to remote slave server
type Client interface {
// CreateUploadSession creates remote upload session
CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error
CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64, overwrite bool) error
// GetUploadURL signs an url for uploading file
GetUploadURL(ttl int64, sessionID string) (string, string, error)
// Upload uploads file to remote server
@@ -82,12 +82,11 @@ func (c *remoteClient) Upload(ctx context.Context, file fsctx.FileHeader) error
}
// Create upload session
if err := c.CreateUploadSession(ctx, session, int64(ttl)); err != nil {
overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite
if err := c.CreateUploadSession(ctx, session, int64(ttl), overwrite); err != nil {
return fmt.Errorf("failed to create upload session: %w", err)
}
overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite
// Initial chunk groups
chunks := chunk.NewChunkGroup(file, c.policy.OptionsSerialized.ChunkSize, &backoff.ConstantBackoff{
Max: model.GetIntSetting("chunk_retries", 5),
@@ -130,10 +129,11 @@ func (c *remoteClient) DeleteUploadSession(ctx context.Context, sessionID string
return nil
}
func (c *remoteClient) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error {
func (c *remoteClient) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64, overwrite bool) error {
reqBodyEncoded, err := json.Marshal(map[string]interface{}{
"session": session,
"ttl": ttl,
"session": session,
"ttl": ttl,
"overwrite": overwrite,
})
if err != nil {
return err
+1 -1
View File
@@ -281,7 +281,7 @@ func (handler *Driver) Token(ctx context.Context, ttl int64, uploadSession *seri
// 在从机端创建上传会话
uploadSession.Callback = apiURL.String()
if err := handler.uploadClient.CreateUploadSession(ctx, uploadSession, ttl); err != nil {
if err := handler.uploadClient.CreateUploadSession(ctx, uploadSession, ttl, false); err != nil {
return nil, err
}
+3 -3
View File
@@ -391,7 +391,7 @@ func TestHandler_Token(t *testing.T) {
{
clientMock := &remoteclientmock.RemoteClientMock{}
handler.uploadClient = clientMock
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10)).Return(errors.New("error"))
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10), false).Return(errors.New("error"))
res, err := handler.Token(context.Background(), 10, &serializer.UploadSession{}, &fsctx.FileStream{})
a.Error(err)
a.Contains(err.Error(), "error")
@@ -403,7 +403,7 @@ func TestHandler_Token(t *testing.T) {
{
clientMock := &remoteclientmock.RemoteClientMock{}
handler.uploadClient = clientMock
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10)).Return(nil)
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10), false).Return(nil)
clientMock.On("GetUploadURL", int64(10), "").Return("", "", errors.New("error"))
res, err := handler.Token(context.Background(), 10, &serializer.UploadSession{}, &fsctx.FileStream{})
a.Error(err)
@@ -416,7 +416,7 @@ func TestHandler_Token(t *testing.T) {
{
clientMock := &remoteclientmock.RemoteClientMock{}
handler.uploadClient = clientMock
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10)).Return(nil)
clientMock.On("CreateUploadSession", testMock.Anything, testMock.Anything, int64(10), false).Return(nil)
clientMock.On("GetUploadURL", int64(10), "").Return("1", "2", nil)
res, err := handler.Token(context.Background(), 10, &serializer.UploadSession{}, &fsctx.FileStream{})
a.NoError(err)
+4 -8
View File
@@ -203,14 +203,10 @@ func SlaveAfterUpload(session *serializer.UploadSession) Hook {
func GenericAfterUpload(ctx context.Context, fs *FileSystem, fileHeader fsctx.FileHeader) error {
fileInfo := fileHeader.Info()
// 检查路径是否存在,不存在就创建
isExist, folder := fs.IsPathExist(fileInfo.VirtualPath)
if !isExist {
newFolder, err := fs.CreateDirectory(ctx, fileInfo.VirtualPath)
if err != nil {
return err
}
folder = newFolder
// 创建或查找根目录
folder, err := fs.CreateDirectory(ctx, fileInfo.VirtualPath)
if err != nil {
return err
}
// 检查文件是否存在
+24 -12
View File
@@ -113,10 +113,15 @@ func TestGenericAfterUpload(t *testing.T) {
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").
WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "我的文件").
WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
@@ -127,22 +132,19 @@ func TestGenericAfterUpload(t *testing.T) {
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
// 路径不存在
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
mock.NewRows([]string{"name"}),
)
err = GenericAfterUpload(ctx, &fs, file)
asserts.Equal(ErrRootProtected, err)
asserts.NoError(mock.ExpectationsWereMet())
// 文件已存在
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").
WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "我的文件").
WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
mock.NewRows([]string{"name"}).AddRow("test.txt"),
)
@@ -154,10 +156,15 @@ func TestGenericAfterUpload(t *testing.T) {
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").
WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "我的文件").
WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
mock.NewRows([]string{"name", "upload_session_id"}).AddRow("test.txt", "1"),
)
@@ -169,10 +176,15 @@ func TestGenericAfterUpload(t *testing.T) {
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").
WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "我的文件").
WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
mock.ExpectBegin()
+9 -10
View File
@@ -376,12 +376,17 @@ func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []mo
return objects
}
// CreateDirectory 根据给定的完整创建目录,支持递归创建
// CreateDirectory 根据给定的完整创建目录,支持递归创建。如果目录已存在,则直接
// 返回已存在的目录。
func (fs *FileSystem) CreateDirectory(ctx context.Context, fullPath string) (*model.Folder, error) {
if fullPath == "/" || fullPath == "." || fullPath == "" {
if fullPath == "." || fullPath == "" {
return nil, ErrRootProtected
}
if fullPath == "/" {
return fs.User.Root()
}
// 获取要创建目录的父路径和目录名
fullPath = path.Clean(fullPath)
base := path.Dir(fullPath)
@@ -398,10 +403,6 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, fullPath string) (*mo
// 父目录是否存在
isExist, parent := fs.IsPathExist(base)
if !isExist {
// 递归创建父目录
if _, ok := ctx.Value(fsctx.IgnoreDirectoryConflictCtx).(bool); !ok {
ctx = context.WithValue(ctx, fsctx.IgnoreDirectoryConflictCtx, true)
}
newParent, err := fs.CreateDirectory(ctx, base)
if err != nil {
return nil, err
@@ -423,11 +424,9 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, fullPath string) (*mo
_, err := newFolder.Create()
if err != nil {
if _, ok := ctx.Value(fsctx.IgnoreDirectoryConflictCtx).(bool); !ok {
return nil, ErrFolderExisted
}
return nil, fmt.Errorf("failed to create folder: %w", err)
}
return &newFolder, nil
}
+77 -11
View File
@@ -214,7 +214,7 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
asserts.Equal(ErrFileExisted, err)
asserts.NoError(mock.ExpectationsWereMet())
// 存在同名目录
// 存在同名目录,直接返回
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
@@ -225,13 +225,37 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("s"))
mock.ExpectRollback()
_, err = fs.CreateDirectory(ctx, "/ad/ab")
asserts.Error(err)
// ab
mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
mock.ExpectCommit()
res, err := fs.CreateDirectory(ctx, "/ad/ab")
asserts.NoError(err)
asserts.EqualValues(3, res.ID)
asserts.NoError(mock.ExpectationsWereMet())
// 成功创建
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// ad
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "ad").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
_, err = fs.CreateDirectory(ctx, "/ad/ab")
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
// 成功创建, 递归创建父目录
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
@@ -239,20 +263,62 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
// ad
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "ad").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ad
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs("ad", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ab
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
_, err = fs.CreateDirectory(ctx, "/ad/ab")
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
// 父目录不存在
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
_, err = fs.CreateDirectory(ctx, "/ad")
asserts.Equal(ErrRootProtected, err)
// 底层创建失败
// 成功创建
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// ad
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "ad").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ad
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)").
WithArgs("ad", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1)).WillReturnError(errors.New("error"))
mock.ExpectRollback()
_, err = fs.CreateDirectory(ctx, "/ad/ab")
asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet())
// 直接创建根目录
mock.ExpectQuery("SELECT(.+)").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
_, err = fs.CreateDirectory(ctx, "/")
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
}
+2 -2
View File
@@ -11,8 +11,8 @@ type RemoteClientMock struct {
mock.Mock
}
func (r *RemoteClientMock) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error {
return r.Called(ctx, session, ttl).Error(0)
func (r *RemoteClientMock) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64, overwrite bool) error {
return r.Called(ctx, session, ttl, overwrite).Error(0)
}
func (r *RemoteClientMock) GetUploadURL(ttl int64, sessionID string) (string, string, error) {
+7 -11
View File
@@ -150,18 +150,14 @@ func (job *ImportTask) Do() {
if parent, ok := pathCache[virtualPath]; ok {
parentFolder = parent
} else {
exist, folder := fs.IsPathExist(virtualPath)
if exist {
parentFolder = folder
} else {
folder, err := fs.CreateDirectory(context.Background(), virtualPath)
if err != nil {
util.Log().Warning("导入任务无法创建用户目录[%s], %s",
virtualPath, err)
continue
}
parentFolder = folder
folder, err := fs.CreateDirectory(context.Background(), virtualPath)
if err != nil {
util.Log().Warning("导入任务无法创建用户目录[%s], %s",
virtualPath, err)
continue
}
parentFolder = folder
}
// 插入文件记录
+2
View File
@@ -147,6 +147,7 @@ func TestImportTask_Do(t *testing.T) {
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 创建文件时查找父目录,仍然不存在
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
task.Do()
@@ -177,6 +178,7 @@ func TestImportTask_Do(t *testing.T) {
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 创建目录
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectExec("INSERT(.+)folders(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit()
// 插入文件记录
+233 -208
View File
@@ -14,6 +14,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"
model "github.com/cloudreve/Cloudreve/v3/models"
@@ -30,6 +31,7 @@ type Handler struct {
// Logger is an optional error logger. If non-nil, it will be called
// for all HTTP requests.
Logger func(*http.Request, error)
Mutex *sync.Mutex
}
func (h *Handler) stripPrefix(p string, uid uint) (string, int, error) {
@@ -60,13 +62,19 @@ func isPathExist(ctx context.Context, fs *filesystem.FileSystem, path string) (b
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem) {
status, err := http.StatusBadRequest, errUnsupportedMethod
h.Mutex.Lock()
if h.LockSystem == nil {
h.Mutex.Unlock()
status, err = http.StatusInternalServerError, errNoLockSystem
} else {
// 检查并新建LockSystem
if _, ok := h.LockSystem[fs.User.ID]; !ok {
// 检查并新建 LockSystem
ls, ok := h.LockSystem[fs.User.ID]
if !ok {
h.LockSystem[fs.User.ID] = NewMemLS()
ls = h.LockSystem[fs.User.ID]
}
h.Mutex.Unlock()
switch r.Method {
case "OPTIONS":
status, err = h.handleOptions(w, r, fs)
@@ -81,13 +89,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *filesyst
case "COPY", "MOVE":
status, err = h.handleCopyMove(w, r, fs)
case "LOCK":
status, err = h.handleLock(w, r, fs)
status, err = h.handleLock(w, r, fs, ls)
case "UNLOCK":
status, err = h.handleUnlock(w, r, fs)
status, err = h.handleUnlock(w, r, fs, ls)
case "PROPFIND":
status, err = h.handlePropfind(w, r, fs)
status, err = h.handlePropfind(w, r, fs, ls)
case "PROPPATCH":
status, err = h.handleProppatch(w, r, fs)
status, err = h.handleProppatch(w, r, fs, ls)
}
}
@@ -103,98 +111,111 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *filesyst
}
// OK
func (h *Handler) lock(now time.Time, root string, fs *filesystem.FileSystem) (token string, status int, err error) {
token, err = h.LockSystem[fs.User.ID].Create(now, LockDetails{
Root: root,
Duration: infiniteTimeout,
ZeroDepth: true,
})
if err != nil {
if err == ErrLocked {
return "", StatusLocked, err
}
return "", http.StatusInternalServerError, err
}
return token, 0, nil
func (h *Handler) lock(now time.Time, root string, fs *filesystem.FileSystem, ls LockSystem) (token string, status int, err error) {
//token, err = ls.Create(now, LockDetails{
// Root: root,
// Duration: infiniteTimeout,
// ZeroDepth: true,
//})
//if err != nil {
// if err == ErrLocked {
// return "", StatusLocked, err
// }
// return "", http.StatusInternalServerError, err
//}
return fmt.Sprintf("%d", time.Now().Unix()), 0, nil
}
// ok
func (h *Handler) confirmLocks(r *http.Request, src, dst string, fs *filesystem.FileSystem) (release func(), status int, err error) {
hdr := r.Header.Get("If")
if hdr == "" {
// An empty If header means that the client hasn't previously created locks.
// Even if this client doesn't care about locks, we still need to check that
// the resources aren't locked by another client, so we create temporary
// locks that would conflict with another client's locks. These temporary
// locks are unlocked at the end of the HTTP request.
now, srcToken, dstToken := time.Now(), "", ""
if src != "" {
srcToken, status, err = h.lock(now, src, fs)
if err != nil {
return nil, status, err
}
}
if dst != "" {
dstToken, status, err = h.lock(now, dst, fs)
if err != nil {
if srcToken != "" {
h.LockSystem[fs.User.ID].Unlock(now, srcToken)
}
return nil, status, err
}
}
return func() {
if dstToken != "" {
h.LockSystem[fs.User.ID].Unlock(now, dstToken)
}
if srcToken != "" {
h.LockSystem[fs.User.ID].Unlock(now, srcToken)
}
}, 0, nil
}
//hdr := r.Header.Get("If")
//h.Mutex.Lock()
//ls,ok := h.LockSystem[fs.User.ID]
//h.Mutex.Unlock()
//if !ok{
// return nil, http.StatusInternalServerError, errNoLockSystem
//}
//
//if hdr == "" {
// // An empty If header means that the client hasn't previously created locks.
// // Even if this client doesn't care about locks, we still need to check that
// // the resources aren't locked by another client, so we create temporary
// // locks that would conflict with another client's locks. These temporary
// // locks are unlocked at the end of the HTTP request.
// now, srcToken, dstToken := time.Now(), "", ""
// if src != "" {
// srcToken, status, err = h.lock(now, src, fs,ls)
// if err != nil {
// return nil, status, err
// }
// }
// if dst != "" {
// dstToken, status, err = h.lock(now, dst, fs,ls)
// if err != nil {
// if srcToken != "" {
// ls.Unlock(now, srcToken)
// }
// return nil, status, err
// }
// }
//
// return func() {
// if dstToken != "" {
// ls.Unlock(now, dstToken)
// }
// if srcToken != "" {
// ls.Unlock(now, srcToken)
// }
// }, 0, nil
//}
//
//ih, ok := parseIfHeader(hdr)
//if !ok {
// return nil, http.StatusBadRequest, errInvalidIfHeader
//}
//// ih is a disjunction (OR) of ifLists, so any ifList will do.
//for _, l := range ih.lists {
// lsrc := l.resourceTag
// if lsrc == "" {
// lsrc = src
// } else {
// u, err := url.Parse(lsrc)
// if err != nil {
// continue
// }
// //if u.Host != r.Host {
// // continue
// //}
// lsrc, status, err = h.stripPrefix(u.Path, fs.User.ID)
// if err != nil {
// return nil, status, err
// }
// }
// release, err = ls.Confirm(
// time.Now(),
// lsrc,
// dst,
// l.conditions...,
// )
// if err == ErrConfirmationFailed {
// continue
// }
// if err != nil {
// return nil, http.StatusInternalServerError, err
// }
// return release, 0, nil
//}
//// Section 10.4.1 says that "If this header is evaluated and all state lists
//// fail, then the request must fail with a 412 (Precondition Failed) status."
//// We follow the spec even though the cond_put_corrupt_token test case from
//// the litmus test warns on seeing a 412 instead of a 423 (Locked).
//return nil, http.StatusPreconditionFailed, ErrLocked
ih, ok := parseIfHeader(hdr)
if !ok {
return nil, http.StatusBadRequest, errInvalidIfHeader
}
// ih is a disjunction (OR) of ifLists, so any ifList will do.
for _, l := range ih.lists {
lsrc := l.resourceTag
if lsrc == "" {
lsrc = src
} else {
u, err := url.Parse(lsrc)
if err != nil {
continue
}
//if u.Host != r.Host {
// continue
//}
lsrc, status, err = h.stripPrefix(u.Path, fs.User.ID)
if err != nil {
return nil, status, err
}
}
release, err = h.LockSystem[fs.User.ID].Confirm(
time.Now(),
lsrc,
dst,
l.conditions...,
)
if err == ErrConfirmationFailed {
continue
}
if err != nil {
return nil, http.StatusInternalServerError, err
}
return release, 0, nil
}
// Section 10.4.1 says that "If this header is evaluated and all state lists
// fail, then the request must fail with a 412 (Precondition Failed) status."
// We follow the spec even though the cond_put_corrupt_token test case from
// the litmus test warns on seeing a 412 instead of a 423 (Locked).
return nil, http.StatusPreconditionFailed, ErrLocked
return func() {
}, 0, nil
}
//OK
@@ -245,7 +266,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
return http.StatusInternalServerError, err
}
etag, err := findETag(ctx, fs, h.LockSystem[fs.User.ID], reqPath, &fs.FileTarget[0])
etag, err := findETag(ctx, fs, nil, reqPath, &fs.FileTarget[0])
if err != nil {
return http.StatusInternalServerError, err
}
@@ -271,6 +292,7 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *files
if err != nil {
return status, err
}
release, status, err := h.confirmLocks(r, reqPath, "", fs)
if err != nil {
return status, err
@@ -373,7 +395,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *filesyst
return http.StatusMethodNotAllowed, err
}
etag, err := findETag(ctx, fs, h.LockSystem[fs.User.ID], reqPath, fileData.Model.(*model.File))
etag, err := findETag(ctx, fs, nil, reqPath, fileData.Model.(*model.File))
if err != nil {
return http.StatusInternalServerError, err
}
@@ -400,11 +422,7 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *filesy
if r.ContentLength > 0 {
return http.StatusUnsupportedMediaType, nil
}
if strings.Contains(r.UserAgent(), "rclone") {
if _, ok := ctx.Value(fsctx.IgnoreDirectoryConflictCtx).(bool); !ok {
ctx = context.WithValue(ctx, fsctx.IgnoreDirectoryConflictCtx, true)
}
}
if _, err := fs.CreateDirectory(ctx, reqPath); err != nil {
return http.StatusConflict, err
}
@@ -498,130 +516,137 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *fil
}
// OK
func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem) (retStatus int, retErr error) {
func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem, ls LockSystem) (retStatus int, retErr error) {
defer fs.Recycle()
duration, err := parseTimeout(r.Header.Get("Timeout"))
if err != nil {
return http.StatusBadRequest, err
}
li, status, err := readLockInfo(r.Body)
reqPath, status, err := h.stripPrefix(r.URL.Path, fs.User.ID)
if err != nil {
return status, err
}
//ctx := r.Context()
token, ld, now, created := "", LockDetails{}, time.Now(), false
if li == (lockInfo{}) {
// An empty lockInfo means to refresh the lock.
ih, ok := parseIfHeader(r.Header.Get("If"))
if !ok {
return http.StatusBadRequest, errInvalidIfHeader
}
if len(ih.lists) == 1 && len(ih.lists[0].conditions) == 1 {
token = ih.lists[0].conditions[0].Token
}
if token == "" {
return http.StatusBadRequest, errInvalidLockToken
}
ld, err = h.LockSystem[fs.User.ID].Refresh(now, token, duration)
if err != nil {
if err == ErrNoSuchLock {
return http.StatusPreconditionFailed, err
}
return http.StatusInternalServerError, err
}
////ctx := r.Context()
//token, ld, now, created := "", LockDetails{}, time.Now(), false
//if li == (lockInfo{}) {
// // An empty lockInfo means to refresh the lock.
// ih, ok := parseIfHeader(r.Header.Get("If"))
// if !ok {
// return http.StatusBadRequest, errInvalidIfHeader
// }
// if len(ih.lists) == 1 && len(ih.lists[0].conditions) == 1 {
// token = ih.lists[0].conditions[0].Token
// }
// if token == "" {
// return http.StatusBadRequest, errInvalidLockToken
// }
// ld, err = ls.Refresh(now, token, duration)
// if err != nil {
// if err == ErrNoSuchLock {
// return http.StatusPreconditionFailed, err
// }
// return http.StatusInternalServerError, err
// }
//
//} else {
// // Section 9.10.3 says that "If no Depth header is submitted on a LOCK request,
// // then the request MUST act as if a "Depth:infinity" had been submitted."
// depth := infiniteDepth
// if hdr := r.Header.Get("Depth"); hdr != "" {
// depth = parseDepth(hdr)
// if depth != 0 && depth != infiniteDepth {
// // Section 9.10.3 says that "Values other than 0 or infinity must not be
// // used with the Depth header on a LOCK method".
// return http.StatusBadRequest, errInvalidDepth
// }
// }
// reqPath, status, err := h.stripPrefix(r.URL.Path, fs.User.ID)
// if err != nil {
// return status, err
// }
// ld = LockDetails{
// Root: reqPath,
// Duration: duration,
// OwnerXML: li.Owner.InnerXML,
// ZeroDepth: depth == 0,
// }
// token, err = ls.Create(now, ld)
// if err != nil {
// if err == ErrLocked {
// return StatusLocked, err
// }
// return http.StatusInternalServerError, err
// }
// defer func() {
// if retErr != nil {
// ls.Unlock(now, token)
// }
// }()
//
// // Create the resource if it didn't previously exist.
// //if _, err := h.FileSystem.Stat(ctx, reqPath); err != nil {
// // f, err := h.FileSystem.OpenFile(ctx, reqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
// // if err != nil {
// // // TODO: detect missing intermediate dirs and return http.StatusConflict?
// // return http.StatusInternalServerError, err
// // }
// // f.Close()
// // created = true
// //}
//
// // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
// // Lock-Token value is a Coded-URL. We add angle brackets.
// w.Header().Set("Lock-Token", "<"+token+">")
//}
//
//w.Header().Set("Content-Type", "application/xml; charset=utf-8")
//if created {
// // This is "w.WriteHeader(http.StatusCreated)" and not "return
// // http.StatusCreated, nil" because we write our own (XML) response to w
// // and Handler.ServeHTTP would otherwise write "Created".
// w.WriteHeader(http.StatusCreated)
//}
} else {
// Section 9.10.3 says that "If no Depth header is submitted on a LOCK request,
// then the request MUST act as if a "Depth:infinity" had been submitted."
depth := infiniteDepth
if hdr := r.Header.Get("Depth"); hdr != "" {
depth = parseDepth(hdr)
if depth != 0 && depth != infiniteDepth {
// Section 9.10.3 says that "Values other than 0 or infinity must not be
// used with the Depth header on a LOCK method".
return http.StatusBadRequest, errInvalidDepth
}
}
reqPath, status, err := h.stripPrefix(r.URL.Path, fs.User.ID)
if err != nil {
return status, err
}
ld = LockDetails{
Root: reqPath,
Duration: duration,
OwnerXML: li.Owner.InnerXML,
ZeroDepth: depth == 0,
}
token, err = h.LockSystem[fs.User.ID].Create(now, ld)
if err != nil {
if err == ErrLocked {
return StatusLocked, err
}
return http.StatusInternalServerError, err
}
defer func() {
if retErr != nil {
h.LockSystem[fs.User.ID].Unlock(now, token)
}
}()
// Create the resource if it didn't previously exist.
//if _, err := h.FileSystem.Stat(ctx, reqPath); err != nil {
// f, err := h.FileSystem.OpenFile(ctx, reqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
// if err != nil {
// // TODO: detect missing intermediate dirs and return http.StatusConflict?
// return http.StatusInternalServerError, err
// }
// f.Close()
// created = true
//}
// http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
// Lock-Token value is a Coded-URL. We add angle brackets.
w.Header().Set("Lock-Token", "<"+token+">")
}
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
if created {
// This is "w.WriteHeader(http.StatusCreated)" and not "return
// http.StatusCreated, nil" because we write our own (XML) response to w
// and Handler.ServeHTTP would otherwise write "Created".
w.WriteHeader(http.StatusCreated)
}
writeLockInfo(w, token, ld)
writeLockInfo(w, fmt.Sprintf("%d", time.Now().UnixNano()), LockDetails{
Duration: duration,
OwnerXML: fs.User.Email,
Root: reqPath,
})
return 0, nil
}
// OK
func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem) (status int, err error) {
func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem, ls LockSystem) (status int, err error) {
defer fs.Recycle()
return http.StatusNoContent, err
// http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
// Lock-Token value is a Coded-URL. We strip its angle brackets.
t := r.Header.Get("Lock-Token")
if len(t) < 2 || t[0] != '<' || t[len(t)-1] != '>' {
return http.StatusBadRequest, errInvalidLockToken
}
t = t[1 : len(t)-1]
switch err = h.LockSystem[fs.User.ID].Unlock(time.Now(), t); err {
case nil:
return http.StatusNoContent, err
case ErrForbidden:
return http.StatusForbidden, err
case ErrLocked:
return StatusLocked, err
case ErrNoSuchLock:
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
//// http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
//// Lock-Token value is a Coded-URL. We strip its angle brackets.
//t := r.Header.Get("Lock-Token")
//if len(t) < 2 || t[0] != '<' || t[len(t)-1] != '>' {
// return http.StatusBadRequest, errInvalidLockToken
//}
//t = t[1 : len(t)-1]
//
//switch err = ls.Unlock(time.Now(), t); err {
//case nil:
// return http.StatusNoContent, err
//case ErrForbidden:
// return http.StatusForbidden, err
//case ErrLocked:
// return StatusLocked, err
//case ErrNoSuchLock:
// return http.StatusConflict, err
//default:
// return http.StatusInternalServerError, err
//}
}
// OK
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem) (status int, err error) {
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem, ls LockSystem) (status int, err error) {
defer fs.Recycle()
reqPath, status, err := h.stripPrefix(r.URL.Path, fs.User.ID)
@@ -655,7 +680,7 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *fil
}
var pstats []Propstat
if pf.Propname != nil {
pnames, err := propnames(ctx, fs, h.LockSystem[fs.User.ID], info)
pnames, err := propnames(ctx, fs, ls, info)
if err != nil {
return err
}
@@ -665,9 +690,9 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *fil
}
pstats = append(pstats, pstat)
} else if pf.Allprop != nil {
pstats, err = allprop(ctx, fs, h.LockSystem[fs.User.ID], info, pf.Prop)
pstats, err = allprop(ctx, fs, ls, info, pf.Prop)
} else {
pstats, err = props(ctx, fs, h.LockSystem[fs.User.ID], info, pf.Prop)
pstats, err = props(ctx, fs, ls, info, pf.Prop)
}
if err != nil {
return err
@@ -690,7 +715,7 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *fil
return 0, nil
}
func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem) (status int, err error) {
func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request, fs *filesystem.FileSystem, ls LockSystem) (status int, err error) {
defer fs.Recycle()
reqPath, status, err := h.stripPrefix(r.URL.Path, fs.User.ID)
@@ -712,7 +737,7 @@ func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request, fs *fi
if err != nil {
return status, err
}
pstats, err := patch(ctx, fs, h.LockSystem[fs.User.ID], reqPath, patches)
pstats, err := patch(ctx, fs, ls, reqPath, patches)
if err != nil {
return http.StatusInternalServerError, err
}
+2
View File
@@ -7,6 +7,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/webdav"
"github.com/cloudreve/Cloudreve/v3/service/setting"
"github.com/gin-gonic/gin"
"sync"
)
var handler *webdav.Handler
@@ -15,6 +16,7 @@ func init() {
handler = &webdav.Handler{
Prefix: "/dav",
LockSystem: make(map[uint]webdav.LockSystem),
Mutex: &sync.Mutex{},
}
}
+1 -1
View File
@@ -179,7 +179,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response
isSizeCheckFailed = false
}
if isSizeCheckFailed || info.GetSourcePath() != actualPath {
if isSizeCheckFailed || !strings.EqualFold(info.GetSourcePath(), actualPath) {
fs.Handler.(onedrive.Driver).Client.Delete(context.Background(), []string{info.GetSourcePath()})
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
}
+8 -5
View File
@@ -391,11 +391,6 @@ func (service *ItemPropertyService) GetProperty(ctx context.Context, c *gin.Cont
return serializer.Err(serializer.CodeNotFound, "对象不存在", err)
}
// 如果对象是目录, 先尝试返回缓存结果
if cacheRes, ok := cache.Get(fmt.Sprintf("folder_props_%d", res)); ok {
return serializer.Response{Data: cacheRes.(serializer.ObjectProps)}
}
folder, err := model.GetFoldersByIDs([]uint{res}, user.ID)
if err != nil {
return serializer.DBErr("找不到目录", err)
@@ -404,6 +399,14 @@ func (service *ItemPropertyService) GetProperty(ctx context.Context, c *gin.Cont
props.CreatedAt = folder[0].CreatedAt
props.UpdatedAt = folder[0].UpdatedAt
// 如果对象是目录, 先尝试返回缓存结果
if cacheRes, ok := cache.Get(fmt.Sprintf("folder_props_%d", res)); ok {
res := cacheRes.(serializer.ObjectProps)
res.CreatedAt = props.CreatedAt
res.UpdatedAt = props.UpdatedAt
return serializer.Response{Data: res}
}
// 统计子目录
childFolders, err := model.GetRecursiveChildFolder([]uint{folder[0].ID},
user.ID, true)
+4 -3
View File
@@ -167,13 +167,14 @@ func CreateTransferTask(c *gin.Context, req *serializer.SlaveTransferReq) serial
// SlaveListService 从机上传会话服务
type SlaveCreateUploadSessionService struct {
Session serializer.UploadSession `json:"session" binding:"required"`
TTL int64 `json:"ttl"`
Session serializer.UploadSession `json:"session" binding:"required"`
TTL int64 `json:"ttl"`
Overwrite bool `json:"overwrite"`
}
// Create 从机创建上传会话
func (service *SlaveCreateUploadSessionService) Create(ctx context.Context, c *gin.Context) serializer.Response {
if util.Exists(service.Session.SavePath) {
if !service.Overwrite && util.Exists(service.Session.SavePath) {
return serializer.Err(serializer.CodeConflict, "placeholder file already exist", nil)
}