feat(storage): add named attachment storage drivers (#6184)

This commit is contained in:
Johnny 2026-08-13 22:37:11 +08:00 committed by GitHub
parent bba1d6d497
commit 7d971b1c61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 3973 additions and 461 deletions

5
go.mod
View file

@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/feeds v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0
github.com/johannesboyne/gofakes3 v1.2.0
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v5 v5.1.0
github.com/lib/pq v1.12.3
@ -28,6 +29,7 @@ require (
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.11.1
github.com/testcontainers/testcontainers-go v0.42.0
github.com/testcontainers/testcontainers-go/modules/minio v0.42.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.42.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0
github.com/yuin/goldmark v1.8.2
@ -95,6 +97,7 @@ require (
github.com/pelletier/go-toml/v2 v2.3.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
github.com/sagikazarmark/locafero v0.12.0 // indirect
github.com/segmentio/asm v1.1.3 // indirect
github.com/segmentio/encoding v0.5.4 // indirect
@ -117,9 +120,11 @@ require (
go.opentelemetry.io/otel v1.43.0 // indirect
go.opentelemetry.io/otel/metric v1.43.0 // indirect
go.opentelemetry.io/otel/trace v1.43.0 // indirect
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect
golang.org/x/image v0.39.0 // indirect
golang.org/x/tools v0.44.0 // indirect
google.golang.org/api v0.276.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260427160629-7cedc36a6bc4 // indirect
modernc.org/libc v1.72.1 // indirect

34
go.sum
View file

@ -32,6 +32,8 @@ github.com/aws/aws-sdk-go-v2/credentials v1.19.15 h1:fyvgWTszojq8hEnMi8PPBTvZdTt
github.com/aws/aws-sdk-go-v2/credentials v1.19.15/go.mod h1:gJiYyMOjNg8OEdRWOf3CrFQxM2a98qmrtjx1zuiQfB8=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.22 h1:IOGsJ1xVWhsi+ZO7/NW8OuZZBtMJLZbk4P5HDjJO0jQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.22/go.mod h1:b+hYdbU+jGKfXE8kKM6g1+h+L/Go3vMvzlxBsiuGsxg=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.75 h1:S61/E3N01oral6B3y9hZ2E1iFDqCZPPOBoBQretCnBI=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.75/go.mod h1:bDMQbkI1vJbNjnvJYpPTSNYBkI/VIv18ngWb/K84tkk=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.22 h1:GmLa5Kw1ESqtFpXsx5MmC84QWa/ZrLZvlJGa2y+4kcQ=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.22/go.mod h1:6sW9iWm9DK9YRpRGga/qzrzNLgKpT2cIxb7Vo2eNOp0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.22 h1:dY4kWZiSaXIzxnKlj17nHnBcXXBfac6UlsAx2qL6XrU=
@ -62,6 +64,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cevatbarisyilmaz/ara v0.0.4 h1:SGH10hXpBJhhTlObuZzTuFn1rrdmjQImITXnZVPSodc=
github.com/cevatbarisyilmaz/ara v0.0.4/go.mod h1:BfFOxnUd6Mj6xmcvRxHN3Sr21Z1T3U2MYkYOmoQe4Ts=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
@ -145,10 +149,16 @@ github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/johannesboyne/gofakes3 v1.2.0 h1:I9VEzPWvvAUAGzDlhYFoZjF0AXMlkcEyZlmBwiI6Oms=
github.com/johannesboyne/gofakes3 v1.2.0/go.mod h1:UHhRZRod9rENGFrUWTYnQHZqlNgSmjOq8DaD/ATQYRM=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE=
github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@ -167,6 +177,12 @@ github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.68 h1:hTqSIfLlpXaKuNy4baAp4Jjy2sqZEN9hRxD0M4aOfrQ=
github.com/minio/minio-go/v7 v7.0.68/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
@ -187,6 +203,10 @@ github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
github.com/modelcontextprotocol/go-sdk v1.6.1 h1:0zOSupjKUxPKSocPT1Wtago+mUHU2/uZ4xSOY0FGReU=
github.com/modelcontextprotocol/go-sdk v1.6.1/go.mod h1:kzm3kzFL1/+AziGOE0nUs3gvPoNxMCvkxokMkuFapXQ=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/openai/openai-go/v3 v3.32.0 h1:aHp/3wkX1W6jB8zTtf9xV0aK0qPFSVDqS7AHmlJ4hXs=
@ -209,7 +229,11 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4=
github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI=
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
@ -239,6 +263,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/testcontainers/testcontainers-go v0.42.0 h1:He3IhTzTZOygSXLJPMX7n44XtK+qhjat1nI9cneBbUY=
github.com/testcontainers/testcontainers-go v0.42.0/go.mod h1:vZjdY1YmUA1qEForxOIOazfsrdyORJAbhi0bp8plN30=
github.com/testcontainers/testcontainers-go/modules/minio v0.42.0 h1:8yTWNv8ALG7JQHYvm1n9PegH0uJT7dRtWNHf6eQeTRs=
github.com/testcontainers/testcontainers-go/modules/minio v0.42.0/go.mod h1:bcjonmVMA/aEzxFFIh/FRwSkeZ+fnxwvkGN/Z4EiW28=
github.com/testcontainers/testcontainers-go/modules/mysql v0.42.0 h1:Yhv1k7vDpyzZePntg5R5Oj4ZMCyWpAfpJeRu1ROsgiU=
github.com/testcontainers/testcontainers-go/modules/mysql v0.42.0/go.mod h1:Z7SCTuiZlghAdRjkv3Ir0iXJKC2T2avbtxLR0DRe+ng=
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0 h1:GCbb1ndrF7OTDiIvxXyItaDab4qkzTFJ48LKFdM7EIo=
@ -264,6 +290,8 @@ github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.68.0 h1:CqXxU8VOmDefoh0+ztfGaymYbhdB/tT3zs79QaZTNGY=
@ -278,6 +306,8 @@ go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfC
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d h1:Ns9kd1Rwzw7t0BR8XMphenji4SmIoNZPn8zhYmaVKP8=
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d/go.mod h1:92Uoe3l++MlthCm+koNi0tcUCX3anayogF0Pa/sp24k=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
@ -329,6 +359,10 @@ google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=

View file

@ -0,0 +1,36 @@
package storage
import (
"context"
"io"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/storage/s3"
storepb "github.com/usememos/memos/proto/gen/store"
)
// Driver provides object operations for a configured attachment storage.
type Driver interface {
UploadObject(ctx context.Context, key string, fileType string, content io.Reader) (string, error)
PresignGetObject(ctx context.Context, key string) (string, error)
GetObject(ctx context.Context, key string) ([]byte, error)
GetObjectStream(ctx context.Context, key string) (io.ReadCloser, error)
DeleteObject(ctx context.Context, key string) error
}
// NewDriver creates the driver for a configured storage.
func NewDriver(ctx context.Context, configuredStorage *storepb.Storage) (Driver, error) {
if configuredStorage == nil {
return nil, errors.New("storage is required")
}
switch configuredStorage.Type {
case storepb.StorageType_STORAGE_TYPE_S3:
if configuredStorage.GetS3Config() == nil {
return nil, errors.Errorf("S3 config is missing for storage %q", configuredStorage.Id)
}
return s3.NewDriver(ctx, configuredStorage.GetS3Config())
default:
return nil, errors.Errorf("storage %q has unsupported driver type %s", configuredStorage.Id, configuredStorage.Type.String())
}
}

View file

@ -0,0 +1,58 @@
package storage
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/internal/testutil/fakes3"
storepb "github.com/usememos/memos/proto/gen/store"
)
func TestNewDriver(t *testing.T) {
ctx := context.Background()
t.Run("creates S3 driver", func(t *testing.T) {
fake := fakes3.New(t, "driver-factory")
driver, err := NewDriver(ctx, &storepb.Storage{
Id: "s3-primary",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: fake.Config("driver-factory")},
})
require.NoError(t, err)
content := []byte("created through the storage driver factory")
key, err := driver.UploadObject(ctx, "factory/object.txt", "text/plain", bytes.NewReader(content))
require.NoError(t, err)
downloaded, err := driver.GetObject(ctx, key)
require.NoError(t, err)
require.Equal(t, content, downloaded)
})
tests := []struct {
name string
storage *storepb.Storage
wantErr string
}{
{name: "missing storage", wantErr: "storage is required"},
{
name: "missing S3 config",
storage: &storepb.Storage{Id: "s3-missing", Type: storepb.StorageType_STORAGE_TYPE_S3},
wantErr: `S3 config is missing for storage "s3-missing"`,
},
{
name: "unsupported storage type",
storage: &storepb.Storage{Id: "local", Type: storepb.StorageType_STORAGE_TYPE_LOCAL},
wantErr: `storage "local" has unsupported driver type STORAGE_TYPE_LOCAL`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
driver, err := NewDriver(ctx, test.storage)
require.Nil(t, driver)
require.EqualError(t, err, test.wantErr)
})
}
}

View file

@ -17,12 +17,14 @@ import (
storepb "github.com/usememos/memos/proto/gen/store"
)
type Client struct {
// Driver stores attachment objects in an S3-compatible object store.
type Driver struct {
Client *s3.Client
Bucket *string
}
func NewClient(ctx context.Context, s3Config *storepb.StorageS3Config) (*Client, error) {
// NewDriver creates an S3 storage driver from the supplied configuration.
func NewDriver(ctx context.Context, s3Config *storepb.StorageS3Config) (*Driver, error) {
loadOptions := []func(*config.LoadOptions) error{
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(s3Config.AccessKeyId, s3Config.AccessKeySecret, "")),
config.WithRegion(s3Config.Region),
@ -47,14 +49,14 @@ func NewClient(ctx context.Context, s3Config *storepb.StorageS3Config) (*Client,
o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired
o.ResponseChecksumValidation = aws.ResponseChecksumValidationWhenRequired
})
return &Client{
return &Driver{
Client: client,
Bucket: aws.String(s3Config.Bucket),
}, nil
}
// UploadObject uploads an object to S3.
func (c *Client) UploadObject(ctx context.Context, key string, fileType string, content io.Reader) (string, error) {
func (c *Driver) UploadObject(ctx context.Context, key string, fileType string, content io.Reader) (string, error) {
putInput := s3.PutObjectInput{
Bucket: c.Bucket,
Key: aws.String(key),
@ -62,13 +64,13 @@ func (c *Client) UploadObject(ctx context.Context, key string, fileType string,
Body: content,
}
if _, err := c.Client.PutObject(ctx, &putInput); err != nil {
return "", err
return "", errors.Wrap(err, "failed to upload object")
}
return key, nil
}
// PresignGetObject presigns an object in S3.
func (c *Client) PresignGetObject(ctx context.Context, key string) (string, error) {
func (c *Driver) PresignGetObject(ctx context.Context, key string) (string, error) {
presignClient := s3.NewPresignClient(c.Client)
presignResult, err := presignClient.PresignGetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(*c.Bucket),
@ -85,7 +87,7 @@ func (c *Client) PresignGetObject(ctx context.Context, key string) (string, erro
}
// GetObject retrieves an object from S3.
func (c *Client) GetObject(ctx context.Context, key string) ([]byte, error) {
func (c *Driver) GetObject(ctx context.Context, key string) ([]byte, error) {
output, err := c.Client.GetObject(ctx, &s3.GetObjectInput{
Bucket: c.Bucket,
Key: aws.String(key),
@ -102,7 +104,7 @@ func (c *Client) GetObject(ctx context.Context, key string) ([]byte, error) {
}
// GetObjectStream retrieves an object from S3 as a stream.
func (c *Client) GetObjectStream(ctx context.Context, key string) (io.ReadCloser, error) {
func (c *Driver) GetObjectStream(ctx context.Context, key string) (io.ReadCloser, error) {
output, err := c.Client.GetObject(ctx, &s3.GetObjectInput{
Bucket: c.Bucket,
Key: aws.String(key),
@ -114,7 +116,7 @@ func (c *Client) GetObjectStream(ctx context.Context, key string) (io.ReadCloser
}
// DeleteObject deletes an object in S3.
func (c *Client) DeleteObject(ctx context.Context, key string) error {
func (c *Driver) DeleteObject(ctx context.Context, key string) error {
_, err := c.Client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: c.Bucket,
Key: aws.String(key),

View file

@ -1,17 +1,78 @@
package s3
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/internal/testutil/fakes3"
testminio "github.com/usememos/memos/internal/testutil/minio"
storepb "github.com/usememos/memos/proto/gen/store"
)
func TestNewClientInsecureSkipTLSVerify(t *testing.T) {
// assertObjectLifecycle exercises the full driver contract — upload, download,
// stream, presign + fetch, delete — against whichever backend the driver targets.
func assertObjectLifecycle(ctx context.Context, t *testing.T, driver *Driver, key string, content []byte) {
t.Helper()
uploadedKey, err := driver.UploadObject(ctx, key, "text/plain", bytes.NewReader(content))
require.NoError(t, err)
require.Equal(t, key, uploadedKey)
downloaded, err := driver.GetObject(ctx, key)
require.NoError(t, err)
require.Equal(t, content, downloaded)
stream, err := driver.GetObjectStream(ctx, key)
require.NoError(t, err)
streamed, err := io.ReadAll(stream)
require.NoError(t, err)
require.NoError(t, stream.Close())
require.Equal(t, content, streamed)
presignedURL, err := driver.PresignGetObject(ctx, key)
require.NoError(t, err)
require.Contains(t, presignedURL, "X-Amz-Signature=")
request, err := http.NewRequestWithContext(ctx, http.MethodGet, presignedURL, nil)
require.NoError(t, err)
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
defer response.Body.Close()
require.Equal(t, http.StatusOK, response.StatusCode)
presignedContent, err := io.ReadAll(response.Body)
require.NoError(t, err)
require.Equal(t, content, presignedContent)
require.NoError(t, driver.DeleteObject(ctx, key))
_, err = driver.GetObject(ctx, key)
require.Error(t, err)
}
func TestDriverObjectLifecycle(t *testing.T) {
ctx := context.Background()
fake := fakes3.New(t, "attachments")
driver, err := NewDriver(ctx, fake.Config("attachments"))
require.NoError(t, err)
assertObjectLifecycle(ctx, t, driver, "assets/notes/test.txt", []byte("attachment stored in fake S3"))
}
func TestDriverMinIOCompatibility(t *testing.T) {
ctx := context.Background()
server := testminio.New(t, "attachments")
config := server.Config("attachments")
driver, err := NewDriver(ctx, config)
require.NoError(t, err)
assertObjectLifecycle(ctx, t, driver, "compatibility/test.txt", []byte("attachment stored in MinIO"))
}
func TestNewDriverInsecureSkipTLSVerify(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method)
require.Equal(t, "/memos/note.txt", r.URL.Path)
@ -33,19 +94,19 @@ func TestNewClientInsecureSkipTLSVerify(t *testing.T) {
}
t.Run("rejects self-signed certificate by default", func(t *testing.T) {
client, err := NewClient(context.Background(), newConfig(false))
driver, err := NewDriver(context.Background(), newConfig(false))
require.NoError(t, err)
_, err = client.GetObject(context.Background(), "note.txt")
_, err = driver.GetObject(context.Background(), "note.txt")
require.Error(t, err)
require.ErrorContains(t, err, "certificate")
})
t.Run("accepts self-signed certificate when enabled", func(t *testing.T) {
client, err := NewClient(context.Background(), newConfig(true))
driver, err := NewDriver(context.Background(), newConfig(true))
require.NoError(t, err)
content, err := client.GetObject(context.Background(), "note.txt")
content, err := driver.GetObject(context.Background(), "note.txt")
require.NoError(t, err)
require.Equal(t, []byte("stored memo"), content)
})

View file

@ -0,0 +1,61 @@
// Package fakes3 provides an in-process S3-compatible service for tests.
package fakes3
import (
"io"
"net/http/httptest"
"testing"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3mem"
storepb "github.com/usememos/memos/proto/gen/store"
)
// Server is an in-memory S3-compatible HTTP service.
type Server struct {
URL string
memory *s3mem.Backend
}
// New starts an in-process S3 service with the requested buckets.
func New(t testing.TB, buckets ...string) *Server {
t.Helper()
memory := s3mem.New()
for _, bucket := range buckets {
if err := memory.CreateBucket(bucket); err != nil {
t.Fatalf("failed to create fake S3 bucket %q: %v", bucket, err)
}
}
httpServer := httptest.NewServer(gofakes3.New(memory).Server())
t.Cleanup(httpServer.Close)
return &Server{
URL: httpServer.URL,
memory: memory,
}
}
// Config returns a storage configuration that connects to the fake service.
func (s *Server) Config(bucket string) *storepb.StorageS3Config {
return &storepb.StorageS3Config{
AccessKeyId: "test-access-key",
AccessKeySecret: "test-secret-key",
Endpoint: s.URL,
Region: "us-east-1",
Bucket: bucket,
UsePathStyle: true,
}
}
// GetObject reads an object directly from the in-memory store.
func (s *Server) GetObject(bucket, key string) ([]byte, error) {
object, err := s.memory.GetObject(bucket, key, nil)
if err != nil {
return nil, err
}
defer object.Contents.Close()
return io.ReadAll(object.Contents)
}

View file

@ -0,0 +1,124 @@
// Package minio provides a real S3-compatible service backed by a MinIO
// Testcontainer for integration tests.
package minio
import (
"bytes"
"context"
"io"
"os"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
tcminio "github.com/testcontainers/testcontainers-go/modules/minio"
storepb "github.com/usememos/memos/proto/gen/store"
)
const image = "minio/minio:RELEASE.2024-01-16T16-07-38Z"
// Server is an S3-compatible MinIO container with an AWS SDK client for
// arranging and inspecting test objects.
type Server struct {
Endpoint string
Region string
Username string
Password string
client *awss3.Client
}
// New starts a MinIO container and creates the requested buckets.
func New(t *testing.T, buckets ...string) *Server {
t.Helper()
if testing.Short() {
t.Skip("skipping MinIO integration test in short mode")
}
if os.Getenv("SKIP_CONTAINER_TESTS") == "1" {
t.Skip("skipping MinIO integration test (SKIP_CONTAINER_TESTS=1)")
}
testcontainers.SkipIfProviderIsNotHealthy(t)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
container, err := tcminio.Run(
ctx,
image,
tcminio.WithUsername("memos-test-access"),
tcminio.WithPassword("memos-test-secret"),
)
require.NoError(t, err)
testcontainers.CleanupContainer(t, container)
connection, err := container.ConnectionString(ctx)
require.NoError(t, err)
server := &Server{
Endpoint: "http://" + connection,
Region: "us-east-1",
Username: container.Username,
Password: container.Password,
}
config, err := awsconfig.LoadDefaultConfig(
ctx,
awsconfig.WithRegion(server.Region),
awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(server.Username, server.Password, "")),
)
require.NoError(t, err)
server.client = awss3.NewFromConfig(config, func(options *awss3.Options) {
options.BaseEndpoint = aws.String(server.Endpoint)
options.UsePathStyle = true
})
for _, bucket := range buckets {
_, err := server.client.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String(bucket)})
require.NoError(t, err)
}
return server
}
// Config returns a storage configuration connected to the requested bucket.
func (s *Server) Config(bucket string) *storepb.StorageS3Config {
return &storepb.StorageS3Config{
AccessKeyId: s.Username,
AccessKeySecret: s.Password,
Endpoint: s.Endpoint,
Region: s.Region,
Bucket: bucket,
UsePathStyle: true,
}
}
// PutObject uploads an object directly for arranging a test fixture.
func (s *Server) PutObject(bucket, key, contentType string, content []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := s.client.PutObject(ctx, &awss3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ContentType: aws.String(contentType),
Body: bytes.NewReader(content),
})
return err
}
// GetObject reads an object directly for asserting test results.
func (s *Server) GetObject(bucket, key string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
object, err := s.client.GetObject(ctx, &awss3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
defer object.Body.Close()
return io.ReadAll(object.Body)
}

View file

@ -155,27 +155,29 @@ message InstanceSetting {
}
}
// Storage configuration settings for instance attachments.
message StorageSetting {
// Storage type enumeration for different storage backends.
enum StorageType {
STORAGE_TYPE_UNSPECIFIED = 0;
// DATABASE is the database storage type.
DATABASE = 1;
// LOCAL is the local storage type.
LOCAL = 2;
// S3 is the S3 storage type.
S3 = 3;
}
// storage_type is the storage type.
StorageType storage_type = 1;
// The template of file path.
// e.g. assets/{timestamp}_{filename}
string filepath_template = 2;
// The max upload size in megabytes.
int64 upload_size_limit_mb = 3;
enum StorageType {
STORAGE_TYPE_UNSPECIFIED = 0;
// DATABASE stores attachment content in the application database.
DATABASE = 1;
// LOCAL stores attachment content on the local filesystem.
LOCAL = 2;
// S3 stores attachment content in an S3-compatible object store.
S3 = 3;
}
// S3 configuration for cloud storage backend.
// Storage is a configured attachment storage instance.
message Storage {
// id is the stable identifier referenced by attachments.
string id = 1;
// name is the human-readable storage name.
string name = 2;
StorageType type = 3;
oneof config {
S3Config s3_config = 10;
}
// S3 configuration for an S3-compatible object store.
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
message S3Config {
string access_key_id = 1;
@ -189,8 +191,48 @@ message InstanceSetting {
// certificate; it removes protection against man-in-the-middle attacks.
bool insecure_skip_tls_verify = 7;
}
// The S3 config.
}
// Storage configuration settings for instance attachments.
message StorageSetting {
// Legacy storage type enum retained for compatibility with existing clients.
enum StorageType {
STORAGE_TYPE_UNSPECIFIED = 0;
// DATABASE is the database storage type.
DATABASE = 1;
// LOCAL is the local storage type.
LOCAL = 2;
// S3 is the S3 storage type.
S3 = 3;
}
// Legacy compatibility field. New clients use default_storage_id.
StorageType storage_type = 1;
// The template of file path.
// e.g. assets/{timestamp}_{filename}
string filepath_template = 2;
// The max upload size in megabytes.
int64 upload_size_limit_mb = 3;
// Legacy S3 configuration retained for compatibility with existing clients.
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
message S3Config {
string access_key_id = 1;
string access_key_secret = 2 [(google.api.field_behavior) = INPUT_ONLY];
string endpoint = 3;
string region = 4;
string bucket = 5;
bool use_path_style = 6;
// insecure_skip_tls_verify disables TLS certificate verification when connecting
// to the S3 endpoint. Only enable this for trusted endpoints that use a self-signed
// certificate; it removes protection against man-in-the-middle attacks.
bool insecure_skip_tls_verify = 7;
}
// Legacy compatibility field. New clients use storages.
S3Config s3_config = 4;
// Configured storage instances, including inactive instances referenced by attachments.
repeated Storage storages = 5;
// Storage used for new attachments.
string default_storage_id = 6;
}
// Memo-related instance settings and policies.

View file

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.11
// protoc-gen-go v1.36.12
// protoc (unknown)
// source: api/v1/instance_service.proto
@ -94,6 +94,61 @@ func (InstanceSetting_Key) EnumDescriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 0}
}
type InstanceSetting_StorageType int32
const (
InstanceSetting_STORAGE_TYPE_UNSPECIFIED InstanceSetting_StorageType = 0
// DATABASE stores attachment content in the application database.
InstanceSetting_DATABASE InstanceSetting_StorageType = 1
// LOCAL stores attachment content on the local filesystem.
InstanceSetting_LOCAL InstanceSetting_StorageType = 2
// S3 stores attachment content in an S3-compatible object store.
InstanceSetting_S3 InstanceSetting_StorageType = 3
)
// Enum value maps for InstanceSetting_StorageType.
var (
InstanceSetting_StorageType_name = map[int32]string{
0: "STORAGE_TYPE_UNSPECIFIED",
1: "DATABASE",
2: "LOCAL",
3: "S3",
}
InstanceSetting_StorageType_value = map[string]int32{
"STORAGE_TYPE_UNSPECIFIED": 0,
"DATABASE": 1,
"LOCAL": 2,
"S3": 3,
}
)
func (x InstanceSetting_StorageType) Enum() *InstanceSetting_StorageType {
p := new(InstanceSetting_StorageType)
*p = x
return p
}
func (x InstanceSetting_StorageType) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (InstanceSetting_StorageType) Descriptor() protoreflect.EnumDescriptor {
return file_api_v1_instance_service_proto_enumTypes[1].Descriptor()
}
func (InstanceSetting_StorageType) Type() protoreflect.EnumType {
return &file_api_v1_instance_service_proto_enumTypes[1]
}
func (x InstanceSetting_StorageType) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Use InstanceSetting_StorageType.Descriptor instead.
func (InstanceSetting_StorageType) EnumDescriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1}
}
// AIProviderType is the provider implementation type.
type InstanceSetting_AIProviderType int32
@ -128,11 +183,11 @@ func (x InstanceSetting_AIProviderType) String() string {
}
func (InstanceSetting_AIProviderType) Descriptor() protoreflect.EnumDescriptor {
return file_api_v1_instance_service_proto_enumTypes[1].Descriptor()
return file_api_v1_instance_service_proto_enumTypes[2].Descriptor()
}
func (InstanceSetting_AIProviderType) Type() protoreflect.EnumType {
return &file_api_v1_instance_service_proto_enumTypes[1]
return &file_api_v1_instance_service_proto_enumTypes[2]
}
func (x InstanceSetting_AIProviderType) Number() protoreflect.EnumNumber {
@ -141,10 +196,10 @@ func (x InstanceSetting_AIProviderType) Number() protoreflect.EnumNumber {
// Deprecated: Use InstanceSetting_AIProviderType.Descriptor instead.
func (InstanceSetting_AIProviderType) EnumDescriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 2}
}
// Storage type enumeration for different storage backends.
// Legacy storage type enum retained for compatibility with existing clients.
type InstanceSetting_StorageSetting_StorageType int32
const (
@ -184,11 +239,11 @@ func (x InstanceSetting_StorageSetting_StorageType) String() string {
}
func (InstanceSetting_StorageSetting_StorageType) Descriptor() protoreflect.EnumDescriptor {
return file_api_v1_instance_service_proto_enumTypes[2].Descriptor()
return file_api_v1_instance_service_proto_enumTypes[3].Descriptor()
}
func (InstanceSetting_StorageSetting_StorageType) Type() protoreflect.EnumType {
return &file_api_v1_instance_service_proto_enumTypes[2]
return &file_api_v1_instance_service_proto_enumTypes[3]
}
func (x InstanceSetting_StorageSetting_StorageType) Number() protoreflect.EnumNumber {
@ -197,7 +252,7 @@ func (x InstanceSetting_StorageSetting_StorageType) Number() protoreflect.EnumNu
// Deprecated: Use InstanceSetting_StorageSetting_StorageType.Descriptor instead.
func (InstanceSetting_StorageSetting_StorageType) EnumDescriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1, 0}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 2, 0}
}
// Instance profile message containing basic instance information.
@ -951,25 +1006,122 @@ func (x *InstanceSetting_GeneralSetting) GetDisallowChangeNickname() bool {
return false
}
// Storage is a configured attachment storage instance.
type InstanceSetting_Storage struct {
state protoimpl.MessageState `protogen:"open.v1"`
// id is the stable identifier referenced by attachments.
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// name is the human-readable storage name.
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Type InstanceSetting_StorageType `protobuf:"varint,3,opt,name=type,proto3,enum=memos.api.v1.InstanceSetting_StorageType" json:"type,omitempty"`
// Types that are valid to be assigned to Config:
//
// *InstanceSetting_Storage_S3Config_
Config isInstanceSetting_Storage_Config `protobuf_oneof:"config"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *InstanceSetting_Storage) Reset() {
*x = InstanceSetting_Storage{}
mi := &file_api_v1_instance_service_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *InstanceSetting_Storage) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*InstanceSetting_Storage) ProtoMessage() {}
func (x *InstanceSetting_Storage) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[11]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use InstanceSetting_Storage.ProtoReflect.Descriptor instead.
func (*InstanceSetting_Storage) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1}
}
func (x *InstanceSetting_Storage) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *InstanceSetting_Storage) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *InstanceSetting_Storage) GetType() InstanceSetting_StorageType {
if x != nil {
return x.Type
}
return InstanceSetting_STORAGE_TYPE_UNSPECIFIED
}
func (x *InstanceSetting_Storage) GetConfig() isInstanceSetting_Storage_Config {
if x != nil {
return x.Config
}
return nil
}
func (x *InstanceSetting_Storage) GetS3Config() *InstanceSetting_Storage_S3Config {
if x != nil {
if x, ok := x.Config.(*InstanceSetting_Storage_S3Config_); ok {
return x.S3Config
}
}
return nil
}
type isInstanceSetting_Storage_Config interface {
isInstanceSetting_Storage_Config()
}
type InstanceSetting_Storage_S3Config_ struct {
S3Config *InstanceSetting_Storage_S3Config `protobuf:"bytes,10,opt,name=s3_config,json=s3Config,proto3,oneof"`
}
func (*InstanceSetting_Storage_S3Config_) isInstanceSetting_Storage_Config() {}
// Storage configuration settings for instance attachments.
type InstanceSetting_StorageSetting struct {
state protoimpl.MessageState `protogen:"open.v1"`
// storage_type is the storage type.
// Legacy compatibility field. New clients use default_storage_id.
StorageType InstanceSetting_StorageSetting_StorageType `protobuf:"varint,1,opt,name=storage_type,json=storageType,proto3,enum=memos.api.v1.InstanceSetting_StorageSetting_StorageType" json:"storage_type,omitempty"`
// The template of file path.
// e.g. assets/{timestamp}_{filename}
FilepathTemplate string `protobuf:"bytes,2,opt,name=filepath_template,json=filepathTemplate,proto3" json:"filepath_template,omitempty"`
// The max upload size in megabytes.
UploadSizeLimitMb int64 `protobuf:"varint,3,opt,name=upload_size_limit_mb,json=uploadSizeLimitMb,proto3" json:"upload_size_limit_mb,omitempty"`
// The S3 config.
S3Config *InstanceSetting_StorageSetting_S3Config `protobuf:"bytes,4,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
// Legacy compatibility field. New clients use storages.
S3Config *InstanceSetting_StorageSetting_S3Config `protobuf:"bytes,4,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
// Configured storage instances, including inactive instances referenced by attachments.
Storages []*InstanceSetting_Storage `protobuf:"bytes,5,rep,name=storages,proto3" json:"storages,omitempty"`
// Storage used for new attachments.
DefaultStorageId string `protobuf:"bytes,6,opt,name=default_storage_id,json=defaultStorageId,proto3" json:"default_storage_id,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *InstanceSetting_StorageSetting) Reset() {
*x = InstanceSetting_StorageSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[11]
mi := &file_api_v1_instance_service_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -981,7 +1133,7 @@ func (x *InstanceSetting_StorageSetting) String() string {
func (*InstanceSetting_StorageSetting) ProtoMessage() {}
func (x *InstanceSetting_StorageSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[11]
mi := &file_api_v1_instance_service_proto_msgTypes[12]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -994,7 +1146,7 @@ func (x *InstanceSetting_StorageSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceSetting_StorageSetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_StorageSetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 2}
}
func (x *InstanceSetting_StorageSetting) GetStorageType() InstanceSetting_StorageSetting_StorageType {
@ -1025,6 +1177,20 @@ func (x *InstanceSetting_StorageSetting) GetS3Config() *InstanceSetting_StorageS
return nil
}
func (x *InstanceSetting_StorageSetting) GetStorages() []*InstanceSetting_Storage {
if x != nil {
return x.Storages
}
return nil
}
func (x *InstanceSetting_StorageSetting) GetDefaultStorageId() string {
if x != nil {
return x.DefaultStorageId
}
return ""
}
// Memo-related instance settings and policies.
type InstanceSetting_MemoRelatedSetting struct {
state protoimpl.MessageState `protogen:"open.v1"`
@ -1040,7 +1206,7 @@ type InstanceSetting_MemoRelatedSetting struct {
func (x *InstanceSetting_MemoRelatedSetting) Reset() {
*x = InstanceSetting_MemoRelatedSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[12]
mi := &file_api_v1_instance_service_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1052,7 +1218,7 @@ func (x *InstanceSetting_MemoRelatedSetting) String() string {
func (*InstanceSetting_MemoRelatedSetting) ProtoMessage() {}
func (x *InstanceSetting_MemoRelatedSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[12]
mi := &file_api_v1_instance_service_proto_msgTypes[13]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1065,7 +1231,7 @@ func (x *InstanceSetting_MemoRelatedSetting) ProtoReflect() protoreflect.Message
// Deprecated: Use InstanceSetting_MemoRelatedSetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_MemoRelatedSetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 2}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 3}
}
func (x *InstanceSetting_MemoRelatedSetting) GetContentLengthLimit() int32 {
@ -1103,7 +1269,7 @@ type InstanceSetting_TagMetadata struct {
func (x *InstanceSetting_TagMetadata) Reset() {
*x = InstanceSetting_TagMetadata{}
mi := &file_api_v1_instance_service_proto_msgTypes[13]
mi := &file_api_v1_instance_service_proto_msgTypes[14]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1115,7 +1281,7 @@ func (x *InstanceSetting_TagMetadata) String() string {
func (*InstanceSetting_TagMetadata) ProtoMessage() {}
func (x *InstanceSetting_TagMetadata) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[13]
mi := &file_api_v1_instance_service_proto_msgTypes[14]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1128,7 +1294,7 @@ func (x *InstanceSetting_TagMetadata) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceSetting_TagMetadata.ProtoReflect.Descriptor instead.
func (*InstanceSetting_TagMetadata) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 3}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 4}
}
func (x *InstanceSetting_TagMetadata) GetBackgroundColor() *color.Color {
@ -1161,7 +1327,7 @@ type InstanceSetting_TagsSetting struct {
func (x *InstanceSetting_TagsSetting) Reset() {
*x = InstanceSetting_TagsSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[14]
mi := &file_api_v1_instance_service_proto_msgTypes[15]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1173,7 +1339,7 @@ func (x *InstanceSetting_TagsSetting) String() string {
func (*InstanceSetting_TagsSetting) ProtoMessage() {}
func (x *InstanceSetting_TagsSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[14]
mi := &file_api_v1_instance_service_proto_msgTypes[15]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1186,7 +1352,7 @@ func (x *InstanceSetting_TagsSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceSetting_TagsSetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_TagsSetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 4}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 5}
}
func (x *InstanceSetting_TagsSetting) GetTags() map[string]*InstanceSetting_TagMetadata {
@ -1206,7 +1372,7 @@ type InstanceSetting_NotificationSetting struct {
func (x *InstanceSetting_NotificationSetting) Reset() {
*x = InstanceSetting_NotificationSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[15]
mi := &file_api_v1_instance_service_proto_msgTypes[16]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1218,7 +1384,7 @@ func (x *InstanceSetting_NotificationSetting) String() string {
func (*InstanceSetting_NotificationSetting) ProtoMessage() {}
func (x *InstanceSetting_NotificationSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[15]
mi := &file_api_v1_instance_service_proto_msgTypes[16]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1231,7 +1397,7 @@ func (x *InstanceSetting_NotificationSetting) ProtoReflect() protoreflect.Messag
// Deprecated: Use InstanceSetting_NotificationSetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_NotificationSetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 5}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 6}
}
func (x *InstanceSetting_NotificationSetting) GetEmail() *InstanceSetting_NotificationSetting_EmailSetting {
@ -1255,7 +1421,7 @@ type InstanceSetting_AISetting struct {
func (x *InstanceSetting_AISetting) Reset() {
*x = InstanceSetting_AISetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[16]
mi := &file_api_v1_instance_service_proto_msgTypes[17]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1267,7 +1433,7 @@ func (x *InstanceSetting_AISetting) String() string {
func (*InstanceSetting_AISetting) ProtoMessage() {}
func (x *InstanceSetting_AISetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[16]
mi := &file_api_v1_instance_service_proto_msgTypes[17]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1280,7 +1446,7 @@ func (x *InstanceSetting_AISetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceSetting_AISetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_AISetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 6}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 7}
}
func (x *InstanceSetting_AISetting) GetProviders() []*InstanceSetting_AIProviderConfig {
@ -1316,7 +1482,7 @@ type InstanceSetting_AIProviderConfig struct {
func (x *InstanceSetting_AIProviderConfig) Reset() {
*x = InstanceSetting_AIProviderConfig{}
mi := &file_api_v1_instance_service_proto_msgTypes[17]
mi := &file_api_v1_instance_service_proto_msgTypes[18]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1328,7 +1494,7 @@ func (x *InstanceSetting_AIProviderConfig) String() string {
func (*InstanceSetting_AIProviderConfig) ProtoMessage() {}
func (x *InstanceSetting_AIProviderConfig) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[17]
mi := &file_api_v1_instance_service_proto_msgTypes[18]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1341,7 +1507,7 @@ func (x *InstanceSetting_AIProviderConfig) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceSetting_AIProviderConfig.ProtoReflect.Descriptor instead.
func (*InstanceSetting_AIProviderConfig) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 7}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 8}
}
func (x *InstanceSetting_AIProviderConfig) GetId() string {
@ -1414,7 +1580,7 @@ type InstanceSetting_TranscriptionConfig struct {
func (x *InstanceSetting_TranscriptionConfig) Reset() {
*x = InstanceSetting_TranscriptionConfig{}
mi := &file_api_v1_instance_service_proto_msgTypes[18]
mi := &file_api_v1_instance_service_proto_msgTypes[19]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1426,7 +1592,7 @@ func (x *InstanceSetting_TranscriptionConfig) String() string {
func (*InstanceSetting_TranscriptionConfig) ProtoMessage() {}
func (x *InstanceSetting_TranscriptionConfig) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[18]
mi := &file_api_v1_instance_service_proto_msgTypes[19]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1439,7 +1605,7 @@ func (x *InstanceSetting_TranscriptionConfig) ProtoReflect() protoreflect.Messag
// Deprecated: Use InstanceSetting_TranscriptionConfig.ProtoReflect.Descriptor instead.
func (*InstanceSetting_TranscriptionConfig) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 8}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 9}
}
func (x *InstanceSetting_TranscriptionConfig) GetProviderId() string {
@ -1482,7 +1648,7 @@ type InstanceSetting_GeneralSetting_CustomProfile struct {
func (x *InstanceSetting_GeneralSetting_CustomProfile) Reset() {
*x = InstanceSetting_GeneralSetting_CustomProfile{}
mi := &file_api_v1_instance_service_proto_msgTypes[19]
mi := &file_api_v1_instance_service_proto_msgTypes[20]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1494,7 +1660,7 @@ func (x *InstanceSetting_GeneralSetting_CustomProfile) String() string {
func (*InstanceSetting_GeneralSetting_CustomProfile) ProtoMessage() {}
func (x *InstanceSetting_GeneralSetting_CustomProfile) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[19]
mi := &file_api_v1_instance_service_proto_msgTypes[20]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1531,7 +1697,104 @@ func (x *InstanceSetting_GeneralSetting_CustomProfile) GetLogoUrl() string {
return ""
}
// S3 configuration for cloud storage backend.
// S3 configuration for an S3-compatible object store.
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
type InstanceSetting_Storage_S3Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
AccessKeyId string `protobuf:"bytes,1,opt,name=access_key_id,json=accessKeyId,proto3" json:"access_key_id,omitempty"`
AccessKeySecret string `protobuf:"bytes,2,opt,name=access_key_secret,json=accessKeySecret,proto3" json:"access_key_secret,omitempty"`
Endpoint string `protobuf:"bytes,3,opt,name=endpoint,proto3" json:"endpoint,omitempty"`
Region string `protobuf:"bytes,4,opt,name=region,proto3" json:"region,omitempty"`
Bucket string `protobuf:"bytes,5,opt,name=bucket,proto3" json:"bucket,omitempty"`
UsePathStyle bool `protobuf:"varint,6,opt,name=use_path_style,json=usePathStyle,proto3" json:"use_path_style,omitempty"`
// insecure_skip_tls_verify disables TLS certificate verification when connecting
// to the S3 endpoint. Only enable this for trusted endpoints that use a self-signed
// certificate; it removes protection against man-in-the-middle attacks.
InsecureSkipTlsVerify bool `protobuf:"varint,7,opt,name=insecure_skip_tls_verify,json=insecureSkipTlsVerify,proto3" json:"insecure_skip_tls_verify,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *InstanceSetting_Storage_S3Config) Reset() {
*x = InstanceSetting_Storage_S3Config{}
mi := &file_api_v1_instance_service_proto_msgTypes[21]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *InstanceSetting_Storage_S3Config) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*InstanceSetting_Storage_S3Config) ProtoMessage() {}
func (x *InstanceSetting_Storage_S3Config) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[21]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use InstanceSetting_Storage_S3Config.ProtoReflect.Descriptor instead.
func (*InstanceSetting_Storage_S3Config) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1, 0}
}
func (x *InstanceSetting_Storage_S3Config) GetAccessKeyId() string {
if x != nil {
return x.AccessKeyId
}
return ""
}
func (x *InstanceSetting_Storage_S3Config) GetAccessKeySecret() string {
if x != nil {
return x.AccessKeySecret
}
return ""
}
func (x *InstanceSetting_Storage_S3Config) GetEndpoint() string {
if x != nil {
return x.Endpoint
}
return ""
}
func (x *InstanceSetting_Storage_S3Config) GetRegion() string {
if x != nil {
return x.Region
}
return ""
}
func (x *InstanceSetting_Storage_S3Config) GetBucket() string {
if x != nil {
return x.Bucket
}
return ""
}
func (x *InstanceSetting_Storage_S3Config) GetUsePathStyle() bool {
if x != nil {
return x.UsePathStyle
}
return false
}
func (x *InstanceSetting_Storage_S3Config) GetInsecureSkipTlsVerify() bool {
if x != nil {
return x.InsecureSkipTlsVerify
}
return false
}
// Legacy S3 configuration retained for compatibility with existing clients.
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
type InstanceSetting_StorageSetting_S3Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
@ -1551,7 +1814,7 @@ type InstanceSetting_StorageSetting_S3Config struct {
func (x *InstanceSetting_StorageSetting_S3Config) Reset() {
*x = InstanceSetting_StorageSetting_S3Config{}
mi := &file_api_v1_instance_service_proto_msgTypes[20]
mi := &file_api_v1_instance_service_proto_msgTypes[22]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1563,7 +1826,7 @@ func (x *InstanceSetting_StorageSetting_S3Config) String() string {
func (*InstanceSetting_StorageSetting_S3Config) ProtoMessage() {}
func (x *InstanceSetting_StorageSetting_S3Config) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[20]
mi := &file_api_v1_instance_service_proto_msgTypes[22]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1576,7 +1839,7 @@ func (x *InstanceSetting_StorageSetting_S3Config) ProtoReflect() protoreflect.Me
// Deprecated: Use InstanceSetting_StorageSetting_S3Config.ProtoReflect.Descriptor instead.
func (*InstanceSetting_StorageSetting_S3Config) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 1, 0}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 2, 0}
}
func (x *InstanceSetting_StorageSetting_S3Config) GetAccessKeyId() string {
@ -1647,7 +1910,7 @@ type InstanceSetting_NotificationSetting_EmailSetting struct {
func (x *InstanceSetting_NotificationSetting_EmailSetting) Reset() {
*x = InstanceSetting_NotificationSetting_EmailSetting{}
mi := &file_api_v1_instance_service_proto_msgTypes[22]
mi := &file_api_v1_instance_service_proto_msgTypes[24]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1659,7 +1922,7 @@ func (x *InstanceSetting_NotificationSetting_EmailSetting) String() string {
func (*InstanceSetting_NotificationSetting_EmailSetting) ProtoMessage() {}
func (x *InstanceSetting_NotificationSetting_EmailSetting) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[22]
mi := &file_api_v1_instance_service_proto_msgTypes[24]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1672,7 +1935,7 @@ func (x *InstanceSetting_NotificationSetting_EmailSetting) ProtoReflect() protor
// Deprecated: Use InstanceSetting_NotificationSetting_EmailSetting.ProtoReflect.Descriptor instead.
func (*InstanceSetting_NotificationSetting_EmailSetting) Descriptor() ([]byte, []int) {
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 5, 0}
return file_api_v1_instance_service_proto_rawDescGZIP(), []int{2, 6, 0}
}
func (x *InstanceSetting_NotificationSetting_EmailSetting) GetEnabled() bool {
@ -1758,7 +2021,7 @@ type InstanceStats_DatabaseStats struct {
func (x *InstanceStats_DatabaseStats) Reset() {
*x = InstanceStats_DatabaseStats{}
mi := &file_api_v1_instance_service_proto_msgTypes[23]
mi := &file_api_v1_instance_service_proto_msgTypes[25]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1770,7 +2033,7 @@ func (x *InstanceStats_DatabaseStats) String() string {
func (*InstanceStats_DatabaseStats) ProtoMessage() {}
func (x *InstanceStats_DatabaseStats) ProtoReflect() protoreflect.Message {
mi := &file_api_v1_instance_service_proto_msgTypes[23]
mi := &file_api_v1_instance_service_proto_msgTypes[25]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1813,7 +2076,7 @@ const file_api_v1_instance_service_proto_rawDesc = "" +
"\x06commit\x18\b \x01(\tR\x06commit\x12\x1f\n" +
"\vneeds_setup\x18\t \x01(\bR\n" +
"needsSetup\"\x1b\n" +
"\x19GetInstanceProfileRequest\"\x86\x1c\n" +
"\x19GetInstanceProfileRequest\"\x9a!\n" +
"\x0fInstanceSetting\x12\x17\n" +
"\x04name\x18\x01 \x01(\tB\x03\xe0A\bR\x04name\x12W\n" +
"\x0fgeneral_setting\x18\x02 \x01(\v2,.memos.api.v1.InstanceSetting.GeneralSettingH\x00R\x0egeneralSetting\x12W\n" +
@ -1835,12 +2098,29 @@ const file_api_v1_instance_service_proto_rawDesc = "" +
"\rCustomProfile\x12\x14\n" +
"\x05title\x18\x01 \x01(\tR\x05title\x12 \n" +
"\vdescription\x18\x02 \x01(\tR\vdescription\x12\x19\n" +
"\blogo_url\x18\x03 \x01(\tR\alogoUrl\x1a\xfa\x04\n" +
"\blogo_url\x18\x03 \x01(\tR\alogoUrl\x1a\xd2\x03\n" +
"\aStorage\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12=\n" +
"\x04type\x18\x03 \x01(\x0e2).memos.api.v1.InstanceSetting.StorageTypeR\x04type\x12M\n" +
"\ts3_config\x18\n" +
" \x01(\v2..memos.api.v1.InstanceSetting.Storage.S3ConfigH\x00R\bs3Config\x1a\x8a\x02\n" +
"\bS3Config\x12\"\n" +
"\raccess_key_id\x18\x01 \x01(\tR\vaccessKeyId\x12/\n" +
"\x11access_key_secret\x18\x02 \x01(\tB\x03\xe0A\x04R\x0faccessKeySecret\x12\x1a\n" +
"\bendpoint\x18\x03 \x01(\tR\bendpoint\x12\x16\n" +
"\x06region\x18\x04 \x01(\tR\x06region\x12\x16\n" +
"\x06bucket\x18\x05 \x01(\tR\x06bucket\x12$\n" +
"\x0euse_path_style\x18\x06 \x01(\bR\fusePathStyle\x127\n" +
"\x18insecure_skip_tls_verify\x18\a \x01(\bR\x15insecureSkipTlsVerifyB\b\n" +
"\x06config\x1a\xeb\x05\n" +
"\x0eStorageSetting\x12[\n" +
"\fstorage_type\x18\x01 \x01(\x0e28.memos.api.v1.InstanceSetting.StorageSetting.StorageTypeR\vstorageType\x12+\n" +
"\x11filepath_template\x18\x02 \x01(\tR\x10filepathTemplate\x12/\n" +
"\x14upload_size_limit_mb\x18\x03 \x01(\x03R\x11uploadSizeLimitMb\x12R\n" +
"\ts3_config\x18\x04 \x01(\v25.memos.api.v1.InstanceSetting.StorageSetting.S3ConfigR\bs3Config\x1a\x8a\x02\n" +
"\ts3_config\x18\x04 \x01(\v25.memos.api.v1.InstanceSetting.StorageSetting.S3ConfigR\bs3Config\x12A\n" +
"\bstorages\x18\x05 \x03(\v2%.memos.api.v1.InstanceSetting.StorageR\bstorages\x12,\n" +
"\x12default_storage_id\x18\x06 \x01(\tR\x10defaultStorageId\x1a\x8a\x02\n" +
"\bS3Config\x12\"\n" +
"\raccess_key_id\x18\x01 \x01(\tR\vaccessKeyId\x12/\n" +
"\x11access_key_secret\x18\x02 \x01(\tB\x03\xe0A\x04R\x0faccessKeySecret\x12\x1a\n" +
@ -1906,7 +2186,12 @@ const file_api_v1_instance_service_proto_rawDesc = "" +
"\fMEMO_RELATED\x10\x03\x12\b\n" +
"\x04TAGS\x10\x04\x12\x10\n" +
"\fNOTIFICATION\x10\x05\x12\x06\n" +
"\x02AI\x10\x06\"J\n" +
"\x02AI\x10\x06\"L\n" +
"\vStorageType\x12\x1c\n" +
"\x18STORAGE_TYPE_UNSPECIFIED\x10\x00\x12\f\n" +
"\bDATABASE\x10\x01\x12\t\n" +
"\x05LOCAL\x10\x02\x12\x06\n" +
"\x02S3\x10\x03\"J\n" +
"\x0eAIProviderType\x12 \n" +
"\x1cAI_PROVIDER_TYPE_UNSPECIFIED\x10\x00\x12\n" +
"\n" +
@ -1960,83 +2245,89 @@ func file_api_v1_instance_service_proto_rawDescGZIP() []byte {
return file_api_v1_instance_service_proto_rawDescData
}
var file_api_v1_instance_service_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
var file_api_v1_instance_service_proto_msgTypes = make([]protoimpl.MessageInfo, 24)
var file_api_v1_instance_service_proto_enumTypes = make([]protoimpl.EnumInfo, 4)
var file_api_v1_instance_service_proto_msgTypes = make([]protoimpl.MessageInfo, 26)
var file_api_v1_instance_service_proto_goTypes = []any{
(InstanceSetting_Key)(0), // 0: memos.api.v1.InstanceSetting.Key
(InstanceSetting_AIProviderType)(0), // 1: memos.api.v1.InstanceSetting.AIProviderType
(InstanceSetting_StorageSetting_StorageType)(0), // 2: memos.api.v1.InstanceSetting.StorageSetting.StorageType
(*InstanceProfile)(nil), // 3: memos.api.v1.InstanceProfile
(*GetInstanceProfileRequest)(nil), // 4: memos.api.v1.GetInstanceProfileRequest
(*InstanceSetting)(nil), // 5: memos.api.v1.InstanceSetting
(*GetInstanceSettingRequest)(nil), // 6: memos.api.v1.GetInstanceSettingRequest
(*BatchGetInstanceSettingsRequest)(nil), // 7: memos.api.v1.BatchGetInstanceSettingsRequest
(*BatchGetInstanceSettingsResponse)(nil), // 8: memos.api.v1.BatchGetInstanceSettingsResponse
(*UpdateInstanceSettingRequest)(nil), // 9: memos.api.v1.UpdateInstanceSettingRequest
(*TestInstanceEmailSettingRequest)(nil), // 10: memos.api.v1.TestInstanceEmailSettingRequest
(*GetInstanceStatsRequest)(nil), // 11: memos.api.v1.GetInstanceStatsRequest
(*InstanceStats)(nil), // 12: memos.api.v1.InstanceStats
(*InstanceSetting_GeneralSetting)(nil), // 13: memos.api.v1.InstanceSetting.GeneralSetting
(*InstanceSetting_StorageSetting)(nil), // 14: memos.api.v1.InstanceSetting.StorageSetting
(*InstanceSetting_MemoRelatedSetting)(nil), // 15: memos.api.v1.InstanceSetting.MemoRelatedSetting
(*InstanceSetting_TagMetadata)(nil), // 16: memos.api.v1.InstanceSetting.TagMetadata
(*InstanceSetting_TagsSetting)(nil), // 17: memos.api.v1.InstanceSetting.TagsSetting
(*InstanceSetting_NotificationSetting)(nil), // 18: memos.api.v1.InstanceSetting.NotificationSetting
(*InstanceSetting_AISetting)(nil), // 19: memos.api.v1.InstanceSetting.AISetting
(*InstanceSetting_AIProviderConfig)(nil), // 20: memos.api.v1.InstanceSetting.AIProviderConfig
(*InstanceSetting_TranscriptionConfig)(nil), // 21: memos.api.v1.InstanceSetting.TranscriptionConfig
(*InstanceSetting_GeneralSetting_CustomProfile)(nil), // 22: memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
(*InstanceSetting_StorageSetting_S3Config)(nil), // 23: memos.api.v1.InstanceSetting.StorageSetting.S3Config
nil, // 24: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
(*InstanceSetting_NotificationSetting_EmailSetting)(nil), // 25: memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
(*InstanceStats_DatabaseStats)(nil), // 26: memos.api.v1.InstanceStats.DatabaseStats
(*User)(nil), // 27: memos.api.v1.User
(*fieldmaskpb.FieldMask)(nil), // 28: google.protobuf.FieldMask
(*timestamppb.Timestamp)(nil), // 29: google.protobuf.Timestamp
(*color.Color)(nil), // 30: google.type.Color
(*emptypb.Empty)(nil), // 31: google.protobuf.Empty
(InstanceSetting_StorageType)(0), // 1: memos.api.v1.InstanceSetting.StorageType
(InstanceSetting_AIProviderType)(0), // 2: memos.api.v1.InstanceSetting.AIProviderType
(InstanceSetting_StorageSetting_StorageType)(0), // 3: memos.api.v1.InstanceSetting.StorageSetting.StorageType
(*InstanceProfile)(nil), // 4: memos.api.v1.InstanceProfile
(*GetInstanceProfileRequest)(nil), // 5: memos.api.v1.GetInstanceProfileRequest
(*InstanceSetting)(nil), // 6: memos.api.v1.InstanceSetting
(*GetInstanceSettingRequest)(nil), // 7: memos.api.v1.GetInstanceSettingRequest
(*BatchGetInstanceSettingsRequest)(nil), // 8: memos.api.v1.BatchGetInstanceSettingsRequest
(*BatchGetInstanceSettingsResponse)(nil), // 9: memos.api.v1.BatchGetInstanceSettingsResponse
(*UpdateInstanceSettingRequest)(nil), // 10: memos.api.v1.UpdateInstanceSettingRequest
(*TestInstanceEmailSettingRequest)(nil), // 11: memos.api.v1.TestInstanceEmailSettingRequest
(*GetInstanceStatsRequest)(nil), // 12: memos.api.v1.GetInstanceStatsRequest
(*InstanceStats)(nil), // 13: memos.api.v1.InstanceStats
(*InstanceSetting_GeneralSetting)(nil), // 14: memos.api.v1.InstanceSetting.GeneralSetting
(*InstanceSetting_Storage)(nil), // 15: memos.api.v1.InstanceSetting.Storage
(*InstanceSetting_StorageSetting)(nil), // 16: memos.api.v1.InstanceSetting.StorageSetting
(*InstanceSetting_MemoRelatedSetting)(nil), // 17: memos.api.v1.InstanceSetting.MemoRelatedSetting
(*InstanceSetting_TagMetadata)(nil), // 18: memos.api.v1.InstanceSetting.TagMetadata
(*InstanceSetting_TagsSetting)(nil), // 19: memos.api.v1.InstanceSetting.TagsSetting
(*InstanceSetting_NotificationSetting)(nil), // 20: memos.api.v1.InstanceSetting.NotificationSetting
(*InstanceSetting_AISetting)(nil), // 21: memos.api.v1.InstanceSetting.AISetting
(*InstanceSetting_AIProviderConfig)(nil), // 22: memos.api.v1.InstanceSetting.AIProviderConfig
(*InstanceSetting_TranscriptionConfig)(nil), // 23: memos.api.v1.InstanceSetting.TranscriptionConfig
(*InstanceSetting_GeneralSetting_CustomProfile)(nil), // 24: memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
(*InstanceSetting_Storage_S3Config)(nil), // 25: memos.api.v1.InstanceSetting.Storage.S3Config
(*InstanceSetting_StorageSetting_S3Config)(nil), // 26: memos.api.v1.InstanceSetting.StorageSetting.S3Config
nil, // 27: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
(*InstanceSetting_NotificationSetting_EmailSetting)(nil), // 28: memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
(*InstanceStats_DatabaseStats)(nil), // 29: memos.api.v1.InstanceStats.DatabaseStats
(*User)(nil), // 30: memos.api.v1.User
(*fieldmaskpb.FieldMask)(nil), // 31: google.protobuf.FieldMask
(*timestamppb.Timestamp)(nil), // 32: google.protobuf.Timestamp
(*color.Color)(nil), // 33: google.type.Color
(*emptypb.Empty)(nil), // 34: google.protobuf.Empty
}
var file_api_v1_instance_service_proto_depIdxs = []int32{
27, // 0: memos.api.v1.InstanceProfile.admin:type_name -> memos.api.v1.User
13, // 1: memos.api.v1.InstanceSetting.general_setting:type_name -> memos.api.v1.InstanceSetting.GeneralSetting
14, // 2: memos.api.v1.InstanceSetting.storage_setting:type_name -> memos.api.v1.InstanceSetting.StorageSetting
15, // 3: memos.api.v1.InstanceSetting.memo_related_setting:type_name -> memos.api.v1.InstanceSetting.MemoRelatedSetting
17, // 4: memos.api.v1.InstanceSetting.tags_setting:type_name -> memos.api.v1.InstanceSetting.TagsSetting
18, // 5: memos.api.v1.InstanceSetting.notification_setting:type_name -> memos.api.v1.InstanceSetting.NotificationSetting
19, // 6: memos.api.v1.InstanceSetting.ai_setting:type_name -> memos.api.v1.InstanceSetting.AISetting
5, // 7: memos.api.v1.BatchGetInstanceSettingsResponse.settings:type_name -> memos.api.v1.InstanceSetting
5, // 8: memos.api.v1.UpdateInstanceSettingRequest.setting:type_name -> memos.api.v1.InstanceSetting
28, // 9: memos.api.v1.UpdateInstanceSettingRequest.update_mask:type_name -> google.protobuf.FieldMask
25, // 10: memos.api.v1.TestInstanceEmailSettingRequest.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
26, // 11: memos.api.v1.InstanceStats.database:type_name -> memos.api.v1.InstanceStats.DatabaseStats
29, // 12: memos.api.v1.InstanceStats.generated_time:type_name -> google.protobuf.Timestamp
22, // 13: memos.api.v1.InstanceSetting.GeneralSetting.custom_profile:type_name -> memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
2, // 14: memos.api.v1.InstanceSetting.StorageSetting.storage_type:type_name -> memos.api.v1.InstanceSetting.StorageSetting.StorageType
23, // 15: memos.api.v1.InstanceSetting.StorageSetting.s3_config:type_name -> memos.api.v1.InstanceSetting.StorageSetting.S3Config
30, // 16: memos.api.v1.InstanceSetting.TagMetadata.background_color:type_name -> google.type.Color
24, // 17: memos.api.v1.InstanceSetting.TagsSetting.tags:type_name -> memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
25, // 18: memos.api.v1.InstanceSetting.NotificationSetting.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
20, // 19: memos.api.v1.InstanceSetting.AISetting.providers:type_name -> memos.api.v1.InstanceSetting.AIProviderConfig
21, // 20: memos.api.v1.InstanceSetting.AISetting.transcription:type_name -> memos.api.v1.InstanceSetting.TranscriptionConfig
1, // 21: memos.api.v1.InstanceSetting.AIProviderConfig.type:type_name -> memos.api.v1.InstanceSetting.AIProviderType
16, // 22: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry.value:type_name -> memos.api.v1.InstanceSetting.TagMetadata
4, // 23: memos.api.v1.InstanceService.GetInstanceProfile:input_type -> memos.api.v1.GetInstanceProfileRequest
6, // 24: memos.api.v1.InstanceService.GetInstanceSetting:input_type -> memos.api.v1.GetInstanceSettingRequest
7, // 25: memos.api.v1.InstanceService.BatchGetInstanceSettings:input_type -> memos.api.v1.BatchGetInstanceSettingsRequest
9, // 26: memos.api.v1.InstanceService.UpdateInstanceSetting:input_type -> memos.api.v1.UpdateInstanceSettingRequest
10, // 27: memos.api.v1.InstanceService.TestInstanceEmailSetting:input_type -> memos.api.v1.TestInstanceEmailSettingRequest
11, // 28: memos.api.v1.InstanceService.GetInstanceStats:input_type -> memos.api.v1.GetInstanceStatsRequest
3, // 29: memos.api.v1.InstanceService.GetInstanceProfile:output_type -> memos.api.v1.InstanceProfile
5, // 30: memos.api.v1.InstanceService.GetInstanceSetting:output_type -> memos.api.v1.InstanceSetting
8, // 31: memos.api.v1.InstanceService.BatchGetInstanceSettings:output_type -> memos.api.v1.BatchGetInstanceSettingsResponse
5, // 32: memos.api.v1.InstanceService.UpdateInstanceSetting:output_type -> memos.api.v1.InstanceSetting
31, // 33: memos.api.v1.InstanceService.TestInstanceEmailSetting:output_type -> google.protobuf.Empty
12, // 34: memos.api.v1.InstanceService.GetInstanceStats:output_type -> memos.api.v1.InstanceStats
29, // [29:35] is the sub-list for method output_type
23, // [23:29] is the sub-list for method input_type
23, // [23:23] is the sub-list for extension type_name
23, // [23:23] is the sub-list for extension extendee
0, // [0:23] is the sub-list for field type_name
30, // 0: memos.api.v1.InstanceProfile.admin:type_name -> memos.api.v1.User
14, // 1: memos.api.v1.InstanceSetting.general_setting:type_name -> memos.api.v1.InstanceSetting.GeneralSetting
16, // 2: memos.api.v1.InstanceSetting.storage_setting:type_name -> memos.api.v1.InstanceSetting.StorageSetting
17, // 3: memos.api.v1.InstanceSetting.memo_related_setting:type_name -> memos.api.v1.InstanceSetting.MemoRelatedSetting
19, // 4: memos.api.v1.InstanceSetting.tags_setting:type_name -> memos.api.v1.InstanceSetting.TagsSetting
20, // 5: memos.api.v1.InstanceSetting.notification_setting:type_name -> memos.api.v1.InstanceSetting.NotificationSetting
21, // 6: memos.api.v1.InstanceSetting.ai_setting:type_name -> memos.api.v1.InstanceSetting.AISetting
6, // 7: memos.api.v1.BatchGetInstanceSettingsResponse.settings:type_name -> memos.api.v1.InstanceSetting
6, // 8: memos.api.v1.UpdateInstanceSettingRequest.setting:type_name -> memos.api.v1.InstanceSetting
31, // 9: memos.api.v1.UpdateInstanceSettingRequest.update_mask:type_name -> google.protobuf.FieldMask
28, // 10: memos.api.v1.TestInstanceEmailSettingRequest.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
29, // 11: memos.api.v1.InstanceStats.database:type_name -> memos.api.v1.InstanceStats.DatabaseStats
32, // 12: memos.api.v1.InstanceStats.generated_time:type_name -> google.protobuf.Timestamp
24, // 13: memos.api.v1.InstanceSetting.GeneralSetting.custom_profile:type_name -> memos.api.v1.InstanceSetting.GeneralSetting.CustomProfile
1, // 14: memos.api.v1.InstanceSetting.Storage.type:type_name -> memos.api.v1.InstanceSetting.StorageType
25, // 15: memos.api.v1.InstanceSetting.Storage.s3_config:type_name -> memos.api.v1.InstanceSetting.Storage.S3Config
3, // 16: memos.api.v1.InstanceSetting.StorageSetting.storage_type:type_name -> memos.api.v1.InstanceSetting.StorageSetting.StorageType
26, // 17: memos.api.v1.InstanceSetting.StorageSetting.s3_config:type_name -> memos.api.v1.InstanceSetting.StorageSetting.S3Config
15, // 18: memos.api.v1.InstanceSetting.StorageSetting.storages:type_name -> memos.api.v1.InstanceSetting.Storage
33, // 19: memos.api.v1.InstanceSetting.TagMetadata.background_color:type_name -> google.type.Color
27, // 20: memos.api.v1.InstanceSetting.TagsSetting.tags:type_name -> memos.api.v1.InstanceSetting.TagsSetting.TagsEntry
28, // 21: memos.api.v1.InstanceSetting.NotificationSetting.email:type_name -> memos.api.v1.InstanceSetting.NotificationSetting.EmailSetting
22, // 22: memos.api.v1.InstanceSetting.AISetting.providers:type_name -> memos.api.v1.InstanceSetting.AIProviderConfig
23, // 23: memos.api.v1.InstanceSetting.AISetting.transcription:type_name -> memos.api.v1.InstanceSetting.TranscriptionConfig
2, // 24: memos.api.v1.InstanceSetting.AIProviderConfig.type:type_name -> memos.api.v1.InstanceSetting.AIProviderType
18, // 25: memos.api.v1.InstanceSetting.TagsSetting.TagsEntry.value:type_name -> memos.api.v1.InstanceSetting.TagMetadata
5, // 26: memos.api.v1.InstanceService.GetInstanceProfile:input_type -> memos.api.v1.GetInstanceProfileRequest
7, // 27: memos.api.v1.InstanceService.GetInstanceSetting:input_type -> memos.api.v1.GetInstanceSettingRequest
8, // 28: memos.api.v1.InstanceService.BatchGetInstanceSettings:input_type -> memos.api.v1.BatchGetInstanceSettingsRequest
10, // 29: memos.api.v1.InstanceService.UpdateInstanceSetting:input_type -> memos.api.v1.UpdateInstanceSettingRequest
11, // 30: memos.api.v1.InstanceService.TestInstanceEmailSetting:input_type -> memos.api.v1.TestInstanceEmailSettingRequest
12, // 31: memos.api.v1.InstanceService.GetInstanceStats:input_type -> memos.api.v1.GetInstanceStatsRequest
4, // 32: memos.api.v1.InstanceService.GetInstanceProfile:output_type -> memos.api.v1.InstanceProfile
6, // 33: memos.api.v1.InstanceService.GetInstanceSetting:output_type -> memos.api.v1.InstanceSetting
9, // 34: memos.api.v1.InstanceService.BatchGetInstanceSettings:output_type -> memos.api.v1.BatchGetInstanceSettingsResponse
6, // 35: memos.api.v1.InstanceService.UpdateInstanceSetting:output_type -> memos.api.v1.InstanceSetting
34, // 36: memos.api.v1.InstanceService.TestInstanceEmailSetting:output_type -> google.protobuf.Empty
13, // 37: memos.api.v1.InstanceService.GetInstanceStats:output_type -> memos.api.v1.InstanceStats
32, // [32:38] is the sub-list for method output_type
26, // [26:32] is the sub-list for method input_type
26, // [26:26] is the sub-list for extension type_name
26, // [26:26] is the sub-list for extension extendee
0, // [0:26] is the sub-list for field type_name
}
func init() { file_api_v1_instance_service_proto_init() }
@ -2053,13 +2344,16 @@ func file_api_v1_instance_service_proto_init() {
(*InstanceSetting_NotificationSetting_)(nil),
(*InstanceSetting_AiSetting)(nil),
}
file_api_v1_instance_service_proto_msgTypes[11].OneofWrappers = []any{
(*InstanceSetting_Storage_S3Config_)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_api_v1_instance_service_proto_rawDesc), len(file_api_v1_instance_service_proto_rawDesc)),
NumEnums: 3,
NumMessages: 24,
NumEnums: 4,
NumMessages: 26,
NumExtensions: 0,
NumServices: 1,
},

View file

@ -2911,6 +2911,26 @@ components:
email:
$ref: '#/components/schemas/NotificationSetting_EmailSetting'
description: Notification transport configuration.
InstanceSetting_Storage:
type: object
properties:
id:
type: string
description: id is the stable identifier referenced by attachments.
name:
type: string
description: name is the human-readable storage name.
type:
enum:
- STORAGE_TYPE_UNSPECIFIED
- DATABASE
- LOCAL
- S3
type: string
format: enum
s3Config:
$ref: '#/components/schemas/Storage_S3Config'
description: Storage is a configured attachment storage instance.
InstanceSetting_StorageSetting:
type: object
properties:
@ -2921,7 +2941,7 @@ components:
- LOCAL
- S3
type: string
description: storage_type is the storage type.
description: Legacy compatibility field. New clients use default_storage_id.
format: enum
filepathTemplate:
type: string
@ -2934,7 +2954,15 @@ components:
s3Config:
allOf:
- $ref: '#/components/schemas/StorageSetting_S3Config'
description: The S3 config.
description: Legacy compatibility field. New clients use storages.
storages:
type: array
items:
$ref: '#/components/schemas/InstanceSetting_Storage'
description: Configured storage instances, including inactive instances referenced by attachments.
defaultStorageId:
type: string
description: Storage used for new attachments.
description: Storage configuration settings for instance attachments.
InstanceSetting_TagMetadata:
type: object
@ -3751,7 +3779,32 @@ components:
to the S3 endpoint. Only enable this for trusted endpoints that use a self-signed
certificate; it removes protection against man-in-the-middle attacks.
description: |-
S3 configuration for cloud storage backend.
Legacy S3 configuration retained for compatibility with existing clients.
Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
Storage_S3Config:
type: object
properties:
accessKeyId:
type: string
accessKeySecret:
writeOnly: true
type: string
endpoint:
type: string
region:
type: string
bucket:
type: string
usePathStyle:
type: boolean
insecureSkipTlsVerify:
type: boolean
description: |-
insecure_skip_tls_verify disables TLS certificate verification when connecting
to the S3 endpoint. Only enable this for trusted endpoints that use a self-signed
certificate; it removes protection against man-in-the-middle attacks.
description: |-
S3 configuration for an S3-compatible object store.
Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
TestInstanceEmailSettingRequest:
type: object

View file

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.11
// protoc-gen-go v1.36.12
// protoc (unknown)
// source: store/attachment.proto
@ -707,15 +707,19 @@ type AttachmentPayload_S3Object_ struct {
func (*AttachmentPayload_S3Object_) isAttachmentPayload_Payload() {}
type AttachmentPayload_S3Object struct {
state protoimpl.MessageState `protogen:"open.v1"`
S3Config *StorageS3Config `protobuf:"bytes,1,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
state protoimpl.MessageState `protogen:"open.v1"`
// Legacy attachments embedded their complete S3 configuration.
// New attachments resolve storage through storage_id.
S3Config *StorageS3Config `protobuf:"bytes,1,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
// key is the S3 object key.
Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
// last_presigned_time is the last time the object was presigned.
// This is used to determine if the presigned URL is still valid.
LastPresignedTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=last_presigned_time,json=lastPresignedTime,proto3" json:"last_presigned_time,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
// storage_id identifies the configured storage containing this object.
StorageId string `protobuf:"bytes,4,opt,name=storage_id,json=storageId,proto3" json:"storage_id,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *AttachmentPayload_S3Object) Reset() {
@ -769,6 +773,13 @@ func (x *AttachmentPayload_S3Object) GetLastPresignedTime() *timestamppb.Timesta
return nil
}
func (x *AttachmentPayload_S3Object) GetStorageId() string {
if x != nil {
return x.StorageId
}
return ""
}
var File_store_attachment_proto protoreflect.FileDescriptor
const file_store_attachment_proto_rawDesc = "" +
@ -822,16 +833,18 @@ const file_store_attachment_proto_rawDesc = "" +
"\x10_altitude_meters\"T\n" +
"\rVideoMetadata\x12.\n" +
"\x10duration_seconds\x18\x01 \x01(\x01H\x00R\x0fdurationSeconds\x88\x01\x01B\x13\n" +
"\x11_duration_seconds\"\x8c\x03\n" +
"\x11_duration_seconds\"\xab\x03\n" +
"\x11AttachmentPayload\x12F\n" +
"\ts3_object\x18\x01 \x01(\v2'.memos.store.AttachmentPayload.S3ObjectH\x00R\bs3Object\x12;\n" +
"\fmotion_media\x18\n" +
" \x01(\v2\x18.memos.store.MotionMediaR\vmotionMedia\x12A\n" +
"\x0emedia_metadata\x18\v \x01(\v2\x1a.memos.store.MediaMetadataR\rmediaMetadata\x1a\xa3\x01\n" +
"\x0emedia_metadata\x18\v \x01(\v2\x1a.memos.store.MediaMetadataR\rmediaMetadata\x1a\xc2\x01\n" +
"\bS3Object\x129\n" +
"\ts3_config\x18\x01 \x01(\v2\x1c.memos.store.StorageS3ConfigR\bs3Config\x12\x10\n" +
"\x03key\x18\x02 \x01(\tR\x03key\x12J\n" +
"\x13last_presigned_time\x18\x03 \x01(\v2\x1a.google.protobuf.TimestampR\x11lastPresignedTimeB\t\n" +
"\x13last_presigned_time\x18\x03 \x01(\v2\x1a.google.protobuf.TimestampR\x11lastPresignedTime\x12\x1d\n" +
"\n" +
"storage_id\x18\x04 \x01(\tR\tstorageIdB\t\n" +
"\apayload*a\n" +
"\x15AttachmentStorageType\x12'\n" +
"#ATTACHMENT_STORAGE_TYPE_UNSPECIFIED\x10\x00\x12\t\n" +

View file

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.11
// protoc-gen-go v1.36.12
// protoc (unknown)
// source: store/instance_setting.proto
@ -93,6 +93,61 @@ func (InstanceSettingKey) EnumDescriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{0}
}
type StorageType int32
const (
StorageType_STORAGE_TYPE_UNSPECIFIED StorageType = 0
// DATABASE stores attachment content in the application database.
StorageType_STORAGE_TYPE_DATABASE StorageType = 1
// LOCAL stores attachment content on the local filesystem.
StorageType_STORAGE_TYPE_LOCAL StorageType = 2
// S3 stores attachment content in an S3-compatible object store.
StorageType_STORAGE_TYPE_S3 StorageType = 3
)
// Enum value maps for StorageType.
var (
StorageType_name = map[int32]string{
0: "STORAGE_TYPE_UNSPECIFIED",
1: "STORAGE_TYPE_DATABASE",
2: "STORAGE_TYPE_LOCAL",
3: "STORAGE_TYPE_S3",
}
StorageType_value = map[string]int32{
"STORAGE_TYPE_UNSPECIFIED": 0,
"STORAGE_TYPE_DATABASE": 1,
"STORAGE_TYPE_LOCAL": 2,
"STORAGE_TYPE_S3": 3,
}
)
func (x StorageType) Enum() *StorageType {
p := new(StorageType)
*p = x
return p
}
func (x StorageType) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (StorageType) Descriptor() protoreflect.EnumDescriptor {
return file_store_instance_setting_proto_enumTypes[1].Descriptor()
}
func (StorageType) Type() protoreflect.EnumType {
return &file_store_instance_setting_proto_enumTypes[1]
}
func (x StorageType) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Use StorageType.Descriptor instead.
func (StorageType) EnumDescriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{1}
}
type AIProviderType int32
const (
@ -126,11 +181,11 @@ func (x AIProviderType) String() string {
}
func (AIProviderType) Descriptor() protoreflect.EnumDescriptor {
return file_store_instance_setting_proto_enumTypes[1].Descriptor()
return file_store_instance_setting_proto_enumTypes[2].Descriptor()
}
func (AIProviderType) Type() protoreflect.EnumType {
return &file_store_instance_setting_proto_enumTypes[1]
return &file_store_instance_setting_proto_enumTypes[2]
}
func (x AIProviderType) Number() protoreflect.EnumNumber {
@ -139,9 +194,10 @@ func (x AIProviderType) Number() protoreflect.EnumNumber {
// Deprecated: Use AIProviderType.Descriptor instead.
func (AIProviderType) EnumDescriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{1}
return file_store_instance_setting_proto_rawDescGZIP(), []int{2}
}
// Legacy storage type enum retained for compatibility with existing settings.
type InstanceStorageSetting_StorageType int32
const (
@ -181,11 +237,11 @@ func (x InstanceStorageSetting_StorageType) String() string {
}
func (InstanceStorageSetting_StorageType) Descriptor() protoreflect.EnumDescriptor {
return file_store_instance_setting_proto_enumTypes[2].Descriptor()
return file_store_instance_setting_proto_enumTypes[3].Descriptor()
}
func (InstanceStorageSetting_StorageType) Type() protoreflect.EnumType {
return &file_store_instance_setting_proto_enumTypes[2]
return &file_store_instance_setting_proto_enumTypes[3]
}
func (x InstanceStorageSetting_StorageType) Number() protoreflect.EnumNumber {
@ -194,7 +250,7 @@ func (x InstanceStorageSetting_StorageType) Number() protoreflect.EnumNumber {
// Deprecated: Use InstanceStorageSetting_StorageType.Descriptor instead.
func (InstanceStorageSetting_StorageType) EnumDescriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{4, 0}
return file_store_instance_setting_proto_rawDescGZIP(), []int{5, 0}
}
type InstanceSetting struct {
@ -591,24 +647,122 @@ func (x *InstanceCustomProfile) GetLogoUrl() string {
return ""
}
// Storage is a configured attachment storage instance.
type Storage struct {
state protoimpl.MessageState `protogen:"open.v1"`
// id is the stable identifier referenced by attachments.
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// name is the human-readable storage name.
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Type StorageType `protobuf:"varint,3,opt,name=type,proto3,enum=memos.store.StorageType" json:"type,omitempty"`
// Types that are valid to be assigned to Config:
//
// *Storage_S3Config
Config isStorage_Config `protobuf_oneof:"config"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *Storage) Reset() {
*x = Storage{}
mi := &file_store_instance_setting_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *Storage) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Storage) ProtoMessage() {}
func (x *Storage) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[4]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Storage.ProtoReflect.Descriptor instead.
func (*Storage) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{4}
}
func (x *Storage) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *Storage) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Storage) GetType() StorageType {
if x != nil {
return x.Type
}
return StorageType_STORAGE_TYPE_UNSPECIFIED
}
func (x *Storage) GetConfig() isStorage_Config {
if x != nil {
return x.Config
}
return nil
}
func (x *Storage) GetS3Config() *StorageS3Config {
if x != nil {
if x, ok := x.Config.(*Storage_S3Config); ok {
return x.S3Config
}
}
return nil
}
type isStorage_Config interface {
isStorage_Config()
}
type Storage_S3Config struct {
S3Config *StorageS3Config `protobuf:"bytes,10,opt,name=s3_config,json=s3Config,proto3,oneof"`
}
func (*Storage_S3Config) isStorage_Config() {}
type InstanceStorageSetting struct {
state protoimpl.MessageState `protogen:"open.v1"`
// storage_type is the storage type.
// Legacy compatibility field. New code uses default_storage_id.
StorageType InstanceStorageSetting_StorageType `protobuf:"varint,1,opt,name=storage_type,json=storageType,proto3,enum=memos.store.InstanceStorageSetting_StorageType" json:"storage_type,omitempty"`
// The template of file path.
// e.g. assets/{timestamp}_{filename}
FilepathTemplate string `protobuf:"bytes,2,opt,name=filepath_template,json=filepathTemplate,proto3" json:"filepath_template,omitempty"`
// The max upload size in megabytes.
UploadSizeLimitMb int64 `protobuf:"varint,3,opt,name=upload_size_limit_mb,json=uploadSizeLimitMb,proto3" json:"upload_size_limit_mb,omitempty"`
// The S3 config.
S3Config *StorageS3Config `protobuf:"bytes,4,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
// Legacy compatibility field. New code uses storages.
S3Config *StorageS3Config `protobuf:"bytes,4,opt,name=s3_config,json=s3Config,proto3" json:"s3_config,omitempty"`
// storages contains configured storage instances, including inactive instances
// that may still be referenced by existing attachments.
Storages []*Storage `protobuf:"bytes,5,rep,name=storages,proto3" json:"storages,omitempty"`
// default_storage_id identifies the storage used for new attachments.
DefaultStorageId string `protobuf:"bytes,6,opt,name=default_storage_id,json=defaultStorageId,proto3" json:"default_storage_id,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *InstanceStorageSetting) Reset() {
*x = InstanceStorageSetting{}
mi := &file_store_instance_setting_proto_msgTypes[4]
mi := &file_store_instance_setting_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -620,7 +774,7 @@ func (x *InstanceStorageSetting) String() string {
func (*InstanceStorageSetting) ProtoMessage() {}
func (x *InstanceStorageSetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[4]
mi := &file_store_instance_setting_proto_msgTypes[5]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -633,7 +787,7 @@ func (x *InstanceStorageSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceStorageSetting.ProtoReflect.Descriptor instead.
func (*InstanceStorageSetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{4}
return file_store_instance_setting_proto_rawDescGZIP(), []int{5}
}
func (x *InstanceStorageSetting) GetStorageType() InstanceStorageSetting_StorageType {
@ -664,6 +818,20 @@ func (x *InstanceStorageSetting) GetS3Config() *StorageS3Config {
return nil
}
func (x *InstanceStorageSetting) GetStorages() []*Storage {
if x != nil {
return x.Storages
}
return nil
}
func (x *InstanceStorageSetting) GetDefaultStorageId() string {
if x != nil {
return x.DefaultStorageId
}
return ""
}
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/
type StorageS3Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
@ -683,7 +851,7 @@ type StorageS3Config struct {
func (x *StorageS3Config) Reset() {
*x = StorageS3Config{}
mi := &file_store_instance_setting_proto_msgTypes[5]
mi := &file_store_instance_setting_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -695,7 +863,7 @@ func (x *StorageS3Config) String() string {
func (*StorageS3Config) ProtoMessage() {}
func (x *StorageS3Config) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[5]
mi := &file_store_instance_setting_proto_msgTypes[6]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -708,7 +876,7 @@ func (x *StorageS3Config) ProtoReflect() protoreflect.Message {
// Deprecated: Use StorageS3Config.ProtoReflect.Descriptor instead.
func (*StorageS3Config) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{5}
return file_store_instance_setting_proto_rawDescGZIP(), []int{6}
}
func (x *StorageS3Config) GetAccessKeyId() string {
@ -774,7 +942,7 @@ type InstanceMemoRelatedSetting struct {
func (x *InstanceMemoRelatedSetting) Reset() {
*x = InstanceMemoRelatedSetting{}
mi := &file_store_instance_setting_proto_msgTypes[6]
mi := &file_store_instance_setting_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -786,7 +954,7 @@ func (x *InstanceMemoRelatedSetting) String() string {
func (*InstanceMemoRelatedSetting) ProtoMessage() {}
func (x *InstanceMemoRelatedSetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[6]
mi := &file_store_instance_setting_proto_msgTypes[7]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -799,7 +967,7 @@ func (x *InstanceMemoRelatedSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceMemoRelatedSetting.ProtoReflect.Descriptor instead.
func (*InstanceMemoRelatedSetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{6}
return file_store_instance_setting_proto_rawDescGZIP(), []int{7}
}
func (x *InstanceMemoRelatedSetting) GetContentLengthLimit() int32 {
@ -836,7 +1004,7 @@ type InstanceTagMetadata struct {
func (x *InstanceTagMetadata) Reset() {
*x = InstanceTagMetadata{}
mi := &file_store_instance_setting_proto_msgTypes[7]
mi := &file_store_instance_setting_proto_msgTypes[8]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -848,7 +1016,7 @@ func (x *InstanceTagMetadata) String() string {
func (*InstanceTagMetadata) ProtoMessage() {}
func (x *InstanceTagMetadata) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[7]
mi := &file_store_instance_setting_proto_msgTypes[8]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -861,7 +1029,7 @@ func (x *InstanceTagMetadata) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceTagMetadata.ProtoReflect.Descriptor instead.
func (*InstanceTagMetadata) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{7}
return file_store_instance_setting_proto_rawDescGZIP(), []int{8}
}
func (x *InstanceTagMetadata) GetBackgroundColor() *color.Color {
@ -893,7 +1061,7 @@ type InstanceTagsSetting struct {
func (x *InstanceTagsSetting) Reset() {
*x = InstanceTagsSetting{}
mi := &file_store_instance_setting_proto_msgTypes[8]
mi := &file_store_instance_setting_proto_msgTypes[9]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -905,7 +1073,7 @@ func (x *InstanceTagsSetting) String() string {
func (*InstanceTagsSetting) ProtoMessage() {}
func (x *InstanceTagsSetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[8]
mi := &file_store_instance_setting_proto_msgTypes[9]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -918,7 +1086,7 @@ func (x *InstanceTagsSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceTagsSetting.ProtoReflect.Descriptor instead.
func (*InstanceTagsSetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{8}
return file_store_instance_setting_proto_rawDescGZIP(), []int{9}
}
func (x *InstanceTagsSetting) GetTags() map[string]*InstanceTagMetadata {
@ -937,7 +1105,7 @@ type InstanceNotificationSetting struct {
func (x *InstanceNotificationSetting) Reset() {
*x = InstanceNotificationSetting{}
mi := &file_store_instance_setting_proto_msgTypes[9]
mi := &file_store_instance_setting_proto_msgTypes[10]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -949,7 +1117,7 @@ func (x *InstanceNotificationSetting) String() string {
func (*InstanceNotificationSetting) ProtoMessage() {}
func (x *InstanceNotificationSetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[9]
mi := &file_store_instance_setting_proto_msgTypes[10]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -962,7 +1130,7 @@ func (x *InstanceNotificationSetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceNotificationSetting.ProtoReflect.Descriptor instead.
func (*InstanceNotificationSetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{9}
return file_store_instance_setting_proto_rawDescGZIP(), []int{10}
}
func (x *InstanceNotificationSetting) GetEmail() *InstanceNotificationSetting_EmailSetting {
@ -985,7 +1153,7 @@ type InstanceAISetting struct {
func (x *InstanceAISetting) Reset() {
*x = InstanceAISetting{}
mi := &file_store_instance_setting_proto_msgTypes[10]
mi := &file_store_instance_setting_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -997,7 +1165,7 @@ func (x *InstanceAISetting) String() string {
func (*InstanceAISetting) ProtoMessage() {}
func (x *InstanceAISetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[10]
mi := &file_store_instance_setting_proto_msgTypes[11]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1010,7 +1178,7 @@ func (x *InstanceAISetting) ProtoReflect() protoreflect.Message {
// Deprecated: Use InstanceAISetting.ProtoReflect.Descriptor instead.
func (*InstanceAISetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{10}
return file_store_instance_setting_proto_rawDescGZIP(), []int{11}
}
func (x *InstanceAISetting) GetProviders() []*AIProviderConfig {
@ -1041,7 +1209,7 @@ type AIProviderConfig struct {
func (x *AIProviderConfig) Reset() {
*x = AIProviderConfig{}
mi := &file_store_instance_setting_proto_msgTypes[11]
mi := &file_store_instance_setting_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1053,7 +1221,7 @@ func (x *AIProviderConfig) String() string {
func (*AIProviderConfig) ProtoMessage() {}
func (x *AIProviderConfig) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[11]
mi := &file_store_instance_setting_proto_msgTypes[12]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1066,7 +1234,7 @@ func (x *AIProviderConfig) ProtoReflect() protoreflect.Message {
// Deprecated: Use AIProviderConfig.ProtoReflect.Descriptor instead.
func (*AIProviderConfig) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{11}
return file_store_instance_setting_proto_rawDescGZIP(), []int{12}
}
func (x *AIProviderConfig) GetId() string {
@ -1135,7 +1303,7 @@ type TranscriptionConfig struct {
func (x *TranscriptionConfig) Reset() {
*x = TranscriptionConfig{}
mi := &file_store_instance_setting_proto_msgTypes[12]
mi := &file_store_instance_setting_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1147,7 +1315,7 @@ func (x *TranscriptionConfig) String() string {
func (*TranscriptionConfig) ProtoMessage() {}
func (x *TranscriptionConfig) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[12]
mi := &file_store_instance_setting_proto_msgTypes[13]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1160,7 +1328,7 @@ func (x *TranscriptionConfig) ProtoReflect() protoreflect.Message {
// Deprecated: Use TranscriptionConfig.ProtoReflect.Descriptor instead.
func (*TranscriptionConfig) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{12}
return file_store_instance_setting_proto_rawDescGZIP(), []int{13}
}
func (x *TranscriptionConfig) GetProviderId() string {
@ -1209,7 +1377,7 @@ type InstanceNotificationSetting_EmailSetting struct {
func (x *InstanceNotificationSetting_EmailSetting) Reset() {
*x = InstanceNotificationSetting_EmailSetting{}
mi := &file_store_instance_setting_proto_msgTypes[14]
mi := &file_store_instance_setting_proto_msgTypes[15]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -1221,7 +1389,7 @@ func (x *InstanceNotificationSetting_EmailSetting) String() string {
func (*InstanceNotificationSetting_EmailSetting) ProtoMessage() {}
func (x *InstanceNotificationSetting_EmailSetting) ProtoReflect() protoreflect.Message {
mi := &file_store_instance_setting_proto_msgTypes[14]
mi := &file_store_instance_setting_proto_msgTypes[15]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -1234,7 +1402,7 @@ func (x *InstanceNotificationSetting_EmailSetting) ProtoReflect() protoreflect.M
// Deprecated: Use InstanceNotificationSetting_EmailSetting.ProtoReflect.Descriptor instead.
func (*InstanceNotificationSetting_EmailSetting) Descriptor() ([]byte, []int) {
return file_store_instance_setting_proto_rawDescGZIP(), []int{9, 0}
return file_store_instance_setting_proto_rawDescGZIP(), []int{10, 0}
}
func (x *InstanceNotificationSetting_EmailSetting) GetEnabled() bool {
@ -1339,12 +1507,21 @@ const file_store_instance_setting_proto_rawDesc = "" +
"\x15InstanceCustomProfile\x12\x14\n" +
"\x05title\x18\x01 \x01(\tR\x05title\x12 \n" +
"\vdescription\x18\x02 \x01(\tR\vdescription\x12\x19\n" +
"\blogo_url\x18\x03 \x01(\tR\alogoUrl\"\xd3\x02\n" +
"\blogo_url\x18\x03 \x01(\tR\alogoUrl\"\xa2\x01\n" +
"\aStorage\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12,\n" +
"\x04type\x18\x03 \x01(\x0e2\x18.memos.store.StorageTypeR\x04type\x12;\n" +
"\ts3_config\x18\n" +
" \x01(\v2\x1c.memos.store.StorageS3ConfigH\x00R\bs3ConfigB\b\n" +
"\x06config\"\xb3\x03\n" +
"\x16InstanceStorageSetting\x12R\n" +
"\fstorage_type\x18\x01 \x01(\x0e2/.memos.store.InstanceStorageSetting.StorageTypeR\vstorageType\x12+\n" +
"\x11filepath_template\x18\x02 \x01(\tR\x10filepathTemplate\x12/\n" +
"\x14upload_size_limit_mb\x18\x03 \x01(\x03R\x11uploadSizeLimitMb\x129\n" +
"\ts3_config\x18\x04 \x01(\v2\x1c.memos.store.StorageS3ConfigR\bs3Config\"L\n" +
"\ts3_config\x18\x04 \x01(\v2\x1c.memos.store.StorageS3ConfigR\bs3Config\x120\n" +
"\bstorages\x18\x05 \x03(\v2\x14.memos.store.StorageR\bstorages\x12,\n" +
"\x12default_storage_id\x18\x06 \x01(\tR\x10defaultStorageId\"L\n" +
"\vStorageType\x12\x1c\n" +
"\x18STORAGE_TYPE_UNSPECIFIED\x10\x00\x12\f\n" +
"\bDATABASE\x10\x01\x12\t\n" +
@ -1408,7 +1585,12 @@ const file_store_instance_setting_proto_rawDesc = "" +
"\fMEMO_RELATED\x10\x04\x12\b\n" +
"\x04TAGS\x10\x05\x12\x10\n" +
"\fNOTIFICATION\x10\x06\x12\x06\n" +
"\x02AI\x10\a*J\n" +
"\x02AI\x10\a*s\n" +
"\vStorageType\x12\x1c\n" +
"\x18STORAGE_TYPE_UNSPECIFIED\x10\x00\x12\x19\n" +
"\x15STORAGE_TYPE_DATABASE\x10\x01\x12\x16\n" +
"\x12STORAGE_TYPE_LOCAL\x10\x02\x12\x13\n" +
"\x0fSTORAGE_TYPE_S3\x10\x03*J\n" +
"\x0eAIProviderType\x12 \n" +
"\x1cAI_PROVIDER_TYPE_UNSPECIFIED\x10\x00\x12\n" +
"\n" +
@ -1429,53 +1611,58 @@ func file_store_instance_setting_proto_rawDescGZIP() []byte {
return file_store_instance_setting_proto_rawDescData
}
var file_store_instance_setting_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
var file_store_instance_setting_proto_msgTypes = make([]protoimpl.MessageInfo, 15)
var file_store_instance_setting_proto_enumTypes = make([]protoimpl.EnumInfo, 4)
var file_store_instance_setting_proto_msgTypes = make([]protoimpl.MessageInfo, 16)
var file_store_instance_setting_proto_goTypes = []any{
(InstanceSettingKey)(0), // 0: memos.store.InstanceSettingKey
(AIProviderType)(0), // 1: memos.store.AIProviderType
(InstanceStorageSetting_StorageType)(0), // 2: memos.store.InstanceStorageSetting.StorageType
(*InstanceSetting)(nil), // 3: memos.store.InstanceSetting
(*InstanceBasicSetting)(nil), // 4: memos.store.InstanceBasicSetting
(*InstanceGeneralSetting)(nil), // 5: memos.store.InstanceGeneralSetting
(*InstanceCustomProfile)(nil), // 6: memos.store.InstanceCustomProfile
(*InstanceStorageSetting)(nil), // 7: memos.store.InstanceStorageSetting
(*StorageS3Config)(nil), // 8: memos.store.StorageS3Config
(*InstanceMemoRelatedSetting)(nil), // 9: memos.store.InstanceMemoRelatedSetting
(*InstanceTagMetadata)(nil), // 10: memos.store.InstanceTagMetadata
(*InstanceTagsSetting)(nil), // 11: memos.store.InstanceTagsSetting
(*InstanceNotificationSetting)(nil), // 12: memos.store.InstanceNotificationSetting
(*InstanceAISetting)(nil), // 13: memos.store.InstanceAISetting
(*AIProviderConfig)(nil), // 14: memos.store.AIProviderConfig
(*TranscriptionConfig)(nil), // 15: memos.store.TranscriptionConfig
nil, // 16: memos.store.InstanceTagsSetting.TagsEntry
(*InstanceNotificationSetting_EmailSetting)(nil), // 17: memos.store.InstanceNotificationSetting.EmailSetting
(*color.Color)(nil), // 18: google.type.Color
(StorageType)(0), // 1: memos.store.StorageType
(AIProviderType)(0), // 2: memos.store.AIProviderType
(InstanceStorageSetting_StorageType)(0), // 3: memos.store.InstanceStorageSetting.StorageType
(*InstanceSetting)(nil), // 4: memos.store.InstanceSetting
(*InstanceBasicSetting)(nil), // 5: memos.store.InstanceBasicSetting
(*InstanceGeneralSetting)(nil), // 6: memos.store.InstanceGeneralSetting
(*InstanceCustomProfile)(nil), // 7: memos.store.InstanceCustomProfile
(*Storage)(nil), // 8: memos.store.Storage
(*InstanceStorageSetting)(nil), // 9: memos.store.InstanceStorageSetting
(*StorageS3Config)(nil), // 10: memos.store.StorageS3Config
(*InstanceMemoRelatedSetting)(nil), // 11: memos.store.InstanceMemoRelatedSetting
(*InstanceTagMetadata)(nil), // 12: memos.store.InstanceTagMetadata
(*InstanceTagsSetting)(nil), // 13: memos.store.InstanceTagsSetting
(*InstanceNotificationSetting)(nil), // 14: memos.store.InstanceNotificationSetting
(*InstanceAISetting)(nil), // 15: memos.store.InstanceAISetting
(*AIProviderConfig)(nil), // 16: memos.store.AIProviderConfig
(*TranscriptionConfig)(nil), // 17: memos.store.TranscriptionConfig
nil, // 18: memos.store.InstanceTagsSetting.TagsEntry
(*InstanceNotificationSetting_EmailSetting)(nil), // 19: memos.store.InstanceNotificationSetting.EmailSetting
(*color.Color)(nil), // 20: google.type.Color
}
var file_store_instance_setting_proto_depIdxs = []int32{
0, // 0: memos.store.InstanceSetting.key:type_name -> memos.store.InstanceSettingKey
4, // 1: memos.store.InstanceSetting.basic_setting:type_name -> memos.store.InstanceBasicSetting
5, // 2: memos.store.InstanceSetting.general_setting:type_name -> memos.store.InstanceGeneralSetting
7, // 3: memos.store.InstanceSetting.storage_setting:type_name -> memos.store.InstanceStorageSetting
9, // 4: memos.store.InstanceSetting.memo_related_setting:type_name -> memos.store.InstanceMemoRelatedSetting
11, // 5: memos.store.InstanceSetting.tags_setting:type_name -> memos.store.InstanceTagsSetting
12, // 6: memos.store.InstanceSetting.notification_setting:type_name -> memos.store.InstanceNotificationSetting
13, // 7: memos.store.InstanceSetting.ai_setting:type_name -> memos.store.InstanceAISetting
6, // 8: memos.store.InstanceGeneralSetting.custom_profile:type_name -> memos.store.InstanceCustomProfile
2, // 9: memos.store.InstanceStorageSetting.storage_type:type_name -> memos.store.InstanceStorageSetting.StorageType
8, // 10: memos.store.InstanceStorageSetting.s3_config:type_name -> memos.store.StorageS3Config
18, // 11: memos.store.InstanceTagMetadata.background_color:type_name -> google.type.Color
16, // 12: memos.store.InstanceTagsSetting.tags:type_name -> memos.store.InstanceTagsSetting.TagsEntry
17, // 13: memos.store.InstanceNotificationSetting.email:type_name -> memos.store.InstanceNotificationSetting.EmailSetting
14, // 14: memos.store.InstanceAISetting.providers:type_name -> memos.store.AIProviderConfig
15, // 15: memos.store.InstanceAISetting.transcription:type_name -> memos.store.TranscriptionConfig
1, // 16: memos.store.AIProviderConfig.type:type_name -> memos.store.AIProviderType
10, // 17: memos.store.InstanceTagsSetting.TagsEntry.value:type_name -> memos.store.InstanceTagMetadata
18, // [18:18] is the sub-list for method output_type
18, // [18:18] is the sub-list for method input_type
18, // [18:18] is the sub-list for extension type_name
18, // [18:18] is the sub-list for extension extendee
0, // [0:18] is the sub-list for field type_name
5, // 1: memos.store.InstanceSetting.basic_setting:type_name -> memos.store.InstanceBasicSetting
6, // 2: memos.store.InstanceSetting.general_setting:type_name -> memos.store.InstanceGeneralSetting
9, // 3: memos.store.InstanceSetting.storage_setting:type_name -> memos.store.InstanceStorageSetting
11, // 4: memos.store.InstanceSetting.memo_related_setting:type_name -> memos.store.InstanceMemoRelatedSetting
13, // 5: memos.store.InstanceSetting.tags_setting:type_name -> memos.store.InstanceTagsSetting
14, // 6: memos.store.InstanceSetting.notification_setting:type_name -> memos.store.InstanceNotificationSetting
15, // 7: memos.store.InstanceSetting.ai_setting:type_name -> memos.store.InstanceAISetting
7, // 8: memos.store.InstanceGeneralSetting.custom_profile:type_name -> memos.store.InstanceCustomProfile
1, // 9: memos.store.Storage.type:type_name -> memos.store.StorageType
10, // 10: memos.store.Storage.s3_config:type_name -> memos.store.StorageS3Config
3, // 11: memos.store.InstanceStorageSetting.storage_type:type_name -> memos.store.InstanceStorageSetting.StorageType
10, // 12: memos.store.InstanceStorageSetting.s3_config:type_name -> memos.store.StorageS3Config
8, // 13: memos.store.InstanceStorageSetting.storages:type_name -> memos.store.Storage
20, // 14: memos.store.InstanceTagMetadata.background_color:type_name -> google.type.Color
18, // 15: memos.store.InstanceTagsSetting.tags:type_name -> memos.store.InstanceTagsSetting.TagsEntry
19, // 16: memos.store.InstanceNotificationSetting.email:type_name -> memos.store.InstanceNotificationSetting.EmailSetting
16, // 17: memos.store.InstanceAISetting.providers:type_name -> memos.store.AIProviderConfig
17, // 18: memos.store.InstanceAISetting.transcription:type_name -> memos.store.TranscriptionConfig
2, // 19: memos.store.AIProviderConfig.type:type_name -> memos.store.AIProviderType
12, // 20: memos.store.InstanceTagsSetting.TagsEntry.value:type_name -> memos.store.InstanceTagMetadata
21, // [21:21] is the sub-list for method output_type
21, // [21:21] is the sub-list for method input_type
21, // [21:21] is the sub-list for extension type_name
21, // [21:21] is the sub-list for extension extendee
0, // [0:21] is the sub-list for field type_name
}
func init() { file_store_instance_setting_proto_init() }
@ -1492,13 +1679,16 @@ func file_store_instance_setting_proto_init() {
(*InstanceSetting_NotificationSetting)(nil),
(*InstanceSetting_AiSetting)(nil),
}
file_store_instance_setting_proto_msgTypes[4].OneofWrappers = []any{
(*Storage_S3Config)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_store_instance_setting_proto_rawDesc), len(file_store_instance_setting_proto_rawDesc)),
NumEnums: 3,
NumMessages: 15,
NumEnums: 4,
NumMessages: 16,
NumExtensions: 0,
NumServices: 0,
},

View file

@ -85,11 +85,15 @@ message AttachmentPayload {
MediaMetadata media_metadata = 11;
message S3Object {
// Legacy attachments embedded their complete S3 configuration.
// New attachments resolve storage through storage_id.
StorageS3Config s3_config = 1;
// key is the S3 object key.
string key = 2;
// last_presigned_time is the last time the object was presigned.
// This is used to determine if the presigned URL is still valid.
google.protobuf.Timestamp last_presigned_time = 3;
// storage_id identifies the configured storage containing this object.
string storage_id = 4;
}
}

View file

@ -71,7 +71,31 @@ message InstanceCustomProfile {
string logo_url = 3;
}
enum StorageType {
STORAGE_TYPE_UNSPECIFIED = 0;
// DATABASE stores attachment content in the application database.
STORAGE_TYPE_DATABASE = 1;
// LOCAL stores attachment content on the local filesystem.
STORAGE_TYPE_LOCAL = 2;
// S3 stores attachment content in an S3-compatible object store.
STORAGE_TYPE_S3 = 3;
}
// Storage is a configured attachment storage instance.
message Storage {
// id is the stable identifier referenced by attachments.
string id = 1;
// name is the human-readable storage name.
string name = 2;
StorageType type = 3;
oneof config {
StorageS3Config s3_config = 10;
}
}
message InstanceStorageSetting {
// Legacy storage type enum retained for compatibility with existing settings.
enum StorageType {
STORAGE_TYPE_UNSPECIFIED = 0;
// STORAGE_TYPE_DATABASE is the database storage type.
@ -81,15 +105,20 @@ message InstanceStorageSetting {
// STORAGE_TYPE_S3 is the S3 storage type.
S3 = 3;
}
// storage_type is the storage type.
// Legacy compatibility field. New code uses default_storage_id.
StorageType storage_type = 1;
// The template of file path.
// e.g. assets/{timestamp}_{filename}
string filepath_template = 2;
// The max upload size in megabytes.
int64 upload_size_limit_mb = 3;
// The S3 config.
// Legacy compatibility field. New code uses storages.
StorageS3Config s3_config = 4;
// storages contains configured storage instances, including inactive instances
// that may still be referenced by existing attachments.
repeated Storage storages = 5;
// default_storage_id identifies the storage used for new attachments.
string default_storage_id = 6;
}
// Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/

View file

@ -16,7 +16,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/profile"
"github.com/usememos/memos/internal/storage/s3"
"github.com/usememos/memos/internal/storage"
"github.com/usememos/memos/internal/util"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
@ -54,7 +54,12 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s
return errors.Wrap(err, "Failed to find instance storage setting")
}
if instanceStorageSetting.StorageType == storepb.InstanceStorageSetting_LOCAL {
defaultStorage := store.GetDefaultStorage(instanceStorageSetting)
if defaultStorage == nil {
return errors.New("default storage is not configured")
}
if defaultStorage.Type == storepb.StorageType_STORAGE_TYPE_LOCAL {
filepathTemplate := "assets/{timestamp}_{uuid}_{filename}"
if instanceStorageSetting.FilepathTemplate != "" {
filepathTemplate = instanceStorageSetting.FilepathTemplate
@ -93,14 +98,10 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s
create.Reference = internalPath
create.Blob = nil
create.StorageType = storepb.AttachmentStorageType_LOCAL
} else if instanceStorageSetting.StorageType == storepb.InstanceStorageSetting_S3 {
s3Config := instanceStorageSetting.S3Config
if s3Config == nil {
return errors.Errorf("No activated external storage found")
}
s3Client, err := s3.NewClient(ctx, s3Config)
} else if defaultStorage.Type == storepb.StorageType_STORAGE_TYPE_S3 {
driver, err := storage.NewDriver(ctx, defaultStorage)
if err != nil {
return errors.Wrap(err, "Failed to create s3 client")
return errors.Wrap(err, "failed to create storage driver")
}
filepathTemplate := instanceStorageSetting.FilepathTemplate
@ -108,13 +109,13 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s
filepathTemplate = filepath.Join(filepathTemplate, "{filename}")
}
filepathTemplate = replaceFilenameWithPathTemplate(filepathTemplate, create.Filename)
key, err := s3Client.UploadObject(ctx, filepathTemplate, create.Type, bytes.NewReader(create.Blob))
key, err := driver.UploadObject(ctx, filepathTemplate, create.Type, bytes.NewReader(create.Blob))
if err != nil {
return errors.Wrap(err, "Failed to upload via s3 client")
return errors.Wrap(err, "failed to upload via storage driver")
}
presignURL, err := s3Client.PresignGetObject(ctx, key)
presignURL, err := driver.PresignGetObject(ctx, key)
if err != nil {
return errors.Wrap(err, "Failed to presign via s3 client")
return errors.Wrap(err, "failed to presign via storage driver")
}
create.Reference = presignURL
@ -123,9 +124,9 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s
payload := ensureAttachmentPayload(create.Payload)
payload.Payload = &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{
S3Config: s3Config,
Key: key,
LastPresignedTime: timestamppb.New(time.Now()),
StorageId: defaultStorage.Id,
},
}
create.Payload = payload
@ -134,7 +135,8 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s
return nil
}
func (s *APIV1Service) GetAttachmentBlob(attachment *store.Attachment) ([]byte, error) {
// GetAttachmentBlob reads an attachment from its configured storage.
func (s *APIV1Service) GetAttachmentBlob(ctx context.Context, attachment *store.Attachment) ([]byte, error) {
// For local storage, read the file from the local disk.
if attachment.StorageType == storepb.AttachmentStorageType_LOCAL {
attachmentPath := filepath.FromSlash(attachment.Reference)
@ -158,26 +160,12 @@ func (s *APIV1Service) GetAttachmentBlob(attachment *store.Attachment) ([]byte,
}
// For S3 storage, download the file from S3.
if attachment.StorageType == storepb.AttachmentStorageType_S3 {
if attachment.Payload == nil {
return nil, errors.New("attachment payload is missing")
}
s3Object := attachment.Payload.GetS3Object()
if s3Object == nil {
return nil, errors.New("S3 object payload is missing")
}
if s3Object.S3Config == nil {
return nil, errors.New("S3 config is missing")
}
if s3Object.Key == "" {
return nil, errors.New("S3 object key is missing")
}
s3Client, err := s3.NewClient(context.Background(), s3Object.S3Config)
driver, s3Object, err := s.Store.ResolveAttachmentS3Driver(ctx, attachment)
if err != nil {
return nil, errors.Wrap(err, "failed to create S3 client")
return nil, errors.Wrap(err, "failed to resolve S3 attachment driver")
}
blob, err := s3Client.GetObject(context.Background(), s3Object.Key)
blob, err := driver.GetObject(ctx, s3Object.Key)
if err != nil {
return nil, errors.Wrap(err, "failed to get object from S3")
}

View file

@ -2,6 +2,7 @@ package v1
import (
"context"
"log/slog"
"strings"
"github.com/pkg/errors"
@ -223,11 +224,15 @@ func (s *APIV1Service) UpdateInstanceSetting(ctx context.Context, request *v1pb.
}
}
case storepb.InstanceSettingKey_STORAGE:
if storage := updateSetting.GetStorageSetting(); storage != nil && storage.S3Config != nil && storage.S3Config.AccessKeySecret == "" {
existing, err := s.Store.GetInstanceStorageSetting(ctx)
if err == nil && existing != nil && existing.S3Config != nil {
storage.S3Config.AccessKeySecret = existing.S3Config.AccessKeySecret
}
existing, err := s.Store.GetInstanceStorageSetting(ctx)
if err != nil {
// A corrupt stored setting must not block repair: treat it as unset so
// a valid update can overwrite it.
slog.Warn("failed to load existing storage setting; treating it as unset", "error", err)
existing = nil
}
if err := store.PrepareInstanceStorageSettingUpdate(updateSetting.GetStorageSetting(), existing); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid storage setting: %v", err)
}
case storepb.InstanceSettingKey_AI:
if err := s.prepareInstanceAISettingForUpdate(ctx, updateSetting.GetAiSetting()); err != nil {

View file

@ -136,6 +136,10 @@ func convertInstanceStorageSettingFromStore(settingpb *storepb.InstanceStorageSe
StorageType: v1pb.InstanceSetting_StorageSetting_StorageType(settingpb.StorageType),
FilepathTemplate: settingpb.FilepathTemplate,
UploadSizeLimitMb: settingpb.UploadSizeLimitMb,
DefaultStorageId: settingpb.DefaultStorageId,
}
for _, storagepb := range settingpb.Storages {
setting.Storages = append(setting.Storages, convertStorageFromStore(storagepb))
}
if settingpb.S3Config != nil {
setting.S3Config = &v1pb.InstanceSetting_StorageSetting_S3Config{
@ -159,6 +163,10 @@ func convertInstanceStorageSettingToStore(setting *v1pb.InstanceSetting_StorageS
StorageType: storepb.InstanceStorageSetting_StorageType(setting.StorageType),
FilepathTemplate: setting.FilepathTemplate,
UploadSizeLimitMb: setting.UploadSizeLimitMb,
DefaultStorageId: setting.DefaultStorageId,
}
for _, storage := range setting.Storages {
settingpb.Storages = append(settingpb.Storages, convertStorageToStore(storage))
}
if setting.S3Config != nil {
settingpb.S3Config = &storepb.StorageS3Config{
@ -174,6 +182,56 @@ func convertInstanceStorageSettingToStore(setting *v1pb.InstanceSetting_StorageS
return settingpb
}
func convertStorageFromStore(storagepb *storepb.Storage) *v1pb.InstanceSetting_Storage {
if storagepb == nil {
return nil
}
storage := &v1pb.InstanceSetting_Storage{
Id: storagepb.Id,
Name: storagepb.Name,
Type: v1pb.InstanceSetting_StorageType(storagepb.Type),
}
if s3Config := storagepb.GetS3Config(); s3Config != nil {
storage.Config = &v1pb.InstanceSetting_Storage_S3Config_{
S3Config: &v1pb.InstanceSetting_Storage_S3Config{
AccessKeyId: s3Config.AccessKeyId,
// AccessKeySecret is write-only: never returned in responses.
Endpoint: s3Config.Endpoint,
Region: s3Config.Region,
Bucket: s3Config.Bucket,
UsePathStyle: s3Config.UsePathStyle,
InsecureSkipTlsVerify: s3Config.InsecureSkipTlsVerify,
},
}
}
return storage
}
func convertStorageToStore(storage *v1pb.InstanceSetting_Storage) *storepb.Storage {
if storage == nil {
return nil
}
storagepb := &storepb.Storage{
Id: storage.Id,
Name: storage.Name,
Type: storepb.StorageType(storage.Type),
}
if s3Config := storage.GetS3Config(); s3Config != nil {
storagepb.Config = &storepb.Storage_S3Config{
S3Config: &storepb.StorageS3Config{
AccessKeyId: s3Config.AccessKeyId,
AccessKeySecret: s3Config.AccessKeySecret,
Endpoint: s3Config.Endpoint,
Region: s3Config.Region,
Bucket: s3Config.Bucket,
UsePathStyle: s3Config.UsePathStyle,
InsecureSkipTlsVerify: s3Config.InsecureSkipTlsVerify,
},
}
}
return storagepb
}
func convertInstanceMemoRelatedSettingFromStore(setting *storepb.InstanceMemoRelatedSetting) *v1pb.InstanceSetting_MemoRelatedSetting {
if setting == nil {
return nil

View file

@ -0,0 +1,234 @@
package test
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/internal/storage/s3"
"github.com/usememos/memos/internal/testutil/fakes3"
testminio "github.com/usememos/memos/internal/testutil/minio"
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
apiv1 "github.com/usememos/memos/server/router/api/v1"
"github.com/usememos/memos/store"
)
func TestS3AttachmentLifecycleAcrossStorageChange(t *testing.T) {
fake := fakes3.New(t, "attachments-old", "attachments-new")
runS3AttachmentLifecycleAcrossStorageChange(t, fake)
}
func TestS3AttachmentLifecycleAcrossStorageChangeMinIO(t *testing.T) {
server := testminio.New(t, "attachments-old", "attachments-new")
runS3AttachmentLifecycleAcrossStorageChange(t, server)
}
type s3ObjectStore interface {
Config(bucket string) *storepb.StorageS3Config
GetObject(bucket, key string) ([]byte, error)
}
func runS3AttachmentLifecycleAcrossStorageChange(t *testing.T, objectStore s3ObjectStore) {
t.Helper()
ctx := context.Background()
ts := NewTestService(t)
defer ts.Cleanup()
user, err := ts.CreateRegularUser(ctx, "s3-attachment-user")
require.NoError(t, err)
userCtx := ts.CreateUserContext(ctx, user.ID)
oldStorage := fakeStorage("s3-old", "Old S3", objectStore.Config("attachments-old"))
upsertS3StorageSetting(ctx, t, ts, oldStorage.Id, oldStorage)
firstContent := []byte("first attachment in old storage")
first, err := ts.Service.CreateAttachment(userCtx, &v1pb.CreateAttachmentRequest{
Attachment: &v1pb.Attachment{
Filename: "first.txt",
Type: "text/plain",
Content: firstContent,
},
})
require.NoError(t, err)
firstUID, err := apiv1.ExtractAttachmentUIDFromName(first.Name)
require.NoError(t, err)
storedFirst, err := ts.Store.GetAttachment(ctx, &store.FindAttachment{UID: &firstUID})
require.NoError(t, err)
require.NotNil(t, storedFirst)
require.Equal(t, storepb.AttachmentStorageType_S3, storedFirst.StorageType)
require.Empty(t, storedFirst.Blob)
require.NotEmpty(t, storedFirst.Reference)
require.Equal(t, oldStorage.Id, storedFirst.Payload.GetS3Object().GetStorageId())
require.Nil(t, storedFirst.Payload.GetS3Object().GetS3Config(), "new attachments must not embed S3 credentials")
oldKey := storedFirst.Payload.GetS3Object().GetKey()
storedInOldBucket, err := objectStore.GetObject("attachments-old", oldKey)
require.NoError(t, err)
require.Equal(t, firstContent, storedInOldBucket)
firstDownloaded, err := ts.Service.GetAttachmentBlob(ctx, storedFirst)
require.NoError(t, err)
require.Equal(t, firstContent, firstDownloaded)
firstMetadata, err := ts.Service.GetAttachment(userCtx, &v1pb.GetAttachmentRequest{Name: first.Name})
require.NoError(t, err)
require.Equal(t, first.Name, firstMetadata.Name)
require.Empty(t, firstMetadata.ExternalLink)
newStorage := fakeStorage("s3-new", "New S3", objectStore.Config("attachments-new"))
upsertS3StorageSetting(ctx, t, ts, newStorage.Id, oldStorage, newStorage)
secondContent := []byte("second attachment in new storage")
second, err := ts.Service.CreateAttachment(userCtx, &v1pb.CreateAttachmentRequest{
Attachment: &v1pb.Attachment{
Filename: "second.txt",
Type: "text/plain",
Content: secondContent,
},
})
require.NoError(t, err)
secondUID, err := apiv1.ExtractAttachmentUIDFromName(second.Name)
require.NoError(t, err)
storedSecond, err := ts.Store.GetAttachment(ctx, &store.FindAttachment{UID: &secondUID})
require.NoError(t, err)
require.Equal(t, newStorage.Id, storedSecond.Payload.GetS3Object().GetStorageId())
newKey := storedSecond.Payload.GetS3Object().GetKey()
storedInNewBucket, err := objectStore.GetObject("attachments-new", newKey)
require.NoError(t, err)
require.Equal(t, secondContent, storedInNewBucket)
// Changing the default storage only affects new uploads. The first attachment
// must continue to resolve its original storage by ID.
firstDownloaded, err = ts.Service.GetAttachmentBlob(ctx, storedFirst)
require.NoError(t, err)
require.Equal(t, firstContent, firstDownloaded)
secondDownloaded, err := ts.Service.GetAttachmentBlob(ctx, storedSecond)
require.NoError(t, err)
require.Equal(t, secondContent, secondDownloaded)
_, err = ts.Service.DeleteAttachment(userCtx, &v1pb.DeleteAttachmentRequest{Name: first.Name})
require.NoError(t, err)
_, err = objectStore.GetObject("attachments-old", oldKey)
require.Error(t, err, "deleting an attachment must delete its S3 object")
remaining, err := objectStore.GetObject("attachments-new", newKey)
require.NoError(t, err)
require.Equal(t, secondContent, remaining)
}
func TestGetLegacyS3AttachmentBlob(t *testing.T) {
ctx := context.Background()
fake := fakes3.New(t, "legacy-fallback", "legacy-current")
ts := NewTestService(t)
defer ts.Cleanup()
user, err := ts.CreateRegularUser(ctx, "legacy-s3-user")
require.NoError(t, err)
userCtx := ts.CreateUserContext(ctx, user.ID)
t.Run("falls back to embedded config when storage is not registered", func(t *testing.T) {
legacyConfig := fake.Config("legacy-fallback")
driver, err := s3.NewDriver(ctx, legacyConfig)
require.NoError(t, err)
content := []byte("attachment created before storage IDs")
key, err := driver.UploadObject(ctx, "legacy/fallback.txt", "text/plain", bytes.NewReader(content))
require.NoError(t, err)
legacyAttachment, err := ts.Store.CreateAttachment(ctx, &store.Attachment{
UID: "legacy-s3-fallback",
CreatorID: user.ID,
Filename: "fallback.txt",
Type: "text/plain",
Size: int64(len(content)),
StorageType: storepb.AttachmentStorageType_S3,
Payload: &storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{
S3Config: legacyConfig,
Key: key,
},
},
},
})
require.NoError(t, err)
downloaded, err := ts.Service.GetAttachmentBlob(ctx, legacyAttachment)
require.NoError(t, err)
require.Equal(t, content, downloaded)
})
t.Run("uses current storage options for matching namespace", func(t *testing.T) {
currentConfig := fake.Config("legacy-current")
driver, err := s3.NewDriver(ctx, currentConfig)
require.NoError(t, err)
content := []byte("legacy attachment using rotated storage config")
key, err := driver.UploadObject(ctx, "legacy/current.txt", "text/plain", bytes.NewReader(content))
require.NoError(t, err)
configuredStorage := fakeStorage("s3-current", "Current S3", currentConfig)
upsertS3StorageSetting(ctx, t, ts, configuredStorage.Id, configuredStorage)
// Virtual-hosted addressing cannot resolve against httptest's IP endpoint.
// Retrieval only succeeds if the legacy namespace is matched to the named
// storage and its current path-style option is used.
legacyConfig := &storepb.StorageS3Config{
AccessKeyId: "old-access-key",
AccessKeySecret: "old-secret-key",
Endpoint: currentConfig.Endpoint + "/",
Region: currentConfig.Region,
Bucket: currentConfig.Bucket,
UsePathStyle: false,
}
legacyAttachment, err := ts.Store.CreateAttachment(ctx, &store.Attachment{
UID: "legacy-s3-current",
CreatorID: user.ID,
Filename: "current.txt",
Type: "text/plain",
Size: int64(len(content)),
StorageType: storepb.AttachmentStorageType_S3,
Payload: &storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{
S3Config: legacyConfig,
Key: key,
},
},
},
})
require.NoError(t, err)
downloaded, err := ts.Service.GetAttachmentBlob(ctx, legacyAttachment)
require.NoError(t, err)
require.Equal(t, content, downloaded)
_, err = ts.Service.DeleteAttachment(userCtx, &v1pb.DeleteAttachmentRequest{Name: apiv1.AttachmentNamePrefix + legacyAttachment.UID})
require.NoError(t, err)
_, err = fake.GetObject(currentConfig.Bucket, key)
require.Error(t, err, "deleting a legacy attachment must use the current matching storage configuration")
})
}
func fakeStorage(id, name string, config *storepb.StorageS3Config) *storepb.Storage {
return &storepb.Storage{
Id: id,
Name: name,
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: config},
}
}
func upsertS3StorageSetting(ctx context.Context, t testing.TB, ts *TestService, defaultStorageID string, storages ...*storepb.Storage) {
t.Helper()
_, err := ts.Store.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{
StorageSetting: &storepb.InstanceStorageSetting{
FilepathTemplate: "attachments/{uuid}_{filename}",
UploadSizeLimitMb: 30,
Storages: storages,
DefaultStorageId: defaultStorageID,
},
},
})
require.NoError(t, err)
}

View file

@ -135,9 +135,9 @@ func TestCreateAttachment(t *testing.T) {
require.NotEqual(t, firstStoreAttachment.Reference, secondStoreAttachment.Reference)
firstBlob, err := ts.Service.GetAttachmentBlob(firstStoreAttachment)
firstBlob, err := ts.Service.GetAttachmentBlob(ctx, firstStoreAttachment)
require.NoError(t, err)
secondBlob, err := ts.Service.GetAttachmentBlob(secondStoreAttachment)
secondBlob, err := ts.Service.GetAttachmentBlob(ctx, secondStoreAttachment)
require.NoError(t, err)
require.Equal(t, []byte("first-image"), firstBlob)
require.Equal(t, []byte("second-image"), secondBlob)

View file

@ -733,6 +733,15 @@ func TestUpdateInstanceSetting(t *testing.T) {
"AccessKeySecret must never be returned in responses")
require.True(t, resp.GetStorageSetting().GetS3Config().GetInsecureSkipTlsVerify(),
"insecure_skip_tls_verify must round-trip through the API")
var defaultStorage *v1pb.InstanceSetting_Storage
for _, configuredStorage := range resp.GetStorageSetting().GetStorages() {
if configuredStorage.GetId() == resp.GetStorageSetting().GetDefaultStorageId() {
defaultStorage = configuredStorage
}
}
require.NotNil(t, defaultStorage)
require.Empty(t, defaultStorage.GetS3Config().GetAccessKeySecret(),
"AccessKeySecret must be write-only in named storage responses")
// Update with empty secret; original must be preserved in the store.
_, err = ts.Service.UpdateInstanceSetting(adminCtx, &v1pb.UpdateInstanceSettingRequest{
@ -760,6 +769,18 @@ func TestUpdateInstanceSetting(t *testing.T) {
"existing AccessKeySecret must be preserved when an empty value is sent")
require.Equal(t, "s3-v2.example.com", stored.GetS3Config().GetEndpoint())
require.True(t, stored.GetS3Config().GetInsecureSkipTlsVerify())
var s3StorageCount int
var previousStorageFound bool
for _, configuredStorage := range stored.GetStorages() {
if configuredStorage.GetType() == storepb.StorageType_STORAGE_TYPE_S3 {
s3StorageCount++
}
if configuredStorage.GetS3Config().GetEndpoint() == "s3.example.com" {
previousStorageFound = true
}
}
require.Equal(t, 2, s3StorageCount, "changing the S3 namespace must preserve the previous storage")
require.True(t, previousStorageFound)
})
t.Run("UpdateInstanceSetting - AI provider keys are write-only and preserved on empty", func(t *testing.T) {

View file

@ -21,7 +21,6 @@ import (
"github.com/usememos/memos/internal/motionphoto"
"github.com/usememos/memos/internal/profile"
"github.com/usememos/memos/internal/storage/s3"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/access"
"github.com/usememos/memos/server/auth"
@ -331,11 +330,11 @@ func (s *FileServerService) getAttachmentReader(ctx context.Context, attachment
return file, nil
case storepb.AttachmentStorageType_S3:
s3Client, s3Object, err := s.createS3Client(attachment)
driver, s3Object, err := s.Store.ResolveAttachmentS3Driver(ctx, attachment)
if err != nil {
return nil, err
}
reader, err := s3Client.GetObjectStream(ctx, s3Object.Key)
reader, err := driver.GetObjectStream(ctx, s3Object.Key)
if err != nil {
return nil, errors.Wrap(err, "failed to stream from S3")
}
@ -378,37 +377,14 @@ func (s *FileServerService) readLocalFile(reference string) ([]byte, error) {
return blob, nil
}
// createS3Client creates an S3 client from attachment payload.
func (*FileServerService) createS3Client(attachment *store.Attachment) (*s3.Client, *storepb.AttachmentPayload_S3Object, error) {
if attachment.Payload == nil {
return nil, nil, errors.New("attachment payload is missing")
}
s3Object := attachment.Payload.GetS3Object()
if s3Object == nil {
return nil, nil, errors.New("S3 object payload is missing")
}
if s3Object.S3Config == nil {
return nil, nil, errors.New("S3 config is missing")
}
if s3Object.Key == "" {
return nil, nil, errors.New("S3 object key is missing")
}
client, err := s3.NewClient(context.Background(), s3Object.S3Config)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create S3 client")
}
return client, s3Object, nil
}
// downloadFromS3 downloads the entire object from S3.
func (s *FileServerService) downloadFromS3(ctx context.Context, attachment *store.Attachment) ([]byte, error) {
client, s3Object, err := s.createS3Client(attachment)
driver, s3Object, err := s.Store.ResolveAttachmentS3Driver(ctx, attachment)
if err != nil {
return nil, err
}
blob, err := client.GetObject(ctx, s3Object.Key)
blob, err := driver.GetObject(ctx, s3Object.Key)
if err != nil {
return nil, errors.Wrap(err, "failed to download from S3")
}
@ -417,12 +393,12 @@ func (s *FileServerService) downloadFromS3(ctx context.Context, attachment *stor
// getS3PresignedURL generates a presigned URL for direct S3 access.
func (s *FileServerService) getS3PresignedURL(ctx context.Context, attachment *store.Attachment) (string, error) {
client, s3Object, err := s.createS3Client(attachment)
driver, s3Object, err := s.Store.ResolveAttachmentS3Driver(ctx, attachment)
if err != nil {
return "", err
}
url, err := client.PresignGetObject(ctx, s3Object.Key)
url, err := driver.PresignGetObject(ctx, s3Object.Key)
if err != nil {
return "", errors.Wrap(err, "failed to presign URL")
}

View file

@ -10,6 +10,7 @@ import (
"image"
"image/color"
"image/png"
"io"
"net/http"
"net/http/httptest"
"strings"
@ -24,6 +25,8 @@ import (
"github.com/usememos/memos/internal/markdown"
"github.com/usememos/memos/internal/profile"
"github.com/usememos/memos/internal/testutil"
"github.com/usememos/memos/internal/testutil/fakes3"
testminio "github.com/usememos/memos/internal/testutil/minio"
"github.com/usememos/memos/internal/util"
apiv1 "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
@ -33,6 +36,169 @@ import (
teststore "github.com/usememos/memos/store/test"
)
func TestServeAttachmentFile_S3(t *testing.T) {
ctx := context.Background()
fake := fakes3.New(t, "file-server-attachments")
svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)
defer cleanup()
configuredStorage := &storepb.Storage{
Id: "s3-files",
Name: "File server S3",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: fake.Config("file-server-attachments")},
}
_, err := stores.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{
FilepathTemplate: "files/{uuid}_{filename}",
UploadSizeLimitMb: 30,
Storages: []*storepb.Storage{configuredStorage},
DefaultStorageId: configuredStorage.Id,
}},
})
require.NoError(t, err)
creator, err := stores.CreateUser(ctx, &store.User{
Username: "s3-file-owner",
Role: store.RoleUser,
Email: "s3-file-owner@example.com",
})
require.NoError(t, err)
creatorCtx := context.WithValue(ctx, auth.UserIDContextKey, creator.ID)
content := []byte("content streamed from S3")
attachment, err := svc.CreateAttachment(creatorCtx, &apiv1.CreateAttachmentRequest{Attachment: &apiv1.Attachment{
Filename: "document.txt",
Type: "text/plain",
Content: content,
}})
require.NoError(t, err)
_, err = svc.CreateMemo(creatorCtx, &apiv1.CreateMemoRequest{Memo: &apiv1.Memo{
Content: "public S3 attachment",
Visibility: apiv1.Visibility_PUBLIC,
Attachments: []*apiv1.Attachment{{Name: attachment.Name}},
}})
require.NoError(t, err)
e := echo.New()
fs.RegisterRoutes(e)
recorder := httptest.NewRecorder()
e.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/%s/%s", attachment.Name, attachment.Filename), nil))
require.Equal(t, http.StatusOK, recorder.Code)
require.Equal(t, content, recorder.Body.Bytes())
require.Equal(t, "text/plain; charset=utf-8", recorder.Header().Get(echo.HeaderContentType))
}
func TestServeAttachmentFile_S3MinIO(t *testing.T) {
ctx := context.Background()
server := testminio.New(t, "file-server-attachments")
svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)
defer cleanup()
configuredStorage := &storepb.Storage{
Id: "s3-minio-files",
Name: "MinIO files",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: server.Config("file-server-attachments")},
}
_, err := stores.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{
FilepathTemplate: "files/{uuid}_{filename}",
UploadSizeLimitMb: 30,
Storages: []*storepb.Storage{configuredStorage},
DefaultStorageId: configuredStorage.Id,
}},
})
require.NoError(t, err)
creator, err := stores.CreateUser(ctx, &store.User{
Username: "s3-minio-file-owner",
Role: store.RoleUser,
Email: "s3-minio-file-owner@example.com",
})
require.NoError(t, err)
creatorCtx := context.WithValue(ctx, auth.UserIDContextKey, creator.ID)
textContent := []byte("content streamed through Memos from MinIO")
textAttachment, err := svc.CreateAttachment(creatorCtx, &apiv1.CreateAttachmentRequest{Attachment: &apiv1.Attachment{
Filename: "document.txt",
Type: "text/plain",
Content: textContent,
}})
require.NoError(t, err)
videoContent := []byte("0123456789abcdef")
videoAttachment, err := svc.CreateAttachment(creatorCtx, &apiv1.CreateAttachmentRequest{Attachment: &apiv1.Attachment{
Filename: "clip.mp4",
Type: "video/mp4",
Content: videoContent,
}})
require.NoError(t, err)
_, err = svc.CreateMemo(creatorCtx, &apiv1.CreateMemoRequest{Memo: &apiv1.Memo{
Content: "public MinIO attachments",
Visibility: apiv1.Visibility_PUBLIC,
Attachments: []*apiv1.Attachment{
{Name: textAttachment.Name},
{Name: videoAttachment.Name},
},
}})
require.NoError(t, err)
e := echo.New()
fs.RegisterRoutes(e)
textURL := fmt.Sprintf("/file/%s/%s", textAttachment.Name, textAttachment.Filename)
// Authorization happens before storage resolution, so a private instance
// must not expose either object bytes or a presigned URL.
fs.Profile.InstanceURL = ""
privateRecorder := httptest.NewRecorder()
e.ServeHTTP(privateRecorder, httptest.NewRequest(http.MethodGet, textURL, nil))
require.Equal(t, http.StatusUnauthorized, privateRecorder.Code)
require.Empty(t, privateRecorder.Header().Get(echo.HeaderLocation))
fs.Profile.InstanceURL = "http://localhost:8080"
textRecorder := httptest.NewRecorder()
e.ServeHTTP(textRecorder, httptest.NewRequest(http.MethodGet, textURL, nil))
require.Equal(t, http.StatusOK, textRecorder.Code)
require.Equal(t, textContent, textRecorder.Body.Bytes())
// A migrated key-only attachment carries the legacy "s3" ID. If the
// registry was rebuilt with a different ID, the resolver must still fall
// back to the migrated singleton configuration and serve the original key.
textUID, err := apiv1service.ExtractAttachmentUIDFromName(textAttachment.Name)
require.NoError(t, err)
storedTextAttachment, err := stores.GetAttachment(ctx, &store.FindAttachment{UID: &textUID})
require.NoError(t, err)
require.NotNil(t, storedTextAttachment)
storedTextAttachment.Payload.GetS3Object().StorageId = "s3"
require.NoError(t, stores.UpdateAttachment(ctx, &store.UpdateAttachment{
ID: storedTextAttachment.ID,
Payload: storedTextAttachment.Payload,
}))
legacyRecorder := httptest.NewRecorder()
e.ServeHTTP(legacyRecorder, httptest.NewRequest(http.MethodGet, textURL, nil))
require.Equal(t, http.StatusOK, legacyRecorder.Code)
require.Equal(t, textContent, legacyRecorder.Body.Bytes())
videoURL := fmt.Sprintf("/file/%s/%s", videoAttachment.Name, videoAttachment.Filename)
videoRecorder := httptest.NewRecorder()
e.ServeHTTP(videoRecorder, httptest.NewRequest(http.MethodGet, videoURL, nil))
require.Equal(t, http.StatusTemporaryRedirect, videoRecorder.Code)
presignedURL := videoRecorder.Header().Get(echo.HeaderLocation)
require.Contains(t, presignedURL, "X-Amz-Signature=")
rangeRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, presignedURL, nil)
require.NoError(t, err)
rangeRequest.Header.Set("Range", "bytes=4-7")
rangeResponse, err := http.DefaultClient.Do(rangeRequest)
require.NoError(t, err)
defer rangeResponse.Body.Close()
require.Equal(t, http.StatusPartialContent, rangeResponse.StatusCode)
rangeContent, err := io.ReadAll(rangeResponse.Body)
require.NoError(t, err)
require.Equal(t, []byte("4567"), rangeContent)
}
func TestServeAttachmentFile_ShareTokenAllowsDirectMemoAttachment(t *testing.T) {
ctx := context.Background()
svc, fs, _, cleanup := newShareAttachmentTestServices(ctx, t)

View file

@ -8,7 +8,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/storage/s3"
"github.com/usememos/memos/internal/storage"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
@ -51,6 +51,9 @@ func (r *Runner) CheckAndPresign(ctx context.Context) {
}
s3StorageType := storepb.AttachmentStorageType_S3
// Most attachments reference the same configured storage, so reuse drivers
// instead of building an S3 client per attachment.
driversByStorageID := map[string]storage.Driver{}
// Limit attachments to a reasonable batch size
const batchSize = 100
offset := 0
@ -90,28 +93,35 @@ func (r *Runner) CheckAndPresign(ctx context.Context) {
}
}
s3Config := instanceStorageSetting.GetS3Config()
if s3ObjectPayload.S3Config != nil {
s3Config = s3ObjectPayload.S3Config
}
if s3Config == nil {
slog.Error("S3 config is not found")
continue
}
s3Client, err := s3.NewClient(ctx, s3Config)
resolvedStorage, err := store.ResolveStorage(instanceStorageSetting, s3ObjectPayload.StorageId, s3ObjectPayload.S3Config)
if err != nil {
slog.Error("Failed to create S3 client", "error", err)
slog.Error("Failed to resolve storage", "error", err, "attachmentID", attachment.ID, "storageID", s3ObjectPayload.StorageId)
continue
}
driver := driversByStorageID[resolvedStorage.Id]
if driver == nil {
driver, err = storage.NewDriver(ctx, resolvedStorage)
if err != nil {
slog.Error("Failed to create storage driver", "error", err, "attachmentID", attachment.ID, "storageID", resolvedStorage.Id)
continue
}
if resolvedStorage.Id != "" {
driversByStorageID[resolvedStorage.Id] = driver
}
}
presignURL, err := s3Client.PresignGetObject(ctx, s3ObjectPayload.Key)
presignURL, err := driver.PresignGetObject(ctx, s3ObjectPayload.Key)
if err != nil {
slog.Error("Failed to presign URL", "error", err, "attachmentID", attachment.ID)
continue
}
s3ObjectPayload.S3Config = s3Config
// Pin attachments that predate storage IDs to the registry entry that
// served this presign, so a later default-storage change cannot rebind
// them to a different namespace.
if s3ObjectPayload.StorageId == "" && resolvedStorage.Id != "" {
s3ObjectPayload.StorageId = resolvedStorage.Id
}
s3ObjectPayload.LastPresignedTime = timestamppb.New(time.Now())
if err := r.Store.UpdateAttachment(ctx, &store.UpdateAttachment{
ID: attachment.ID,

View file

@ -1,14 +1,181 @@
package s3presign
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/storage/s3"
"github.com/usememos/memos/internal/testutil/fakes3"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
teststore "github.com/usememos/memos/store/test"
)
func TestCheckAndPresignRefreshesAndPinsAttachments(t *testing.T) {
ctx := context.Background()
fake := fakes3.New(t, "presign-attachments")
config := fake.Config("presign-attachments")
ts := teststore.NewTestingStore(ctx, t)
defer ts.Close()
storageID := "s3-current"
_, err := ts.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{
Storages: []*storepb.Storage{{
Id: storageID,
Name: "Current S3",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: config},
}},
DefaultStorageId: storageID,
}},
})
require.NoError(t, err)
user, err := ts.CreateUser(ctx, &store.User{
Username: "presign-runner-user",
Role: store.RoleUser,
Email: "presign-runner-user@example.com",
})
require.NoError(t, err)
driver, err := s3.NewDriver(ctx, config)
require.NoError(t, err)
content := []byte("legacy attachment refreshed by the runner")
legacyKey, err := driver.UploadObject(ctx, "legacy/presign.txt", "text/plain", bytes.NewReader(content))
require.NoError(t, err)
freshTime := timestamppb.Now()
createS3Attachment(ctx, t, ts, user.ID, "presign-legacy", legacyKey, "", config, nil, "")
createS3Attachment(ctx, t, ts, user.ID, "presign-expired", "expired.txt", storageID, nil, timestamppb.New(time.Now().Add(-5*24*time.Hour)), "expired-reference")
createS3Attachment(ctx, t, ts, user.ID, "presign-fresh", "fresh.txt", storageID, nil, freshTime, "still-valid-reference")
createS3Attachment(ctx, t, ts, user.ID, "presign-dangling", "dangling.txt", "missing-storage", nil, nil, "dangling-reference")
NewRunner(ts).RunOnce(ctx)
legacy := getAttachmentByUID(ctx, t, ts, "presign-legacy")
require.Equal(t, storageID, legacy.Payload.GetS3Object().GetStorageId())
require.NotNil(t, legacy.Payload.GetS3Object().GetLastPresignedTime())
require.Contains(t, legacy.Reference, "X-Amz-Signature=")
request, err := http.NewRequestWithContext(ctx, http.MethodGet, legacy.Reference, nil)
require.NoError(t, err)
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
defer response.Body.Close()
require.Equal(t, http.StatusOK, response.StatusCode)
downloaded, err := io.ReadAll(response.Body)
require.NoError(t, err)
require.Equal(t, content, downloaded)
expired := getAttachmentByUID(ctx, t, ts, "presign-expired")
require.NotEqual(t, "expired-reference", expired.Reference)
require.Contains(t, expired.Reference, "X-Amz-Signature=")
require.True(t, expired.Payload.GetS3Object().GetLastPresignedTime().AsTime().After(time.Now().Add(-time.Minute)))
fresh := getAttachmentByUID(ctx, t, ts, "presign-fresh")
require.Equal(t, "still-valid-reference", fresh.Reference)
require.Equal(t, freshTime.AsTime(), fresh.Payload.GetS3Object().GetLastPresignedTime().AsTime())
fallback := getAttachmentByUID(ctx, t, ts, "presign-dangling")
require.Contains(t, fallback.Reference, "X-Amz-Signature=")
require.NotNil(t, fallback.Payload.GetS3Object().GetLastPresignedTime())
require.Equal(t, "missing-storage", fallback.Payload.GetS3Object().GetStorageId())
}
func TestCheckAndPresignProcessesMultipleBatches(t *testing.T) {
ctx := context.Background()
fake := fakes3.New(t, "presign-batches")
config := fake.Config("presign-batches")
ts := teststore.NewTestingStore(ctx, t)
defer ts.Close()
const storageID = "s3-batches"
_, err := ts.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{
Storages: []*storepb.Storage{{
Id: storageID,
Name: "Batch S3",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: config},
}},
DefaultStorageId: storageID,
}},
})
require.NoError(t, err)
user, err := ts.CreateUser(ctx, &store.User{
Username: "presign-batch-user",
Role: store.RoleUser,
Email: "presign-batch-user@example.com",
})
require.NoError(t, err)
for i := 0; i < 101; i++ {
uid := fmt.Sprintf("presign-batch-%03d", i)
createS3Attachment(ctx, t, ts, user.ID, uid, uid+".txt", storageID, nil, nil, "")
}
NewRunner(ts).RunOnce(ctx)
s3StorageType := storepb.AttachmentStorageType_S3
attachments, err := ts.ListAttachments(ctx, &store.FindAttachment{StorageType: &s3StorageType, SkipDefaultLimit: true})
require.NoError(t, err)
require.Len(t, attachments, 101)
for _, attachment := range attachments {
require.Contains(t, attachment.Reference, "X-Amz-Signature=", attachment.UID)
require.NotNil(t, attachment.Payload.GetS3Object().GetLastPresignedTime(), attachment.UID)
}
}
func createS3Attachment(
ctx context.Context,
t *testing.T,
ts *store.Store,
creatorID int32,
uid string,
key string,
storageID string,
embeddedConfig *storepb.StorageS3Config,
lastPresignedTime *timestamppb.Timestamp,
reference string,
) {
t.Helper()
_, err := ts.CreateAttachment(ctx, &store.Attachment{
UID: uid,
CreatorID: creatorID,
Filename: uid + ".txt",
Type: "text/plain",
Size: 1,
StorageType: storepb.AttachmentStorageType_S3,
Reference: reference,
Payload: &storepb.AttachmentPayload{Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{
Key: key,
StorageId: storageID,
S3Config: embeddedConfig,
LastPresignedTime: lastPresignedTime,
},
}},
})
require.NoError(t, err)
}
func getAttachmentByUID(ctx context.Context, t *testing.T, ts *store.Store, uid string) *store.Attachment {
t.Helper()
attachment, err := ts.GetAttachment(ctx, &store.FindAttachment{UID: &uid})
require.NoError(t, err)
require.NotNil(t, attachment)
return attachment
}
func TestCloneAttachmentPayloadPreservesMotionMedia(t *testing.T) {
payload := &storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{

View file

@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/usememos/memos/internal/base"
"github.com/usememos/memos/internal/storage/s3"
"github.com/usememos/memos/internal/storage"
storepb "github.com/usememos/memos/proto/gen/store"
)
@ -228,26 +228,27 @@ func (s *Store) deleteAttachmentStorageImpl(ctx context.Context, attachment *Att
if s3ObjectPayload == nil {
return errors.Errorf("No s3 object found")
}
s3Config := s3ObjectPayload.S3Config
if s3Config == nil {
if instanceStorageSetting == nil {
var err error
instanceStorageSetting, err = s.GetInstanceStorageSetting(ctx)
if err != nil {
// Deletes resolve with the registry like reads do, so a legacy
// attachment picks up rotated credentials for its namespace instead
// of deleting with the stale embedded config.
if instanceStorageSetting == nil {
var err error
instanceStorageSetting, err = s.GetInstanceStorageSetting(ctx)
if err != nil {
if AttachmentNeedsInstanceStorageSetting(attachment) {
return errors.Wrap(err, "failed to get instance storage setting")
}
// The embedded config is sufficient on its own; keep the
// delete best-effort when the settings read fails.
instanceStorageSetting = nil
}
if instanceStorageSetting.S3Config == nil {
return errors.Errorf("S3 config is not found")
}
s3Config = instanceStorageSetting.S3Config
}
s3Client, err := s3.NewClient(ctx, s3Config)
driver, err := ResolveStorageDriver(ctx, instanceStorageSetting, s3ObjectPayload.StorageId, s3ObjectPayload.S3Config)
if err != nil {
return errors.Wrap(err, "Failed to create s3 client")
return errors.Wrap(err, "failed to resolve storage driver")
}
if err := s3Client.DeleteObject(ctx, s3ObjectPayload.Key); err != nil {
if err := driver.DeleteObject(ctx, s3ObjectPayload.Key); err != nil {
return errors.Wrap(err, "Failed to delete s3 object")
}
return nil
@ -273,13 +274,41 @@ func (s *Store) getAttachmentStorageCleanupInstanceSetting(ctx context.Context,
return nil, nil
}
// AttachmentNeedsInstanceStorageSetting reports whether S3 cleanup needs the instance fallback storage setting.
// ResolveAttachmentS3Driver resolves the storage driver referenced by an S3
// attachment payload, validating the payload and supplying the instance setting.
func (s *Store) ResolveAttachmentS3Driver(ctx context.Context, attachment *Attachment) (storage.Driver, *storepb.AttachmentPayload_S3Object, error) {
if attachment == nil {
return nil, nil, errors.New("attachment is missing")
}
if attachment.Payload == nil {
return nil, nil, errors.New("attachment payload is missing")
}
s3Object := attachment.Payload.GetS3Object()
if s3Object == nil {
return nil, nil, errors.New("S3 object payload is missing")
}
if s3Object.Key == "" {
return nil, nil, errors.New("S3 object key is missing")
}
instanceStorageSetting, err := s.GetInstanceStorageSetting(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get instance storage setting")
}
driver, err := ResolveStorageDriver(ctx, instanceStorageSetting, s3Object.StorageId, s3Object.S3Config)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to resolve storage driver")
}
return driver, s3Object, nil
}
// AttachmentNeedsInstanceStorageSetting reports whether cleanup should load
// the configured storage registry for an S3 attachment.
func AttachmentNeedsInstanceStorageSetting(attachment *Attachment) bool {
if attachment == nil || attachment.StorageType != storepb.AttachmentStorageType_S3 {
return false
}
s3ObjectPayload := attachment.Payload.GetS3Object()
return s3ObjectPayload != nil && s3ObjectPayload.S3Config == nil
return attachment.Payload.GetS3Object() != nil
}
func (s *Store) deleteAttachmentDerivedCaches(attachment *Attachment) {

View file

@ -233,25 +233,33 @@ func validateAndNormalizeDeploymentInstanceSetting(setting *storepb.InstanceSett
if storage == nil {
return errors.New("storageSetting must be populated for key STORAGE")
}
// Normalization would silently self-heal this misconfiguration to LOCAL;
// a deployment file declaring S3 without a config should fail loudly.
if storage.StorageType == storepb.InstanceStorageSetting_S3 && storage.S3Config == nil && len(storage.Storages) == 0 {
return errors.New("storageSetting.s3Config is required for S3")
}
NormalizeInstanceStorageSetting(storage)
if storage.UploadSizeLimitMb < 0 {
return errors.New("storageSetting.uploadSizeLimitMb must not be negative")
}
if storage.StorageType == storepb.InstanceStorageSetting_S3 {
if storage.S3Config == nil {
return errors.New("storageSetting.s3Config is required for S3")
defaultStorage := GetDefaultStorage(storage)
if defaultStorage != nil && defaultStorage.Type == storepb.StorageType_STORAGE_TYPE_S3 {
s3Config := defaultStorage.GetS3Config()
if s3Config == nil {
return errors.New("storageSetting default storage S3 config is required")
}
for _, field := range []struct {
name string
value string
}{
{name: "accessKeyId", value: storage.S3Config.AccessKeyId},
{name: "accessKeySecret", value: storage.S3Config.AccessKeySecret},
{name: "endpoint", value: storage.S3Config.Endpoint},
{name: "region", value: storage.S3Config.Region},
{name: "bucket", value: storage.S3Config.Bucket},
{name: "accessKeyId", value: s3Config.AccessKeyId},
{name: "accessKeySecret", value: s3Config.AccessKeySecret},
{name: "endpoint", value: s3Config.Endpoint},
{name: "region", value: s3Config.Region},
{name: "bucket", value: s3Config.Bucket},
} {
if strings.TrimSpace(field.value) == "" {
return errors.Errorf("storageSetting.s3Config.%s is required", field.name)
return errors.Errorf("storageSetting default S3 config.%s is required", field.name)
}
}
}

View file

@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
storepb "github.com/usememos/memos/proto/gen/store"
)
@ -36,6 +37,7 @@ func (s *Store) UpsertInstanceSetting(ctx context.Context, upsert *storepb.Insta
} else if upsert.Key == storepb.InstanceSettingKey_GENERAL {
valueBytes, err = protojson.Marshal(upsert.GetGeneralSetting())
} else if upsert.Key == storepb.InstanceSettingKey_STORAGE {
NormalizeInstanceStorageSetting(upsert.GetStorageSetting())
valueBytes, err = protojson.Marshal(upsert.GetStorageSetting())
} else if upsert.Key == storepb.InstanceSettingKey_MEMO_RELATED {
valueBytes, err = protojson.Marshal(upsert.GetMemoRelatedSetting())
@ -317,10 +319,12 @@ func (s *Store) GetInstanceStorageSetting(ctx context.Context) (*storepb.Instanc
return nil, errors.Wrap(err, "failed to get instance storage setting")
}
stored := instanceSetting.GetStorageSetting()
instanceStorageSetting := &storepb.InstanceStorageSetting{}
if instanceSetting != nil {
instanceStorageSetting = instanceSetting.GetStorageSetting()
if stored != nil {
instanceStorageSetting = proto.CloneOf(stored)
}
NormalizeInstanceStorageSetting(instanceStorageSetting)
if instanceStorageSetting.StorageType == storepb.InstanceStorageSetting_STORAGE_TYPE_UNSPECIFIED {
instanceStorageSetting.StorageType = defaultInstanceStorageType
}
@ -330,10 +334,16 @@ func (s *Store) GetInstanceStorageSetting(ctx context.Context) (*storepb.Instanc
if instanceStorageSetting.FilepathTemplate == "" {
instanceStorageSetting.FilepathTemplate = defaultInstanceFilepathTemplate
}
s.cacheInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: instanceStorageSetting},
})
// Only write back when normalization changed the cached value; on the common
// path the cache already holds the normalized setting.
if !proto.Equal(stored, instanceStorageSetting) {
s.cacheInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{
StorageSetting: proto.CloneOf(instanceStorageSetting),
},
})
}
return instanceStorageSetting, nil
}

View file

@ -0,0 +1,88 @@
-- Expands the legacy singleton storage setting into the named storage model.
-- Legacy fields remain in place so older clients and rollback versions can
-- continue to read the active storage configuration.
UPDATE system_setting
SET value = JSON_SET(
value,
'$.storages', JSON_ARRAY(JSON_OBJECT(
'id', 'database',
'name', 'Database',
'type', 'STORAGE_TYPE_DATABASE'
)),
'$.defaultStorageId', 'database'
)
WHERE name = 'STORAGE'
AND CASE
WHEN JSON_VALID(value) THEN
JSON_TYPE(value) = 'OBJECT'
AND NOT JSON_CONTAINS_PATH(value, 'one', '$.storages')
AND JSON_UNQUOTE(JSON_EXTRACT(value, '$.storageType')) IN ('DATABASE', '1')
ELSE FALSE
END;
UPDATE system_setting
SET value = JSON_SET(
value,
'$.storages', JSON_ARRAY(JSON_OBJECT(
'id', 'local',
'name', 'Local',
'type', 'STORAGE_TYPE_LOCAL'
)),
'$.defaultStorageId', 'local'
)
WHERE name = 'STORAGE'
AND CASE
WHEN JSON_VALID(value) THEN
JSON_TYPE(value) = 'OBJECT'
AND NOT JSON_CONTAINS_PATH(value, 'one', '$.storages')
AND (
NOT JSON_CONTAINS_PATH(value, 'one', '$.storageType')
OR JSON_UNQUOTE(JSON_EXTRACT(value, '$.storageType')) IN ('STORAGE_TYPE_UNSPECIFIED', 'LOCAL', '0', '2')
)
ELSE FALSE
END;
UPDATE system_setting
SET value = JSON_SET(
value,
'$.storages', JSON_ARRAY(JSON_OBJECT(
'id', 's3',
'name', 'S3',
'type', 'STORAGE_TYPE_S3',
's3Config', JSON_EXTRACT(value, '$.s3Config')
)),
'$.defaultStorageId', 's3'
)
WHERE name = 'STORAGE'
AND CASE
WHEN JSON_VALID(value) THEN
JSON_TYPE(value) = 'OBJECT'
AND NOT JSON_CONTAINS_PATH(value, 'one', '$.storages')
AND JSON_UNQUOTE(JSON_EXTRACT(value, '$.storageType')) IN ('S3', '3')
AND JSON_TYPE(JSON_EXTRACT(value, '$.s3Config')) = 'OBJECT'
ELSE FALSE
END;
-- Attachments created before S3 configs were embedded only stored the object
-- key. Bind them to the namespace captured above before that default changes.
UPDATE attachment
SET payload = JSON_SET(payload, '$.s3Object.storageId', 's3')
WHERE storage_type = 'S3'
AND CASE
WHEN JSON_VALID(payload) THEN
JSON_TYPE(payload) = 'OBJECT'
AND JSON_TYPE(JSON_EXTRACT(payload, '$.s3Object')) = 'OBJECT'
AND NOT JSON_CONTAINS_PATH(payload, 'one', '$.s3Object.storageId')
AND NOT JSON_CONTAINS_PATH(payload, 'one', '$.s3Object.s3Config')
ELSE FALSE
END
AND EXISTS (
SELECT 1
FROM system_setting AS storage_setting
WHERE storage_setting.name = 'STORAGE'
AND CASE
WHEN JSON_VALID(storage_setting.value) THEN
JSON_UNQUOTE(JSON_EXTRACT(storage_setting.value, '$.defaultStorageId')) = 's3'
ELSE FALSE
END
);

View file

@ -0,0 +1,104 @@
-- Expands the legacy singleton storage setting into the named storage model.
-- Legacy fields remain in place so older clients and rollback versions can
-- continue to read the active storage configuration.
UPDATE system_setting
SET value = jsonb_set(
jsonb_set(
value::jsonb,
'{storages}',
jsonb_build_array(jsonb_build_object(
'id', 'database',
'name', 'Database',
'type', 'STORAGE_TYPE_DATABASE'
))
),
'{defaultStorageId}',
to_jsonb('database'::text)
)::text
WHERE name = 'STORAGE'
AND CASE
WHEN pg_input_is_valid(value, 'jsonb') THEN
jsonb_typeof(value::jsonb) = 'object'
AND NOT value::jsonb ? 'storages'
AND value::jsonb->>'storageType' IN ('DATABASE', '1')
ELSE FALSE
END;
UPDATE system_setting
SET value = jsonb_set(
jsonb_set(
value::jsonb,
'{storages}',
jsonb_build_array(jsonb_build_object(
'id', 'local',
'name', 'Local',
'type', 'STORAGE_TYPE_LOCAL'
))
),
'{defaultStorageId}',
to_jsonb('local'::text)
)::text
WHERE name = 'STORAGE'
AND CASE
WHEN pg_input_is_valid(value, 'jsonb') THEN
jsonb_typeof(value::jsonb) = 'object'
AND NOT value::jsonb ? 'storages'
AND (
NOT value::jsonb ? 'storageType'
OR value::jsonb->>'storageType' IN ('STORAGE_TYPE_UNSPECIFIED', 'LOCAL', '0', '2')
)
ELSE FALSE
END;
UPDATE system_setting
SET value = jsonb_set(
jsonb_set(
value::jsonb,
'{storages}',
jsonb_build_array(jsonb_build_object(
'id', 's3',
'name', 'S3',
'type', 'STORAGE_TYPE_S3',
's3Config', value::jsonb->'s3Config'
))
),
'{defaultStorageId}',
to_jsonb('s3'::text)
)::text
WHERE name = 'STORAGE'
AND CASE
WHEN pg_input_is_valid(value, 'jsonb') THEN
jsonb_typeof(value::jsonb) = 'object'
AND NOT value::jsonb ? 'storages'
AND value::jsonb->>'storageType' IN ('S3', '3')
AND jsonb_typeof(value::jsonb->'s3Config') = 'object'
ELSE FALSE
END;
-- Attachments created before S3 configs were embedded only stored the object
-- key. Bind them to the namespace captured above before that default changes.
UPDATE attachment
SET payload = jsonb_set(
payload::jsonb,
'{s3Object,storageId}',
to_jsonb('s3'::text)
)::text
WHERE storage_type = 'S3'
AND CASE
WHEN pg_input_is_valid(payload, 'jsonb') THEN
jsonb_typeof(payload::jsonb) = 'object'
AND jsonb_typeof(payload::jsonb->'s3Object') = 'object'
AND NOT payload::jsonb->'s3Object' ? 'storageId'
AND NOT payload::jsonb->'s3Object' ? 's3Config'
ELSE FALSE
END
AND EXISTS (
SELECT 1
FROM system_setting AS storage_setting
WHERE storage_setting.name = 'STORAGE'
AND CASE
WHEN pg_input_is_valid(storage_setting.value, 'jsonb') THEN
storage_setting.value::jsonb->>'defaultStorageId' = 's3'
ELSE FALSE
END
);

View file

@ -0,0 +1,88 @@
-- Expands the legacy singleton storage setting into the named storage model.
-- Legacy fields remain in place so older clients and rollback versions can
-- continue to read the active storage configuration.
UPDATE system_setting
SET value = json_set(
value,
'$.storages', json_array(json_object(
'id', 'database',
'name', 'Database',
'type', 'STORAGE_TYPE_DATABASE'
)),
'$.defaultStorageId', 'database'
)
WHERE name = 'STORAGE'
AND CASE
WHEN json_valid(value) THEN
json_type(value) = 'object'
AND json_type(value, '$.storages') IS NULL
AND json_extract(value, '$.storageType') IN ('DATABASE', 1)
ELSE FALSE
END;
UPDATE system_setting
SET value = json_set(
value,
'$.storages', json_array(json_object(
'id', 'local',
'name', 'Local',
'type', 'STORAGE_TYPE_LOCAL'
)),
'$.defaultStorageId', 'local'
)
WHERE name = 'STORAGE'
AND CASE
WHEN json_valid(value) THEN
json_type(value) = 'object'
AND json_type(value, '$.storages') IS NULL
AND (
json_type(value, '$.storageType') IS NULL
OR json_extract(value, '$.storageType') IN ('STORAGE_TYPE_UNSPECIFIED', 'LOCAL', 0, 2)
)
ELSE FALSE
END;
UPDATE system_setting
SET value = json_set(
value,
'$.storages', json_array(json_object(
'id', 's3',
'name', 'S3',
'type', 'STORAGE_TYPE_S3',
's3Config', json_extract(value, '$.s3Config')
)),
'$.defaultStorageId', 's3'
)
WHERE name = 'STORAGE'
AND CASE
WHEN json_valid(value) THEN
json_type(value) = 'object'
AND json_type(value, '$.storages') IS NULL
AND json_extract(value, '$.storageType') IN ('S3', 3)
AND json_type(value, '$.s3Config') = 'object'
ELSE FALSE
END;
-- Attachments created before S3 configs were embedded only stored the object
-- key. Bind them to the namespace captured above before that default changes.
UPDATE attachment
SET payload = json_set(payload, '$.s3Object.storageId', 's3')
WHERE storage_type = 'S3'
AND CASE
WHEN json_valid(payload) THEN
json_type(payload) = 'object'
AND json_type(payload, '$.s3Object') = 'object'
AND json_type(payload, '$.s3Object.storageId') IS NULL
AND json_type(payload, '$.s3Object.s3Config') IS NULL
ELSE FALSE
END
AND EXISTS (
SELECT 1
FROM system_setting AS storage_setting
WHERE storage_setting.name = 'STORAGE'
AND CASE
WHEN json_valid(storage_setting.value) THEN
json_extract(storage_setting.value, '$.defaultStorageId') = 's3'
ELSE FALSE
END
);

486
store/storage.go Normal file
View file

@ -0,0 +1,486 @@
package store
import (
"context"
"crypto/sha256"
"encoding/hex"
"strings"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/usememos/memos/internal/storage"
storepb "github.com/usememos/memos/proto/gen/store"
)
const (
databaseStorageID = "database"
localStorageID = "local"
)
// NormalizeInstanceStorageSetting populates the named storage model from legacy
// fields and keeps the legacy fields synchronized for older clients.
func NormalizeInstanceStorageSetting(setting *storepb.InstanceStorageSetting) {
if setting == nil {
return
}
normalized := make([]*storepb.Storage, 0, len(setting.Storages)+1)
seenIDs := map[string]bool{}
for _, storage := range setting.Storages {
if storage == nil {
continue
}
normalizeStorage(storage)
if storage.Id == "" || seenIDs[storage.Id] {
continue
}
seenIDs[storage.Id] = true
normalized = append(normalized, storage)
}
setting.Storages = normalized
if setting.S3Config != nil {
legacyS3Storage := &storepb.Storage{
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: setting.S3Config},
}
// Equivalence is checked before normalizeStorage so the ID hash is only
// computed when the legacy config actually needs to be registered.
if findEquivalentStorage(setting.Storages, legacyS3Storage) == nil {
normalizeStorage(legacyS3Storage)
if legacyS3Storage.Id != "" {
setting.Storages = append(setting.Storages, legacyS3Storage)
}
}
}
if setting.DefaultStorageId == "" {
legacyStorage := storageFromLegacySetting(setting)
if legacyStorage != nil {
if existing := findEquivalentStorage(setting.Storages, legacyStorage); existing != nil {
setting.DefaultStorageId = existing.Id
} else if legacyStorage.Id != "" {
// An ID-less storage (S3 declared without a config) would wedge the
// fallback below into selecting it forever; let the LOCAL fallback
// self-heal instead.
setting.Storages = append(setting.Storages, legacyStorage)
setting.DefaultStorageId = legacyStorage.Id
}
}
}
if FindStorage(setting, setting.DefaultStorageId) == nil {
fallback := builtinStorage(storepb.StorageType_STORAGE_TYPE_LOCAL)
if len(setting.Storages) > 0 {
fallback = setting.Storages[0]
} else {
setting.Storages = append(setting.Storages, fallback)
}
setting.DefaultStorageId = fallback.Id
}
moveDefaultStorageFirst(setting)
synchronizeLegacyStorageFields(setting)
}
// PrepareInstanceStorageSettingUpdate preserves storages still referenced by
// existing attachments and assigns a new identity when a physical namespace changes.
func PrepareInstanceStorageSettingUpdate(incoming, existing *storepb.InstanceStorageSetting) error {
if incoming == nil {
return errors.New("storage setting is required")
}
if existing != nil {
NormalizeInstanceStorageSetting(existing)
}
// A request may reference a storage the server preserves without resending
// it; adopt the stored entry so the reference survives validation and
// normalization instead of being silently replaced by a fallback default.
if incoming.DefaultStorageId != "" && FindStorage(incoming, incoming.DefaultStorageId) == nil {
preserved := FindStorage(existing, incoming.DefaultStorageId)
if preserved == nil {
return errors.Errorf("default storage %q is not configured", incoming.DefaultStorageId)
}
incoming.Storages = append(incoming.Storages, proto.CloneOf(preserved))
}
legacyRequest := len(incoming.Storages) == 0
if !legacyRequest {
if err := validateInstanceStorages(incoming); err != nil {
return err
}
}
// Normalization guarantees every invariant validateInstanceStorages checks,
// so only the raw canonical request above needs explicit validation.
NormalizeInstanceStorageSetting(incoming)
if existing == nil {
return nil
}
existingByID := make(map[string]*storepb.Storage, len(existing.Storages))
for _, storage := range existing.Storages {
existingByID[storage.Id] = storage
}
if legacyRequest {
incomingDefault := GetDefaultStorage(incoming)
if matching := findEquivalentStorage(existing.Storages, incomingDefault); matching != nil {
incomingDefault.Id = matching.Id
if incomingDefault.Name == defaultStorageName(incomingDefault) {
incomingDefault.Name = matching.Name
}
incoming.DefaultStorageId = matching.Id
}
}
reidentifiedStorages := map[*storepb.Storage]bool{}
for _, storage := range incoming.Storages {
previous := existingByID[storage.Id]
if previous != nil && !StorageNamespaceEqual(previous, storage) {
oldID := storage.Id
storage.Id = storageID(storage)
reidentifiedStorages[storage] = true
if incoming.DefaultStorageId == oldID {
incoming.DefaultStorageId = storage.Id
}
previous = nil
}
if storage.GetS3Config() != nil && storage.GetS3Config().AccessKeySecret == "" {
if previous != nil && previous.GetS3Config() != nil {
storage.GetS3Config().AccessKeySecret = previous.GetS3Config().AccessKeySecret
} else {
candidate := findStorageCredentialSource(existing.Storages, storage)
// Legacy clients have always used an empty secret to preserve the
// current credential while editing any S3 field. Keep that contract,
// while canonical named-storage requests use the scoped lookup above.
if candidate == nil && legacyRequest {
candidate = findLegacyStorageCredentialSource(existing.Storages, storage.GetS3Config().AccessKeyId)
}
if candidate != nil {
storage.GetS3Config().AccessKeySecret = candidate.GetS3Config().AccessKeySecret
}
}
}
}
// A namespace edit can move a storage onto an identity already present in
// the request. Resolve the duplicate here in favor of the re-identified
// entry — it carries the admin's latest input — instead of letting the
// normalization dedupe silently discard it.
dedupedStorages := make([]*storepb.Storage, 0, len(incoming.Storages))
indexByID := make(map[string]int, len(incoming.Storages))
for _, storage := range incoming.Storages {
if i, ok := indexByID[storage.Id]; ok {
if reidentifiedStorages[storage] && !reidentifiedStorages[dedupedStorages[i]] {
dedupedStorages[i] = storage
}
continue
}
indexByID[storage.Id] = len(dedupedStorages)
dedupedStorages = append(dedupedStorages, storage)
}
incoming.Storages = dedupedStorages
// Secrets are backfilled from stored credentials above; a storage that still
// has none would fail at runtime with an opaque S3 auth error, so reject it
// at save time instead.
for _, storage := range incoming.Storages {
if s3Config := storage.GetS3Config(); s3Config != nil && s3Config.AccessKeyId != "" && s3Config.AccessKeySecret == "" {
return errors.Errorf("storage %q access key secret is required", storage.Id)
}
}
incomingIDs := make(map[string]bool, len(incoming.Storages))
for _, storage := range incoming.Storages {
incomingIDs[storage.Id] = true
}
for _, storage := range existing.Storages {
if !incomingIDs[storage.Id] {
incoming.Storages = append(incoming.Storages, proto.CloneOf(storage))
}
}
NormalizeInstanceStorageSetting(incoming)
return nil
}
// ResolveStorage resolves the configured storage referenced by an attachment.
// It returns a registry entry when one matches — its Id is the stable identity —
// or a synthesized storage wrapping the legacy config otherwise (empty Id).
// legacyS3Config supports attachments written before storage IDs were introduced.
func ResolveStorage(
setting *storepb.InstanceStorageSetting,
storageID string,
legacyS3Config *storepb.StorageS3Config,
) (*storepb.Storage, error) {
if storageID != "" {
if configuredStorage := FindStorage(setting, storageID); configuredStorage != nil {
return configuredStorage, nil
}
// The registry may have been rebuilt without preserving IDs (deployment
// config file, restored backup, rollback round-trip); fall through to the
// legacy chain rather than permanently orphaning the attachment.
}
s3Config := legacyS3Config
if s3Config == nil {
s3Config = setting.GetS3Config()
}
if s3Config != nil {
// Prefer the configured storage addressing the same namespace so legacy
// attachments pick up rotated credentials and transport options.
legacyStorage := &storepb.Storage{
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: s3Config},
}
if configuredStorage := findEquivalentStorage(setting.GetStorages(), legacyStorage); configuredStorage != nil {
return configuredStorage, nil
}
return legacyStorage, nil
}
if storageID != "" {
return nil, errors.Errorf("storage %q is not configured", storageID)
}
return nil, errors.New("attachment storage is not configured")
}
// ResolveStorageDriver resolves the configured storage referenced by an attachment
// and returns its driver. legacyS3Config supports attachments written before
// storage IDs were introduced.
func ResolveStorageDriver(
ctx context.Context,
setting *storepb.InstanceStorageSetting,
storageID string,
legacyS3Config *storepb.StorageS3Config,
) (storage.Driver, error) {
resolvedStorage, err := ResolveStorage(setting, storageID, legacyS3Config)
if err != nil {
return nil, err
}
return storage.NewDriver(ctx, resolvedStorage)
}
// FindStorage returns the configured storage with the given stable identifier.
func FindStorage(setting *storepb.InstanceStorageSetting, storageID string) *storepb.Storage {
if setting == nil || storageID == "" {
return nil
}
for _, storage := range setting.Storages {
if storage != nil && storage.Id == storageID {
return storage
}
}
return nil
}
// GetDefaultStorage returns the storage used for new attachments.
func GetDefaultStorage(setting *storepb.InstanceStorageSetting) *storepb.Storage {
if setting == nil {
return nil
}
return FindStorage(setting, setting.DefaultStorageId)
}
// StorageNamespaceEqual reports whether two configurations address the same
// physical storage namespace. Credentials and transport options are deliberately
// excluded so they can be rotated without changing storage identity.
func StorageNamespaceEqual(a, b *storepb.Storage) bool {
if a == nil || b == nil || a.Type != b.Type {
return false
}
if a.Type != storepb.StorageType_STORAGE_TYPE_S3 {
return true
}
aConfig, bConfig := a.GetS3Config(), b.GetS3Config()
if aConfig == nil || bConfig == nil {
return aConfig == nil && bConfig == nil
}
return normalizeEndpoint(aConfig.Endpoint) == normalizeEndpoint(bConfig.Endpoint) &&
strings.TrimSpace(aConfig.Region) == strings.TrimSpace(bConfig.Region) &&
strings.TrimSpace(aConfig.Bucket) == strings.TrimSpace(bConfig.Bucket)
}
func storageFromLegacySetting(setting *storepb.InstanceStorageSetting) *storepb.Storage {
storageType := storepb.StorageType(setting.StorageType)
if storageType == storepb.StorageType_STORAGE_TYPE_UNSPECIFIED {
// Match the 0.31 migration and the pre-registry runtime default: an
// unspecified type is LOCAL even when a legacy S3 config is present
// (the config is still registered as a selectable storage above).
storageType = storepb.StorageType_STORAGE_TYPE_LOCAL
}
storage := builtinStorage(storageType)
if storageType == storepb.StorageType_STORAGE_TYPE_S3 {
storage.Config = &storepb.Storage_S3Config{S3Config: setting.S3Config}
normalizeStorage(storage)
}
return storage
}
func builtinStorage(storageType storepb.StorageType) *storepb.Storage {
storage := &storepb.Storage{Type: storageType}
normalizeStorage(storage)
return storage
}
func normalizeStorage(storage *storepb.Storage) {
if storage == nil {
return
}
if storage.Type == storepb.StorageType_STORAGE_TYPE_UNSPECIFIED && storage.GetS3Config() != nil {
storage.Type = storepb.StorageType_STORAGE_TYPE_S3
}
if storage.Id == "" {
storage.Id = storageID(storage)
}
if strings.TrimSpace(storage.Name) == "" {
storage.Name = defaultStorageName(storage)
}
}
func storageID(storage *storepb.Storage) string {
if storage == nil {
return ""
}
switch storage.Type {
case storepb.StorageType_STORAGE_TYPE_DATABASE:
return databaseStorageID
case storepb.StorageType_STORAGE_TYPE_LOCAL:
return localStorageID
case storepb.StorageType_STORAGE_TYPE_S3:
config := storage.GetS3Config()
if config == nil {
return ""
}
namespace := strings.Join([]string{
normalizeEndpoint(config.Endpoint),
strings.TrimSpace(config.Region),
strings.TrimSpace(config.Bucket),
}, "\x00")
hash := sha256.Sum256([]byte(namespace))
return "s3-" + hex.EncodeToString(hash[:])[:16]
default:
return ""
}
}
func defaultStorageName(storage *storepb.Storage) string {
if storage == nil {
return ""
}
switch storage.Type {
case storepb.StorageType_STORAGE_TYPE_DATABASE:
return "Database"
case storepb.StorageType_STORAGE_TYPE_LOCAL:
return "Local"
case storepb.StorageType_STORAGE_TYPE_S3:
if bucket := strings.TrimSpace(storage.GetS3Config().GetBucket()); bucket != "" {
return bucket
}
return "S3"
default:
return "Storage"
}
}
func findEquivalentStorage(storages []*storepb.Storage, target *storepb.Storage) *storepb.Storage {
for _, storage := range storages {
if StorageNamespaceEqual(storage, target) {
return storage
}
}
return nil
}
func findStorageCredentialSource(storages []*storepb.Storage, target *storepb.Storage) *storepb.Storage {
targetConfig := target.GetS3Config()
if targetConfig == nil {
return nil
}
// Credentials may authorize multiple buckets, but must not cross S3
// providers that happen to use the same access key ID.
for _, storage := range storages {
config := storage.GetS3Config()
if config != nil &&
config.AccessKeyId == targetConfig.AccessKeyId &&
config.AccessKeySecret != "" &&
normalizeEndpoint(config.Endpoint) == normalizeEndpoint(targetConfig.Endpoint) &&
strings.TrimSpace(config.Region) == strings.TrimSpace(targetConfig.Region) {
return storage
}
}
return nil
}
func findLegacyStorageCredentialSource(storages []*storepb.Storage, accessKeyID string) *storepb.Storage {
for _, storage := range storages {
config := storage.GetS3Config()
if config != nil && config.AccessKeyId == accessKeyID && config.AccessKeySecret != "" {
return storage
}
}
return nil
}
// moveDefaultStorageFirst keeps the registry ordered by most recent activation.
// This lets clients fall back to the most recently used storage of a given type
// while attachments continue to resolve storages by ID.
func moveDefaultStorageFirst(setting *storepb.InstanceStorageSetting) {
for i, storage := range setting.Storages {
if storage.Id != setting.DefaultStorageId || i == 0 {
continue
}
copy(setting.Storages[1:i+1], setting.Storages[:i])
setting.Storages[0] = storage
return
}
}
func synchronizeLegacyStorageFields(setting *storepb.InstanceStorageSetting) {
storage := GetDefaultStorage(setting)
if storage == nil {
return
}
setting.StorageType = storepb.InstanceStorageSetting_StorageType(storage.Type)
if storage.GetS3Config() != nil {
setting.S3Config = proto.CloneOf(storage.GetS3Config())
return
}
// Keep the most recently configured S3 storage available to legacy
// attachments that predate both storage IDs and embedded configurations.
for _, configuredStorage := range setting.Storages {
if configuredStorage.GetS3Config() != nil {
setting.S3Config = proto.CloneOf(configuredStorage.GetS3Config())
return
}
}
setting.S3Config = nil
}
func validateInstanceStorages(setting *storepb.InstanceStorageSetting) error {
seenIDs := map[string]bool{}
for _, storage := range setting.Storages {
if storage == nil || storage.Id == "" {
return errors.New("storage ID is required")
}
if seenIDs[storage.Id] {
return errors.Errorf("duplicate storage ID %q", storage.Id)
}
seenIDs[storage.Id] = true
if storage.Type == storepb.StorageType_STORAGE_TYPE_UNSPECIFIED {
return errors.Errorf("storage %q type is required", storage.Id)
}
if storage.Type == storepb.StorageType_STORAGE_TYPE_S3 && storage.GetS3Config() == nil {
return errors.Errorf("storage %q S3 config is required", storage.Id)
}
}
if !seenIDs[setting.DefaultStorageId] {
return errors.Errorf("default storage %q is not configured", setting.DefaultStorageId)
}
return nil
}
func normalizeEndpoint(endpoint string) string {
return strings.TrimRight(strings.TrimSpace(endpoint), "/")
}

376
store/storage_test.go Normal file
View file

@ -0,0 +1,376 @@
package store_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/usememos/memos/internal/storage/s3"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
func TestNormalizeInstanceStorageSettingMigratesLegacyS3Config(t *testing.T) {
setting := legacyS3StorageSetting("https://s3.example.com", "memos", "secret")
store.NormalizeInstanceStorageSetting(setting)
require.Len(t, setting.Storages, 1)
require.NotEmpty(t, setting.DefaultStorageId)
configuredStorage := store.GetDefaultStorage(setting)
require.Equal(t, storepb.StorageType_STORAGE_TYPE_S3, configuredStorage.Type)
require.Equal(t, setting.DefaultStorageId, configuredStorage.Id)
require.Equal(t, "secret", configuredStorage.GetS3Config().AccessKeySecret)
require.NotSame(t, configuredStorage.GetS3Config(), setting.S3Config)
require.True(t, proto.Equal(configuredStorage.GetS3Config(), setting.S3Config))
}
func TestPrepareInstanceStorageSettingUpdateRotatesCredentialsInPlace(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "memos", "old-secret")
store.NormalizeInstanceStorageSetting(existing)
existingID := existing.DefaultStorageId
incoming := legacyS3StorageSetting("https://s3.example.com/", "memos", "")
incoming.S3Config.AccessKeyId = "new-access-key"
require.NoError(t, store.PrepareInstanceStorageSettingUpdate(incoming, existing))
require.Equal(t, existingID, incoming.DefaultStorageId)
require.Len(t, incoming.Storages, 1)
require.Equal(t, "old-secret", store.GetDefaultStorage(incoming).GetS3Config().AccessKeySecret)
}
func TestPrepareInstanceStorageSettingUpdatePreservesPreviousNamespace(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "old-bucket", "secret")
store.NormalizeInstanceStorageSetting(existing)
previousID := existing.DefaultStorageId
incoming := legacyS3StorageSetting("https://s3.example.com", "new-bucket", "")
require.NoError(t, store.PrepareInstanceStorageSettingUpdate(incoming, existing))
require.NotEqual(t, previousID, incoming.DefaultStorageId)
require.Len(t, incoming.Storages, 2)
require.NotNil(t, store.FindStorage(incoming, previousID))
require.Equal(t, "old-bucket", store.FindStorage(incoming, previousID).GetS3Config().Bucket)
require.Equal(t, "new-bucket", store.GetDefaultStorage(incoming).GetS3Config().Bucket)
require.Equal(t, "secret", store.GetDefaultStorage(incoming).GetS3Config().AccessKeySecret)
}
func TestPrepareInstanceStorageSettingUpdateDoesNotReuseCredentialsAcrossEndpoints(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "old-bucket", "secret")
store.NormalizeInstanceStorageSetting(existing)
incomingStorage := s3Storage("new-storage", "new-bucket")
incomingStorage.GetS3Config().AccessKeyId = "access-key"
incomingStorage.GetS3Config().Endpoint = "https://other-s3.example.com"
incoming := &storepb.InstanceStorageSetting{
DefaultStorageId: incomingStorage.Id,
Storages: []*storepb.Storage{incomingStorage},
}
// The stored secret must not leak to a different endpoint, and a storage
// without any recoverable secret must fail loudly instead of persisting
// empty credentials that break uploads at runtime.
err := store.PrepareInstanceStorageSettingUpdate(incoming, existing)
require.ErrorContains(t, err, "access key secret is required")
}
func TestPrepareInstanceStorageSettingUpdateKeepsReferencedDefault(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "memos", "secret")
store.NormalizeInstanceStorageSetting(existing)
existingID := existing.DefaultStorageId
// A default-only request references a storage the server preserves without
// resending it; the requested default must survive, not reset to local.
incoming := &storepb.InstanceStorageSetting{DefaultStorageId: existingID}
require.NoError(t, store.PrepareInstanceStorageSettingUpdate(incoming, existing))
require.Equal(t, existingID, incoming.DefaultStorageId)
require.Equal(t, storepb.StorageType_STORAGE_TYPE_S3, store.GetDefaultStorage(incoming).GetType())
require.Equal(t, "secret", store.GetDefaultStorage(incoming).GetS3Config().AccessKeySecret)
unknown := &storepb.InstanceStorageSetting{DefaultStorageId: "unknown"}
err := store.PrepareInstanceStorageSettingUpdate(unknown, existing)
require.ErrorContains(t, err, `default storage "unknown" is not configured`)
}
func TestPrepareInstanceStorageSettingUpdateKeepsEditedStorageOnIdentityCollision(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "memos", "secret-a")
store.NormalizeInstanceStorageSetting(existing)
hashID := existing.DefaultStorageId
otherStorage := s3Storage("custom-b", "other-bucket")
otherStorage.GetS3Config().AccessKeyId = "key-b"
otherStorage.GetS3Config().AccessKeySecret = "secret-b"
existing.Storages = append(existing.Storages, otherStorage)
// Editing custom-b onto the first storage's namespace re-identifies it onto
// the same hash ID; the edited entry must win, not be silently dropped.
incoming := &storepb.InstanceStorageSetting{
DefaultStorageId: "custom-b",
Storages: []*storepb.Storage{
proto.CloneOf(store.FindStorage(existing, hashID)),
{
Id: "custom-b",
Name: "Rotated",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
AccessKeyId: "key-b-new",
AccessKeySecret: "secret-b-new",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
}},
},
},
}
require.NoError(t, store.PrepareInstanceStorageSettingUpdate(incoming, existing))
require.Equal(t, hashID, incoming.DefaultStorageId)
require.Equal(t, "key-b-new", store.GetDefaultStorage(incoming).GetS3Config().AccessKeyId)
require.Equal(t, "secret-b-new", store.GetDefaultStorage(incoming).GetS3Config().AccessKeySecret)
// The other storage's previous identity stays available to attachments.
require.Equal(t, "other-bucket", store.FindStorage(incoming, "custom-b").GetS3Config().Bucket)
}
func TestPrepareInstanceStorageSettingUpdateDoesNotMutateExistingStorageIdentity(t *testing.T) {
existing := legacyS3StorageSetting("https://s3.example.com", "old-bucket", "secret")
store.NormalizeInstanceStorageSetting(existing)
previousID := existing.DefaultStorageId
incoming := &storepb.InstanceStorageSetting{
DefaultStorageId: previousID,
Storages: []*storepb.Storage{
{
Id: previousID,
Name: "Replacement",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "new-bucket",
}},
},
},
}
require.NoError(t, store.PrepareInstanceStorageSettingUpdate(incoming, existing))
require.NotEqual(t, previousID, incoming.DefaultStorageId)
require.Len(t, incoming.Storages, 2)
require.Equal(t, "old-bucket", store.FindStorage(incoming, previousID).GetS3Config().Bucket)
}
func TestPrepareInstanceStorageSettingUpdateRejectsInvalidCanonicalRegistry(t *testing.T) {
t.Run("missing default storage", func(t *testing.T) {
incoming := &storepb.InstanceStorageSetting{
DefaultStorageId: "missing",
Storages: []*storepb.Storage{
{Id: "local", Type: storepb.StorageType_STORAGE_TYPE_LOCAL},
},
}
err := store.PrepareInstanceStorageSettingUpdate(incoming, nil)
require.ErrorContains(t, err, `default storage "missing" is not configured`)
})
t.Run("duplicate storage ID", func(t *testing.T) {
incoming := &storepb.InstanceStorageSetting{
DefaultStorageId: "duplicate",
Storages: []*storepb.Storage{
{Id: "duplicate", Type: storepb.StorageType_STORAGE_TYPE_LOCAL},
{Id: "duplicate", Type: storepb.StorageType_STORAGE_TYPE_DATABASE},
},
}
err := store.PrepareInstanceStorageSettingUpdate(incoming, nil)
require.ErrorContains(t, err, `duplicate storage ID "duplicate"`)
})
}
func TestNormalizeInstanceStorageSettingKeepsMostRecentlyActivatedStorageFirst(t *testing.T) {
recent := s3Storage("recent", "recent-bucket")
old := s3Storage("old", "old-bucket")
local := &storepb.Storage{Id: "local", Type: storepb.StorageType_STORAGE_TYPE_LOCAL}
setting := &storepb.InstanceStorageSetting{
DefaultStorageId: recent.Id,
Storages: []*storepb.Storage{old, recent, local},
}
store.NormalizeInstanceStorageSetting(setting)
require.Equal(t, []string{"recent", "old", "local"}, storageIDs(setting.Storages))
setting.DefaultStorageId = local.Id
store.NormalizeInstanceStorageSetting(setting)
require.Equal(t, []string{"local", "recent", "old"}, storageIDs(setting.Storages))
require.Equal(t, "recent-bucket", setting.S3Config.GetBucket())
}
func TestNormalizeInstanceStorageSettingSelfHealsS3WithoutConfig(t *testing.T) {
setting := &storepb.InstanceStorageSetting{StorageType: storepb.InstanceStorageSetting_S3}
store.NormalizeInstanceStorageSetting(setting)
require.Equal(t, "local", setting.DefaultStorageId)
require.Equal(t, storepb.StorageType_STORAGE_TYPE_LOCAL, store.GetDefaultStorage(setting).GetType())
for _, configuredStorage := range setting.Storages {
require.NotEmpty(t, configuredStorage.Id)
}
}
func TestNormalizeInstanceStorageSettingUnspecifiedTypeDefaultsToLocal(t *testing.T) {
// Matches the 0.31 migration and the pre-registry runtime default: a setting
// that never chose a type stays LOCAL even when an S3 config is present.
setting := &storepb.InstanceStorageSetting{
S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
},
}
store.NormalizeInstanceStorageSetting(setting)
require.Equal(t, storepb.StorageType_STORAGE_TYPE_LOCAL, store.GetDefaultStorage(setting).GetType())
var s3Count int
for _, configuredStorage := range setting.Storages {
if configuredStorage.GetType() == storepb.StorageType_STORAGE_TYPE_S3 {
s3Count++
}
}
require.Equal(t, 1, s3Count, "the legacy S3 config must stay registered as a selectable storage")
}
func TestResolveStorageFallsBackWhenStorageIDMissing(t *testing.T) {
setting := legacyS3StorageSetting("https://s3.example.com", "memos", "secret")
store.NormalizeInstanceStorageSetting(setting)
// The registry may be rebuilt without preserving IDs (deployment file,
// restored backup); a dangling reference must fall back to the namespace
// chain instead of permanently orphaning the attachment.
resolved, err := store.ResolveStorage(setting, "s3-dangling", nil)
require.NoError(t, err)
require.Equal(t, setting.DefaultStorageId, resolved.Id)
embedded := &storepb.StorageS3Config{
AccessKeyId: "old-access-key",
AccessKeySecret: "old-secret",
Endpoint: "https://s3.example.com/",
Region: "us-east-1",
Bucket: "memos",
}
resolved, err = store.ResolveStorage(setting, "s3-dangling", embedded)
require.NoError(t, err)
require.Equal(t, setting.DefaultStorageId, resolved.Id)
_, err = store.ResolveStorage(&storepb.InstanceStorageSetting{}, "s3-dangling", nil)
require.ErrorContains(t, err, `storage "s3-dangling" is not configured`)
}
func TestResolveStorageDriverByStorageID(t *testing.T) {
setting := &storepb.InstanceStorageSetting{
DefaultStorageId: "primary",
Storages: []*storepb.Storage{
{
Id: "primary",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
}},
},
},
}
driver, err := store.ResolveStorageDriver(context.Background(), setting, "primary", nil)
require.NoError(t, err)
require.IsType(t, &s3.Driver{}, driver)
_, err = store.ResolveStorageDriver(context.Background(), setting, "missing", nil)
require.ErrorContains(t, err, `storage "missing" is not configured`)
}
func TestResolveStorageDriverSupportsLegacyEmbeddedConfig(t *testing.T) {
legacyConfig := &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://legacy-s3.example.com",
Region: "us-east-1",
Bucket: "legacy",
}
driver, err := store.ResolveStorageDriver(context.Background(), nil, "", legacyConfig)
require.NoError(t, err)
require.IsType(t, &s3.Driver{}, driver)
}
func TestResolveStorageDriverUsesCurrentCredentialsForLegacyAttachment(t *testing.T) {
legacyConfig := &storepb.StorageS3Config{
AccessKeyId: "old-access-key",
AccessKeySecret: "old-secret",
Endpoint: "https://s3.example.com/",
Region: "us-east-1",
Bucket: "memos",
}
setting := &storepb.InstanceStorageSetting{
DefaultStorageId: "primary",
Storages: []*storepb.Storage{
{
Id: "primary",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
AccessKeyId: "new-access-key",
AccessKeySecret: "new-secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
UsePathStyle: true,
}},
},
},
}
driver, err := store.ResolveStorageDriver(context.Background(), setting, "", legacyConfig)
require.NoError(t, err)
s3Driver, ok := driver.(*s3.Driver)
require.True(t, ok)
require.True(t, s3Driver.Client.Options().UsePathStyle)
}
func legacyS3StorageSetting(endpoint, bucket, secret string) *storepb.InstanceStorageSetting {
return &storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_S3,
S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: secret,
Endpoint: endpoint,
Region: "us-east-1",
Bucket: bucket,
},
}
}
func s3Storage(id, bucket string) *storepb.Storage {
return &storepb.Storage{
Id: id,
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: bucket,
}},
}
}
func storageIDs(storages []*storepb.Storage) []string {
ids := make([]string, 0, len(storages))
for _, configuredStorage := range storages {
ids = append(ids, configuredStorage.Id)
}
return ids
}

View file

@ -47,6 +47,7 @@ func TestAttachmentNeedsInstanceStorageSetting(t *testing.T) {
},
},
},
want: true,
},
{
name: "s3 attachment without embedded config",
@ -60,6 +61,18 @@ func TestAttachmentNeedsInstanceStorageSetting(t *testing.T) {
},
want: true,
},
{
name: "s3 attachment with storage ID",
attachment: &store.Attachment{
StorageType: storepb.AttachmentStorageType_S3,
Payload: &storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{StorageId: "s3-primary"},
},
},
},
want: true,
},
}
for _, test := range tests {
@ -84,7 +97,7 @@ func TestAttachmentStore(t *testing.T) {
Size: 637607,
Payload: &storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{Key: "attachments/test.jpg"},
S3Object: &storepb.AttachmentPayload_S3Object{Key: "attachments/test.jpg", StorageId: "s3-primary"},
},
MotionMedia: &storepb.MotionMedia{
Family: storepb.MotionMediaFamily_APPLE_LIVE_PHOTO,
@ -115,6 +128,7 @@ func TestAttachmentStore(t *testing.T) {
require.Equal(t, "Test Camera", attachment.Payload.GetMediaMetadata().GetPhoto().GetCameraMake())
require.Equal(t, int32(6), attachment.Payload.GetMediaMetadata().GetPhoto().GetSourceExifOrientation())
require.Equal(t, "attachments/test.jpg", attachment.Payload.GetS3Object().GetKey())
require.Equal(t, "s3-primary", attachment.Payload.GetS3Object().GetStorageId())
require.Equal(t, "live-photo-pair", attachment.Payload.GetMotionMedia().GetGroupId())
notFoundAttachment, err := ts.GetAttachment(ctx, &store.FindAttachment{

View file

@ -2,6 +2,7 @@ package test
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
@ -221,6 +222,51 @@ func TestInstanceSettingStorageSetting(t *testing.T) {
ts.Close()
}
func TestInstanceStorageSettingCacheIsolation(t *testing.T) {
t.Parallel()
ctx := context.Background()
ts := NewTestingStore(ctx, t)
defer ts.Close()
_, err := ts.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{
Key: storepb.InstanceSettingKey_STORAGE,
Value: &storepb.InstanceSetting_StorageSetting{
StorageSetting: &storepb.InstanceStorageSetting{StorageType: storepb.InstanceStorageSetting_LOCAL},
},
})
require.NoError(t, err)
first, err := ts.GetInstanceStorageSetting(ctx)
require.NoError(t, err)
require.NotEmpty(t, first.Storages)
first.Storages[0].Name = "caller mutation"
second, err := ts.GetInstanceStorageSetting(ctx)
require.NoError(t, err)
require.Equal(t, "Local", second.Storages[0].Name)
start := make(chan struct{})
errCh := make(chan error, 16)
var wg sync.WaitGroup
for range 16 {
wg.Go(func() {
<-start
for range 50 {
if _, err := ts.GetInstanceStorageSetting(ctx); err != nil {
errCh <- err
return
}
}
})
}
close(start)
wg.Wait()
close(errCh)
for err := range errCh {
require.NoError(t, err)
}
}
func TestInstanceSettingTagsSetting(t *testing.T) {
t.Parallel()
ctx := context.Background()

View file

@ -11,6 +11,7 @@ import (
colorpb "google.golang.org/genproto/googleapis/type/color"
"google.golang.org/protobuf/encoding/protojson"
"github.com/usememos/memos/internal/testutil/minio"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
@ -114,6 +115,329 @@ func TestMigrationMultipleReRuns(t *testing.T) {
require.Equal(t, initialVersion, finalVersion, "version should remain unchanged after multiple re-runs")
}
// TestMigrationStorageSetting verifies that the legacy singleton storage
// configuration is expanded into a named storage without removing fields used
// by older clients and rollback versions.
func TestMigrationStorageSetting(t *testing.T) {
ctx := context.Background()
driver := getDriverFromEnv()
legacyDatabase, err := protojson.Marshal(&storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_DATABASE,
})
require.NoError(t, err)
legacyLocal, err := protojson.Marshal(&storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_LOCAL,
FilepathTemplate: "assets/{filename}",
})
require.NoError(t, err)
legacyS3, err := protojson.Marshal(&storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_S3,
S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
},
})
require.NoError(t, err)
alreadyNamed, err := protojson.Marshal(&storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_S3,
DefaultStorageId: "custom-s3",
S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
},
Storages: []*storepb.Storage{
{
Id: "custom-s3",
Name: "Primary",
Type: storepb.StorageType_STORAGE_TYPE_S3,
Config: &storepb.Storage_S3Config{S3Config: &storepb.StorageS3Config{
AccessKeyId: "access-key",
AccessKeySecret: "secret",
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "memos",
}},
},
},
})
require.NoError(t, err)
tests := []struct {
name string
value string
wantStorageID string
wantStorageType storepb.StorageType
wantLegacyType storepb.InstanceStorageSetting_StorageType
wantName string
wantRawUnchanged bool
}{
{
name: "database",
value: string(legacyDatabase),
wantStorageID: "database",
wantStorageType: storepb.StorageType_STORAGE_TYPE_DATABASE,
wantLegacyType: storepb.InstanceStorageSetting_DATABASE,
wantName: "Database",
},
{
name: "local",
value: string(legacyLocal),
wantStorageID: "local",
wantStorageType: storepb.StorageType_STORAGE_TYPE_LOCAL,
wantLegacyType: storepb.InstanceStorageSetting_LOCAL,
wantName: "Local",
},
{
name: "s3",
value: string(legacyS3),
wantStorageID: "s3",
wantStorageType: storepb.StorageType_STORAGE_TYPE_S3,
wantLegacyType: storepb.InstanceStorageSetting_S3,
wantName: "S3",
},
{
name: "already named",
value: string(alreadyNamed),
wantStorageID: "custom-s3",
wantStorageType: storepb.StorageType_STORAGE_TYPE_S3,
wantLegacyType: storepb.InstanceStorageSetting_S3,
wantName: "Primary",
},
{
name: "invalid JSON",
value: `{oops}`,
wantRawUnchanged: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dsn := getTestingProfileForDriver(t, driver).DSN
db, err := sql.Open(driver, dsn)
require.NoError(t, err)
_, err = db.ExecContext(ctx, legacySchemaFixture(driver))
require.NoError(t, err)
basicSettingBytes, err := protojson.Marshal(&storepb.InstanceBasicSetting{SchemaVersion: "0.31.1"})
require.NoError(t, err)
insertSetting := "INSERT INTO system_setting (name, value, description) VALUES (?, ?, '')"
if driver == "postgres" {
insertSetting = "INSERT INTO system_setting (name, value, description) VALUES ($1, $2, '')"
}
_, err = db.ExecContext(ctx, insertSetting, "BASIC", string(basicSettingBytes))
require.NoError(t, err)
_, err = db.ExecContext(ctx, insertSetting, "STORAGE", test.value)
require.NoError(t, err)
if test.name == "s3" {
insertStorageMigrationAttachments(ctx, t, db, driver)
}
require.NoError(t, db.Close())
ts := NewTestingStoreWithDSN(ctx, t, driver, dsn)
require.NoError(t, ts.Migrate(ctx))
defer ts.Close()
if test.wantRawUnchanged {
query := "SELECT value FROM system_setting WHERE name = ?"
if driver == "postgres" {
query = "SELECT value FROM system_setting WHERE name = $1"
}
var raw string
require.NoError(t, ts.GetDriver().GetDB().QueryRowContext(ctx, query, "STORAGE").Scan(&raw))
require.Equal(t, test.value, raw)
return
}
stored, err := ts.GetStoredInstanceSetting(ctx, &store.FindInstanceSetting{Name: storepb.InstanceSettingKey_STORAGE.String()})
require.NoError(t, err)
require.NotNil(t, stored)
setting := stored.GetStorageSetting()
require.Equal(t, test.wantLegacyType, setting.GetStorageType(), "legacy storage type must be preserved")
require.Equal(t, test.wantStorageID, setting.GetDefaultStorageId())
require.Len(t, setting.GetStorages(), 1)
require.Equal(t, test.wantStorageID, setting.GetStorages()[0].GetId())
require.Equal(t, test.wantName, setting.GetStorages()[0].GetName())
require.Equal(t, test.wantStorageType, setting.GetStorages()[0].GetType())
if test.wantStorageType == storepb.StorageType_STORAGE_TYPE_S3 {
require.Equal(t, "secret", setting.GetS3Config().GetAccessKeySecret(), "legacy S3 config must be preserved")
require.Equal(t, "secret", setting.GetStorages()[0].GetS3Config().GetAccessKeySecret())
}
if test.name == "s3" {
assertStorageMigrationAttachments(ctx, t, ts)
}
})
}
}
func insertStorageMigrationAttachments(ctx context.Context, t *testing.T, db *sql.DB, driver string) {
t.Helper()
legacyPayload, err := protojson.Marshal(&storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{Key: "legacy/no-config.txt"},
},
})
require.NoError(t, err)
embeddedPayload, err := protojson.Marshal(&storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{
Key: "legacy/embedded-config.txt",
S3Config: &storepb.StorageS3Config{
Endpoint: "https://old-s3.example.com",
Region: "us-east-1",
Bucket: "old-bucket",
},
},
},
})
require.NoError(t, err)
namedPayload, err := protojson.Marshal(&storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{Key: "named/object.txt", StorageId: "existing-s3"},
},
})
require.NoError(t, err)
insertAttachment := "INSERT INTO attachment (uid, creator_id, filename, type, size, storage_type, reference, payload) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
if driver == "postgres" {
insertAttachment = "INSERT INTO attachment (uid, creator_id, filename, type, size, storage_type, reference, payload) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
}
for _, attachment := range []struct {
uid string
payload []byte
}{
{uid: "legacy-s3-no-config", payload: legacyPayload},
{uid: "legacy-s3-embedded-config", payload: embeddedPayload},
{uid: "legacy-s3-existing-id", payload: namedPayload},
} {
_, err := db.ExecContext(ctx, insertAttachment, attachment.uid, 1, attachment.uid+".txt", "text/plain", 1, "S3", "", string(attachment.payload))
require.NoError(t, err)
}
}
func assertStorageMigrationAttachments(ctx context.Context, t *testing.T, ts *store.Store) {
t.Helper()
find := func(uid string) *storepb.AttachmentPayload_S3Object {
query := "SELECT payload FROM attachment WHERE uid = ?"
if getDriverFromEnv() == "postgres" {
query = "SELECT payload FROM attachment WHERE uid = $1"
}
var rawPayload []byte
err := ts.GetDriver().GetDB().QueryRowContext(ctx, query, uid).Scan(&rawPayload)
require.NoError(t, err)
payload := &storepb.AttachmentPayload{}
require.NoError(t, protojson.Unmarshal(rawPayload, payload))
require.NotNil(t, payload.GetS3Object())
return payload.GetS3Object()
}
legacy := find("legacy-s3-no-config")
require.Equal(t, "s3", legacy.GetStorageId())
require.Nil(t, legacy.GetS3Config())
embedded := find("legacy-s3-embedded-config")
require.Empty(t, embedded.GetStorageId(), "embedded namespace must not be rebound to the current storage")
require.Equal(t, "old-bucket", embedded.GetS3Config().GetBucket())
named := find("legacy-s3-existing-id")
require.Equal(t, "existing-s3", named.GetStorageId())
}
// TestMigrationLegacyS3AttachmentMinIO verifies the storage upgrade path for an
// attachment created before storage IDs existed. The migration must bind the
// payload to the migrated registry entry, after which the production resolver
// and driver must read and delete it using the real S3 protocol.
func TestMigrationLegacyS3AttachmentMinIO(t *testing.T) {
ctx := context.Background()
driver := getDriverFromEnv()
server := minio.New(t, "legacy-attachments")
config := server.Config("legacy-attachments")
content := []byte("attachment uploaded before named storage migration")
const objectKey = "legacy/no-storage-id.txt"
require.NoError(t, server.PutObject(config.Bucket, objectKey, "text/plain", content))
storageSetting, err := protojson.Marshal(&storepb.InstanceStorageSetting{
StorageType: storepb.InstanceStorageSetting_S3,
S3Config: config,
})
require.NoError(t, err)
attachmentPayload, err := protojson.Marshal(&storepb.AttachmentPayload{
Payload: &storepb.AttachmentPayload_S3Object_{
S3Object: &storepb.AttachmentPayload_S3Object{Key: objectKey},
},
})
require.NoError(t, err)
dsn := getTestingProfileForDriver(t, driver).DSN
db, err := sql.Open(driver, dsn)
require.NoError(t, err)
_, err = db.ExecContext(ctx, legacySchemaFixture(driver))
require.NoError(t, err)
basicSetting, err := protojson.Marshal(&storepb.InstanceBasicSetting{SchemaVersion: "0.31.1"})
require.NoError(t, err)
insertSetting := "INSERT INTO system_setting (name, value, description) VALUES (?, ?, '')"
insertAttachment := "INSERT INTO attachment (uid, creator_id, filename, type, size, storage_type, reference, payload) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
if driver == "postgres" {
insertSetting = "INSERT INTO system_setting (name, value, description) VALUES ($1, $2, '')"
insertAttachment = "INSERT INTO attachment (uid, creator_id, filename, type, size, storage_type, reference, payload) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
}
_, err = db.ExecContext(ctx, insertSetting, "BASIC", string(basicSetting))
require.NoError(t, err)
_, err = db.ExecContext(ctx, insertSetting, "STORAGE", string(storageSetting))
require.NoError(t, err)
_, err = db.ExecContext(
ctx,
insertAttachment,
"legacy-s3-minio",
1,
"legacy.txt",
"text/plain",
len(content),
"S3",
"",
string(attachmentPayload),
)
require.NoError(t, err)
require.NoError(t, db.Close())
ts := NewTestingStoreWithDSN(ctx, t, driver, dsn)
require.NoError(t, ts.Migrate(ctx))
defer ts.Close()
query := "SELECT payload FROM attachment WHERE uid = ?"
if driver == "postgres" {
query = "SELECT payload FROM attachment WHERE uid = $1"
}
var rawPayload []byte
err = ts.GetDriver().GetDB().QueryRowContext(ctx, query, "legacy-s3-minio").Scan(&rawPayload)
require.NoError(t, err)
migratedPayload := &storepb.AttachmentPayload{}
require.NoError(t, protojson.Unmarshal(rawPayload, migratedPayload))
s3Object := migratedPayload.GetS3Object()
require.NotNil(t, s3Object)
require.Equal(t, "s3", s3Object.GetStorageId())
require.Nil(t, s3Object.GetS3Config())
storedSetting, err := ts.GetStoredInstanceSetting(ctx, &store.FindInstanceSetting{Name: storepb.InstanceSettingKey_STORAGE.String()})
require.NoError(t, err)
require.NotNil(t, storedSetting)
storageDriver, err := store.ResolveStorageDriver(ctx, storedSetting.GetStorageSetting(), s3Object.StorageId, s3Object.S3Config)
require.NoError(t, err)
downloaded, err := storageDriver.GetObject(ctx, s3Object.Key)
require.NoError(t, err)
require.Equal(t, content, downloaded)
require.NoError(t, storageDriver.DeleteObject(ctx, s3Object.Key))
_, err = server.GetObject(config.Bucket, objectKey)
require.Error(t, err, "deleting the migrated attachment must remove its MinIO object")
}
// TestMigrationMemoViewSetting verifies the 0.31 rename of the SHORTCUTS user setting
// to MEMO_VIEWS on every driver, since each one rewrites the stored JSON with its own
// dialect-specific functions.
@ -255,6 +579,11 @@ func TestMigrationCopiesInstanceTagsToUserSettings(t *testing.T) {
CREATE TABLE memo (
id INTEGER PRIMARY KEY AUTOINCREMENT
);
CREATE TABLE attachment (
id INTEGER PRIMARY KEY AUTOINCREMENT,
storage_type TEXT NOT NULL DEFAULT '',
payload TEXT NOT NULL DEFAULT '{}'
);
`)
require.NoError(t, err)
@ -401,6 +730,20 @@ func legacySchemaFixture(driver string) string {
UNIQUE(user_id, ` + "`key`" + `)
);
CREATE TABLE memo (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE attachment (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
uid VARCHAR(256) NOT NULL UNIQUE,
creator_id INT NOT NULL,
created_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
filename TEXT NOT NULL,
type VARCHAR(256) NOT NULL DEFAULT '',
size INT NOT NULL DEFAULT 0,
memo_id INT DEFAULT NULL,
storage_type VARCHAR(256) NOT NULL DEFAULT '',
reference TEXT NOT NULL,
payload TEXT NOT NULL
);
`
case "postgres":
return `
@ -429,6 +772,21 @@ func legacySchemaFixture(driver string) string {
UNIQUE(user_id, key)
);
CREATE TABLE memo (id SERIAL PRIMARY KEY);
CREATE TABLE attachment (
id SERIAL PRIMARY KEY,
uid TEXT NOT NULL UNIQUE,
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
filename TEXT NOT NULL,
blob BYTEA,
type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
memo_id INTEGER DEFAULT NULL,
storage_type TEXT NOT NULL DEFAULT '',
reference TEXT NOT NULL DEFAULT '',
payload TEXT NOT NULL DEFAULT '{}'
);
`
case "sqlite":
return `
@ -458,6 +816,21 @@ func legacySchemaFixture(driver string) string {
UNIQUE(user_id, key)
);
CREATE TABLE memo (id INTEGER PRIMARY KEY AUTOINCREMENT);
CREATE TABLE attachment (
id INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT NOT NULL UNIQUE,
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
filename TEXT NOT NULL DEFAULT '',
blob BLOB DEFAULT NULL,
type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
memo_id INTEGER,
storage_type TEXT NOT NULL DEFAULT '',
reference TEXT NOT NULL DEFAULT '',
payload TEXT NOT NULL DEFAULT '{}'
);
`
default:
return ""

View file

@ -70,7 +70,8 @@
"tailwind-merge": "^3.5.0",
"tailwindcss": "^4.2.4",
"unified": "^11.0.5",
"unist-util-visit": "^5.1.0"
"unist-util-visit": "^5.1.0",
"uuid": "^11.1.0"
},
"devDependencies": {
"@babel/core": "7.29.0",

View file

@ -173,6 +173,9 @@ importers:
unist-util-visit:
specifier: ^5.1.0
version: 5.1.0
uuid:
specifier: ^11.1.0
version: 11.1.0
devDependencies:
'@babel/core':
specifier: 7.29.0

View file

@ -2,6 +2,7 @@ import { create } from "@bufbuild/protobuf";
import { isEqual } from "lodash-es";
import { CloudIcon, DatabaseIcon, FolderIcon, LucideIcon } from "lucide-react";
import React, { useEffect, useMemo, useState } from "react";
import { v4 as uuidv4 } from "uuid";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
@ -12,11 +13,14 @@ import { useInstance } from "@/contexts/InstanceContext";
import { cn } from "@/lib/utils";
import {
InstanceSetting_Key,
InstanceSetting_Storage,
InstanceSetting_Storage_S3Config,
InstanceSetting_Storage_S3ConfigSchema,
InstanceSetting_StorageSchema,
InstanceSetting_StorageSetting,
InstanceSetting_StorageSetting_S3Config,
InstanceSetting_StorageSetting_S3ConfigSchema,
InstanceSetting_StorageSetting_StorageType,
InstanceSetting_StorageSettingSchema,
InstanceSetting_StorageType,
InstanceSettingSchema,
} from "@/types/proto/api/v1/instance_service_pb";
import { useTranslate } from "@/utils/i18n";
@ -29,7 +33,7 @@ import useInstanceSettingUpdater, { buildInstanceSettingName } from "./useInstan
const DEFAULT_FILEPATH_TEMPLATE = "assets/{timestamp}_{uuid}_{filename}";
type StorageTypeOption = {
storageType: InstanceSetting_StorageSetting_StorageType;
storageType: InstanceSetting_StorageType;
id: string;
titleKey: "setting.storage.type-database" | "setting.storage.type-local" | "setting.storage.type-s3";
descriptionKey: "setting.storage.database-description" | "setting.storage.local-description" | "setting.storage.s3-description";
@ -43,7 +47,7 @@ type StorageTypeOption = {
const storageTypeOptions: StorageTypeOption[] = [
{
storageType: InstanceSetting_StorageSetting_StorageType.LOCAL,
storageType: InstanceSetting_StorageType.LOCAL,
id: "storage-type-local",
titleKey: "setting.storage.type-local",
descriptionKey: "setting.storage.local-description",
@ -52,7 +56,7 @@ const storageTypeOptions: StorageTypeOption[] = [
badges: ["setting.storage.badge-default", "setting.storage.badge-recommended"],
},
{
storageType: InstanceSetting_StorageSetting_StorageType.DATABASE,
storageType: InstanceSetting_StorageType.DATABASE,
id: "storage-type-database",
titleKey: "setting.storage.type-database",
descriptionKey: "setting.storage.database-description",
@ -60,7 +64,7 @@ const storageTypeOptions: StorageTypeOption[] = [
icon: DatabaseIcon,
},
{
storageType: InstanceSetting_StorageSetting_StorageType.S3,
storageType: InstanceSetting_StorageType.S3,
id: "storage-type-s3",
titleKey: "setting.storage.type-s3",
descriptionKey: "setting.storage.s3-description",
@ -69,15 +73,95 @@ const storageTypeOptions: StorageTypeOption[] = [
},
];
const getDefaultStorage = (setting: InstanceSetting_StorageSetting): InstanceSetting_Storage | undefined =>
setting.storages.find((storage) => storage.id === setting.defaultStorageId);
const getSelectedStorageType = (setting: InstanceSetting_StorageSetting): InstanceSetting_StorageType =>
getDefaultStorage(setting)?.type ?? (setting.storageType as number as InstanceSetting_StorageType);
const getS3Config = (
setting: InstanceSetting_StorageSetting,
): InstanceSetting_Storage_S3Config | InstanceSetting_StorageSetting_S3Config | undefined => {
const storage = getDefaultStorage(setting);
if (storage?.config.case === "s3Config") {
return storage.config.value;
}
return setting.s3Config;
};
// The server keeps the storage registry ordered by most recent activation, so
// the first storage of a type is the most recently used one.
const findStorageForType = (
setting: InstanceSetting_StorageSetting,
storageType: InstanceSetting_StorageType,
): InstanceSetting_Storage | undefined => setting.storages.find((storage) => storage.type === storageType);
const normalizeEndpoint = (endpoint: string): string => endpoint.trim().replace(/\/+$/, "");
const hasReusableS3Credential = (
setting: InstanceSetting_StorageSetting,
target: InstanceSetting_Storage_S3Config | InstanceSetting_StorageSetting_S3Config | undefined,
): boolean => {
if (!target) {
return false;
}
// A credential can authorize multiple buckets, so match its provider scope
// rather than requiring the exact storage namespace.
const existingConfigs: (InstanceSetting_Storage_S3Config | InstanceSetting_StorageSetting_S3Config)[] = setting.storages
.map((storage) => (storage.config.case === "s3Config" ? storage.config.value : undefined))
.filter((config): config is InstanceSetting_Storage_S3Config => config !== undefined);
if (setting.s3Config) {
existingConfigs.push(setting.s3Config);
}
return existingConfigs.some(
(config) =>
config.accessKeyId === target.accessKeyId &&
normalizeEndpoint(config.endpoint) === normalizeEndpoint(target.endpoint) &&
config.region.trim() === target.region.trim(),
);
};
const createStorage = (storageType: InstanceSetting_StorageType): InstanceSetting_Storage => {
const storageTypeName = InstanceSetting_StorageType[storageType];
const id = storageType === InstanceSetting_StorageType.S3 ? `s3-${uuidv4()}` : storageTypeName.toLowerCase();
return create(InstanceSetting_StorageSchema, {
id,
name: storageType === InstanceSetting_StorageType.S3 ? "S3" : `${storageTypeName[0]}${storageTypeName.slice(1).toLowerCase()}`,
type: storageType,
config:
storageType === InstanceSetting_StorageType.S3
? { case: "s3Config", value: create(InstanceSetting_Storage_S3ConfigSchema, {}) }
: { case: undefined },
});
};
// Browsing the storage-type options creates placeholder entries so the form has
// something to select and edit. Only the selected storage and entries the server
// already knows deserve to be persisted; abandoned placeholders are dropped.
const canonicalizeStorageSetting = (
setting: InstanceSetting_StorageSetting,
original: InstanceSetting_StorageSetting,
): InstanceSetting_StorageSetting => {
const originalIds = new Set(original.storages.map((storage) => storage.id));
return create(InstanceSetting_StorageSettingSchema, {
...setting,
storages: setting.storages.filter((storage) => storage.id === setting.defaultStorageId || originalIds.has(storage.id)),
});
};
const StorageSection = () => {
const t = useTranslate();
const saveInstanceSetting = useInstanceSettingUpdater();
const { storageSetting: originalSetting } = useInstance();
const [instanceStorageSetting, setInstanceStorageSetting] = useState<InstanceSetting_StorageSetting>(originalSetting);
const selectedStorageType = getSelectedStorageType(instanceStorageSetting);
const selectedS3Config = getS3Config(instanceStorageSetting);
const canReuseSelectedS3Secret = hasReusableS3Credential(originalSetting, selectedS3Config);
const selectedStorageOption = useMemo(
() => storageTypeOptions.find((option) => option.storageType === instanceStorageSetting.storageType) ?? storageTypeOptions[0],
[instanceStorageSetting.storageType],
() => storageTypeOptions.find((option) => option.storageType === selectedStorageType) ?? storageTypeOptions[0],
[selectedStorageType],
);
const SelectedStorageIcon = selectedStorageOption.icon;
@ -90,77 +174,99 @@ const StorageSection = () => {
return false;
}
if (instanceStorageSetting.storageType === InstanceSetting_StorageSetting_StorageType.LOCAL) {
if (selectedStorageType === InstanceSetting_StorageType.LOCAL) {
if (instanceStorageSetting.filepathTemplate.length === 0) {
return false;
}
} else if (instanceStorageSetting.storageType === InstanceSetting_StorageSetting_StorageType.S3) {
const hasExistingS3Config = originalSetting.s3Config !== undefined;
} else if (selectedStorageType === InstanceSetting_StorageType.S3) {
if (
!instanceStorageSetting.filepathTemplate ||
!instanceStorageSetting.s3Config?.accessKeyId ||
(!hasExistingS3Config && !instanceStorageSetting.s3Config?.accessKeySecret) ||
!instanceStorageSetting.s3Config?.endpoint ||
!instanceStorageSetting.s3Config?.region ||
!instanceStorageSetting.s3Config?.bucket
!selectedS3Config?.accessKeyId ||
(!canReuseSelectedS3Secret && !selectedS3Config?.accessKeySecret) ||
!selectedS3Config?.endpoint ||
!selectedS3Config?.region ||
!selectedS3Config?.bucket
) {
return false;
}
}
return !isEqual(originalSetting, instanceStorageSetting);
}, [instanceStorageSetting, originalSetting]);
return !isEqual(originalSetting, canonicalizeStorageSetting(instanceStorageSetting, originalSetting));
}, [canReuseSelectedS3Secret, instanceStorageSetting, originalSetting, selectedS3Config, selectedStorageType]);
const handleMaxUploadSizeChanged = (event: React.ChangeEvent<HTMLInputElement>) => {
let num = parseInt(event.target.value);
if (Number.isNaN(num)) {
num = 0;
}
setInstanceStorageSetting(
setInstanceStorageSetting((current) =>
create(InstanceSetting_StorageSettingSchema, {
...instanceStorageSetting,
...current,
uploadSizeLimitMb: BigInt(num),
}),
);
};
const handleFilepathTemplateChanged = (event: React.ChangeEvent<HTMLInputElement>) => {
setInstanceStorageSetting(
setInstanceStorageSetting((current) =>
create(InstanceSetting_StorageSettingSchema, {
...instanceStorageSetting,
...current,
filepathTemplate: event.target.value,
}),
);
};
const handleS3FieldChange = (field: keyof InstanceSetting_StorageSetting_S3Config, value: string | boolean) => {
const existing = instanceStorageSetting.s3Config;
setInstanceStorageSetting(
create(InstanceSetting_StorageSettingSchema, {
storageType: instanceStorageSetting.storageType,
filepathTemplate: instanceStorageSetting.filepathTemplate,
uploadSizeLimitMb: instanceStorageSetting.uploadSizeLimitMb,
s3Config: create(InstanceSetting_StorageSetting_S3ConfigSchema, {
accessKeyId: existing?.accessKeyId ?? "",
accessKeySecret: existing?.accessKeySecret ?? "",
endpoint: existing?.endpoint ?? "",
region: existing?.region ?? "",
bucket: existing?.bucket ?? "",
usePathStyle: existing?.usePathStyle ?? false,
insecureSkipTlsVerify: existing?.insecureSkipTlsVerify ?? false,
[field]: value,
}),
}),
);
// Functional update: batched events (e.g. password-manager autofill firing
// several inputs in one task) must not overwrite one another.
setInstanceStorageSetting((current) => {
const existing = getS3Config(current);
let configuredStorage = getDefaultStorage(current);
if (configuredStorage?.type !== InstanceSetting_StorageType.S3) {
configuredStorage = findStorageForType(current, InstanceSetting_StorageType.S3) ?? createStorage(InstanceSetting_StorageType.S3);
}
const updatedStorage = create(InstanceSetting_StorageSchema, {
...configuredStorage,
config: {
case: "s3Config",
value: create(InstanceSetting_Storage_S3ConfigSchema, {
accessKeyId: existing?.accessKeyId ?? "",
accessKeySecret: existing?.accessKeySecret ?? "",
endpoint: existing?.endpoint ?? "",
region: existing?.region ?? "",
bucket: existing?.bucket ?? "",
usePathStyle: existing?.usePathStyle ?? false,
insecureSkipTlsVerify: existing?.insecureSkipTlsVerify ?? false,
[field]: value,
}),
},
});
// Replace in place to preserve the registry order the server maintains.
// The legacy storageType/s3Config mirror fields are left untouched: the
// server re-derives them from the named model on every update.
const storages = current.storages.some((storage) => storage.id === updatedStorage.id)
? current.storages.map((storage) => (storage.id === updatedStorage.id ? updatedStorage : storage))
: [updatedStorage, ...current.storages];
return create(InstanceSetting_StorageSettingSchema, {
...current,
storages,
defaultStorageId: updatedStorage.id,
});
});
};
const handleStorageTypeChanged = (storageType: InstanceSetting_StorageSetting_StorageType) => {
setInstanceStorageSetting(
create(InstanceSetting_StorageSettingSchema, {
...instanceStorageSetting,
storageType,
filepathTemplate: instanceStorageSetting.filepathTemplate || DEFAULT_FILEPATH_TEMPLATE,
}),
);
const handleStorageTypeChanged = (storageType: InstanceSetting_StorageType) => {
setInstanceStorageSetting((current) => {
const configuredStorage = findStorageForType(current, storageType) ?? createStorage(storageType);
const storages = current.storages.some((storage) => storage.id === configuredStorage.id)
? current.storages
: [...current.storages, configuredStorage];
return create(InstanceSetting_StorageSettingSchema, {
...current,
storages,
defaultStorageId: configuredStorage.id,
filepathTemplate: current.filepathTemplate || DEFAULT_FILEPATH_TEMPLATE,
});
});
};
const saveInstanceStorageSetting = async () => {
@ -170,7 +276,7 @@ const StorageSection = () => {
name: buildInstanceSettingName(InstanceSetting_Key.STORAGE),
value: {
case: "storageSetting",
value: instanceStorageSetting,
value: canonicalizeStorageSetting(instanceStorageSetting, originalSetting),
},
}),
errorContext: "Update storage settings",
@ -181,15 +287,15 @@ const StorageSection = () => {
<SettingSection title={t("setting.storage.label")}>
<SettingGroup title={t("setting.storage.current-storage")} description={t("setting.storage.current-storage-description")}>
<RadioGroup
value={String(instanceStorageSetting.storageType)}
value={String(selectedStorageType)}
onValueChange={(value) => {
handleStorageTypeChanged(Number(value) as InstanceSetting_StorageSetting_StorageType);
handleStorageTypeChanged(Number(value) as InstanceSetting_StorageType);
}}
className="overflow-hidden rounded-lg border border-border bg-background divide-y divide-border gap-0"
>
{storageTypeOptions.map((option) => {
const Icon = option.icon;
const selected = instanceStorageSetting.storageType === option.storageType;
const selected = selectedStorageType === option.storageType;
return (
<div
key={option.id}
@ -238,7 +344,7 @@ const StorageSection = () => {
<SettingPanel className="rounded-md bg-muted/20 px-3 py-2.5">
<div className="mb-2 flex items-center gap-2 text-xs font-medium text-muted-foreground">
<SelectedStorageIcon className="size-3.5" />
<span>{t("setting.storage.selected-backend")}</span>
<span>{t("setting.storage.current-storage")}</span>
<span className="text-foreground">{t(selectedStorageOption.titleKey)}</span>
</div>
<ul className="flex flex-col gap-1.5 text-xs leading-5 text-muted-foreground">
@ -259,7 +365,7 @@ const StorageSection = () => {
/>
</SettingRow>
{instanceStorageSetting.storageType !== InstanceSetting_StorageSetting_StorageType.DATABASE && (
{selectedStorageType !== InstanceSetting_StorageType.DATABASE && (
<SettingRow
label={t("setting.storage.filepath-template")}
description={t("setting.storage.filepath-template-description")}
@ -275,7 +381,7 @@ const StorageSection = () => {
)}
</SettingGroup>
{instanceStorageSetting.storageType === InstanceSetting_StorageSetting_StorageType.S3 && (
{selectedStorageType === InstanceSetting_StorageType.S3 && (
<SettingGroup
title={t("setting.storage.s3-configuration")}
description={t("setting.storage.s3-configuration-description")}
@ -284,7 +390,7 @@ const StorageSection = () => {
<SettingRow label={t("setting.storage.accesskey")} description={t("setting.storage.accesskey-description")}>
<Input
className="w-64"
value={instanceStorageSetting.s3Config?.accessKeyId ?? ""}
value={selectedS3Config?.accessKeyId ?? ""}
onChange={(e) => handleS3FieldChange("accessKeyId", e.target.value)}
/>
</SettingRow>
@ -292,13 +398,13 @@ const StorageSection = () => {
<SettingRow
label={t("setting.storage.secretkey")}
description={
originalSetting.s3Config ? t("setting.storage.secretkey-preserve-description") : t("setting.storage.secretkey-description")
canReuseSelectedS3Secret ? t("setting.storage.secretkey-preserve-description") : t("setting.storage.secretkey-description")
}
>
<Input
className="w-64"
type="password"
value={instanceStorageSetting.s3Config?.accessKeySecret ?? ""}
value={selectedS3Config?.accessKeySecret ?? ""}
onChange={(e) => handleS3FieldChange("accessKeySecret", e.target.value)}
/>
</SettingRow>
@ -306,7 +412,7 @@ const StorageSection = () => {
<SettingRow label={t("setting.storage.endpoint")} description={t("setting.storage.endpoint-description")}>
<Input
className="w-64"
value={instanceStorageSetting.s3Config?.endpoint ?? ""}
value={selectedS3Config?.endpoint ?? ""}
onChange={(e) => handleS3FieldChange("endpoint", e.target.value)}
/>
</SettingRow>
@ -314,7 +420,7 @@ const StorageSection = () => {
<SettingRow label={t("setting.storage.region")} description={t("setting.storage.region-description")}>
<Input
className="w-64"
value={instanceStorageSetting.s3Config?.region ?? ""}
value={selectedS3Config?.region ?? ""}
onChange={(e) => handleS3FieldChange("region", e.target.value)}
/>
</SettingRow>
@ -322,14 +428,14 @@ const StorageSection = () => {
<SettingRow label={t("setting.storage.bucket")} description={t("setting.storage.bucket-description")}>
<Input
className="w-64"
value={instanceStorageSetting.s3Config?.bucket ?? ""}
value={selectedS3Config?.bucket ?? ""}
onChange={(e) => handleS3FieldChange("bucket", e.target.value)}
/>
</SettingRow>
<SettingRow label={t("setting.storage.use-path-style")} description={t("setting.storage.use-path-style-description")}>
<Switch
checked={instanceStorageSetting.s3Config?.usePathStyle ?? false}
checked={selectedS3Config?.usePathStyle ?? false}
onCheckedChange={(checked) => handleS3FieldChange("usePathStyle", checked)}
/>
</SettingRow>
@ -339,7 +445,7 @@ const StorageSection = () => {
description={t("setting.storage.insecure-skip-tls-verify-description")}
>
<Switch
checked={instanceStorageSetting.s3Config?.insecureSkipTlsVerify ?? false}
checked={selectedS3Config?.insecureSkipTlsVerify ?? false}
onCheckedChange={(checked) => handleS3FieldChange("insecureSkipTlsVerify", checked)}
/>
</SettingRow>

File diff suppressed because one or more lines are too long