file.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dara
  2. import (
  3. "os"
  4. )
  5. // File struct to represent the file
  6. type DaraFile struct {
  7. path string
  8. fileInfo os.FileInfo
  9. file *os.File
  10. position int64
  11. }
  12. // NewFile creates a new instance of File
  13. func NewDaraFile(path string) *DaraFile {
  14. return &DaraFile{
  15. path: path,
  16. position: 0,
  17. }
  18. }
  19. // Path returns the path of the file
  20. func (tf *DaraFile) Path() string {
  21. return tf.path
  22. }
  23. // CreateTime returns the creation time of the file
  24. func (tf *DaraFile) CreateTime() (*Date, error) {
  25. if tf.fileInfo == nil {
  26. var err error
  27. tf.fileInfo, err = os.Stat(tf.path)
  28. if err != nil {
  29. return nil, err
  30. }
  31. }
  32. return &Date{tf.fileInfo.ModTime()}, nil
  33. }
  34. // ModifyTime returns the modification time of the file
  35. func (tf *DaraFile) ModifyTime() (*Date, error) {
  36. if tf.fileInfo == nil {
  37. var err error
  38. tf.fileInfo, err = os.Stat(tf.path)
  39. if err != nil {
  40. return nil, err
  41. }
  42. }
  43. return &Date{tf.fileInfo.ModTime()}, nil
  44. }
  45. // Length returns the size of the file
  46. func (tf *DaraFile) Length() (int64, error) {
  47. if tf.fileInfo == nil {
  48. var err error
  49. tf.fileInfo, err = os.Stat(tf.path)
  50. if err != nil {
  51. return 0, err
  52. }
  53. }
  54. return tf.fileInfo.Size(), nil
  55. }
  56. // Read reads a specified number of bytes from the file
  57. func (tf *DaraFile) Read(size int) ([]byte, error) {
  58. if tf.file == nil {
  59. file, err := os.OpenFile(tf.path, os.O_RDWR|os.O_CREATE, 0755)
  60. if err != nil {
  61. return nil, err
  62. }
  63. tf.file = file
  64. }
  65. fileInfo, err := tf.file.Stat()
  66. if err != nil {
  67. return nil, err
  68. }
  69. // 获取文件大小
  70. fileSize := fileInfo.Size()
  71. // 计算可以读取的实际大小
  72. if tf.position >= fileSize {
  73. return nil, nil // End of file reached
  74. }
  75. // 确保 size 不超过剩余文件大小
  76. actualSize := size
  77. if tf.position+int64(size) > fileSize {
  78. actualSize = int(fileSize - tf.position)
  79. }
  80. buf := make([]byte, actualSize)
  81. bytesRead, err := tf.file.ReadAt(buf, tf.position)
  82. if err != nil {
  83. return nil, err
  84. }
  85. tf.position += int64(bytesRead)
  86. return buf[:bytesRead], nil
  87. }
  88. // Write writes data to the file
  89. func (tf *DaraFile) Write(data []byte) error {
  90. if tf.file == nil {
  91. file, err := os.OpenFile(tf.path, os.O_RDWR|os.O_CREATE, 0755)
  92. if err != nil {
  93. return err
  94. }
  95. tf.file = file
  96. }
  97. _, err := tf.file.Write(data)
  98. if err != nil {
  99. return err
  100. }
  101. tf.fileInfo, err = os.Stat(tf.path) // Update fileInfo after write
  102. return err
  103. }
  104. // Close closes the file
  105. func (tf *DaraFile) Close() error {
  106. if tf.file == nil {
  107. return nil
  108. }
  109. return tf.file.Close()
  110. }
  111. // Exists checks if the file exists
  112. func Exists(path string) (bool, error) {
  113. _, err := os.Stat(path)
  114. if os.IsNotExist(err) {
  115. return false, nil
  116. }
  117. return err == nil, err
  118. }
  119. // CreateReadStream would typically return an os.File or similar
  120. func CreateReadStream(path string) (*os.File, error) {
  121. return os.Open(path)
  122. }
  123. // CreateWriteStream would typically return an os.File or similar
  124. func CreateWriteStream(path string) (*os.File, error) {
  125. return os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
  126. }