github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_info.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	protobuf "google.golang.org/protobuf/proto"
    28  )
    29  
    30  // FileInfo contains the details about a file stored on Hedera
    31  type FileInfo struct {
    32  	FileID         FileID
    33  	Size           int64
    34  	ExpirationTime time.Time
    35  	IsDeleted      bool
    36  	Keys           KeyList
    37  	FileMemo       string
    38  	LedgerID       LedgerID
    39  }
    40  
    41  func _FileInfoFromProtobuf(fileInfo *services.FileGetInfoResponse_FileInfo) (FileInfo, error) {
    42  	if fileInfo == nil {
    43  		return FileInfo{}, errParameterNull
    44  	}
    45  	var keys KeyList
    46  	var err error
    47  	if fileInfo.Keys != nil {
    48  		keys, err = _KeyListFromProtobuf(fileInfo.Keys)
    49  		if err != nil {
    50  			return FileInfo{}, err
    51  		}
    52  	}
    53  
    54  	fileID := FileID{}
    55  	if fileInfo.FileID != nil {
    56  		fileID = *_FileIDFromProtobuf(fileInfo.FileID)
    57  	}
    58  
    59  	return FileInfo{
    60  		FileID:         fileID,
    61  		Size:           fileInfo.Size,
    62  		ExpirationTime: _TimeFromProtobuf(fileInfo.ExpirationTime),
    63  		IsDeleted:      fileInfo.Deleted,
    64  		Keys:           keys,
    65  		FileMemo:       fileInfo.Memo,
    66  		LedgerID:       LedgerID{fileInfo.LedgerId},
    67  	}, nil
    68  }
    69  
    70  func (fileInfo *FileInfo) _ToProtobuf() *services.FileGetInfoResponse_FileInfo {
    71  	return &services.FileGetInfoResponse_FileInfo{
    72  		FileID: fileInfo.FileID._ToProtobuf(),
    73  		Size:   fileInfo.Size,
    74  		ExpirationTime: &services.Timestamp{
    75  			Seconds: int64(fileInfo.ExpirationTime.Second()),
    76  			Nanos:   int32(fileInfo.ExpirationTime.Nanosecond()),
    77  		},
    78  		Deleted:  fileInfo.IsDeleted,
    79  		Keys:     fileInfo.Keys._ToProtoKeyList(),
    80  		Memo:     fileInfo.FileMemo,
    81  		LedgerId: fileInfo.LedgerID.ToBytes(),
    82  	}
    83  }
    84  
    85  // ToBytes returns the byte representation of the FileInfo
    86  func (fileInfo FileInfo) ToBytes() []byte {
    87  	data, err := protobuf.Marshal(fileInfo._ToProtobuf())
    88  	if err != nil {
    89  		return make([]byte, 0)
    90  	}
    91  
    92  	return data
    93  }
    94  
    95  // FileInfoFromBytes returns a FileInfo object from a raw byte array
    96  func FileInfoFromBytes(data []byte) (FileInfo, error) {
    97  	if data == nil {
    98  		return FileInfo{}, errByteArrayNull
    99  	}
   100  	pb := services.FileGetInfoResponse_FileInfo{}
   101  	err := protobuf.Unmarshal(data, &pb)
   102  	if err != nil {
   103  		return FileInfo{}, err
   104  	}
   105  
   106  	info, err := _FileInfoFromProtobuf(&pb)
   107  	if err != nil {
   108  		return FileInfo{}, err
   109  	}
   110  
   111  	return info, nil
   112  }