local.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package storage
  2. import (
  3. "io"
  4. "os"
  5. )
  6. type Local struct{}
  7. func (l *Local) Read(path string) ([]byte, error) {
  8. return os.ReadFile(path)
  9. }
  10. func (l *Local) ReadStream(path string) (io.ReadCloser, error) {
  11. return os.Open(path)
  12. }
  13. func (l *Local) Write(path string, data []byte) error {
  14. return os.WriteFile(path, data, 0644)
  15. }
  16. func (l *Local) WriteStream(path string, data io.Reader) error {
  17. file, err := os.Create(path)
  18. if err != nil {
  19. return err
  20. }
  21. defer file.Close()
  22. _, err = io.Copy(file, data)
  23. return err
  24. }
  25. func (l *Local) List(path string) ([]FileInfo, error) {
  26. entries, err := os.ReadDir(path)
  27. if err != nil {
  28. return nil, err
  29. }
  30. file_infos := make([]FileInfo, len(entries))
  31. for i, entry := range entries {
  32. info, err := entry.Info()
  33. if err != nil {
  34. return nil, err
  35. }
  36. file_infos[i] = info
  37. }
  38. return file_infos, nil
  39. }
  40. func (l *Local) Stat(path string) (FileInfo, error) {
  41. return os.Stat(path)
  42. }
  43. func (l *Local) Delete(path string) error {
  44. return os.Remove(path)
  45. }
  46. func (l *Local) Mkdir(path string, perm os.FileMode) error {
  47. return os.MkdirAll(path, perm)
  48. }
  49. func (l *Local) Rename(oldpath, newpath string) error {
  50. return os.Rename(oldpath, newpath)
  51. }
  52. func (l *Local) Exists(path string) (bool, error) {
  53. _, err := os.Stat(path)
  54. if err == nil {
  55. return true, nil
  56. }
  57. if os.IsNotExist(err) {
  58. return false, nil
  59. }
  60. return false, err
  61. }