Skip to content

Commit

Permalink
fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jan 12, 2023
1 parent a5ceee3 commit 5fb1e85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Added video and audio file previews.

- Added `filesystem.GetFile()` helper to read files through the FileSystem abstraction ([#1578](https://github.com/pocketbase/pocketbase/pull/1578); thanks @avarabyeu).


## v0.11.1

Expand Down
22 changes: 12 additions & 10 deletions tools/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ func (s *System) Attributes(fileKey string) (*blob.Attributes, error) {
return s.bucket.Attributes(s.ctx, fileKey)
}

// GetFile returns a file content reader for the given fileKey.
//
// NB! Make sure to call `Close()` after you are done working with it.
func (s *System) GetFile(fileKey string) (io.ReadCloser, error) {
br, err := s.bucket.NewReader(s.ctx, fileKey, nil)
if err != nil {
return nil, err
}

return br, nil
}

// Upload writes content into the fileKey location.
func (s *System) Upload(content []byte, fileKey string) error {
opts := &blob.WriterOptions{
Expand Down Expand Up @@ -285,16 +297,6 @@ var manualExtensionContentTypes = map[string]string{
".css": "text/css", // (see https://github.com/gabriel-vasile/mimetype/pull/113)
}

// / GetFile returns a file content reader for given file key
// / NB! Make sure to call `Close()` after you are done working with it.
func (s *System) GetFile(fileKey string) (io.ReadCloser, error) {
br, readErr := s.bucket.NewReader(s.ctx, fileKey, nil)
if readErr != nil {
return nil, readErr
}
return br, nil
}

// Serve serves the file at fileKey location to an HTTP response.
func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error {
br, readErr := s.bucket.NewReader(s.ctx, fileKey, nil)
Expand Down
9 changes: 6 additions & 3 deletions tools/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func TestFileSystemServe(t *testing.T) {
}
}
}

func TestFileSystemGetFile(t *testing.T) {
dir := createTestDir(t)
defer os.RemoveAll(dir)
Expand All @@ -358,15 +359,17 @@ func TestFileSystemGetFile(t *testing.T) {
}
defer fs.Close()

f, fErr := fs.GetFile("image.png")
if fErr != nil {
t.Fatal(fErr)
f, err := fs.GetFile("image.png")
if err != nil {
t.Fatal(err)
}
defer f.Close()

if f == nil {
t.Fatal("File is supposed to be found")
}
}

func TestFileSystemServeSingleRange(t *testing.T) {
dir := createTestDir(t)
defer os.RemoveAll(dir)
Expand Down

0 comments on commit 5fb1e85

Please sign in to comment.