1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/shroom/unattach.go

33 lines
941 B
Go
Raw Normal View History

2021-02-20 14:03:54 +00:00
package shroom
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/user"
)
2022-02-20 09:27:30 +00:00
// RemoveMedia removes media from the media hypha and makes a history record about that. If it only had media, the hypha will be deleted. If it also had text, the hypha will become textual.
func RemoveMedia(u *user.User, h *hyphae.MediaHypha) error {
hop := history.
2022-02-26 07:33:09 +00:00
Operation(history.TypeRemoveMedia).
WithFilesRemoved(h.MediaFilePath()).
2022-02-26 07:33:09 +00:00
WithMsg(fmt.Sprintf("Remove media from %s", h.CanonicalName())).
2021-02-20 14:03:54 +00:00
WithUser(u).
Apply()
if len(hop.Errs) > 0 {
2022-02-26 07:33:09 +00:00
rejectRemoveMediaLog(h, u, "fail")
2021-02-26 16:43:45 +00:00
// FIXME: something may be wrong here
return fmt.Errorf("Could not unattach this hypha due to internal server errors: <code>%v</code>", hop.Errs)
2021-02-20 14:03:54 +00:00
}
if h.HasTextFile() {
hyphae.Insert(hyphae.ShrinkMediaToTextual(h))
2022-02-19 08:26:38 +00:00
} else {
hyphae.DeleteHypha(h)
2021-02-20 14:03:54 +00:00
}
return nil
2021-02-20 14:03:54 +00:00
}