🐛 Fix shares not being locally created and deleted

This commit is contained in:
Andreas Schneider 2020-11-01 15:36:13 +01:00
parent e5a996452d
commit a8b2db986a
3 changed files with 25 additions and 7 deletions

View File

@ -92,14 +92,14 @@ func (cmd *CmdShareCreate) Run(app *app) error {
// Best effort cleanup.
_ = app.shareStore.RemoveShare(share.UUID)
return fmt.Errorf("cannot set share attributes: %v", err)
return fmt.Errorf("cannot set share attributes: %w", err)
}
if err := os.MkdirAll(path.Join(app.DataDirectory, share.UUID.String()), 0750); err != nil {
// Best effort cleanup.
_ = app.shareStore.RemoveShare(share.UUID)
return fmt.Errorf("cannot create data dir: %v", err)
return fmt.Errorf("cannot create data dir: %w", err)
}
fmt.Printf("Share created: %s\n", share.UUID.String())
@ -116,11 +116,11 @@ type CmdShareDelete struct {
func (cmd *CmdShareDelete) Run(app *app) error {
if err := os.RemoveAll(path.Join(app.DataDirectory, cmd.UUID.String())); err != nil {
return fmt.Errorf("cannot remove data directory: %v", err)
return fmt.Errorf("cannot remove data directory: %w", err)
}
if err := app.shareStore.RemoveShare(cmd.UUID); err != nil {
return fmt.Errorf("cannot remove share: %v", err)
return fmt.Errorf("cannot remove share: %w", err)
}
return nil

View File

@ -28,6 +28,7 @@
<div id="share-{{$share.UUID}}" class="share">
UUID: {{ $share.UUID }} {{ if $share.IsAdmin }}
<form style="display: inline-block;" action="delete-share" method="post">
<input type="hidden" name="source" value="my-shares"/>
<input type="hidden" name="share" value="{{ $share.UUID }}"/>
<input type="submit" value="Delete" class="delete"/>
</form>{{ end }}

View File

@ -509,6 +509,15 @@ Are you sure you want to continue?`, loginName)
return
}
if err := os.MkdirAll(path.Join(app.DataDirectory, share.UUID.String()), 0750); err != nil {
// Best effort cleanup.
_ = app.shareStore.RemoveShare(share.UUID)
fmt.Fprintf(os.Stderr, "cannot create data dir: %v\n", err)
sessionContext.RenderError(template.HTML("Internal server error."), "")
return
}
if owned {
if err := app.shareStore.AddUserToShare(share, sessionContext.user.Username, ShareRoleAdmin); err != nil {
sessionContext.RenderError(template.HTML("Cannot add self to share: "+err.Error()), "")
@ -523,9 +532,11 @@ Are you sure you want to continue?`, loginName)
ar.Post("/delete-share", func(w http.ResponseWriter, r *http.Request) {
sessionContext := h.buildSessionContext(w, r)
returnURL := r.FormValue("source")
share, err := app.shareStore.GetShare(r.FormValue("share"))
if err != nil {
sessionContext.RenderError(template.HTML("Internal error: "+err.Error()), "")
sessionContext.RenderError(template.HTML("Internal error: "+err.Error()), returnURL)
return
}
@ -541,12 +552,18 @@ Are you sure you want to continue?`, share.UUID, share.Name)
// We have already rendered. Nothing to do.
return
} else if confirmStatus == confirmAccepted {
if err := os.RemoveAll(path.Join(app.DataDirectory, share.UUID.String())); err != nil {
fmt.Fprintf(os.Stderr, "cannot remove data dir: %v\n", err)
sessionContext.RenderError(template.HTML("Internal server error."), returnURL)
return
}
if err := app.shareStore.RemoveShare(share.UUID); err != nil {
sessionContext.RenderError(template.HTML("Share cannot be removed: "+err.Error()), "shares")
sessionContext.RenderError(template.HTML("Share cannot be removed: "+err.Error()), returnURL)
return
}
}
sessionContext.Redirect("shares")
sessionContext.Redirect(returnURL)
})
ar.Route("/create-user", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {