1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/shroom/unattach.go

33 lines
931 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.
Operation(history.TypeUnattachHypha).
WithFilesRemoved(h.MediaFilePath()).
2022-02-03 22:29:01 +00:00
WithMsg(fmt.Sprintf("Unattach %s", h.CanonicalName())).
2021-02-20 14:03:54 +00:00
WithUser(u).
Apply()
if len(hop.Errs) > 0 {
rejectUnattachLog(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
}