@@ -46,7 +46,7 @@ func CheckPermission(src string) bool {
4646
4747// IsNotExistMkDir create a directory if it does not exist
4848func IsNotExistMkDir (src string ) error {
49- if notExist := CheckNotExist (src ); notExist {
49+ if CheckNotExist (src ) {
5050 if err := MkDir (src ); err != nil {
5151 return err
5252 }
@@ -121,9 +121,9 @@ func MustOpen(fileName, filePath string) (*os.File, error) {
121121 return f , nil
122122}
123123
124- // 判断所给路径文件/文件夹是否存在
124+ // Exists checks if a file or directory exists
125125func Exists (path string ) bool {
126- _ , err := os .Stat (path ) // os.Stat获取文件信息
126+ _ , err := os .Stat (path )
127127 if err != nil {
128128 if os .IsExist (err ) {
129129 return true
@@ -133,7 +133,7 @@ func Exists(path string) bool {
133133 return true
134134}
135135
136- // 判断所给路径是否为文件夹
136+ // IsDir checks if the given path is a directory
137137func IsDir (path string ) bool {
138138 s , err := os .Stat (path )
139139 if err != nil {
@@ -142,7 +142,7 @@ func IsDir(path string) bool {
142142 return s .IsDir ()
143143}
144144
145- // 判断所给路径是否为文件
145+ // IsFile checks if the given path is a file
146146func IsFile (path string ) bool {
147147 return ! IsDir (path )
148148}
@@ -173,7 +173,7 @@ func CreateFileAndWriteContent(path, content string) error {
173173
174174// IsNotExistCreateFile create a file if it does not exist
175175func IsNotExistCreateFile (src string ) error {
176- if notExist := CheckNotExist (src ); notExist {
176+ if CheckNotExist (src ) {
177177 if err := CreateFile (src ); err != nil {
178178 return err
179179 }
@@ -198,7 +198,6 @@ func ReadFullFile(path string) []byte {
198198// copyFileContents holds the logic shared by CopyFile and CopySingleFile:
199199// remove an existing destination (unless style == "skip"), stream src -> dst,
200200// and mirror the source file's permissions (capped to a safe mask).
201- // Extracting this removes the near-duplicate block SonarCloud was flagging.
202201func copyFileContents (src , dst , style string ) error {
203202 if Exists (dst ) {
204203 if style == "skip" {
@@ -250,7 +249,7 @@ func CopySingleFile(src, dst, style string) error {
250249 return copyFileContents (src , dst , style )
251250}
252251
253- // Check for duplicate file names
252+ // GetNoDuplicateFileName checks for duplicate file names and returns a unique name
254253func GetNoDuplicateFileName (fullPath string ) string {
255254 dirPath , fileName := filepath .Split (fullPath )
256255 fileSuffix := path .Ext (fileName )
@@ -261,7 +260,7 @@ func GetNoDuplicateFileName(fullPath string) string {
261260 return fullPath
262261}
263262
264- // Dir copies a whole directory recursively
263+ // CopyDir copies a whole directory recursively
265264func CopyDir (src , dst , style string ) error {
266265 var err error
267266 var fds []os.FileInfo
@@ -282,9 +281,8 @@ func CopyDir(src, dst, style string) error {
282281 if Exists (dst ) {
283282 if style == "skip" {
284283 return nil
285- } else {
286- os .Remove (dst )
287284 }
285+ os .Remove (dst )
288286 }
289287 if err = os .MkdirAll (dst , srcinfo .Mode ()& 0o750 ); err != nil {
290288 return err
@@ -341,7 +339,7 @@ func WriteToFullPath(data []byte, fullPath string, perm fs.FileMode) error {
341339 return err
342340}
343341
344- // 最终拼接
342+ // SpliceFiles concatenates multiple file chunks into a single file
345343func SpliceFiles (dir , path string , length int , startPoint int ) error {
346344 fullPath := path
347345
@@ -488,7 +486,7 @@ func GetFileOrDirSize(path string) (int64, error) {
488486 return fileInfo .Size (), nil
489487}
490488
491- // getFileSize get file size by path(B)
489+ // DirSizeB calculates the total size of a directory in bytes
492490func DirSizeB (path string ) (int64 , error ) {
493491 var size int64
494492 err := filepath .Walk (path , func (_ string , info os.FileInfo , err error ) error {
@@ -613,42 +611,92 @@ func ReadToBoundary(boundary []byte, stream io.ReadCloser, target io.WriteCloser
613611 return nil , reach_end , nil
614612}
615613
614+ // parseFileHeaderFromData extracts file header map from raw data
615+ func parseFileHeaderFromData (data , boundary []byte ) (map [string ]string , bool ) {
616+ arr := bytes .Split (data , boundary )
617+ result := make (map [string ]string )
618+ for _ , item := range arr {
619+ tarr := bytes .Split (item , []byte (";" ))
620+ if len (tarr ) != 2 {
621+ continue
622+ }
623+
624+ tbyte := tarr [1 ]
625+ tbyte = bytes .ReplaceAll (tbyte , []byte ("\r \n --" ), []byte ("" ))
626+ tbyte = bytes .ReplaceAll (tbyte , []byte ("name=\" " ), []byte ("" ))
627+ tempArr := bytes .Split (tbyte , []byte ("\" \r \n \r \n " ))
628+ if len (tempArr ) != 2 {
629+ continue
630+ }
631+ result [strings .TrimSpace (string (tempArr [0 ]))] = strings .TrimSpace (string (tempArr [1 ]))
632+ }
633+ return result , true
634+ }
635+
636+ // findBoundaryInData locates the boundary in the data buffer
637+ func findBoundaryInData (data []byte , boundary []byte , readTotal int ) int {
638+ return bytes .LastIndex (data [:readTotal ], boundary )
639+ }
640+
641+ // extractFileHeader extracts the file header from the read buffer
642+ func extractFileHeader (data []byte , boundary []byte , readTotal int ) (map [string ]string , int , bool , error ) {
643+ boundaryLoc := findBoundaryInData (data , boundary , readTotal )
644+ if boundaryLoc == - 1 {
645+ return nil , 0 , false , nil
646+ }
647+
648+ startLoc := boundaryLoc + len (boundary )
649+ fileHeadLoc := bytes .Index (data [startLoc :readTotal ], []byte ("\r \n \r \n " ))
650+ if fileHeadLoc == - 1 {
651+ return nil , 0 , false , nil
652+ }
653+ fileHeadLoc += startLoc
654+
655+ headMap , ok := parseFileHeaderFromData (data , boundary )
656+ if ! ok {
657+ return headMap , 0 , false , fmt .Errorf ("ParseFileHeader fail: %s" , string (data [startLoc :fileHeadLoc ]))
658+ }
659+ return headMap , fileHeadLoc + 4 , true , nil
660+ }
661+
662+ // ParseFromHead parses the file header from the beginning of the stream
616663func ParseFromHead (read_data []byte , read_total int , boundary []byte , stream io.ReadCloser ) (map [string ]string , []byte , error ) {
617664 buf := make ([]byte , 1024 * 8 )
618- found_boundary := false
619- boundary_loc := - 1
665+ foundBoundary := false
666+ boundaryLoc := - 1
620667
621668 for {
622- read_len , err := stream .Read (buf )
623- if err != nil {
624- if err != io . EOF {
625- return nil , nil , err
626- }
669+ readLen , err := stream .Read (buf )
670+ if err != nil && err != io . EOF {
671+ return nil , nil , err
672+ }
673+ if readLen <= 0 {
627674 break
628675 }
629- if read_total + read_len > cap (read_data ) {
676+
677+ if read_total + readLen > cap (read_data ) {
630678 return nil , nil , fmt .Errorf ("not found boundary" )
631679 }
632- copy (read_data [read_total :], buf [:read_len ])
633- read_total += read_len
634- if ! found_boundary {
635- boundary_loc = bytes .LastIndex (read_data [:read_total ], boundary )
636- if boundary_loc == - 1 {
680+
681+ copy (read_data [read_total :], buf [:readLen ])
682+ read_total += readLen
683+
684+ if ! foundBoundary {
685+ boundaryLoc = findBoundaryInData (read_data , boundary , read_total )
686+ if boundaryLoc == - 1 {
637687 continue
638688 }
639- found_boundary = true
689+ foundBoundary = true
640690 }
641- start_loc := boundary_loc + len ( boundary )
642- file_head_loc := bytes . Index (read_data [ start_loc : read_total ], [] byte ( " \r \n \r \n " ) )
643- if file_head_loc == - 1 {
644- continue
691+
692+ headMap , endLoc , ok , err := extractFileHeader (read_data , boundary , read_total )
693+ if err != nil {
694+ return nil , nil , err
645695 }
646- file_head_loc += start_loc
647- headMap , ret := ParseFileHeader (read_data , boundary )
648- if ! ret {
649- return headMap , nil , fmt .Errorf ("ParseFileHeader fail:%s" , string (read_data [start_loc :file_head_loc ]))
696+ if ! ok {
697+ continue
650698 }
651- return headMap , read_data [file_head_loc + 4 : read_total ], nil
699+ return headMap , read_data [endLoc : read_total ], nil
652700 }
653701 return nil , nil , fmt .Errorf ("reach to stream EOF" )
654702}
0 commit comments