Implement file deletion

This commit is contained in:
Daan Sprenkels
2019-11-22 18:41:54 +01:00
parent c82a71f7d0
commit df86d5836a
7 changed files with 83 additions and 47 deletions

View File

@@ -79,7 +79,7 @@ func (t fileUploadState) String() string {
func OpenFileStore(path string) error {
if path == "" {
return errors.New("file store path not set")
return errors.New("file-store not set")
}
// Try to create the file store directory if it does not yet exist
@@ -158,11 +158,23 @@ func (fu *fileUpload) save(tx *bolt.Tx) error {
}
func (fu *fileUpload) delete(tx *bolt.Tx) error {
// Replace the old paste with a new empty paste
return (&fileUpload{
// Remove the file in the backend
filePath := fileStorePath(fu.ID, fu.FileName)
if err := os.Remove(filePath); err != nil {
return err
}
// Update the file in the server
if err := (&fileUpload{
ID: fu.ID,
State: fileUploadStateDeleted,
}).save(tx)
}).save(tx); err != nil {
return err
}
// Cleanup the parent directory
wrap := "deletion succeeded, but removing the file directory has failed"
return errors.Wrap(os.Remove(path.Dir(filePath)), wrap)
}
func (fu *fileUpload) url() *url.URL {