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.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Save save a file
  35. func (l *LocalStorage) Save(path string, r io.Reader) (int64, error) {
  36. p := filepath.Join(l.dir, path)
  37. if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
  38. return 0, err
  39. }
  40. // always override
  41. if err := os.RemoveAll(p); err != nil {
  42. return 0, err
  43. }
  44. f, err := os.Create(p)
  45. if err != nil {
  46. return 0, err
  47. }
  48. defer f.Close()
  49. return io.Copy(f, r)
  50. }
  51. // Delete delete a file
  52. func (l *LocalStorage) Delete(path string) error {
  53. p := filepath.Join(l.dir, path)
  54. return os.Remove(p)
  55. }
  56. func (l *LocalStorage) PresignedGetURL(path string, fileName string) (string, error) {
  57. return "", nil
  58. }
  59. func (l *LocalStorage) PresignedPutURL(path string) (string, error) {
  60. return "", nil
  61. }
  62. func (l *LocalStorage) HasObject(path string) (bool, error) {
  63. return false, nil
  64. }
  65. func (l *LocalStorage) UploadObject(fileName, filePath string) error {
  66. return nil
  67. }
  68. func (l *LocalStorage) DeleteDir(dir string) error {
  69. return nil
  70. }