You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

local_file_system.go 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "time"
  23. )
  24. type LocalFileSystemStorageProvider struct {
  25. path string
  26. }
  27. func NewLocalFileSystemStorageProvider(path string) (*LocalFileSystemStorageProvider, error) {
  28. path = strings.ReplaceAll(path, "\\", "/")
  29. return &LocalFileSystemStorageProvider{path: path}, nil
  30. }
  31. func (p *LocalFileSystemStorageProvider) ListObjects(prefix string) ([]*Object, error) {
  32. objects := []*Object{}
  33. fullPath := p.path
  34. filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
  35. if path == fullPath {
  36. return nil
  37. }
  38. base := filepath.Base(path)
  39. if info.IsDir() && (strings.HasPrefix(base, ".") || base == "node_modules") {
  40. return filepath.SkipDir
  41. }
  42. if err == nil && !info.IsDir() {
  43. modTime := info.ModTime()
  44. path = strings.ReplaceAll(path, "\\", "/")
  45. relativePath := strings.TrimPrefix(path, fullPath)
  46. relativePath = strings.TrimPrefix(relativePath, "/")
  47. url := strings.Replace(path, ":", "|", 1)
  48. url = fmt.Sprintf("storage/%s", url)
  49. objects = append(objects, &Object{
  50. Key: relativePath,
  51. LastModified: modTime.Format(time.RFC3339),
  52. Size: info.Size(),
  53. Url: url,
  54. })
  55. }
  56. return nil
  57. })
  58. return objects, nil
  59. }
  60. func (p *LocalFileSystemStorageProvider) PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) error {
  61. fullPath := filepath.Join(p.path, key)
  62. err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm)
  63. if err != nil {
  64. return fmt.Errorf("Casdoor fails to create folder: \"%s\" for local file system storage provider: %s. Make sure Casdoor process has correct permission to create/access it, or you can create it manually in advance", filepath.Dir(fullPath), err.Error())
  65. }
  66. dst, err := os.Create(filepath.Clean(fullPath))
  67. if err == nil {
  68. _, err = io.Copy(dst, fileBuffer)
  69. }
  70. return err
  71. }
  72. func (p *LocalFileSystemStorageProvider) DeleteObject(key string) error {
  73. return os.Remove(filepath.Join(p.path, key))
  74. }