From 463bacd53b3b6e5d97887ef109f21bf4c3266f5f Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sun, 30 Jul 2023 21:58:07 +0800 Subject: [PATCH] Add GetDirectResources() --- controllers/resource.go | 17 ++++++++++++++- controllers/util.go | 8 +++++++ go.mod | 5 +---- go.sum | 11 ++-------- object/resource_direct.go | 46 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 object/resource_direct.go diff --git a/controllers/resource.go b/controllers/resource.go index 5b4292ef..a4c59471 100644 --- a/controllers/resource.go +++ b/controllers/resource.go @@ -52,7 +52,22 @@ func (c *ApiController) GetResources() { sortField := c.Input().Get("sortField") sortOrder := c.Input().Get("sortOrder") - if limit == "" || page == "" { + if sortField == "Direct" { + provider, err := c.GetProviderFromContext("Storage") + if err != nil { + c.ResponseError(err.Error()) + return + } + + prefix := sortOrder + resources, err := object.GetDirectResources(owner, user, provider, prefix, c.GetAcceptLanguage()) + if err != nil { + c.ResponseError(err.Error()) + return + } + + c.ResponseOk(resources) + } else if limit == "" || page == "" { resources, err := object.GetResources(owner, user) if err != nil { c.ResponseError(err.Error()) diff --git a/controllers/util.go b/controllers/util.go index f4c94bc2..757d26d3 100644 --- a/controllers/util.go +++ b/controllers/util.go @@ -145,6 +145,14 @@ func (c *ApiController) IsMaskedEnabled() (bool, bool) { func (c *ApiController) GetProviderFromContext(category string) (*object.Provider, error) { providerName := c.Input().Get("provider") + if providerName == "" { + field := c.Input().Get("field") + value := c.Input().Get("value") + if field == "provider" && value != "" { + providerName = value + } + } + if providerName != "" { provider, err := object.GetProvider(util.GetId("admin", providerName)) if err != nil { diff --git a/go.mod b/go.mod index 93d7e295..602b31f0 100644 --- a/go.mod +++ b/go.mod @@ -14,11 +14,10 @@ require ( github.com/casbin/casbin/v2 v2.30.1 github.com/casdoor/go-sms-sender v0.6.1 github.com/casdoor/gomail/v2 v2.0.1 - github.com/casdoor/oss v1.2.0 + github.com/casdoor/oss v1.2.1 github.com/casdoor/xorm-adapter/v3 v3.0.4 github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f github.com/denisenkom/go-mssqldb v0.9.0 - github.com/dlclark/regexp2 v1.9.0 // indirect github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect github.com/fogleman/gg v1.3.0 github.com/forestmgy/ldapserver v1.1.0 @@ -42,7 +41,6 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect github.com/nyaruka/phonenumbers v1.1.5 - github.com/pkoukk/tiktoken-go v0.1.1 github.com/pquerna/otp v1.4.0 github.com/prometheus/client_golang v1.11.1 github.com/prometheus/client_model v0.2.0 @@ -50,7 +48,6 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/russellhaering/gosaml2 v0.9.0 github.com/russellhaering/goxmldsig v1.2.0 - github.com/sashabaranov/go-openai v1.12.0 github.com/satori/go.uuid v1.2.0 github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect github.com/shirou/gopsutil v3.21.11+incompatible diff --git a/go.sum b/go.sum index 9022cc3e..e687f01a 100644 --- a/go.sum +++ b/go.sum @@ -120,8 +120,8 @@ github.com/casdoor/go-sms-sender v0.6.1 h1:35HuxpuQ0jGAoRE0/E5lAchYpeoFwaB5NGsEA github.com/casdoor/go-sms-sender v0.6.1/go.mod h1:kBykbqwgRDXbXdMAIxmZKinVM1WjdqEbej5LAbUbcfI= github.com/casdoor/gomail/v2 v2.0.1 h1:J+FG6x80s9e5lBHUn8Sv0Y56mud34KiWih5YdmudR/w= github.com/casdoor/gomail/v2 v2.0.1/go.mod h1:VnGPslEAtpix5FjHisR/WKB1qvZDBaujbikxDe9d+2Q= -github.com/casdoor/oss v1.2.0 h1:ozLAE+nnNdFQBWbzH8U9spzaO8h8NrB57lBcdyMUUQ8= -github.com/casdoor/oss v1.2.0/go.mod h1:qii35VBuxnR/uEuYSKpS0aJ8htQFOcCVsZ4FHgHLuss= +github.com/casdoor/oss v1.2.1 h1:GA8DMgfwswPf9X1NEWZ8tdepMXtBrGF/x/wuAz5Juf4= +github.com/casdoor/oss v1.2.1/go.mod h1:qii35VBuxnR/uEuYSKpS0aJ8htQFOcCVsZ4FHgHLuss= github.com/casdoor/xorm-adapter/v3 v3.0.4 h1:vB04Ao8n2jA7aFBI9F+gGXo9+Aa1IQP6mTdo50913DM= github.com/casdoor/xorm-adapter/v3 v3.0.4/go.mod h1:4WTcUw+bTgBylGHeGHzTtBvuTXRS23dtwzFLl9tsgFM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -165,9 +165,6 @@ github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/dlclark/regexp2 v1.9.0 h1:pTK/l/3qYIKaRXuHnEnIf7Y5NxfRPfpb7dis6/gdlVI= -github.com/dlclark/regexp2 v1.9.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= @@ -495,8 +492,6 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkoukk/tiktoken-go v0.1.1 h1:jtkYlIECjyM9OW1w4rjPmTohK4arORP9V25y6TM6nXo= -github.com/pkoukk/tiktoken-go v0.1.1/go.mod h1:boMWvk9pQCOTx11pgu0DrIdrAKgQzzJKUP6vLXaz7Rw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg= @@ -546,8 +541,6 @@ github.com/russellhaering/goxmldsig v1.2.0 h1:Y6GTTc9Un5hCxSzVz4UIWQ/zuVwDvzJk80 github.com/russellhaering/goxmldsig v1.2.0/go.mod h1:gM4MDENBQf7M+V824SGfyIUVFWydB7n0KkEubVJl+Tw= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sashabaranov/go-openai v1.12.0 h1:aRNHH0gtVfrpIaEolD0sWrLLRnYQNK4cH/bIAHwL8Rk= -github.com/sashabaranov/go-openai v1.12.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= diff --git a/object/resource_direct.go b/object/resource_direct.go new file mode 100644 index 00000000..eeef0879 --- /dev/null +++ b/object/resource_direct.go @@ -0,0 +1,46 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package object + +import ( + "strings" + "time" + + "github.com/casdoor/casdoor/util" +) + +func GetDirectResources(owner string, user string, provider *Provider, prefix string, lang string) ([]*Resource, error) { + storageProvider, err := getStorageProvider(provider, lang) + if err != nil { + return nil, err + } + + res := []*Resource{} + objects, err := storageProvider.List(prefix) + for _, obj := range objects { + resource := &Resource{ + Owner: owner, + Name: strings.TrimPrefix(obj.Path, "/"), + CreatedTime: obj.LastModified.Local().Format(time.RFC3339), + User: user, + Provider: "", + Application: "", + FileSize: int(obj.Size), + Url: util.UrlJoin(provider.Domain, obj.Path), + } + res = append(res, resource) + } + return res, err +}