2021-02-20 14:03:54 +00:00
|
|
|
|
package shroom
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2021-07-02 08:29:55 +00:00
|
|
|
|
"io"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
"log"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2021-06-14 20:27:25 +00:00
|
|
|
|
"strings"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
|
2021-06-19 04:51:10 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/mimetype"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
|
)
|
|
|
|
|
|
2021-06-14 00:38:43 +00:00
|
|
|
|
func UploadText(h *hyphae.Hypha, data []byte, message string, u *user.User) (hop *history.HistoryOp, errtitle string) {
|
2021-02-20 14:03:54 +00:00
|
|
|
|
hop = history.Operation(history.TypeEditText)
|
2021-06-14 00:38:43 +00:00
|
|
|
|
var action string
|
2021-02-20 14:03:54 +00:00
|
|
|
|
if h.Exists {
|
2021-06-14 00:38:43 +00:00
|
|
|
|
action = "Edit"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
} else {
|
2021-06-14 00:38:43 +00:00
|
|
|
|
action = "Create"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 00:38:43 +00:00
|
|
|
|
if message == "" {
|
|
|
|
|
hop.WithMsg(fmt.Sprintf("%s ‘%s’", action, h.Name))
|
|
|
|
|
} else {
|
2021-06-14 07:37:30 +00:00
|
|
|
|
hop.WithMsg(fmt.Sprintf("%s ‘%s’: %s", action, h.Name, message))
|
2021-06-14 00:38:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 14:03:54 +00:00
|
|
|
|
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 (
|
2021-07-25 13:08:59 +00:00
|
|
|
|
hop = history.Operation(history.TypeEditBinary).WithMsg(fmt.Sprintf("Upload attachment for ‘%s’ with type ‘%s’", h.Name, mime))
|
2021-07-02 08:29:55 +00:00
|
|
|
|
data, err = io.ReadAll(file)
|
2021-02-20 14:03:54 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 (
|
2021-06-19 04:51:10 +00:00
|
|
|
|
fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext)
|
2021-02-20 14:03:54 +00:00
|
|
|
|
originalFullPath = &h.TextPath
|
|
|
|
|
)
|
2021-06-19 04:51:10 +00:00
|
|
|
|
// Reject if the path is outside the hyphae dir
|
|
|
|
|
if !strings.HasPrefix(fullPath, files.HyphaeDir()) {
|
2021-06-15 21:00:29 +00:00
|
|
|
|
err := errors.New("bad path")
|
2021-06-14 20:27:25 +00:00
|
|
|
|
return hop.WithErrAbort(err), err.Error()
|
|
|
|
|
}
|
2021-02-20 14:03:54 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 08:29:55 +00:00
|
|
|
|
if err := os.WriteFile(fullPath, data, 0666); 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(), ""
|
|
|
|
|
}
|