Allows files to be read through FileSystem interface.
The functionality is needed while Pocketbase is used in embedded mode
This commit is contained in:
		
							parent
							
								
									59e4939e1d
								
							
						
					
					
						commit
						a5ceee33df
					
				| 
						 | 
					@ -285,6 +285,16 @@ var manualExtensionContentTypes = map[string]string{
 | 
				
			||||||
	".css": "text/css",      // (see https://github.com/gabriel-vasile/mimetype/pull/113)
 | 
						".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.
 | 
					// 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 {
 | 
					func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error {
 | 
				
			||||||
	br, readErr := s.bucket.NewReader(s.ctx, fileKey, nil)
 | 
						br, readErr := s.bucket.NewReader(s.ctx, fileKey, nil)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -348,7 +348,25 @@ func TestFileSystemServe(t *testing.T) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func TestFileSystemGetFile(t *testing.T) {
 | 
				
			||||||
 | 
						dir := createTestDir(t)
 | 
				
			||||||
 | 
						defer os.RemoveAll(dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fs, err := filesystem.NewLocal(dir)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer fs.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						f, fErr := fs.GetFile("image.png")
 | 
				
			||||||
 | 
						if fErr != nil {
 | 
				
			||||||
 | 
							t.Fatal(fErr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
						if f == nil {
 | 
				
			||||||
 | 
							t.Fatal("File is supposed to be found")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
func TestFileSystemServeSingleRange(t *testing.T) {
 | 
					func TestFileSystemServeSingleRange(t *testing.T) {
 | 
				
			||||||
	dir := createTestDir(t)
 | 
						dir := createTestDir(t)
 | 
				
			||||||
	defer os.RemoveAll(dir)
 | 
						defer os.RemoveAll(dir)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue