github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/metadata/storage.go (about)

     1  // Copyright 2018-2022 CERN
     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  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package metadata
    20  
    21  import (
    22  	"context"
    23  	"crypto/md5"
    24  	"encoding/binary"
    25  	"fmt"
    26  	"time"
    27  
    28  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    29  )
    30  
    31  // UploadRequest represents an upload request and its options
    32  type UploadRequest struct {
    33  	Path    string
    34  	Content []byte
    35  
    36  	IfMatchEtag       string
    37  	IfNoneMatch       []string
    38  	IfUnmodifiedSince time.Time
    39  	MTime             time.Time
    40  }
    41  
    42  // UploadResponse represents a upload response
    43  type UploadResponse struct {
    44  	Etag   string
    45  	FileID string // only for cs3 storage
    46  }
    47  
    48  // DownloadRequest represents a download request and its options
    49  type DownloadRequest struct {
    50  	Path        string
    51  	IfNoneMatch []string
    52  }
    53  
    54  // DownloadResponse represents a download response and its options
    55  type DownloadResponse struct {
    56  	Content []byte
    57  
    58  	Etag  string
    59  	Mtime time.Time
    60  }
    61  
    62  // Storage is the interface to maintain metadata in a storage
    63  type Storage interface {
    64  	Backend() string
    65  
    66  	Init(ctx context.Context, name string) (err error)
    67  	Upload(ctx context.Context, req UploadRequest) (*UploadResponse, error)
    68  	Download(ctx context.Context, req DownloadRequest) (*DownloadResponse, error)
    69  	SimpleUpload(ctx context.Context, uploadpath string, content []byte) error
    70  	SimpleDownload(ctx context.Context, path string) ([]byte, error)
    71  	Delete(ctx context.Context, path string) error
    72  	Stat(ctx context.Context, path string) (*provider.ResourceInfo, error)
    73  
    74  	ReadDir(ctx context.Context, path string) ([]string, error)
    75  	ListDir(ctx context.Context, path string) ([]*provider.ResourceInfo, error)
    76  
    77  	CreateSymlink(ctx context.Context, oldname, newname string) error
    78  	ResolveSymlink(ctx context.Context, name string) (string, error)
    79  
    80  	MakeDirIfNotExist(ctx context.Context, name string) error
    81  }
    82  
    83  func calcEtag(mtime time.Time, size int64) (string, error) {
    84  	h := md5.New()
    85  	if err := binary.Write(h, binary.BigEndian, mtime.UnixNano()); err != nil {
    86  		return "", err
    87  	}
    88  	if err := binary.Write(h, binary.BigEndian, size); err != nil {
    89  		return "", err
    90  	}
    91  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    92  }