github.com/cs3org/reva/v2@v2.27.7/pkg/publicshare/manager/json/persistence/cs3/cs3.go (about)

     1  // Copyright 2018-2021 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 cs3
    20  
    21  import (
    22  	"context"
    23  	"encoding/json"
    24  	"fmt"
    25  	"time"
    26  
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/publicshare/manager/json/persistence"
    29  	"github.com/cs3org/reva/v2/pkg/storage/utils/metadata"
    30  	"github.com/cs3org/reva/v2/pkg/utils"
    31  )
    32  
    33  type db struct {
    34  	mtime        time.Time
    35  	publicShares persistence.PublicShares
    36  }
    37  
    38  type cs3 struct {
    39  	initialized bool
    40  	s           metadata.Storage
    41  
    42  	db db
    43  }
    44  
    45  // New returns a new Cache instance
    46  func New(s metadata.Storage) persistence.Persistence {
    47  	return &cs3{
    48  		s: s,
    49  		db: db{
    50  			publicShares: persistence.PublicShares{},
    51  		},
    52  	}
    53  }
    54  
    55  func (p *cs3) Init(ctx context.Context) error {
    56  	if p.initialized {
    57  		return nil
    58  	}
    59  
    60  	err := p.s.Init(ctx, "jsoncs3-public-share-manager-metadata")
    61  	if err != nil {
    62  		return err
    63  	}
    64  	p.initialized = true
    65  
    66  	return nil
    67  }
    68  
    69  func (p *cs3) Read(ctx context.Context) (persistence.PublicShares, error) {
    70  	if !p.initialized {
    71  		return nil, fmt.Errorf("not initialized")
    72  	}
    73  
    74  	info, err := p.s.Stat(ctx, "publicshares.json")
    75  	if err != nil {
    76  		if _, ok := err.(errtypes.NotFound); ok {
    77  			return p.db.publicShares, nil // Nothing to sync against
    78  		}
    79  		return nil, err
    80  	}
    81  
    82  	if utils.TSToTime(info.Mtime).After(p.db.mtime) {
    83  		readBytes, err := p.s.SimpleDownload(ctx, "publicshares.json")
    84  		if err != nil {
    85  			return nil, err
    86  		}
    87  		p.db.publicShares = persistence.PublicShares{}
    88  		if err := json.Unmarshal(readBytes, &p.db.publicShares); err != nil {
    89  			return nil, err
    90  		}
    91  		p.db.mtime = utils.TSToTime(info.Mtime)
    92  	}
    93  	return p.db.publicShares, nil
    94  }
    95  
    96  func (p *cs3) Write(ctx context.Context, db persistence.PublicShares) error {
    97  	if !p.initialized {
    98  		return fmt.Errorf("not initialized")
    99  	}
   100  	dbAsJSON, err := json.Marshal(db)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	_, err = p.s.Upload(ctx, metadata.UploadRequest{
   106  		Content:           dbAsJSON,
   107  		Path:              "publicshares.json",
   108  		IfUnmodifiedSince: p.db.mtime,
   109  	})
   110  	return err
   111  }