Skip to content

Commit

Permalink
Update file count (IceWhaleTech#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong authored Mar 21, 2023
1 parent e319975 commit 858ab5b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
31 changes: 3 additions & 28 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,19 +598,6 @@ func NameAccumulation(name string, dir string) string {
}
}

// / 解析多个文件上传中,每个具体的文件的信息
// type FileHeader struct {
// ContentDisposition string
// Name string
// FileName string
// ///< 文件名
// ContentType string
// ContentLength int64
// }

// / 解析描述文件信息的头部
// / @return FileHeader 文件名等信息的结构体
// / @return bool 解析成功还是失败
func ParseFileHeader(h []byte, boundary []byte) (map[string]string, bool) {
arr := bytes.Split(h, boundary)
//var out_header FileHeader
Expand Down Expand Up @@ -677,10 +664,6 @@ func ParseFileHeader(h []byte, boundary []byte) (map[string]string, bool) {
return result, true
}

// / 从流中一直读到文件的末位
// / @return []byte 没有写到文件且又属于下一个文件的数据
// / @return bool 是否已经读到流的末位了
// / @return error 是否发生错误
func ReadToBoundary(boundary []byte, stream io.ReadCloser, target io.WriteCloser) ([]byte, bool, error) {
read_data := make([]byte, 1024*8)
read_data_len := 0
Expand All @@ -695,15 +678,15 @@ func ReadToBoundary(boundary []byte, stream io.ReadCloser, target io.WriteCloser
}
reach_end = true
}
//todo: 下面这一句很蠢,值得优化
copy(read_data[read_data_len:], buf[:read_len]) //追加到另一块buffer,仅仅只是为了搜索方便

copy(read_data[read_data_len:], buf[:read_len])
read_data_len += read_len
if read_data_len < b_len+4 {
continue
}
loc := bytes.Index(read_data[:read_data_len], boundary)
if loc >= 0 {
//找到了结束位置

target.Write(read_data[:loc-4])
return read_data[loc:read_data_len], reach_end, nil
}
Expand All @@ -715,14 +698,6 @@ func ReadToBoundary(boundary []byte, stream io.ReadCloser, target io.WriteCloser
return nil, reach_end, nil
}

// / 解析表单的头部
// / @param read_data 已经从流中读到的数据
// / @param read_total 已经从流中读到的数据长度
// / @param boundary 表单的分割字符串
// / @param stream 输入流
// / @return FileHeader 文件名等信息头
// /[]byte 已经从流中读到的部分
// /error 是否发生错误
func ParseFromHead(read_data []byte, read_total int, boundary []byte, stream io.ReadCloser) (map[string]string, []byte, error) {

buf := make([]byte, 1024*8)
Expand Down
5 changes: 3 additions & 2 deletions route/v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,15 @@ func PostFileOctet(c *gin.Context) {
}
log.Printf("file :%s\n", file_header)
//
f, err := os.Create(file_header["path"] + "/" + file_header["filename"])
//os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o644)
f, err := os.OpenFile(file_header["path"]+"/"+file_header["filename"], os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
log.Printf("create file fail:%v\n", err)
return
}
f.Write(file_data)
file_data = nil
//需要反复搜索boundary

temp_data, reach_end, err := file.ReadToBoundary(boundary, c.Request.Body, f)
f.Close()
if err != nil {
Expand Down

0 comments on commit 858ab5b

Please sign in to comment.