Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: file read cache #396

Merged
merged 17 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions internal/file/file_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package file

import (
"bufio"
"io"
"os"
)

const readBufferSize = 1024 * 1024 * 5 // 5mb

func Open(req *UploadFileReq) (ReadFile, error) {
file, err := os.Open(req.Filepath)
if err != nil {
Expand All @@ -31,28 +34,38 @@ func Open(req *UploadFileReq) (ReadFile, error) {
_ = file.Close()
return nil, err
}
return &defaultFile{
df := &defaultFile{
file: file,
info: info,
}, nil
}
df.resetReaderBuffer()
return df, nil
}

type defaultFile struct {
file *os.File
info os.FileInfo
file *os.File
info os.FileInfo
reader io.Reader
}

func (d *defaultFile) resetReaderBuffer() {
d.reader = bufio.NewReaderSize(d.file, readBufferSize)
}

func (d *defaultFile) Read(p []byte) (n int, err error) {
return d.file.Read(p)
return d.reader.Read(p)
}

func (d *defaultFile) Close() error {
return d.file.Close()
}

func (d *defaultFile) StartSeek(whence int) error {
_, err := d.file.Seek(io.SeekStart, whence)
return err
if _, err := d.file.Seek(io.SeekStart, whence); err != nil {
return err
}
d.resetReaderBuffer()
return nil
}

func (d *defaultFile) Size() int64 {
Expand Down
29 changes: 26 additions & 3 deletions internal/file/file_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@
package file

import (
"bufio"
"errors"
"github.com/openimsdk/openim-sdk-core/v3/wasm/exec"
"io"
"syscall/js"
)

const readBufferSize = 1024 * 1024 * 5 // 5mb

func Open(req *UploadFileReq) (ReadFile, error) {
file := newJsCallFile(req.Uuid)
size, err := file.Open()
if err != nil {
return nil, err
}
return &jsFile{
jf := &jsFile{
size: size,
file: file,
}, nil
}
jf.resetReaderBuffer()
return jf, nil
}

type jsFile struct {
size int64
file *jsCallFile
whence int
reader io.Reader
}

func (j *jsFile) Read(p []byte) (n int, err error) {
func (j *jsFile) resetReaderBuffer() {
j.reader = bufio.NewReaderSize(&reader{fn: j.read}, readBufferSize)
}

func (j *jsFile) read(p []byte) (n int, err error) {
length := len(p)
if length == 0 {
return 0, errors.New("read buffer is empty")
Expand All @@ -65,6 +75,10 @@ func (j *jsFile) Read(p []byte) (n int, err error) {
return len(data), nil
}

func (j *jsFile) Read(p []byte) (n int, err error) {
return j.reader.Read(p)
}

func (j *jsFile) Close() error {
return j.file.Close()
}
Expand All @@ -78,9 +92,18 @@ func (j *jsFile) StartSeek(whence int) error {
return errors.New("seek whence is out of range")
}
j.whence = whence
j.resetReaderBuffer()
return nil
}

type reader struct {
fn func(p []byte) (n int, err error)
}

func (r *reader) Read(p []byte) (n int, err error) {
return r.fn(p)
}

type jsCallFile struct {
uuid string
}
Expand Down
86 changes: 22 additions & 64 deletions testv2/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,25 @@

package testv2

//import (
// "flag"
// "fmt"
// "github.com/OpenIMSDK/tools/mcontext"
// "open_im_sdk/internal/file"
// "open_im_sdk/open_im_sdk"
// "path/filepath"
// "testing"
// "time"
//)
//
//type FilePutCallback struct{}
//
//func (c *FilePutCallback) Open(size int64) {
// fmt.Println("open put file", size)
//}
//
//func (c *FilePutCallback) HashProgress(current, total int64) {
// //fmt.Println("hash", current, total)
//}
//
//func (c *FilePutCallback) HashComplete(hash string, total int64) {
// fmt.Println("hash complete", hash, total)
//}
//
//func (c *FilePutCallback) PutStart(current, total int64) {
// fmt.Println("put start", current, total)
//}
//
//func (c *FilePutCallback) PutProgress(save int64, current, total int64) {
// fmt.Printf("put progress [%d/%d] put %f%% save %f%%\n", current, total, float64(current)/float64(total)*100, float64(save)/float64(total)*100)
//}
//
//func (c *FilePutCallback) PutComplete(total int64, putType int) {
// fmt.Println("put complete", total, putType)
//}
//
//func TestPut(t *testing.T) {
// ctx := mcontext.NewCtx("123456")
//
// putID := "asdkmjakslaaa"
//
// go func() {
// time.Sleep(time.Second * 3)
// fmt.Println("###################### CANCEL PUT ######################")
// open_im_sdk.UserForSDK.File().Cancel(ctx, putID)
// }()
//
// req := &file.PutArgs{
// PutID: putID,
// Filepath: "C:\\Users\\Admin\\Desktop\\VMware-workstation-full-17.0.0-20800274.exe",
// }
// req.Name = filepath.Base(req.Filepath)
// str, err := open_im_sdk.UserForSDK.File().PutFile(ctx, req, &FilePutCallback{})
// if err != nil {
// t.Fatal(err)
// }
// fmt.Println("url", str)
//}
//
//func Test_Fmt(t *testing.T) {
// i := flag.Int("sn", 2, "sender num")
// fmt.Println(i)
//}
import (
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk"
"path/filepath"
"testing"
)

func TestUploadFile(t *testing.T) {

fp := `C:\Users\openIM\Desktop\dist.zip`

resp, err := open_im_sdk.UserForSDK.File().UploadFile(ctx, &file.UploadFileReq{
Filepath: fp,
Name: filepath.Base(fp),
Cause: "test",
}, nil)
if err != nil {
t.Fatal(err)
}
t.Log(resp)

}
Loading