github.com/minio/console@v1.4.1/api/admin_objects.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"encoding/base64"
    22  	"time"
    23  
    24  	"github.com/minio/mc/cmd"
    25  	"github.com/minio/minio-go/v7"
    26  )
    27  
    28  type objectsListOpts struct {
    29  	BucketName string
    30  	Prefix     string
    31  	Date       time.Time
    32  }
    33  
    34  type ObjectsRequest struct {
    35  	Mode       string `json:"mode,omitempty"`
    36  	BucketName string `json:"bucket_name"`
    37  	Prefix     string `json:"prefix"`
    38  	Date       string `json:"date"`
    39  	RequestID  int64  `json:"request_id"`
    40  }
    41  
    42  type WSResponse struct {
    43  	RequestID  int64            `json:"request_id,omitempty"`
    44  	Error      *CodedAPIError   `json:"error,omitempty"`
    45  	RequestEnd bool             `json:"request_end,omitempty"`
    46  	Prefix     string           `json:"prefix,omitempty"`
    47  	BucketName string           `json:"bucketName,omitempty"`
    48  	Data       []ObjectResponse `json:"data,omitempty"`
    49  }
    50  
    51  type ObjectResponse struct {
    52  	Name         string `json:"name,omitempty"`
    53  	LastModified string `json:"last_modified,omitempty"`
    54  	Size         int64  `json:"size,omitempty"`
    55  	VersionID    string `json:"version_id,omitempty"`
    56  	DeleteMarker bool   `json:"delete_flag,omitempty"`
    57  	IsLatest     bool   `json:"is_latest,omitempty"`
    58  }
    59  
    60  func getObjectsOptionsFromReq(request ObjectsRequest) (*objectsListOpts, error) {
    61  	pOptions := objectsListOpts{
    62  		BucketName: request.BucketName,
    63  		Prefix:     "",
    64  	}
    65  
    66  	prefix := request.Prefix
    67  
    68  	if prefix != "" {
    69  		encodedPrefix := SanitizeEncodedPrefix(prefix)
    70  		decodedPrefix, err := base64.StdEncoding.DecodeString(encodedPrefix)
    71  		if err != nil {
    72  			LogError("error decoding prefix: %v", err)
    73  			return nil, err
    74  		}
    75  
    76  		pOptions.Prefix = string(decodedPrefix)
    77  	}
    78  
    79  	if request.Mode == "rewind" {
    80  		parsedDate, errDate := time.Parse(time.RFC3339, request.Date)
    81  
    82  		if errDate != nil {
    83  			return nil, errDate
    84  		}
    85  
    86  		pOptions.Date = parsedDate
    87  	}
    88  
    89  	return &pOptions, nil
    90  }
    91  
    92  func startObjectsListing(ctx context.Context, client MinioClient, objOpts *objectsListOpts) <-chan minio.ObjectInfo {
    93  	opts := minio.ListObjectsOptions{
    94  		Prefix: objOpts.Prefix,
    95  	}
    96  
    97  	return client.listObjects(ctx, objOpts.BucketName, opts)
    98  }
    99  
   100  func startRewindListing(ctx context.Context, client MCClient, objOpts *objectsListOpts) <-chan *cmd.ClientContent {
   101  	lsRewind := client.list(ctx, cmd.ListOptions{TimeRef: objOpts.Date, WithDeleteMarkers: true})
   102  
   103  	return lsRewind
   104  }