github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/eosfs/upload.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 eosfs
    20  
    21  import (
    22  	"context"
    23  	"os"
    24  	"path"
    25  
    26  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/storage"
    29  	"github.com/cs3org/reva/v2/pkg/storage/utils/chunking"
    30  	"github.com/pkg/errors"
    31  )
    32  
    33  func (fs *eosfs) Upload(ctx context.Context, req storage.UploadRequest, uff storage.UploadFinishedFunc) (*provider.ResourceInfo, error) {
    34  	p, err := fs.resolve(ctx, req.Ref)
    35  	if err != nil {
    36  		return &provider.ResourceInfo{}, errors.Wrap(err, "eos: error resolving reference")
    37  	}
    38  
    39  	if fs.isShareFolder(ctx, p) {
    40  		return &provider.ResourceInfo{}, errtypes.PermissionDenied("eos: cannot upload under the virtual share folder")
    41  	}
    42  
    43  	if chunking.IsChunked(p) {
    44  		var assembledFile string
    45  		p, assembledFile, err = fs.chunkHandler.WriteChunk(p, req.Body)
    46  		if err != nil {
    47  			return &provider.ResourceInfo{}, err
    48  		}
    49  		if p == "" {
    50  			return &provider.ResourceInfo{}, errtypes.PartialContent(req.Ref.String())
    51  		}
    52  		fd, err := os.Open(assembledFile)
    53  		if err != nil {
    54  			return &provider.ResourceInfo{}, errors.Wrap(err, "eos: error opening assembled file")
    55  		}
    56  		defer fd.Close()
    57  		defer os.RemoveAll(assembledFile)
    58  		req.Body = fd
    59  	}
    60  
    61  	fn := fs.wrap(ctx, p)
    62  
    63  	u, err := getUser(ctx)
    64  	if err != nil {
    65  		return &provider.ResourceInfo{}, errors.Wrap(err, "eos: no user in ctx")
    66  	}
    67  
    68  	// We need the auth corresponding to the parent directory
    69  	// as the file might not exist at the moment
    70  	auth, err := fs.getUserAuth(ctx, u, path.Dir(fn))
    71  	if err != nil {
    72  		return &provider.ResourceInfo{}, err
    73  	}
    74  
    75  	if err := fs.c.Write(ctx, auth, fn, req.Body); err != nil {
    76  		return &provider.ResourceInfo{}, err
    77  	}
    78  
    79  	eosFileInfo, err := fs.c.GetFileInfoByPath(ctx, auth, fn)
    80  	if err != nil {
    81  		return &provider.ResourceInfo{}, err
    82  	}
    83  
    84  	ri, err := fs.convertToResourceInfo(ctx, eosFileInfo)
    85  	if err != nil {
    86  		return &provider.ResourceInfo{}, err
    87  	}
    88  
    89  	return ri, nil
    90  }
    91  
    92  func (fs *eosfs) InitiateUpload(ctx context.Context, ref *provider.Reference, uploadLength int64, metadata map[string]string) (map[string]string, error) {
    93  	p, err := fs.resolve(ctx, ref)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	return map[string]string{
    98  		"simple": p,
    99  	}, nil
   100  }