github.com/openimsdk/tools@v0.0.49/s3/cont/id.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     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  package cont
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/json"
    20  	"fmt"
    21  )
    22  
    23  type multipartUploadID struct {
    24  	Type int    `json:"a,omitempty"`
    25  	ID   string `json:"b,omitempty"`
    26  	Key  string `json:"c,omitempty"`
    27  	Size int64  `json:"d,omitempty"`
    28  	Hash string `json:"e,omitempty"`
    29  }
    30  
    31  func newMultipartUploadID(id multipartUploadID) string {
    32  	data, err := json.Marshal(id)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	return base64.StdEncoding.EncodeToString(data)
    37  }
    38  
    39  func parseMultipartUploadID(id string) (*multipartUploadID, error) {
    40  	data, err := base64.StdEncoding.DecodeString(id)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("invalid multipart upload id: %w", err)
    43  	}
    44  	var upload multipartUploadID
    45  	if err := json.Unmarshal(data, &upload); err != nil {
    46  		return nil, fmt.Errorf("invalid multipart upload id: %w", err)
    47  	}
    48  	return &upload, nil
    49  }