2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-08-08 14:18:44 +08:00
//
// 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 storage
import (
2023-09-07 10:49:39 +08:00
"fmt"
2021-08-08 14:18:44 +08:00
"io"
"os"
"path/filepath"
"strings"
2022-05-03 17:59:07 +08:00
"github.com/casdoor/oss"
2021-08-08 14:18:44 +08:00
)
2023-09-07 10:49:39 +08:00
// LocalFileSystemProvider file system storage
type LocalFileSystemProvider struct {
BaseDir string
2021-08-08 14:18:44 +08:00
}
2023-09-07 10:49:39 +08:00
// NewLocalFileSystemStorageProvider initialize the local file system storage
func NewLocalFileSystemStorageProvider ( ) * LocalFileSystemProvider {
baseFolder := "files"
absBase , err := filepath . Abs ( baseFolder )
2021-08-08 14:18:44 +08:00
if err != nil {
2023-09-07 10:49:39 +08:00
panic ( err )
2021-08-08 14:18:44 +08:00
}
2023-09-07 10:49:39 +08:00
return & LocalFileSystemProvider { BaseDir : absBase }
2021-08-08 14:18:44 +08:00
}
// GetFullPath get full path from absolute/relative path
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) GetFullPath ( path string ) string {
2021-08-08 14:18:44 +08:00
fullPath := path
2023-09-07 10:49:39 +08:00
if ! strings . HasPrefix ( path , sp . BaseDir ) {
fullPath , _ = filepath . Abs ( filepath . Join ( sp . BaseDir , path ) )
2021-08-08 14:18:44 +08:00
}
return fullPath
}
// Get receive file with given path
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) Get ( path string ) ( * os . File , error ) {
return os . Open ( sp . GetFullPath ( path ) )
2021-08-08 14:18:44 +08:00
}
// GetStream get file as stream
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) GetStream ( path string ) ( io . ReadCloser , error ) {
return os . Open ( sp . GetFullPath ( path ) )
2021-08-08 14:18:44 +08:00
}
// Put store a reader into given path
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) Put ( path string , reader io . Reader ) ( * oss . Object , error ) {
fullPath := sp . GetFullPath ( path )
2021-08-08 14:18:44 +08:00
2023-09-07 10:49:39 +08:00
err := os . MkdirAll ( filepath . Dir ( fullPath ) , os . ModePerm )
2021-08-08 14:18:44 +08:00
if err != nil {
2023-09-07 10:49:39 +08:00
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 ( ) )
2021-08-08 14:18:44 +08:00
}
2023-06-21 13:55:20 +03:00
dst , err := os . Create ( filepath . Clean ( fullPath ) )
2021-08-08 14:18:44 +08:00
if err == nil {
if seeker , ok := reader . ( io . ReadSeeker ) ; ok {
seeker . Seek ( 0 , 0 )
}
_ , err = io . Copy ( dst , reader )
}
2023-09-07 10:49:39 +08:00
return & oss . Object { Path : path , Name : filepath . Base ( path ) , StorageInterface : sp } , err
2021-08-08 14:18:44 +08:00
}
// Delete delete file
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) Delete ( path string ) error {
return os . Remove ( sp . GetFullPath ( path ) )
2021-08-08 14:18:44 +08:00
}
// List list all objects under current path
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) List ( path string ) ( [ ] * oss . Object , error ) {
objects := [ ] * oss . Object { }
fullPath := sp . GetFullPath ( path )
2021-08-08 14:18:44 +08:00
filepath . Walk ( fullPath , func ( path string , info os . FileInfo , err error ) error {
if path == fullPath {
return nil
}
if err == nil && ! info . IsDir ( ) {
modTime := info . ModTime ( )
objects = append ( objects , & oss . Object {
2023-09-07 10:49:39 +08:00
Path : strings . TrimPrefix ( path , sp . BaseDir ) ,
2021-08-08 14:18:44 +08:00
Name : info . Name ( ) ,
LastModified : & modTime ,
2023-09-07 10:49:39 +08:00
StorageInterface : sp ,
2021-08-08 14:18:44 +08:00
} )
}
return nil
} )
return objects , nil
}
2023-09-07 10:49:39 +08:00
// GetEndpoint get endpoint, LocalFileSystemProvider's endpoint is /
func ( sp LocalFileSystemProvider ) GetEndpoint ( ) string {
2021-08-08 14:18:44 +08:00
return "/"
}
// GetURL get public accessible URL
2023-09-07 10:49:39 +08:00
func ( sp LocalFileSystemProvider ) GetURL ( path string ) ( url string , err error ) {
2021-08-08 14:18:44 +08:00
return path , nil
}