github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/packets/oidb/PrivateImageUpload.go (about)

     1  package oidb
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"fmt"
     7  	"math/rand"
     8  
     9  	"github.com/LagrangeDev/LagrangeGo/client/packets/pb/service/oidb"
    10  	"github.com/LagrangeDev/LagrangeGo/utils"
    11  	"github.com/LagrangeDev/LagrangeGo/utils/crypto"
    12  )
    13  
    14  func BuildImageUploadReq(targetUid string, data []byte) (*OidbPacket, error) {
    15  	// OidbSvcTrpcTcp.0x11c5_100
    16  	md5Hash := crypto.MD5Digest(data)
    17  	sha1Hash := crypto.SHA1Digest(data)
    18  	format, size, err := utils.ImageResolve(data)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	imageExt := format.String()
    23  
    24  	hexString := "0800180020004200500062009201009a0100a2010c080012001800200028003a00"
    25  	bytesPbReserveC2c, err := hex.DecodeString(hexString)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	body := &oidb.NTV2RichMediaReq{
    31  		ReqHead: &oidb.MultiMediaReqHead{
    32  			Common: &oidb.CommonHead{
    33  				RequestId: 1,
    34  				Command:   100,
    35  			},
    36  			Scene: &oidb.SceneInfo{
    37  				RequestType:  2,
    38  				BusinessType: 1,
    39  				SceneType:    1,
    40  				C2C: &oidb.C2CUserInfo{
    41  					AccountType: 2,
    42  					TargetUid:   targetUid,
    43  				},
    44  			},
    45  			Client: &oidb.ClientMeta{
    46  				AgentType: 2,
    47  			},
    48  		},
    49  		Upload: &oidb.UploadReq{
    50  			UploadInfo: []*oidb.UploadInfo{
    51  				{
    52  					FileInfo: &oidb.FileInfo{
    53  						FileSize: uint32(len(data)),
    54  						FileHash: fmt.Sprintf("%x", md5Hash),
    55  						FileSha1: fmt.Sprintf("%x", sha1Hash),
    56  						FileName: fmt.Sprintf("%x.%s", md5Hash, imageExt),
    57  						Type: &oidb.FileType{
    58  							Type:        1,
    59  							PicFormat:   uint32(format),
    60  							VideoFormat: 0,
    61  							VoiceFormat: 0,
    62  						},
    63  						Width:    uint32(size.Width),
    64  						Height:   uint32(size.Height),
    65  						Time:     0,
    66  						Original: 1,
    67  					},
    68  					SubFileType: 0,
    69  				},
    70  			},
    71  			TryFastUploadCompleted: true,
    72  			SrvSendMsg:             false,
    73  			ClientRandomId:         uint64(rand.Int63()),
    74  			CompatQMsgSceneType:    1,
    75  			ExtBizInfo: &oidb.ExtBizInfo{
    76  				Pic: &oidb.PicExtBizInfo{
    77  					BytesPbReserveC2C: bytesPbReserveC2c,
    78  				},
    79  				Video: &oidb.VideoExtBizInfo{
    80  					BytesPbReserve: []byte{},
    81  				},
    82  				Ptt: &oidb.PttExtBizInfo{
    83  					BytesReserve:      []byte{},
    84  					BytesPbReserve:    []byte{},
    85  					BytesGeneralFlags: []byte{},
    86  				},
    87  			},
    88  			ClientSeq:       0,
    89  			NoNeedCompatMsg: false,
    90  		},
    91  	}
    92  	return BuildOidbPacket(0x11c5, 100, body, false, true)
    93  }
    94  
    95  func ParseImageUploadResp(data []byte) (*oidb.NTV2RichMediaResp, error) {
    96  	var resp oidb.NTV2RichMediaResp
    97  	baseResp, err := ParseOidbPacket(data, &resp)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	if baseResp.ErrorCode != 0 {
   102  		return nil, errors.New(baseResp.ErrorMsg)
   103  	}
   104  	return &resp, nil
   105  }