main.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package main
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "file-manger-server/config"
  6. "file-manger-server/db"
  7. "file-manger-server/server"
  8. "file-manger-server/service"
  9. "fmt"
  10. "io/fs"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "sync"
  15. )
  16. func main() {
  17. config.ReadConfig()
  18. db.Open()
  19. go server.Run()
  20. service.RunGin()
  21. //TestHandlerReceiveFile()
  22. //TestFiles()
  23. }
  24. func TestHandlerReceiveFile() {
  25. bytes := []byte{
  26. 0x00,
  27. }
  28. for i := 0; i < 32; i++ {
  29. bytes = append(bytes, byte(i+3))
  30. }
  31. var fileName = "test.txt"
  32. //文件名长度
  33. bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(fileName)))
  34. //文件名
  35. bytes = append(bytes, []byte(fileName)...)
  36. //文件大小
  37. bytes = binary.BigEndian.AppendUint64(bytes, uint64(10))
  38. var path = "./file/" + fileName
  39. bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  40. //文件路径
  41. bytes = append(bytes, []byte(path)...)
  42. //分片大小
  43. bytes = binary.BigEndian.AppendUint32(bytes, uint32(0))
  44. //创建时间
  45. bytes = binary.BigEndian.AppendUint64(bytes, uint64(999))
  46. //修改时间
  47. bytes = binary.BigEndian.AppendUint64(bytes, uint64(9999))
  48. //最后访问时间
  49. bytes = binary.BigEndian.AppendUint64(bytes, uint64(99999))
  50. bytes = append(bytes, []byte("1234567890中文")...)
  51. server.HandlerReceiveFile(nil, bytes)
  52. }
  53. func TestBigFileSend() {
  54. //var str = "\tif err != nil {\n\t\treturn errors.New(\"FileUser transactionId is not found\")\n\t}\n\tif fileUser.UserId == 0 {\n\t\treturn errors.New(\"FileUser userId is not found\")\n\t}"
  55. //var data = []byte(str + str + str + str + str + str + "\t//config.ReadConfig()\n\t//db.Open()\n\t////server.Run()\n\t// server")
  56. ////切片1
  57. //{
  58. // bytes := []byte{
  59. // 0x00,
  60. // }
  61. // for i := 0; i < 32; i++ {
  62. // bytes = append(bytes, byte(i+3))
  63. // }
  64. // var fileName = "test.txt"
  65. // //文件名长度
  66. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(fileName)))
  67. // //文件名
  68. // bytes = append(bytes, []byte(fileName)...)
  69. // //文件大小
  70. // bytes = binary.BigEndian.AppendUint64(bytes, uint64(1024*2+8))
  71. // var path = "file/" + fileName
  72. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  73. // //文件路径
  74. // bytes = append(bytes, []byte(path)...)
  75. // //分片大小
  76. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(2))
  77. // //创建时间
  78. // bytes = binary.BigEndian.AppendUint64(bytes, uint64(999))
  79. // //修改时间
  80. // bytes = binary.BigEndian.AppendUint64(bytes, uint64(9999))
  81. // //最后访问时间
  82. // bytes = binary.BigEndian.AppendUint64(bytes, uint64(99999))
  83. // server.HandlerReceiveFile(nil, bytes)
  84. //}
  85. //{
  86. // //切片2
  87. // bytes := []byte{
  88. // 0x01,
  89. // }
  90. // for i := 0; i < 32; i++ {
  91. // bytes = append(bytes, byte(i+3))
  92. // }
  93. // var fileName = "test.txt"
  94. // //切片id
  95. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(0))
  96. // var path = "file/" + fileName
  97. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  98. // //文件路径
  99. // bytes = append(bytes, []byte(path)...)
  100. // fmt.Println("=================================")
  101. // fmt.Println("data len : ", len(data))
  102. // fmt.Println("=================================")
  103. // bytes = append(bytes, data...)
  104. // server.HandlerReceiveFile(nil, bytes)
  105. //}
  106. //
  107. //{
  108. // //切片4
  109. // bytes := []byte{
  110. // 0x01,
  111. // }
  112. // for i := 0; i < 32; i++ {
  113. // bytes = append(bytes, byte(i+3))
  114. // }
  115. // var fileName = "test.txt"
  116. // //切片id
  117. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(2))
  118. // var path = "file/" + fileName
  119. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  120. // //文件路径
  121. // bytes = append(bytes, []byte(path)...)
  122. // var end = "\n| end |"
  123. // bytes = append(bytes, []byte(end)...)
  124. //
  125. // fmt.Println("=================================")
  126. // fmt.Println("end len : ", len(end))
  127. // fmt.Println("=================================")
  128. // server.HandlerReceiveFile(nil, bytes)
  129. //}
  130. //{
  131. // //切片2
  132. // bytes := []byte{
  133. // 0x01,
  134. // }
  135. // for i := 0; i < 32; i++ {
  136. // bytes = append(bytes, byte(i+3))
  137. // }
  138. // var fileName = "test.txt"
  139. // //切片id
  140. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(1))
  141. // var path = "file/" + fileName
  142. // bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  143. // //文件路径
  144. // bytes = append(bytes, []byte(path)...)
  145. // fmt.Println("=================================")
  146. // fmt.Println("data len : ", len(data))
  147. // fmt.Println("=================================")
  148. // bytes = append(bytes, data...)
  149. // server.HandlerReceiveFile(nil, bytes)
  150. //}
  151. {
  152. bytes := []byte{
  153. 0x00,
  154. }
  155. for i := 0; i < 32; i++ {
  156. bytes = append(bytes, byte(i+3))
  157. }
  158. var fileName = "testtxt"
  159. //文件名长度
  160. bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(fileName)))
  161. //文件名
  162. bytes = append(bytes, []byte(fileName)...)
  163. //文件大小
  164. bytes = binary.BigEndian.AppendUint64(bytes, uint64(0xFFFFFFFFFFFFFFFF))
  165. var path = "file/" + fileName
  166. bytes = binary.BigEndian.AppendUint32(bytes, uint32(len(path)))
  167. //文件路径
  168. bytes = append(bytes, []byte(path)...)
  169. //分片大小
  170. bytes = binary.BigEndian.AppendUint32(bytes, uint32(0))
  171. //创建时间
  172. bytes = binary.BigEndian.AppendUint64(bytes, uint64(1999))
  173. //修改时间
  174. bytes = binary.BigEndian.AppendUint64(bytes, uint64(19999))
  175. //最后访问时间
  176. bytes = binary.BigEndian.AppendUint64(bytes, uint64(199999))
  177. server.HandlerReceiveFile(nil, bytes)
  178. }
  179. }
  180. func TestFiles() {
  181. info, err := GetPathInfo("D:\\")
  182. if err != nil {
  183. panic(err)
  184. }
  185. heavy, err := GoHeavy(info)
  186. if err != nil {
  187. panic(err)
  188. }
  189. fmt.Println(heavy.Dirs)
  190. }
  191. type PathInfo struct {
  192. Dirs []string // 目录路径(包含所有层级)
  193. Files []string // 文件路径
  194. }
  195. var pathPool = sync.Pool{
  196. New: func() interface{} {
  197. return &PathInfo{
  198. Dirs: make([]string, 0, 1024), // 预分配容量
  199. Files: make([]string, 0, 4096),
  200. }
  201. },
  202. }
  203. func GoHeavy(info *PathInfo) (PathInfo, error) {
  204. m := make(map[string]bool)
  205. for i := range info.Files {
  206. path := info.Files[i]
  207. lastIndex := strings.LastIndex(path, "/")
  208. if lastIndex == -1 {
  209. lastIndex = strings.LastIndex(path, "\\")
  210. }
  211. if lastIndex == -1 {
  212. continue
  213. }
  214. //if _, ok := m[path[:lastIndex]]; !ok {
  215. // fmt.Println(path[:lastIndex])
  216. //}
  217. m[path[:lastIndex]] = true
  218. }
  219. var dirs = make([]string, 0, len(m))
  220. fmt.Println(len(info.Dirs))
  221. for i := range info.Dirs {
  222. path := info.Dirs[i]
  223. if !m[path] {
  224. fmt.Println(info.Dirs[i])
  225. dirs = append(dirs, info.Dirs[i])
  226. }
  227. }
  228. info.Dirs = dirs
  229. return *info, nil
  230. }
  231. func GetPathInfo(root string) (*PathInfo, error) {
  232. //获取文件名
  233. index := strings.LastIndex(root, "/")
  234. if index == -1 {
  235. index = strings.LastIndex(root, "\\")
  236. }
  237. //RootFileName := ""
  238. //if index == -1 {
  239. // RootFileName = root
  240. //} else {
  241. // RootFileName = root[:index+1]
  242. //}
  243. // 复用对象减少内存分配[9](@ref)
  244. info := pathPool.Get().(*PathInfo)
  245. // 路径有效性校验[5,6](@ref)
  246. root = filepath.Clean(root)
  247. stat, err := os.Stat(root)
  248. if err != nil {
  249. if errors.Is(err, fs.ErrNotExist) {
  250. return nil, &os.PathError{Op: "stat", Path: root, Err: err} // 增强错误信息[8](@ref)
  251. }
  252. return nil, err
  253. }
  254. // 文件直接返回[4](@ref)
  255. if !stat.IsDir() {
  256. info.Files = append(info.Files, root)
  257. return info, nil
  258. }
  259. // 使用WalkDir优化遍历性能[4,9](@ref)
  260. err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
  261. if err != nil {
  262. // 处理遍历错误但继续执行[7](@ref)
  263. if errors.Is(err, fs.ErrPermission) {
  264. return nil // 跳过权限错误
  265. }
  266. return err
  267. }
  268. // 排除根目录自身
  269. if path == root {
  270. return nil
  271. }
  272. // 动态扩容检测[9](@ref)
  273. if len(info.Dirs)+len(info.Files) > 1e8 {
  274. return errors.New("超出内存安全阈值(1,000,000条路径)")
  275. }
  276. //fmt.Println(path[len(RootFileName):])
  277. if d.IsDir() {
  278. info.Dirs = append(info.Dirs, path)
  279. } else {
  280. info.Files = append(info.Files, path)
  281. }
  282. return nil
  283. })
  284. if err != nil {
  285. return nil, &os.PathError{Op: "walk", Path: root, Err: err} // 错误包装[6](@ref)
  286. }
  287. return info, nil
  288. }
  289. // 流式处理版本(适用于超大数据集)
  290. func StreamPathInfo(root string) (<-chan string, <-chan error) {
  291. paths := make(chan string, 1000)
  292. errs := make(chan error, 1)
  293. go func() {
  294. defer close(paths)
  295. defer close(errs)
  296. err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
  297. if err != nil {
  298. return err
  299. }
  300. if path != root {
  301. select {
  302. case paths <- path:
  303. default:
  304. return errors.New("消费者处理过慢")
  305. }
  306. }
  307. return nil
  308. })
  309. if err != nil {
  310. errs <- &os.PathError{Op: "stream", Path: root, Err: err}
  311. }
  312. }()
  313. return paths, errs
  314. }