func (serv *workspace) dir(request *restful.Request, response *restful.Response) { rq := new(dirRequest) err := request.ReadEntity(&rq) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Illegal Request: %s", err)) return } if len(rq.Filesystem) == 0 { var result dir for _, n := range serv.goworkspace.RootFS() { result.Entries = append(result.Entries, dirEntry{n, n, true}) } response.WriteEntity(&result) return } fs, ok := serv.filesystems[rq.Filesystem] if !ok { sendError(response, http.StatusBadRequest, fmt.Errorf("Unknown filesystem: %s", rq.Filesystem)) return } _, rpath, fl, err := filesystem.AbsolutePath(fs, rq.Path) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Illegal path %s in fs %s: %v", rq.Path, fs.Base(), err)) return } defer fl.Close() serv.dircontent(fs, fl, rpath, request, response) }
func (serv *workspace) file(request *restful.Request, response *restful.Response) { rq := new(fileReadRequest) err := request.ReadEntity(&rq) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Illegal Request: %s", err)) return } fs, ok := serv.filesystems[rq.Filesystem] if !ok { sendError(response, http.StatusBadRequest, fmt.Errorf("Unknown filesystem: %s", rq.Filesystem)) return } _, rpath, fl, err := filesystem.AbsolutePath(fs, rq.Path) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Illegal path %s in fs %s: %v", rq.Path, fs.Base(), err)) return } defer fl.Close() var result fileContent result.Title = filepath.Base(rpath) cnt, err := ioutil.ReadAll(fl) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Cannot read content of '%s' parameter: %s", rq.Path, err)) } fi, err := fs.Stat(rpath) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Cannot stat file '%s': %s", rq.Path, err)) } result.Content = string(cnt) result.FileMode = uint32(fi.Mode()) response.WriteEntity(&result) }
func (serv *workspace) dircontent(fs filesystem.WorkspaceFS, fl filesystem.WorkspaceFile, path string, request *restful.Request, response *restful.Response) { var result dir flz, err := fl.Readdir(-1) if err != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Cannot read contents of '%s': %s", path, err)) } else { for _, fli := range flz { abs, _, _, e := filesystem.AbsolutePath(fs, path) if e != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Cannot read contents of '%s': %s", path, err)) } isd, e := isDir(abs, fli) if e != nil { sendError(response, http.StatusBadRequest, fmt.Errorf("Cannot read dir status of '%s/%#v': %s", abs, fli, err)) } result.Entries = append(result.Entries, dirEntry{fs.Name(), fli.Name(), isd}) } response.WriteEntity(&result) } }