Added the /fs/cat/ api

This commit is contained in:
Diptanu Choudhury
2016-03-28 11:06:22 -07:00
parent e633776365
commit c8f74a3d9c
3 changed files with 64 additions and 13 deletions

View File

@@ -27,6 +27,8 @@ func (s *HTTPServer) FsRequest(resp http.ResponseWriter, req *http.Request) (int
return s.FileStatRequest(resp, req)
case strings.HasPrefix(path, "readat/"):
return s.FileReadAtRequest(resp, req)
case strings.HasPrefix(path, "cat/"):
return s.FileCatRequest(resp, req)
default:
return nil, CodedError(404, ErrInvalidMethod)
}
@@ -94,3 +96,33 @@ func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Reque
io.Copy(resp, r)
return nil, nil
}
func (s *HTTPServer) FileCatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var allocID, path string
var err error
q := req.URL.Query()
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/cat/"); allocID == "" {
return nil, allocIDNotPresentErr
}
if path = q.Get("path"); path == "" {
return nil, fileNameNotPresentErr
}
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
fileInfo, err := fs.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.IsDir {
return nil, fmt.Errorf("file %q is a directory")
}
r, err := fs.ReadAt(path, int64(0), fileInfo.Size)
io.Copy(resp, r)
return nil, nil
}