diff options
| author | Shulhan <ms@kilabit.info> | 2021-12-09 11:33:46 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-12-09 11:33:46 +0700 |
| commit | 10b8a1f91837abc2c548cd5a141714ff8aab29ec (patch) | |
| tree | e93b3d877710aad96ae28ddb57bf313bcc72afcc /http_api.go | |
| parent | fa94025f8e59206c1118e8c404cbff0412af4152 (diff) | |
| download | awwan-10b8a1f91837abc2c548cd5a141714ff8aab29ec.tar.xz | |
all: implement function to delete file on web user interface (WUI)
The WUI now has a button "Remove" under the file system tree that allow
user to remove selected file.
Diffstat (limited to 'http_api.go')
| -rw-r--r-- | http_api.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/http_api.go b/http_api.go index df1bd6d..29eb3b2 100644 --- a/http_api.go +++ b/http_api.go @@ -45,6 +45,18 @@ func (aww *Awwan) registerHttpApis() (err error) { return fmt.Errorf("%s: %w", logp, err) } + apiFsDelete := &libhttp.Endpoint{ + Method: libhttp.RequestMethodDelete, + Path: httpApiFs, + RequestType: libhttp.RequestTypeJSON, + ResponseType: libhttp.ResponseTypeJSON, + Call: aww.httpApiFsDelete, + } + err = aww.httpd.RegisterEndpoint(apiFsDelete) + if err != nil { + return fmt.Errorf("%s: %w", logp, err) + } + err = aww.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: httpApiFs, @@ -106,6 +118,78 @@ func (aww *Awwan) httpApiFs(epr *libhttp.EndpointRequest) ([]byte, error) { } // +// httpApiFsDelete an HTTP API to delete a file. +// +// Request +// +// DELETE /awwan/api/fs +// Content-Type: application/json +// +// { +// "path": <string>, the path to file or directory to be removed. +// "is_dir": <boolean>, true if its directory. +// } +// +// Response +// +// Content-Type: application/json +// +// { +// "code": <number> +// "message": <string> +// } +// +// List of valid response code, +// * 200: OK. +// * 400: Bad request. +// * 401: Unauthorized. +// * 404: File not found. +// +func (aww *Awwan) httpApiFsDelete(epr *libhttp.EndpointRequest) ([]byte, error) { + logp := "httpApiFsDelete" + + res := &libhttp.EndpointResponse{} + res.Code = http.StatusBadRequest + + req := &fsRequest{} + err := json.Unmarshal(epr.RequestBody, req) + if err != nil { + res.Message = err.Error() + return nil, res + } + + parentPath := path.Dir(req.Path) + nodeParent := aww.memfsBase.PathNodes.Get(parentPath) + if nodeParent == nil { + res.Message = fmt.Sprintf("%s: invalid path %s", logp, req.Path) + return nil, res + } + + path := filepath.Join(nodeParent.SysPath, path.Base(req.Path)) + path, err = filepath.Abs(path) + if err != nil { + res.Message = fmt.Sprintf("%s: %s", logp, err) + return nil, res + } + if !strings.HasPrefix(path, aww.memfsBase.Opts.Root) { + res.Message = fmt.Sprintf("%s: invalid path %q", logp, path) + return nil, res + } + + err = os.Remove(path) + if err != nil { + res.Code = http.StatusInternalServerError + res.Message = fmt.Sprintf("%s: %s", logp, err) + return nil, res + } + + res.Code = http.StatusOK + res.Message = fmt.Sprintf("%s: %q has been removed", logp, path) + + return json.Marshal(res) +} + +// // httpApiFsPost create new directory or file. // // Request |
