You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
4.0 KiB
151 lines
4.0 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package drivers
|
||
10 years ago
|
|
||
10 years ago
|
import (
|
||
|
"io"
|
||
10 years ago
|
"regexp"
|
||
10 years ago
|
"time"
|
||
10 years ago
|
"unicode/utf8"
|
||
10 years ago
|
)
|
||
10 years ago
|
|
||
10 years ago
|
// Driver - generic API interface for various drivers - donut, file, memory
|
||
|
type Driver interface {
|
||
10 years ago
|
// Bucket Operations
|
||
10 years ago
|
ListBuckets() ([]BucketMetadata, error)
|
||
10 years ago
|
CreateBucket(bucket string) error
|
||
10 years ago
|
CreateBucketPolicy(bucket string, p BucketPolicy) error
|
||
10 years ago
|
GetBucketPolicy(bucket string) (BucketPolicy, error)
|
||
10 years ago
|
|
||
10 years ago
|
// Object Operations
|
||
10 years ago
|
GetObject(w io.Writer, bucket, object string) (int64, error)
|
||
|
GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error)
|
||
10 years ago
|
GetObjectMetadata(bucket string, object string, prefix string) (ObjectMetadata, error)
|
||
10 years ago
|
ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error)
|
||
10 years ago
|
CreateObject(bucket string, key string, contentType string, md5sum string, data io.Reader) error
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// BucketMetadata - name and create date
|
||
10 years ago
|
type BucketMetadata struct {
|
||
|
Name string
|
||
10 years ago
|
Created time.Time
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// ObjectMetadata - object key and its relevant metadata
|
||
10 years ago
|
type ObjectMetadata struct {
|
||
10 years ago
|
Bucket string
|
||
|
Key string
|
||
10 years ago
|
|
||
|
ContentType string
|
||
|
Created time.Time
|
||
10 years ago
|
Md5 string
|
||
10 years ago
|
Size int64
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// FilterMode type
|
||
|
type FilterMode int
|
||
|
|
||
|
// FilterMode list
|
||
|
const (
|
||
|
DelimiterPrefixMode FilterMode = iota
|
||
|
DelimiterMode
|
||
|
PrefixMode
|
||
|
DefaultMode
|
||
|
)
|
||
|
|
||
10 years ago
|
// BucketResourcesMetadata - various types of bucket resources
|
||
10 years ago
|
type BucketResourcesMetadata struct {
|
||
|
Prefix string
|
||
|
Marker string
|
||
|
Maxkeys int
|
||
|
Delimiter string
|
||
|
IsTruncated bool
|
||
|
CommonPrefixes []string
|
||
10 years ago
|
Mode FilterMode
|
||
10 years ago
|
|
||
|
Policy bool
|
||
|
// TODO
|
||
|
Logging string
|
||
|
Notification string
|
||
|
}
|
||
|
|
||
10 years ago
|
// GetMode - Populate filter mode
|
||
|
func GetMode(resources BucketResourcesMetadata) FilterMode {
|
||
|
var f FilterMode
|
||
|
switch true {
|
||
|
case resources.Delimiter != "" && resources.Prefix != "":
|
||
|
f = DelimiterPrefixMode
|
||
|
case resources.Delimiter != "" && resources.Prefix == "":
|
||
|
f = DelimiterMode
|
||
|
case resources.Delimiter == "" && resources.Prefix != "":
|
||
|
f = PrefixMode
|
||
|
case resources.Delimiter == "" && resources.Prefix == "":
|
||
|
f = DefaultMode
|
||
|
}
|
||
|
|
||
|
return f
|
||
|
}
|
||
|
|
||
|
// IsDelimiterPrefixSet Delimiter and Prefix set
|
||
|
func (b BucketResourcesMetadata) IsDelimiterPrefixSet() bool {
|
||
|
return b.Mode == DelimiterPrefixMode
|
||
|
}
|
||
|
|
||
|
// IsDelimiterSet Delimiter set
|
||
|
func (b BucketResourcesMetadata) IsDelimiterSet() bool {
|
||
|
return b.Mode == DelimiterMode
|
||
|
}
|
||
|
|
||
|
// IsPrefixSet Prefix set
|
||
|
func (b BucketResourcesMetadata) IsPrefixSet() bool {
|
||
|
return b.Mode == PrefixMode
|
||
|
}
|
||
|
|
||
|
// IsDefault No query values
|
||
|
func (b BucketResourcesMetadata) IsDefault() bool {
|
||
|
return b.Mode == DefaultMode
|
||
|
}
|
||
|
|
||
10 years ago
|
// IsValidBucket - verify bucket name in accordance with
|
||
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html
|
||
10 years ago
|
func IsValidBucket(bucket string) bool {
|
||
|
if len(bucket) < 3 || len(bucket) > 63 {
|
||
|
return false
|
||
|
}
|
||
|
if bucket[0] == '.' || bucket[len(bucket)-1] == '.' {
|
||
|
return false
|
||
|
}
|
||
|
if match, _ := regexp.MatchString("\\.\\.", bucket); match == true {
|
||
|
return false
|
||
|
}
|
||
10 years ago
|
// We don't support buckets with '.' in them
|
||
|
match, _ := regexp.MatchString("^[a-zA-Z][a-zA-Z0-9\\-]+[a-zA-Z0-9]$", bucket)
|
||
10 years ago
|
return match
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// IsValidObject - verify object name in accordance with
|
||
|
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
|
||
10 years ago
|
func IsValidObject(object string) bool {
|
||
10 years ago
|
if len(object) > 1024 || len(object) == 0 {
|
||
|
return false
|
||
|
}
|
||
10 years ago
|
if !utf8.ValidString(object) {
|
||
10 years ago
|
return false
|
||
|
}
|
||
10 years ago
|
return true
|
||
|
}
|