mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-22 18:25:47 +08:00
Improve LocalFileSystemProvider's error handling
This commit is contained in:
parent
3459ef1479
commit
7318ee6e3a
@ -15,6 +15,7 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -23,76 +24,69 @@ import (
|
|||||||
"github.com/casdoor/oss"
|
"github.com/casdoor/oss"
|
||||||
)
|
)
|
||||||
|
|
||||||
var baseFolder = "files"
|
// LocalFileSystemProvider file system storage
|
||||||
|
type LocalFileSystemProvider struct {
|
||||||
// FileSystem file system storage
|
BaseDir string
|
||||||
type FileSystem struct {
|
|
||||||
Base string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFileSystem initialize the local file system storage
|
// NewLocalFileSystemStorageProvider initialize the local file system storage
|
||||||
func NewFileSystem(base string) *FileSystem {
|
func NewLocalFileSystemStorageProvider() *LocalFileSystemProvider {
|
||||||
absBase, err := filepath.Abs(base)
|
baseFolder := "files"
|
||||||
|
absBase, err := filepath.Abs(baseFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("local file system storage's base folder is not initialized")
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &FileSystem{Base: absBase}
|
return &LocalFileSystemProvider{BaseDir: absBase}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFullPath get full path from absolute/relative path
|
// GetFullPath get full path from absolute/relative path
|
||||||
func (fileSystem FileSystem) GetFullPath(path string) string {
|
func (sp LocalFileSystemProvider) GetFullPath(path string) string {
|
||||||
fullPath := path
|
fullPath := path
|
||||||
if !strings.HasPrefix(path, fileSystem.Base) {
|
if !strings.HasPrefix(path, sp.BaseDir) {
|
||||||
fullPath, _ = filepath.Abs(filepath.Join(fileSystem.Base, path))
|
fullPath, _ = filepath.Abs(filepath.Join(sp.BaseDir, path))
|
||||||
}
|
}
|
||||||
return fullPath
|
return fullPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get receive file with given path
|
// Get receive file with given path
|
||||||
func (fileSystem FileSystem) Get(path string) (*os.File, error) {
|
func (sp LocalFileSystemProvider) Get(path string) (*os.File, error) {
|
||||||
return os.Open(fileSystem.GetFullPath(path))
|
return os.Open(sp.GetFullPath(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStream get file as stream
|
// GetStream get file as stream
|
||||||
func (fileSystem FileSystem) GetStream(path string) (io.ReadCloser, error) {
|
func (sp LocalFileSystemProvider) GetStream(path string) (io.ReadCloser, error) {
|
||||||
return os.Open(fileSystem.GetFullPath(path))
|
return os.Open(sp.GetFullPath(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put store a reader into given path
|
// Put store a reader into given path
|
||||||
func (fileSystem FileSystem) Put(path string, reader io.Reader) (*oss.Object, error) {
|
func (sp LocalFileSystemProvider) Put(path string, reader io.Reader) (*oss.Object, error) {
|
||||||
var (
|
fullPath := sp.GetFullPath(path)
|
||||||
fullPath = fileSystem.GetFullPath(path)
|
|
||||||
err = os.MkdirAll(filepath.Dir(fullPath), os.ModePerm)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, 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())
|
||||||
}
|
}
|
||||||
|
|
||||||
dst, err := os.Create(filepath.Clean(fullPath))
|
dst, err := os.Create(filepath.Clean(fullPath))
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if seeker, ok := reader.(io.ReadSeeker); ok {
|
if seeker, ok := reader.(io.ReadSeeker); ok {
|
||||||
seeker.Seek(0, 0)
|
seeker.Seek(0, 0)
|
||||||
}
|
}
|
||||||
_, err = io.Copy(dst, reader)
|
_, err = io.Copy(dst, reader)
|
||||||
}
|
}
|
||||||
|
return &oss.Object{Path: path, Name: filepath.Base(path), StorageInterface: sp}, err
|
||||||
return &oss.Object{Path: path, Name: filepath.Base(path), StorageInterface: fileSystem}, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete delete file
|
// Delete delete file
|
||||||
func (fileSystem FileSystem) Delete(path string) error {
|
func (sp LocalFileSystemProvider) Delete(path string) error {
|
||||||
return os.Remove(fileSystem.GetFullPath(path))
|
return os.Remove(sp.GetFullPath(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
// List list all objects under current path
|
// List list all objects under current path
|
||||||
func (fileSystem FileSystem) List(path string) ([]*oss.Object, error) {
|
func (sp LocalFileSystemProvider) List(path string) ([]*oss.Object, error) {
|
||||||
var (
|
objects := []*oss.Object{}
|
||||||
objects []*oss.Object
|
fullPath := sp.GetFullPath(path)
|
||||||
fullPath = fileSystem.GetFullPath(path)
|
|
||||||
)
|
|
||||||
|
|
||||||
filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
|
||||||
if path == fullPath {
|
if path == fullPath {
|
||||||
@ -102,10 +96,10 @@ func (fileSystem FileSystem) List(path string) ([]*oss.Object, error) {
|
|||||||
if err == nil && !info.IsDir() {
|
if err == nil && !info.IsDir() {
|
||||||
modTime := info.ModTime()
|
modTime := info.ModTime()
|
||||||
objects = append(objects, &oss.Object{
|
objects = append(objects, &oss.Object{
|
||||||
Path: strings.TrimPrefix(path, fileSystem.Base),
|
Path: strings.TrimPrefix(path, sp.BaseDir),
|
||||||
Name: info.Name(),
|
Name: info.Name(),
|
||||||
LastModified: &modTime,
|
LastModified: &modTime,
|
||||||
StorageInterface: fileSystem,
|
StorageInterface: sp,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -114,16 +108,12 @@ func (fileSystem FileSystem) List(path string) ([]*oss.Object, error) {
|
|||||||
return objects, nil
|
return objects, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEndpoint get endpoint, FileSystem's endpoint is /
|
// GetEndpoint get endpoint, LocalFileSystemProvider's endpoint is /
|
||||||
func (fileSystem FileSystem) GetEndpoint() string {
|
func (sp LocalFileSystemProvider) GetEndpoint() string {
|
||||||
return "/"
|
return "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetURL get public accessible URL
|
// GetURL get public accessible URL
|
||||||
func (fileSystem FileSystem) GetURL(path string) (url string, err error) {
|
func (sp LocalFileSystemProvider) GetURL(path string) (url string, err error) {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalFileSystemStorageProvider(clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface {
|
|
||||||
return NewFileSystem(baseFolder)
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,7 @@ import "github.com/casdoor/oss"
|
|||||||
func GetStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface {
|
func GetStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string) oss.StorageInterface {
|
||||||
switch providerType {
|
switch providerType {
|
||||||
case "Local File System":
|
case "Local File System":
|
||||||
return NewLocalFileSystemStorageProvider(clientId, clientSecret, region, bucket, endpoint)
|
return NewLocalFileSystemStorageProvider()
|
||||||
case "AWS S3":
|
case "AWS S3":
|
||||||
return NewAwsS3StorageProvider(clientId, clientSecret, region, bucket, endpoint)
|
return NewAwsS3StorageProvider(clientId, clientSecret, region, bucket, endpoint)
|
||||||
case "MinIO":
|
case "MinIO":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user