2021-02-20 14:03:54 +00:00
|
|
|
|
package shroom
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/mimetype"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func UploadText(h *hyphae.Hypha, data []byte, u *user.User) (hop *history.HistoryOp, errtitle string) {
|
|
|
|
|
hop = history.Operation(history.TypeEditText)
|
|
|
|
|
if h.Exists {
|
|
|
|
|
hop.WithMsg(fmt.Sprintf("Edit ‘%s’", h.Name))
|
|
|
|
|
} else {
|
|
|
|
|
hop.WithMsg(fmt.Sprintf("Create ‘%s’", h.Name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err, errtitle := CanEdit(u, h); err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), errtitle
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
if len(data) == 0 {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(errors.New("No data passed")), "Empty"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uploadHelp(h, hop, ".myco", data, u)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UploadBinary(h *hyphae.Hypha, mime string, file multipart.File, u *user.User) (*history.HistoryOp, string) {
|
|
|
|
|
var (
|
|
|
|
|
hop = history.Operation(history.TypeEditBinary).WithMsg(fmt.Sprintf("Upload binary part for ‘%s’ with type ‘%s’", h.Name, mime))
|
|
|
|
|
data, err = ioutil.ReadAll(file)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), err.Error()
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
if err, errtitle := CanAttach(u, h); err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), errtitle
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
if len(data) == 0 {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(errors.New("No data passed")), "Empty"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uploadHelp(h, hop, mimetype.ToExtension(mime), data, u)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// uploadHelp is a helper function for UploadText and UploadBinary
|
|
|
|
|
func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte, u *user.User) (*history.HistoryOp, string) {
|
|
|
|
|
var (
|
|
|
|
|
fullPath = filepath.Join(util.WikiDir, h.Name+ext)
|
|
|
|
|
originalFullPath = &h.TextPath
|
|
|
|
|
)
|
|
|
|
|
if hop.Type == history.TypeEditBinary {
|
|
|
|
|
originalFullPath = &h.BinaryPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(fullPath), 0777); err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), err.Error()
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(fullPath, data, 0644); err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), err.Error()
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if h.Exists && *originalFullPath != fullPath && *originalFullPath != "" {
|
|
|
|
|
if err := history.Rename(*originalFullPath, fullPath); err != nil {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
return hop.WithErrAbort(err), err.Error()
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
log.Println("Move", *originalFullPath, "to", fullPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.InsertIfNew()
|
|
|
|
|
if h.Exists && h.TextPath != "" && hop.Type == history.TypeEditText && !history.FileChanged(fullPath) {
|
|
|
|
|
return hop.Abort(), "No changes"
|
|
|
|
|
}
|
|
|
|
|
*originalFullPath = fullPath
|
|
|
|
|
return hop.WithFiles(fullPath).WithUser(u).Apply(), ""
|
|
|
|
|
}
|