GetObject retrieves an object from file system.
( ctx context.Context, reqHeader http.Header, _, name, _ string, )
| 22 | |
| 23 | // GetObject retrieves an object from file system. |
| 24 | func (s *Storage) GetObject( |
| 25 | ctx context.Context, |
| 26 | reqHeader http.Header, |
| 27 | _, name, _ string, |
| 28 | ) (*storage.ObjectReader, error) { |
| 29 | // If either container or object name is empty, return 404 |
| 30 | if len(name) == 0 { |
| 31 | return storage.NewObjectNotFound( |
| 32 | "invalid FS Storage URL: object name is empty", |
| 33 | ), nil |
| 34 | } |
| 35 | |
| 36 | name = "/" + name |
| 37 | |
| 38 | // check that file exists |
| 39 | f, err := s.fs.Open(name) |
| 40 | if err != nil { |
| 41 | if os.IsNotExist(err) { |
| 42 | return storage.NewObjectNotFound(fmt.Sprintf("%s doesn't exist", name)), nil |
| 43 | } |
| 44 | |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | // check that file is not a directory |
| 49 | fi, err := f.Stat() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | if fi.IsDir() { |
| 55 | return storage.NewObjectNotFound(fmt.Sprintf("%s is directory", name)), nil |
| 56 | } |
| 57 | |
| 58 | // file basic properties |
| 59 | size := fi.Size() |
| 60 | body := io.ReadCloser(f) |
| 61 | |
| 62 | // result headers |
| 63 | header := make(http.Header) |
| 64 | |
| 65 | // set default headers |
| 66 | header.Set(httpheaders.AcceptRanges, "bytes") |
| 67 | |
| 68 | // try to detect content type from magic bytes or extension |
| 69 | if mimetype := detectContentType(f, fi); len(mimetype) > 0 { |
| 70 | header.Set(httpheaders.ContentType, mimetype) |
| 71 | } |
| 72 | |
| 73 | // try requested range |
| 74 | start, end, err := httprange.Parse(reqHeader.Get(httpheaders.Range)) |
| 75 | switch { |
| 76 | case err != nil: |
| 77 | f.Close() |
| 78 | return storage.NewObjectInvalidRange(), nil //nolint:nilerr |
| 79 | |
| 80 | // Range requested: partial content should be returned |
| 81 | case end != 0: |
nothing calls this directly
no test coverage detected