Skip to content

CasaOS - A simple, easy-to-use, elegant open-source Personal Cloud system.

Update file count (#968)

link858ab5b12406
diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go
index 1fd0397..7a1e83c 100644
--- a/pkg/utils/file/file.go
+++ b/pkg/utils/file/file.go
@@ -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
@@ -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
@@ -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
}
@@ -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)
diff --git a/route/v1/file.go b/route/v1/file.go
index 627c71b..baa74aa 100644
--- a/route/v1/file.go
+++ b/route/v1/file.go
@@ -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 {