From 5fb1e85372896f431a49adf2623d9a57bc0078fe Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Thu, 12 Jan 2023 13:44:37 +0200 Subject: [PATCH] fixed formatting --- CHANGELOG.md | 2 ++ tools/filesystem/filesystem.go | 22 ++++++++++++---------- tools/filesystem/filesystem_test.go | 9 ++++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f91a872..396267ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tools/filesystem/filesystem.go b/tools/filesystem/filesystem.go index 1ee69cd2..c1a50695 100644 --- a/tools/filesystem/filesystem.go +++ b/tools/filesystem/filesystem.go @@ -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{ @@ -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) diff --git a/tools/filesystem/filesystem_test.go b/tools/filesystem/filesystem_test.go index 25300dfe..256d01fb 100644 --- a/tools/filesystem/filesystem_test.go +++ b/tools/filesystem/filesystem_test.go @@ -348,6 +348,7 @@ func TestFileSystemServe(t *testing.T) { } } } + func TestFileSystemGetFile(t *testing.T) { dir := createTestDir(t) defer os.RemoveAll(dir) @@ -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)