Skip to content

Commit

Permalink
Add API to delete uploaded files h2oai/q#1147
Browse files Browse the repository at this point in the history
  • Loading branch information
lo5 committed Jul 14, 2020
1 parent b1699fe commit 31fc787
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
21 changes: 21 additions & 0 deletions py/telesync/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ def download(self, url: str, path: str) -> str:

return filepath

def unload(self, url: str):
res = requests.delete(f'{_config.hub_address}{url}')
if res.status_code == 200:
return
raise ServiceError(f'Unload failed (code={res.status_code}): {res.text}')


_client = _BasicAuthClient()

Expand Down Expand Up @@ -573,6 +579,13 @@ def download(self, url: str, path: str) -> str:
"""
return _client.download(url, path)

def unload(self, url: str):
"""
Delete an uploaded file from the site.
:param url: The URL of the file to delete.
"""
_client.unload(url)


class AsyncSite:
"""
Expand Down Expand Up @@ -617,6 +630,14 @@ async def download(self, url: str, path: str) -> str:
path = _client.download(url, path)
return path

async def unload(self, url: str):
"""
Delete an uploaded file from the site.
:param url: The URL of the file to delete.
"""
# XXX use non-blocking aiohttp get
_client.unload(url)


site = Site()

Expand Down
24 changes: 23 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ func newFileServer(dir string) http.Handler {
}
}

var (
errInvalidUnloadPath = errors.New("invalid file path")
)

func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
Expand All @@ -270,14 +274,32 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/_f") // public
fs.handler.ServeHTTP(w, r)

// case http.MethodDelete: // TODO garbage collection
case http.MethodDelete: // TODO garbage collection
if err := fs.unloadFile(r.URL.Path); err != nil {
echo(Log{"t": "file_unload", "path": r.URL.Path, "error": err.Error()})
return
}
echo(Log{"t": "file_unload", "path": r.URL.Path})

default:
echo(Log{"t": "file_download", "method": r.Method, "path": r.URL.Path, "error": "method not allowed"})
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}

func (fs *FileServer) unloadFile(url string) error {
tokens := strings.Split(path.Clean(url), "/")
if len(tokens) != 4 { // /_f/uuid/file.ext
return errInvalidUnloadPath
}
if tokens[0] != "" || tokens[1] != "_f" || path.Ext(tokens[3]) == "" {
return errInvalidUnloadPath
}

dirpath := filepath.Join(fs.dir, tokens[2])
return os.RemoveAll(dirpath)
}

var (
logSep = []byte(" ")
)
Expand Down

0 comments on commit 31fc787

Please sign in to comment.