check if key exists
This commit is contained in:
		
							parent
							
								
									d88552c57f
								
							
						
					
					
						commit
						65410d3210
					
				@ -66,7 +66,14 @@ func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, err
 | 
			
		||||
		Key:  key,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: check if key exists
 | 
			
		||||
	exists, err := c.s3.KeyExists(ctx, key)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !exists {
 | 
			
		||||
		return nil, errors.New("key does not exist")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = c.db.CreateShare(ctx, share)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -39,3 +39,18 @@ func (m *minioClient) GetObject(ctx context.Context, key string) (ObjectReader,
 | 
			
		||||
 | 
			
		||||
	return object, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *minioClient) KeyExists(ctx context.Context, key string) (bool, error) {
 | 
			
		||||
	_, err := m.client.StatObject(ctx, m.bucket, key, minio.GetObjectOptions{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errResponse := minio.ToErrorResponse(err)
 | 
			
		||||
 | 
			
		||||
		if errResponse.Code == "NoSuchKey" {
 | 
			
		||||
			return false, nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -14,4 +14,5 @@ type ObjectReader interface {
 | 
			
		||||
 | 
			
		||||
type S3 interface {
 | 
			
		||||
	GetObject(ctx context.Context, key string) (ObjectReader, error)
 | 
			
		||||
	KeyExists(ctx context.Context, key string) (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user