2021-02-20 19:03:54 +05:00
package shroom
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/user"
)
2022-02-20 12:27:30 +03: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 19:42:32 +03:00
hop := history .
2022-02-26 10:33:09 +03:00
Operation ( history . TypeRemoveMedia ) .
2022-02-20 00:12:28 +03:00
WithFilesRemoved ( h . MediaFilePath ( ) ) .
2022-02-26 10:33:09 +03:00
WithMsg ( fmt . Sprintf ( "Remove media from ‘ %s’ " , h . CanonicalName ( ) ) ) .
2021-02-20 19:03:54 +05:00
WithUser ( u ) .
Apply ( )
if len ( hop . Errs ) > 0 {
2022-02-26 10:33:09 +03:00
rejectRemoveMediaLog ( h , u , "fail" )
2021-02-26 21:43:45 +05:00
// FIXME: something may be wrong here
2022-02-19 19:42:32 +03:00
return fmt . Errorf ( "Could not unattach this hypha due to internal server errors: <code>%v</code>" , hop . Errs )
2021-02-20 19:03:54 +05:00
}
2022-02-20 00:12:28 +03:00
if h . HasTextFile ( ) {
hyphae . Insert ( hyphae . ShrinkMediaToTextual ( h ) )
2022-02-19 11:26:38 +03:00
} else {
2022-02-20 00:12:28 +03:00
hyphae . DeleteHypha ( h )
2021-02-20 19:03:54 +05:00
}
2022-02-19 19:42:32 +03:00
return nil
2021-02-20 19:03:54 +05:00
}