You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

local.go 1.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "io"
  7. "os"
  8. "path/filepath"
  9. )
  10. var (
  11. _ ObjectStorage = &LocalStorage{}
  12. )
  13. // LocalStorage represents a local files storage
  14. type LocalStorage struct {
  15. dir string
  16. }
  17. // NewLocalStorage returns a local files
  18. func NewLocalStorage(bucket string) (*LocalStorage, error) {
  19. if err := os.MkdirAll(bucket, os.ModePerm); err != nil {
  20. return nil, err
  21. }
  22. return &LocalStorage{
  23. dir: bucket,
  24. }, nil
  25. }
  26. // Open open a file
  27. func (l *LocalStorage) Open(path string) (io.ReadCloser, error) {
  28. f, err := os.Open(filepath.Join(l.dir, path))
  29. if err != nil {
  30. return nil, err
  31. }
  32. return f, nil
  33. }
  34. func (l *LocalStorage) DownloadAFile(bucket string, objectName string) (io.ReadCloser, error) {
  35. f, err := os.Open(filepath.Join(l.dir, objectName))
  36. if err != nil {
  37. return nil, err
  38. }
  39. return f, nil
  40. }
  41. // Save save a file
  42. func (l *LocalStorage) Save(path string, r io.Reader) (int64, error) {
  43. p := filepath.Join(l.dir, path)
  44. if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
  45. return 0, err
  46. }
  47. // always override
  48. if err := os.RemoveAll(p); err != nil {
  49. return 0, err
  50. }
  51. f, err := os.Create(p)
  52. if err != nil {
  53. return 0, err
  54. }
  55. defer f.Close()
  56. return io.Copy(f, r)
  57. }
  58. // Delete delete a file
  59. func (l *LocalStorage) Delete(path string) error {
  60. p := filepath.Join(l.dir, path)
  61. return os.Remove(p)
  62. }
  63. func (l *LocalStorage) PresignedGetURL(path string, fileName string) (string, error) {
  64. return "", nil
  65. }
  66. func (l *LocalStorage) PresignedPutURL(path string) (string, error) {
  67. return "", nil
  68. }
  69. func (l *LocalStorage) HasObject(path string) (bool, error) {
  70. return false, nil
  71. }
  72. func (l *LocalStorage) UploadObject(fileName, filePath string) error {
  73. return nil
  74. }
  75. func (l *LocalStorage) DeleteDir(dir string) error {
  76. return nil
  77. }