19
19
package tcvectordb
20
20
21
21
import (
22
+ "bytes"
22
23
"context"
23
24
"encoding/base64"
24
25
"encoding/json"
25
26
"fmt"
27
+ "io"
26
28
"log"
27
29
"net/http"
28
30
"net/url"
@@ -91,7 +93,6 @@ type FlatInterface interface {
91
93
// [ChangePassword] changes the password for the specific user.
92
94
ChangePassword (ctx context.Context , param ChangePasswordParams ) error
93
95
94
- // []
95
96
UploadFile (ctx context.Context , databaseName , collectionName string , param UploadFileParams ) (result * UploadFileResult , err error )
96
97
97
98
GetImageUrl (ctx context.Context , databaseName , collectionName string ,
@@ -334,6 +335,7 @@ func (i *implementerFlatDocument) ChangePassword(ctx context.Context, param Chan
334
335
type UploadFileParams struct {
335
336
FileName string
336
337
LocalFilePath string
338
+ Reader io.Reader
337
339
SplitterPreprocess ai_document_set.DocumentSplitterPreprocess
338
340
EmbeddingModel string
339
341
ParsingProcess * api.ParsingProcess
@@ -355,10 +357,10 @@ func (i *implementerFlatDocument) UploadFile(ctx context.Context, databaseName,
355
357
return uploadFile (ctx , i , databaseName , collectionName , param )
356
358
}
357
359
358
- func checkUploadFileParam (ctx context.Context , param * UploadFileParams ) (size int64 , err error ) {
360
+ func checkUploadFileParam (ctx context.Context , param * UploadFileParams ) (size int64 , reader io. ReadCloser , err error ) {
359
361
if param .FileName == "" {
360
362
if param .LocalFilePath == "" {
361
- return 0 , errors .New ("need param: FileName or LocalFilePath" )
363
+ return 0 , nil , errors .New ("need param: FileName or LocalFilePath" )
362
364
}
363
365
param .FileName = filepath .Base (param .LocalFilePath )
364
366
}
@@ -371,24 +373,41 @@ func checkUploadFileParam(ctx context.Context, param *UploadFileParams) (size in
371
373
log .Printf ("[Warning] %s" , "param SplitterPreprocess.ChunkSplitter will be ommitted, " +
372
374
"because only markdown filetype supports defining ChunkSplitter" )
373
375
}
376
+ if param .LocalFilePath != "" {
377
+ fd , err := os .Open (param .LocalFilePath )
378
+ if err != nil {
379
+ return 0 , nil , err
380
+ }
381
+ reader = fd
382
+ fstat , err := fd .Stat ()
383
+ if err != nil {
384
+ return 0 , nil , err
385
+ }
386
+ size = fstat .Size ()
387
+ } else {
388
+ bytesBuf := bytes .NewBuffer (nil )
389
+ written , err := io .Copy (bytesBuf , param .Reader )
390
+ if err != nil {
391
+ return 0 , nil , err
392
+ }
374
393
375
- fileInfo , err := os .Stat (param .LocalFilePath )
376
- if err != nil {
377
- return 0 , errors .Errorf ("get file size failed. err: %v" , err .Error ())
394
+ size = written
395
+ reader = io .NopCloser (bytesBuf )
378
396
}
379
- size = fileInfo .Size ()
380
397
381
398
if size == 0 {
382
- return 0 , errors .New ("file size cannot be 0" )
399
+ return 0 , nil , errors .New ("file size cannot be 0" )
383
400
}
384
- return size , nil
401
+
402
+ return size , reader , nil
385
403
}
386
404
func uploadFile (ctx context.Context , cli SdkClient , databaseName , collectionName string ,
387
405
param UploadFileParams ) (result * UploadFileResult , err error ) {
388
- size , err := checkUploadFileParam (ctx , & param )
406
+ size , reader , err := checkUploadFileParam (ctx , & param )
389
407
if err != nil {
390
408
return nil , err
391
409
}
410
+ defer reader .Close ()
392
411
393
412
req := new (document.UploadUrlReq )
394
413
req .Database = databaseName
@@ -457,22 +476,36 @@ func uploadFile(ctx context.Context, cli SdkClient, databaseName, collectionName
457
476
return nil , fmt .Errorf ("cos header for param MetaData is too large, it can not be more than 2k" )
458
477
}
459
478
460
- opt := & cos.MultiUploadOptions {
461
- OptIni : & cos.InitiateMultipartUploadOptions {
462
- nil ,
463
- & cos.ObjectPutHeaderOptions {
464
- XCosMetaXXX : & header ,
465
- //Listener: &cos.DefaultProgressListener{},
479
+ if param .LocalFilePath != "" {
480
+ opt := & cos.MultiUploadOptions {
481
+ OptIni : & cos.InitiateMultipartUploadOptions {
482
+ nil ,
483
+ & cos.ObjectPutHeaderOptions {
484
+ XCosMetaXXX : & header ,
485
+ //Listener: &cos.DefaultProgressListener{},
486
+ },
466
487
},
467
- },
468
- // Whether to enable resume from breakpoint, default is false
469
- CheckPoint : true ,
470
- PartSize : 5 ,
471
- }
488
+ // Whether to enable resume from breakpoint, default is false
489
+ CheckPoint : true ,
490
+ PartSize : 5 ,
491
+ }
472
492
473
- _ , _ , err = c .Object .Upload (ctx , res .UploadPath , param .LocalFilePath , opt )
474
- if err != nil {
475
- return nil , err
493
+ _ , _ , err = c .Object .Upload (ctx , res .UploadPath , param .LocalFilePath , opt )
494
+ if err != nil {
495
+ return nil , err
496
+ }
497
+ } else {
498
+
499
+ opt := & cos.ObjectPutOptions {
500
+ ObjectPutHeaderOptions : & cos.ObjectPutHeaderOptions {
501
+ ContentLength : size ,
502
+ XCosMetaXXX : & header ,
503
+ },
504
+ }
505
+ _ , err = c .Object .Put (ctx , res .UploadPath , reader , opt )
506
+ if err != nil {
507
+ return nil , err
508
+ }
476
509
}
477
510
478
511
return result , nil
0 commit comments