fileInfo.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package gio
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "golang.org/x/sys/windows"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "syscall"
  11. "time"
  12. )
  13. const ()
  14. type File struct {
  15. //文件名(包含后缀)
  16. Name string
  17. //文件大小
  18. Size int64
  19. //创建时间
  20. CreateTime time.Time
  21. //文件后缀
  22. Ext string
  23. //完整路径
  24. Path string
  25. //IsOnlyRead bool
  26. //修改时间
  27. ModifyTime time.Time
  28. //文件是否被隐藏(window)
  29. IsHidden bool
  30. IsCanRead bool
  31. IsCanWrite bool
  32. }
  33. // SetHiddenAtt 设置隐藏属性
  34. func (f *File) SetHiddenAtt(ishidden bool) error {
  35. return setFileHiddenAtt(f.Path, ishidden)
  36. }
  37. // ToHash 获取哈希值
  38. func (f *File) ToHash() (string, error) {
  39. ff, err := os.OpenFile(f.Path, os.O_RDONLY, os.ModePerm)
  40. if err != nil {
  41. return "", err
  42. }
  43. defer func(f *os.File) {
  44. _ = f.Close()
  45. }(ff)
  46. hash := md5.New()
  47. if _, err := io.Copy(hash, ff); err != nil {
  48. return "", err
  49. }
  50. return hex.EncodeToString(hash.Sum(nil)), nil
  51. }
  52. // Content 获取内容,返回[]byte指针则修改源文件内容
  53. func (f *File) Content(fn func(bs []byte) *[]byte) error {
  54. ff, err := os.OpenFile(f.Path, os.O_RDONLY|os.O_WRONLY, os.ModePerm)
  55. if err != nil {
  56. return err
  57. }
  58. defer func(f *os.File) {
  59. _ = f.Close()
  60. }(ff)
  61. buffer, err := os.ReadFile(f.Path)
  62. nbs := fn(buffer)
  63. if nbs != nil {
  64. // 清空文件内容
  65. _ = ff.Truncate(0)
  66. _, _ = ff.Seek(0, 0)
  67. // 写入新内容
  68. _, err := ff.Write(*nbs)
  69. return err
  70. }
  71. return nil
  72. }
  73. // Delete 删除文件
  74. func (f *File) Delete() error {
  75. err := os.Remove(f.Path)
  76. return err
  77. }
  78. // isFileHidden 判断文件是否被隐藏(windnow)
  79. func isFileHidden(path string) (bool, error) {
  80. pathPtr, err := syscall.UTF16PtrFromString(path)
  81. if err != nil {
  82. return false, err
  83. }
  84. attributes, err := windows.GetFileAttributes(pathPtr)
  85. if err != nil {
  86. return false, err
  87. }
  88. return attributes&windows.FILE_ATTRIBUTE_HIDDEN == windows.FILE_ATTRIBUTE_HIDDEN, nil
  89. }
  90. // setFileHiddenAtt 设置文件隐藏属性
  91. func setFileHiddenAtt(path string, ishidden bool) error {
  92. pathp, err := syscall.UTF16PtrFromString(path)
  93. if err != nil {
  94. return err
  95. }
  96. if ishidden {
  97. // 应用新的文件属性
  98. return windows.SetFileAttributes(pathp, windows.FILE_ATTRIBUTE_HIDDEN)
  99. } else {
  100. return windows.SetFileAttributes(pathp, windows.FILE_ATTRIBUTE_NORMAL)
  101. }
  102. }
  103. // OpenFile 打开文件 返回文件以及错误信息
  104. func OpenFile(path string) (*File, error) {
  105. f, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
  106. if err != nil {
  107. return nil, err
  108. }
  109. defer func(f *os.File) {
  110. _ = f.Close()
  111. }(f)
  112. fileinfo, err := f.Stat()
  113. if err != nil {
  114. return nil, err
  115. }
  116. var createTime int64
  117. var modifyTime int64
  118. ishidden := false
  119. if runtime.GOOS == "windows" {
  120. //获取文件信息
  121. winFileAttr := fileinfo.Sys().(*syscall.Win32FileAttributeData)
  122. createTime = winFileAttr.CreationTime.Nanoseconds() / 1e9
  123. modifyTime = winFileAttr.LastWriteTime.Nanoseconds() / 1e9
  124. t, _ := isFileHidden(path)
  125. if t {
  126. ishidden = true
  127. }
  128. } else if runtime.GOOS == "linux" {
  129. }
  130. //ti := time.Now()
  131. //fmt.Println(ti)
  132. loc, _ := time.LoadLocation("Asia/Shanghai")
  133. tf := File{Name: f.Name(),
  134. Size: fileinfo.Size(),
  135. CreateTime: time.Unix(createTime, 0).In(loc),
  136. ModifyTime: time.Unix(modifyTime, 0).In(loc),
  137. Path: path, Ext: filepath.Ext(path),
  138. IsHidden: ishidden,
  139. }
  140. mode := fileinfo.Mode()
  141. if mode&0400 == 0400 {
  142. tf.IsCanRead = true
  143. } else {
  144. tf.IsCanRead = false
  145. }
  146. if mode&0200 == 0200 {
  147. tf.IsCanWrite = true
  148. } else {
  149. tf.IsCanWrite = false
  150. }
  151. return &tf, nil
  152. }