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 {
2022-02-19 16:42:32 +00:00
hop := history .
Operation ( history . TypeUnattachHypha ) .
2022-02-19 21:12:28 +00:00
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
2022-02-19 16:42:32 +00:00
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
}
2022-02-19 21:12:28 +00:00
if h . HasTextFile ( ) {
hyphae . Insert ( hyphae . ShrinkMediaToTextual ( h ) )
2022-02-19 08:26:38 +00:00
} else {
2022-02-19 21:12:28 +00:00
hyphae . DeleteHypha ( h )
2021-02-20 14:03:54 +00:00
}
2022-02-19 16:42:32 +00:00
return nil
2021-02-20 14:03:54 +00:00
}