Skip to content

Commit

Permalink
delete method on storage also deletes the uploaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Aug 8, 2020
1 parent 77df41b commit 64d90f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
13 changes: 10 additions & 3 deletions filereceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (f *FileReceiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("efes-file-offset", strconv.FormatInt(newOffset, 10))
case http.MethodDelete:
fi, err := ReadExistingFileInfo(path)
_, err := ReadExistingFileInfo(path)
if os.IsNotExist(err) {
http.Error(w, "offset file does not exist", http.StatusNotFound)
return
Expand All @@ -93,13 +93,16 @@ func (f *FileReceiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.internalServerError("cannot read offset file", err, r, w)
return
}
w.Header().Set("efes-file-sha1", hex.EncodeToString(fi.Digest.Sha1.Sum(nil)))
w.Header().Set("efes-file-crc32", hex.EncodeToString(fi.Digest.CRC32.Sum(nil)))
err = DeleteFileInfo(path)
if err != nil {
f.internalServerError("cannot delete offset file", err, r, w)
return
}
err = deleteFile(path)
if err != nil {
f.internalServerError("cannot delete file", err, r, w)
return
}
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
Expand All @@ -125,6 +128,10 @@ func createFile(path string) error {
return SaveFileInfo(path, newFileInfo())
}

func deleteFile(path string) error {
return os.Remove(path)
}

func saveFile(path string, offset int64, length int64, r io.Reader, log log.Logger) (int64, *Digest, error) {
var fi *FileInfo
var err error
Expand Down
11 changes: 4 additions & 7 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Client) send(path string, r io.Reader, offset, size int64, bo backoff.B
return ChecksumsFromResponse(resp), nil
case requestOffset:
// No bytes sent in last request. The file is read to the end.
return c.deleteOffset(path)
return c.finishFile(path, requestOffset)
}
}
}
Expand Down Expand Up @@ -176,12 +176,9 @@ func (c *Client) getOffset(path string) (int64, error) {
return strconv.ParseInt(resp.Header.Get("efes-file-offset"), 10, 64)
}

func (c *Client) deleteOffset(path string) (*Checksums, error) {
req, err := http.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
// finishFile deletes the info file and returns hashes.
func (c *Client) finishFile(path string, size int64) (*Checksums, error) {
resp, err := c.patch(path, nil, size, size)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 64d90f5

Please sign in to comment.